POJ-3337 Expression Evaluator-表示式求值
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2027 | Accepted: 616 |
Description
This problem is about evaluating some C-style expressions. The expressions to be evaluated will contain only simple integer variables and a limited set of operators; there will be no constants in the expressions. There are 26 variables in the program, named by lower case letters a through z. Before evaluation, the initial values of these variables area = 1,b = 2, ..., z = 26.
The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expressiona +c - d + b has the value 2 (1 + 3 - 4 + 2). Additionally, ++ and –- operators are allowed in the input expression too, which are unary operators, and may come before or after variables. If the ++ operator comes before a variable, then that variable's value is increased (by one) before the variable's value is used in calculating the value of the whole expression. Thus the value of ++c -b is 2. When ++ comes after a variable, that variable is increased (by one) after its value is used to calculate the value of the whole expression. So, the value of thec ++ -b is 1, though c is incremented after the value for entire expression is computed; its value will be 4 too. The -- operator behaves the same way, except that it decreases the value of its operand.
More formally, an expression is evaluated in the following manner:
- Identify every variable that are preceded by ++. Write an assignment statement for incrementing the value of each of them, and omit the ++ from before that variable in the expression.
- Do similarly for the variables with ++ after them.
- At this point, there is no ++ operator in the expression. Write a statement evaluating the remaining expression after the statements determined in step 1, and before those determined in step 2.
- Execute the statements determined in step 1, then those written in step 3, and finally the one written in step 2.
This way, evaluating ++ a + b ++ is the same as computing a = a + 1, result = a + b, and b = b + 1.
Input
The first line of the input contains a single integer T which is the number of test cases, followed byT lines each containing the input expression for a test case. Ignore blanks in the input expression. Be sure that no ambiguity is in the input expressions (likea+++b). Similarly, ++ or -- operators do not appear both before and after one single variable (like ++a++). You may safely assume each variable appears only once in an expression.
Output
For each test case, write each expression as it appears in the input (exactly), then write the value of the complete expression. After this, on separate lines, write the value of each variable after evaluating the expression (write them in sorted order of the variable names). Write only the values of the variables that are used in the expressions. To find out about the output format, follow the style used in the sample output below.
Sample Input
2 a+b c+f--+--a
Sample Output
Expression: a+b value = 3 a = 1 b = 2 Expression: c+f--+--a value = 9 a = 0 c = 3 f = 5
Source
/*
*Copyright (c)2015,煙臺大學計算機與控制工程學院
*All rights reserved.
*作 者:單昕昕
*完成日期:2015年8月22日
*版 本 號:v1.0
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int k,i,t,l,v[26],val,b,c,flag;
char s[1000];
cin>>t;//測試用例數
getchar();
while(t--)
{
bool occur[26];//標記字母是否用過
//以下作初始化
memset(occur,false,sizeof(occur));
for(i=0; i<26; ++i)
v[i]=i+1;
val=0;
b=1;
gets(s);
l=strlen(s);
cout<<"Expression: "<<s<<endl;//輸出表示式
for(i=0; i<l; ++i)//去掉表示式中的空格!!!這個一定要有,一開始以為木有關係結果WA了11次!
{
if(s[i]==' ')
{
k=i;
for(; s[k]!='\0'; ++k)
s[k]=s[k+1];
--i;
}
}
for(k=0; k<l; ++k)
{
//是運算子
if((s[k]=='+')||(s[k]=='-'))
{
if(((s[k]=='+'&&s[k+1]=='+')||(s[k]=='-'&&s[k+1]=='-'))&&(s[k+2]<='z'&&s[k+2]>='a'))//如果是++或--
{
v[int(s[k+2]-'a')]+=(s[k]=='+'?1:-1);
++k;
c=int(s[k+1]-'a');
occur[c]=true;
}
else
b=(s[k]=='+'?1:-1);//只是+或-
}
//是變數
else if(s[k]>='a'&&s[k]<='z')
{
flag=0;//標記
c=int(s[k]-'a');
val+=b*v[c];
if(!occur[c])//沒用過
{
flag=1;
occur[c]=true;
}
if(flag==1&&((s[k+1]=='+'&&s[k+2]=='+')||(s[k+1]=='-'&&s[k+2]=='-')))//變數++或--
{
v[c]+=(s[k+1]=='+'?1:-1);
k+=2;
}
}
}
cout<<"value = "<<val<<endl;//輸出各個值
for(i=0; i<26; ++i)
if(occur[i])
cout<<char('a'+i)<<" = "<<v[i]<<endl;
}
return 0;
}
呵呵呵呵噠,WA11次的慘痛經歷。
對著書上的提示看了半天寫了半天,好不容易編譯木有錯誤了。。結果一直WA的心情真是難以言表。。。
相關文章
- 3.2.5 表示式求值
- Java表示式求值引擎 - AviatorJava
- 動態拼接表示式——ExpressionExpress
- 正規表示式(regular expression)Express
- Oracle正規表示式(regular expression)OracleExpress
- CF552E 字串 表示式求值字串
- 05.表示式目錄樹ExpressionExpress
- 聊聊JavaScript和Scala的表示式 ExpressionJavaScriptExpress
- 正規表示式regular expression詳述Express
- 逆波蘭表示式求值 golang VS pythonGolangPython
- 棧在表示式求值中的應用
- ABAP mesh表示式, JavaScript和Scala的 expressionJavaScriptExpress
- [C# Expression] 之動態建立表示式C#Express
- oracle中的正規表示式(regular expression)OracleExpress
- 字尾表示式的求值的演算法演算法
- 力扣-150. 逆波蘭表示式求值力扣
- 【c#表示式樹】最完善的表示式樹Expression.Dynamic的玩法C#Express
- [CareerCup] 9.11 Parenthesize the Expression 表示式加括號Express
- vi 中的正規表示式 (Regular Expression)(轉)Express
- LeetCode-150- 逆波蘭表示式求值LeetCode
- 利用 Lambda 表示式實現 Java 中的惰性求值Java
- 利用Lambda表示式進行Java中的惰性求值Java
- 一種簡易的表示式求值演算法演算法
- Leetcode——150. 逆波蘭表示式求值LeetCode
- IIFE (Imdiately Invoked Function Expression 立即執行的函式表示式)FunctionExpress函式
- [Shell] awk學習(3)-regular expression 正規表示式Express
- 4、逆波蘭表示式求值——棧(java資料結構)Java資料結構
- 算數表示式求值--c語言課程設計C語言
- 使用棧實現表示式求值,運用棧計算
- [LeetCode] Regular Expression Matching 正規表示式匹配LeetCodeExpress
- oracle的正規表示式(regular expression)簡單介紹OracleExpress
- 分享開源表示式解析器IK-Expression2.0Express
- 【資料結構】棧的應用---四則運算表示式求值(中綴表示式與字尾表示式轉換)資料結構
- PostgreSQL 原始碼解讀(164)- 查詢#84(表示式求值)SQL原始碼
- 【演算法】E.W.Dijkstra算術表示式求值演算法
- [CareerCup] 5.4 Explain Expression ((n & (n-1)) == 0) 解釋表示式AIExpress
- JavaFX教程-增量式求值和懶惰求值Java
- 請問:Cron-Expression如何表示?Express