144. SmallTalk //表达式求值

时间限制 1000 ms 内存限制 65536 KB

题目描述

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+1
5/2
1+1*1

输出样例

2
2
2

AC代码:

#include<bits/stdc++.h>
using namespace std;
int suan(int a,int b,char f)
{
	if(f=='+')return a+b;
	if(f=='-')return a-b;
	if(f=='*')return a*b;
	if(f=='/')return a/b;
}
int main()
{
	char a[53];
	while(~scanf("%s",a))
	{
		stack<int>s;
		stack<char>f;
		while(s.empty()!=true)s.pop();
		while(f.empty()!=true)f.pop();
		for(int i=0;i<strlen(a);++i)
		{
			if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')
			{
				while(f.empty()!=true)
				{
					char fu=f.top();
					f.pop();
					int b=s.top();
					s.pop();
					int a=s.top();
					s.pop();
					s.push(suan(a,b,fu));
					//printf("%d\n",suan(a,b,fu));
				}
				f.push(a[i]);
			}
			
			else
			{
				int x=0;
				while(a[i]>='0'&&a[i]<='9')
					x=x*10+a[i++]-'0';
				s.push(x);
				i--;
				//if(i>=strlen(a)) break;
			}
			
				
		}
		while(f.empty()!=true)
		{
			char fu=f.top();
			f.pop();
			int b=s.top();
			s.pop();
			int a=s.top();
			s.pop();
			s.push(suan(a,b,fu));
			//printf("%d\n",suan(a,b,fu));
		}
		printf("%d\n",s.top());
		s.pop();
		
	}
	
} 

        此题不需考虑各运算符的优先级,刚开始忘记题干这个要求了,按正常优先级做的,怎么提交都错,最后又读了一遍题,发现不需要考虑运算符优先级emmmm,读清楚题干真的很重要!!!

正常优先级求表达式代码:

#include<bits/stdc++.h>
using namespace std;
int suan(int a,int b,char f)
{
	if(f=='+')return a+b;
	if(f=='-')return a-b;
	if(f=='*')return a*b;
	if(f=='/')return a/b;
}
int main()
{
	char a[53];
	while(~scanf("%s",a))
	{
		stack<int>s;
		stack<char>f;
		while(s.empty()!=true)s.pop();
		while(f.empty()!=true)f.pop();
		for(int i=0;i<strlen(a);++i)
		{
			if(a[i]=='+'||a[i]=='-')
			{
				while(f.empty()!=true)
				{
					char fu=f.top();
					f.pop();
					int b=s.top();
					s.pop();
					int a=s.top();
					s.pop();
					s.push(suan(a,b,fu));
					//printf("%d\n",suan(a,b,fu));
				}
				f.push(a[i]);
			}
			else if(a[i]=='*'||a[i]=='/')
			{
				while(f.empty()!=true&&(f.top()=='*'||f.top()=='/'))
				{
					char fu=f.top();
					f.pop();
					int b=s.top();
					s.pop();
					int a=s.top();
					s.pop();
					s.push(suan(a,b,fu));
					//printf("%d\n",suan(a,b,fu));
				}
				f.push(a[i]);
			}
			else
			{
				int x=0;
				while(a[i]>='0'&&a[i]<='9')//处理多位数字 
					x=x*10+a[i++]-'0';
				s.push(x);
				i--;
				//if(i>=strlen(a)) break;
			}
			
				
		}
		while(f.empty()!=true)
		{
			char fu=f.top();
			f.pop();
			int b=s.top();
			s.pop();
			int a=s.top();
			s.pop();
			s.push(suan(a,b,fu));
			//printf("%d\n",suan(a,b,fu));
		}
		printf("%d\n",s.top());
		s.pop();
		
	}
	
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值