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();
		
	}
	
} 

 

發佈了160 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章