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() { // Declaration of variabless string sentence, cont; char charsent[50], c1, c2; // Creation of STACK and QUEUE STACK<char> S; QUEUE<char> Q; S.CreateStack(); Q.CreateQueue(); // While-loop for continue while(true) { cout << endl; cout << "Enter a sentence: "; getline(cin, sentence); strcpy(charsent, sentence.c_str()); // Pushing user input into STACK and QUEUE for (int i = 0; i < sentence.size(); i++) { c1 = charsent[i]; // Checking for alphanumeric if (isalnum(c1)) { S.Push(c1); Q.Push(c1); } else { // do nothing } } // STACK and QUEUE comparision while (!S.EmptyStack()) { c1 = S.Pop(); c2 = Q.Pop(); c1 = toupper(c1); c2 = toupper(c2); if (c1 != c2) { break; } } // If STACK and QUEUE was emptied with no break, Palindrome found if (S.EmptyStack() && Q.EmptyQueue()) { cout << "[" << sentence << "] is a Palindrome!" << endl; } else { cout << "[" << sentence << "] is NOT Palindrome." << endl; } // While-loop continue check cout << endl << "CONTINUE (y/n)? "; cin >> cont; if (cont != "y") { break; } else { // Clearing the STACK and QUEUE for new user input while(!S.EmptyStack() || !Q.EmptyQueue()) { S.Pop(); Q.Pop(); } } cin.ignore(); } 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; }