int main() { //declare objects of stack STACK<char, 100> P; char SENT; P.MakeStack(); //collect the sentence cout << "Please, type a sentence: "; cin.get(SENT); //read first character //collect the sentence a character at a time while( SENT!='\n' ) { if( isalpha(SENT) ) P.PushStack(SENT); cin.get(SENT); //get next character } //display sentence in reverse (*reverse is normal in stack*) while( !P.EmptyStack()!='\0' ) cout<<P.PopStack(); cout<<endl; //end program system("PAUSE"); return 0; }
int main() { char con; do { char num[20]; int count = 0; string out; int OperatorCount = 0; int ParenNumCount = 0; STACK <char, 20> loadNum; STACK <char, 20> loadOperator; STACK <char, 20> flipStack; STACK <char, 20> FlipOperator; cout << "Enter an infix expression:"; cin.getline(num,20); loadNum.ClearStack(); loadOperator.ClearStack(); flipStack.ClearStack(); FlipOperator.ClearStack(); NumCheck: while (num[count] != '$') { if (num[count] == '(') { count++; while (num[count] != ')') { if (isalpha(num[count])) { loadNum.PushStack(num[count]); count++; ParenNumCount++; } else if (num[count] == '*' || num[count] == '+' || num[count] == '-' || num[count] == '/') { loadOperator.PushStack(num[count]); count++; OperatorCount++; } } count++; goto NumCheck; } if (isalpha(num[count])) { loadNum.PushStack(num[count]); count++; goto NumCheck; } if (num[count] == '*' || num[count] == '+' || num[count] == '-' || num[count] == '/') { loadOperator.PushStack(num[count]); count++; OperatorCount++; } if (num[count] == '$') { goto NumCheck; } } //flip while (!loadNum.EmptyStack()) { flipStack.PushStack(loadNum.PopStack()); } while (!loadOperator.EmptyStack()) { FlipOperator.PushStack(loadOperator.PopStack()); } count = 0; //output if (ParenNumCount % 2 == 0) { while (!flipStack.EmptyStack()) { if (count % 2 == 0) { out += flipStack.PopStack(); out += flipStack.PopStack(); if (OperatorCount >= 0) { out += FlipOperator.PopStack(); OperatorCount--; } count++; } else { out += flipStack.PopStack(); if (OperatorCount >= 0) { out += FlipOperator.PopStack(); OperatorCount--; } } } } else if (ParenNumCount % 2 != 0) { while (!flipStack.EmptyStack()) { out += flipStack.PopStack(); } while (!FlipOperator.EmptyStack()) { out += FlipOperator.PopStack(); } } cout << "Postfix form of the expression is:" << out << endl; cout << "Continue y/n:"; cin >> con; cin.ignore(); } while (con != 'n'); system("pause"); return 0; }