BUPT OJ144 SmallTalk

来源:互联网 发布:java水晶报表教程 编辑:程序博客网 时间:2024/05/07 07:54

题目描述

Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and others during the 1970s. The language was first generally released as Smalltalk-80. Smalltalk-like languages are in continuing active development, and have gathered loyal communities of users around them. ANSI Smalltalk was ratified in 1998 and represents the standard version of Smalltalk.
Expressions
An expression can include multiple message sends. In this case expressions are parsed according to a simple order of precedence. Unary messages have the highest precedence, followed by binary messages, followed by keyword messages. For example:
3 factorial + 4 factorial between: 10 and: 100
is evaluated as follows:
1. 3 receives the message "factorial" and answers 6
2. 4 receives the message "factorial" and answers 24
3. 6 receives the message "+" with 24 as the argument and answers 30
4. 30 receives the message "between:and:" with 10 and 100 as arguments and answers true
The answer of the last message sent is the result of the entire expression.
Parentheses can alter the order of evaluation when needed. For example,
(3 factorial + 4) factorial between: 10 and: 100
will change the meaning so that the expression first computes "3 factorial + 4" yielding 10. That 10 then receives the second "factorial" message, yielding 3628800. 3628800 then receives "between:and:", answering false.
Note that because the meaning of binary messages is not hardwired into Smalltalk-80 syntax, all of them are considered to have equal precedence and are evaluated simply from left to right. Because of this, the meaning of Smalltalk expressions using binary messages can be different from their "traditional" interpretation.
Now, you want to implement some of Smaltalk-80's features. Give you a Smaltalk-80 expression, and your task is to evaluate the value of that expression. To simplify the problem, expressions will only contain numbers from 1-9 and +, -, *, / .

输入格式

The input has multiple expressions. Each line describes an expression. The expressions will only contain numbers from 1-9 and +, -, *, / (similar to their meaning in C/C++). There are at most 50 characters in a line.

输出格式

Output the value of the expresion.

输入样例

1+15/21+1*1

输出样例

222

热身赛R2C, 不需要考虑优先级的算式计算, 想太多就会WA →_→

知道的题意应该没有做不出的道理



/*USER_ID: test#birdstormPROBLEM: 144SUBMISSION_TIME: 2014-03-15 11:09:17*/#include <stdio.h> main(){    int ans, tmp, flag=0;    char ch;    while(scanf("%d%c",&tmp,&ch)==2){        if(!flag) ans=tmp;        else if(flag==1) ans+=tmp;        else if(flag==2) ans-=tmp;        else if(flag==3) ans*=tmp;        else if(flag==4) ans/=tmp;        if(ch=='\n') printf("%d\n",ans),flag=0,ans=0;        if(ch=='+') flag=1;        if(ch=='-') flag=2;        if(ch=='*') flag=3;        if(ch=='/') flag=4;    }    return 0;}


0 0
原创粉丝点击