並一一讀取。
讀到運算元就丟入運算元的堆疊中,
讀到運算子就丟入運算子的堆疊中,
並判斷上一個運算子的優先度是否大於等於它,
若是的話,就一直執行,直到遇到比它小的運算子才放進堆疊中。
唯'('和')'比較特別,
丟入堆疊時:
( > * = / = % > + > - > ),
判斷時:
* = / = % > + > - > ( 。
遇到')',要一直輸出,只要輸出到'(',就把'('消去,也不把自己')'存進堆疊中。
這樣做完即可得解。
[C++](6ms, 766KB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<sstream> | |
#include<cctype> | |
using namespace std; | |
int main() | |
{ | |
string s; | |
while( getline( cin, s ) ) | |
{ | |
string oper; | |
stringstream ss; | |
char operators[1000] = {0}; | |
int operands[1000] = {0}; | |
int operators_length = 0; | |
int operands_length = 0; | |
ss.clear(); | |
ss.str(s); | |
while( ss >> oper ) | |
{ | |
if( isdigit(oper[0]) ) | |
{ | |
int number = 0; | |
for( int i = 0 ; i < oper.length() ; i++ ) | |
{ | |
number *= 10; | |
number += oper[i]-'0'; | |
} | |
operands[operands_length++] = number; | |
} | |
else | |
{ | |
switch( oper[0] ) | |
{ | |
case '+': case '-': | |
while( operators_length > 0 ) | |
{ | |
if( operators[operators_length-1] == '+' ) | |
{ | |
operands[operands_length-2] += operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else if( operators[operators_length-1] == '-' ) | |
{ | |
operands[operands_length-2] -= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else if( operators[operators_length-1] == '*' ) | |
{ | |
operands[operands_length-2] *= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else if( operators[operators_length-1] == '/' ) | |
{ | |
operands[operands_length-2] /= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else if( operators[operators_length-1] == '%' ) | |
{ | |
operands[operands_length-2] %= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else break; | |
} | |
operators[operators_length++] = oper[0]; | |
break; | |
case '*': case '/': case '%': | |
while( operators_length > 0 ) | |
{ | |
if( operators[operators_length-1] == '*' ) | |
{ | |
operands[operands_length-2] *= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else if( operators[operators_length-1] == '/' ) | |
{ | |
operands[operands_length-2] /= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else if( operators[operators_length-1] == '%' ) | |
{ | |
operands[operands_length-2] %= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
else break; | |
} | |
operators[operators_length++] = oper[0]; | |
break; | |
case '(': | |
operators[operators_length++] = oper[0]; | |
break; | |
case ')': | |
for( int i = operators_length-1; operators[i] != '(' ; i-- ) | |
{ | |
if( operators[i] == '+' ) | |
{ | |
operands[operands_length-2] += operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
if( operators[i] == '-' ) | |
{ | |
operands[operands_length-2] -= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
if( operators[i] == '*' ) | |
{ | |
operands[operands_length-2] *= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
if( operators[i] == '/' ) | |
{ | |
operands[operands_length-2] /= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
if( operators[i] == '%' ) | |
{ | |
operands[operands_length-2] %= operands[operands_length-1]; | |
operands_length--; | |
operators_length--; | |
} | |
} | |
operators_length--; | |
break; | |
} | |
} | |
} | |
for( operators_length-- ; operators_length >= 0 ; operators_length-- ) | |
{ | |
if( operators[operators_length] == '+' ) | |
{ | |
operands[operands_length-2] += operands[operands_length-1]; | |
operands_length--; | |
} | |
if( operators[operators_length] == '-' ) | |
{ | |
operands[operands_length-2] -= operands[operands_length-1]; | |
operands_length--; | |
} | |
if( operators[operators_length] == '*' ) | |
{ | |
operands[operands_length-2] *= operands[operands_length-1]; | |
operands_length--; | |
} | |
if( operators[operators_length] == '/' ) | |
{ | |
operands[operands_length-2] /= operands[operands_length-1]; | |
operands_length--; | |
} | |
if( operators[operators_length] == '%' ) | |
{ | |
operands[operands_length-2] %= operands[operands_length-1]; | |
operands_length--; | |
} | |
} | |
cout << operands[0] << endl; | |
} | |
return 0; | |
} |
0 意見:
張貼留言