c++スタックを用いて4つの接尾辞式の接尾辞式を簡単に実現し、値を計算する.
6512 ワード
, , 。
, , ,
:
A. , :
a. b. c. d. e. , f. 1). 2). , , , 1)、2) 3). b B. , : a. b. , , pop push 。 b , 。
次は直接コードを貼ります.コード注釈は基本的にプログラムを速く読むのに役立ちます.
#include
#include
using namespace std;
int getPriority(char c); //
bool isLeft(char c); //
bool isRight(char c); //
bool isOperator(char c); // (+-*/)
bool isNumber(char c); //
double calculate(string postfix); // ,
string convertToPostfix(const string& expression); //
int main() {
stack operatorStack;
string expression,postfix;
cout << " :"<"< operatorStack;
string postfix;
//
for(int current = 0; current < expression.length(); current++){
char c = expression.at(current);
if(!isNumber(c) && !isOperator(c) && c !='(' && c != ')'){
cout << " !" << endl;
exit(0);
}
if(c != ' ' && c != '='){
//
if(isLeft(c)) {
operatorStack.push(c);
} else if (isRight(c)){ // pop
while(!operatorStack.empty() && operatorStack.top() != '('){
postfix.append(1,operatorStack.top());
operatorStack.pop();
}
operatorStack.pop(); // pop
} else if(isOperator(c)){ //
if(current+1 < expression.length()){
if (isOperator(expression.at(current+1))) {
cout << " ";
exit(0);
}
}
if(current-1 >= 0){
if(isOperator(expression.at(current-1))){
cout << " ";
exit(0);
}
}
if(operatorStack.empty()){ //
operatorStack.push(c);
} else{ // , ,
while(getPriority(c) <= getPriority(operatorStack.top())){
postfix.append(1,operatorStack.top());
operatorStack.pop();
if(operatorStack.empty())
break;
}
operatorStack.push(c);
}
} else { //
postfix.append(1,c); // , $
if(current+1 < expression.length()){
if (!isNumber(expression.at(current+1))){
postfix.append(1,'#');//
}
}
}
}
}
//
while(!operatorStack.empty()){
if(operatorStack.top() == '(' || operatorStack.top() == ')'){
cout << " , !";
exit(0);
}
postfix.append(1,operatorStack.top());
operatorStack.pop();
}
return postfix;
}
//
int getPriority(char c){
if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return 0;
}
bool isRight(char c){
if(c == ')')
return true;
else
return false;
}
bool isLeft(char c){
if(c == '(')
return true;
else
return false;
}
bool isOperator(char c){
if(c == '*' || c == '/' || c == '+' ||c == '-')
return true;
else
return false;
}
double calculate(string postfix){
stack result;
char c;
for (int i = 0; i < postfix.length();i++) {
c = postfix.at(i);
if (isOperator(c)) {
double a = result.top();
result.pop();
double b = result.top();
result.pop();
double temp;
switch (c) {
case '+' :
temp = b + a;
break;
case '-' :
temp = b - a;
break;
case '/' :
if(a == 0){
cout << " !";
exit(0);
}
temp = b / a;
break;
case '*' :
temp = b * a;
default:
break;
}
result.push(temp);
} else if(isNumber(c)){
double num = c - '0';
while(isNumber(postfix.at(i+1))){
num = num*10+ (postfix.at(i+1) - '0');
i++;
}
result.push(num);
}
}
return result.top();
}
bool isNumber(char c){
if(c>='0'&& c<='9')
return true;
else
return false;
}
もちろん、コードには、左右の括弧が一致していないか、除数がゼロであるか、複数の数をサポートするために、わざわざ数の終わりに接尾辞式の計算値を1つ追加したときに識別するなど、式の誤りを判別するセグメントが含まれています.
postfix.append(1,c); // , # if(current+1 < expression.length()){
if (!isNumber(expression.at(current+1))){
postfix.append(1,'#');//
}
}
#