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
- 中綴表示式轉化為字尾表示式並求值
- 聊聊JavaScript和Scala的表示式 ExpressionJavaScriptExpress
- 05.表示式目錄樹ExpressionExpress
- 逆波蘭表示式求值 golang VS pythonGolangPython
- Leetcode——150. 逆波蘭表示式求值LeetCode
- LeetCode-150- 逆波蘭表示式求值LeetCode
- ABAP mesh表示式, JavaScript和Scala的 expressionJavaScriptExpress
- [C# Expression] 之動態建立表示式C#Express
- 逆波蘭表示式求值——棧與佇列佇列
- 力扣-150. 逆波蘭表示式求值力扣
- 【c#表示式樹】最完善的表示式樹Expression.Dynamic的玩法C#Express
- 利用Lambda表示式進行Java中的惰性求值Java
- 一種簡易的表示式求值演算法演算法
- 利用 Lambda 表示式實現 Java 中的惰性求值Java
- PostgreSQL 原始碼解讀(164)- 查詢#84(表示式求值)SQL原始碼
- 使用棧實現表示式求值,運用棧計算
- 算數表示式求值--c語言課程設計C語言
- 4、逆波蘭表示式求值——棧(java資料結構)Java資料結構
- C#動態查詢:巧用Expression組合多條件表示式C#Express
- 資訊學奧賽複賽複習09-CSP-J2020-03表示式求值前置知識點-中綴表示式求值、摸運算、模運算性質、棧
- LeetCode 之 JavaScript 解答第150題 —— 逆波蘭表示式求值(Evaluate Reverse Polish Notation)LeetCodeJavaScript
- Javascript函式引數求值——Thunk函式JavaScript函式
- [LeetCode Python3]10. Regular Expression Matching手把手詳解——正規表示式(一)LeetCodePythonExpress
- 前端菜鳥的每週一道演算法題(一) - 逆波蘭表示式求值前端演算法
- PAT6-2 多項式求值
- x86彙編反編譯到c語言之——(1)表示式求值及賦值語句編譯C語言賦值
- 通過建立動態型別 動態構建Expression Select表示式來控制Property可見性型別Express
- 函式正規化入門(惰性求值與函式式狀態)函式
- 表示式
- 中綴表示式轉字尾表示式
- JavaScript 表示式JavaScript
- Cron 表示式
- cron表示式
- lambda 表示式
- lambda表示式
- el 表示式