我的做法没有涉及什么语法树,主要是字符串处理,水平略搓写的有点麻烦,题目说忽略空格,youcase比如 a++有可能是a + +,导致WA,后来加上了预处理去掉所有空格才AC。
#include<stdio.h> #include<string.h> char tmpStr[200]; char expr[200]; int appeared[26]; int plus[26]; int minus[26]; char afterExp[200]; int val; void print() { printf("Expression: %s\n", tmpStr); printf(" value = %d\n", val); int j; for (j = 0; j < 26; j++) { if (appeared[j]) { char ch = j + 'a'; int value = j + 1; if (plus[j] != 0) value++; if (minus[j] != 0) value--; printf(" %c = %d\n", ch, value); } } } void init() { val = 0; int i; for (i = 0; i < 26; i++) { appeared[i] = 0; plus[i] = 0; minus[i] = 0; } } void evaluateAndPrint() { init(); int len = strlen(expr); int idx = 0; int i; for (i = 0; i < len; i++) { if (expr[i] >= 'a' && expr[i] <= 'z') { appeared[expr[i] - 'a'] = 1; afterExp[idx++] = expr[i]; if (i - 2 >= 0 && expr[i - 1] == '+' && expr[i - 2] == '+') plus[expr[i] - 'a'] = -1; if (i - 2 >= 0 && expr[i - 1] == '-' && expr[i - 2] == '-') minus[expr[i] - 'a'] = -1; if (i + 2 < len && expr[i + 1] == '+' && expr[i + 2] == '+') plus[expr[i] - 'a'] = 1; if (i + 2 < len && expr[i + 1] == '-' && expr[i + 2] == '-') minus[expr[i] - 'a'] = 1; } else if (expr[i] == '+' || expr[i] == '-') { if (i > 0 && i < len - 1 && expr[i - 1] != expr[i] && expr[i] != expr[i + 1]) afterExp[idx++] = expr[i]; } } afterExp[idx] = 0; int flag = 1; for (i = 0; i < idx; i++) { if (afterExp[i] >= 'a' && afterExp[i] <= 'z') { int tmp = afterExp[i] - 'a' + 1; if (plus[afterExp[i] - 'a'] == -1) tmp++; if (minus[afterExp[i] - 'a'] == -1) tmp--; if (flag) val += tmp; else val -= tmp; } else if (afterExp[i] == '-') flag = 0; else if (afterExp[i] == '+') flag = 1; } print(); } int main() { while (gets(tmpStr)) { int i; int idx = 0; for (i = 0; i < strlen(tmpStr); i++) { if ((tmpStr[i] >= 'a' && tmpStr[i] <= 'z') || tmpStr[i] == '+' || tmpStr[i] == '-') expr[idx++] = tmpStr[i]; } expr[idx] = 0; evaluateAndPrint(); } return 0; }
相关推荐
ISO 20140-2:2018 Automation systems and integration - Evaluating
本教程“Matlab Tutorial - 55 - Evaluating Derivatives at a Point”旨在介绍如何利用Matlab来求解一元或多元函数在指定点的导数值。以下是关于这一主题的详细知识: 1. **符号计算和函数定义**:在Matlab中,...
根据提供的文件信息,下面详细阐述了关于开源平台用于评估轻量级加密算法的硬件实现的知识点。 ...轻量级加密算法是设计用于资源受限环境的密码算法,其特点是占用较少的计算资源,如内存和处理能力,同时保持足够的...
Evaluating Python expressions
在线社会网络(Online Social Networks,OSNs)是指互联网上形成的社交网络系统,它允许用户发表内容、分享信息、记录日常生活以及表达个人意见。随着在线社交网络站点的快速普及,其已成为人们获取信息、表达观点的...
本项目“Evaluating-Infix-Expressions-with-Swift”提供了一个解决方案,允许快速有效地评估中缀表达式。它可能包括一个解析器和一个计算引擎,用于将中缀表达式转换为可执行的形式,并返回结果。 首先,我们需要...
TF Model building/training/evaluating for simple nlp task just by params configuration, training/evaluating monitor and params configure GUI with streamlit.
Appendix C - Tuning Your Backup and Recovery Application Appendix D - Disaster Recovery Planning Kit—From End to Beginning Appendix E - Business Impact Analysis Planning Kit—The Storm Before ...
完整英文版 ISO 20140-2:2018 Automation systems and integration - Evaluating energy efficiency and other factors of manufacturing systems that influence the environment:Environmental performance ...
标准可以在这里免费获取:https://store.ies.org/product/tm-30-20-ies-method-for-evaluating-light-source-color-rendition/ 软件输入测试光源的光谱功率分布(SPD),该分布应为401x1矩阵,代表测试光源SPD在380 -...
Evaluating harmonic-induced transformer heating-00484029pdf,Evaluating harmonic-induced transformer heating-00484029
USCAR-10-REV2 TEST FOR EVALUATING THE TORQUE-TENSION RELATIONSHIP OF BOTH EXTERNAL AND INTERNAL METRIC THREADED FASTENERS.pdf
《Evaluating Derivatives Principles and Techniques of AD-2》是一部深入探讨自动微分算法的经典著作,由著名的AD软件包ADOL-C的创始人撰写。本书作为系列的第二部分,重点介绍了自动微分技术中的高级主题,特别是...
IEC 60587-2022 Test methods for evaluating resistance to tracking and erosion.pdf
Information Retrieval Implementing and Evaluating Search Engines Stefan B¨uttcher Charles L. A. Clarke Gordon V. Cormack
c) 2005IEEEtkde Toward the next generation of recommender systems - A survey of the state-of-the-art and possible extensions 3. 动手能力(实践算法-入门篇) a) 2004ACMtois Item-based top-N ...
GMW 15424-2012 Procedure for Evaluating Parting Lines.pdf
c) 2005IEEEtkde Toward the next generation of recommender systems - A survey of the state-of-the-art and possible extensions 3. 动手能力(实践算法-入门篇) a) 2004ACMtois Item-based top-N recommendation...
"evaluating indicator.rar"这个压缩包文件显然是为了提供五种常用的分类模型性能测试指标,这有助于我们理解并比较不同模型的预测能力。下面,我们将深入探讨这些指标及其应用。 1. 准确率(Accuracy):这是最...