Write a C++ program that takes as input an arithmetic expression. The program ou
ID: 665282 • Letter: W
Question
Write a C++ program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions (25+(3-6) * 8) and 7+8*2 contains matching grouping symbols. However expressions 5+((13+7)/8-2*9 does not contain matching grouping symbols.
Assign the left member of all grouping symbols to a linked list stack and remove the symbols as matches are found in each expression. At the end of your evaluation, display a message indicating if all leftmost grouping symbols are correctly matched.
Explanation / Answer
Here i used the stack to write the program. In the below program i had given a text file as the input, in that text file i am saving an arithmetic expression. If there is no arithmetic expression found then i am displaying the message. once try this code and this will work fine. And all you have to do is to give input from console istead of a text file. #include #include #include #include #include using namespace std; const string LEFTGROUP="{[("; const string RIGHTGROUP="}])"; int main() { char ch; stack stk; ifstream fin; fin.open("G:/assign08stackspq/pr8_1.txt"); if(fin.fail()) coutch; if(stk.top()==LEFTGROUP[0]) { if(ch==RIGHTGROUP[0]) stk.pop(); } else if(stk.top()==LEFTGROUP[1]) { if(ch==RIGHTGROUP[1]) stk.pop(); } else if(stk.top()==LEFTGROUP[2]) { if(ch==RIGHTGROUP[2]) stk.pop(); } } fin.close(); if(!stk.empty()) coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.