中置式を後置式に変換する必要があるデータ構造コースの割り当てを行っています。ほぼ完了しましたが、a+b+c のようなものを入力しようとするとエラーが発生し続けます
a+b と a+b*c を問題なく処理できます。
何が問題なのか本当にわかりません。誰かが私に方向性を示したり、私のコードの問題を見つけたりすることができれば、本当に感謝しています.
#include <iostream>
#include <stack>
using namespace std;
//checks priority of operators.
int priority(char e){
int pri = 0;
if(e == '*' || e == '/' || e == '%'){
pri = 2;
}else{
if(e == '+' || e == '-'){
pri = 1;
}
}
return pri;
}
void main(){
cout << "This program will convert an infix expression to a postfix expression." << endl;
cout << "Please enter your expression without any spaces." << endl;
stack<char> charStack;
char input[100];
char output[100];
char n1;
char *o;
o = &output[0];
cin >> input;
int n = 0;
while(input[n] != 0){
if(isdigit(input[n]) || isalpha(input[n])){
*o = input[n];
n++;
o++;
}
if(input[n] == '('){
charStack.push(input[n]);
n++;
}
if(input[n] == ')'){
n1 = charStack.top();
charStack.pop();
while(n1 != '('){
*o = n1;
o++;
n1 = charStack.top();
charStack.pop();
}
n++;
}
if(input[n] == '+' || input[n] == '-' || input[n] == '*' || input[n] == '/' || input[n] == '%'){
if(charStack.empty() == true){
charStack.push(input[n]);
}else{
n1 = charStack.top();
charStack.pop();
while(priority(n1) >= priority(input[n])){
*o = n1;
o++;
n1 = charStack.top();
charStack.pop();
}
charStack.push(n1);
charStack.push(input[n]);
}
n++;
}
}
while(!charStack.empty()){
*o = charStack.top();
o++;
charStack.pop();
}
*o = '\0';
cout << output << endl;
}