Redo Programming Exercise 10 of Chapter 17 so that it uses the STL class stack to convert the infix expressions to postfix expressions

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

c++

Redo Programming Exercise 10 of Chapter 17 so that it uses the STL class stack to convert the infix expressions to postfix expressions.

Instructions
Instructions
Redo Programming Exercise 10 of Chapter 17 so that it uses the STL
class stack to convert the infix expressions to postfix expressions.
Grading
When you have completed your program, click the Submit button to
record your score.
infixData.txt
1 A+B-C;
2 (A+B)*C;
3 (A+B)*(C-D);
4 A+((B+C)*(E-F)-G)/(H-I);
5 A+B* (C+D)-E/F*G+H;
6
infixData.txt
infixToPostFixlmp.cpp| infixToPostfix.h
infixToPostFixImp.cpp infixToPostfix.h
1 #include<iostream>
2 #include<string>
3 #include<fstream>
4 #include "infixToPostfix.h"
5 using namespace std;
6 int main() {
7
8
9
10
infixToPostfix InfixExp;
string infix;
cout << "Infix: ";
cin >> infix;
11
12
13
14
15 InfixExp.getInfix(infix);
16
// InfixExp.showInfix();
cout << "Postfix: ";
22 return 0;
23 }
24
17
18
19 InfixExp.showPostfix();
20
cout << endl;
21
main.cpp
main.cpp
| +
+
infixData.txt
1 #include<iostream>
2 #include<stack> // for using stl stack
3 using namespace std;
4 class infixToPostfix {
5 string infix;
6 public:
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
infixToPostFixImp.cpp infixToPostfix.h
32
33
34
35
36
void getInfix (string x) {
this->infix = x;
}
void showInfix() {
}
//Function to return order_of_operatoredence of operators
int order_of_operator (char c)
if (c==¹^¹)
cout << this->infix << endl;
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
C
return 1;
else
return -1;
메
string convertInfixtoPostfix (string s) {
main.cpp
for (int i = 0; i < s.size()-1; i++) { // s.size()-1 as to avoid the ";"
char c = s[1];
if ((c>= 'a' && c <= ¹z¹) || (c>= 'A' && c <= 'Z') || (c >= '0' && c
final_string += c;
// If the scanned character is an
// '(', push it to the stack.
else if (c == '(')
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
61
62
63
stack<char> st; //For stack operations, we are using STL class stack accordi 64
string final_string;
65
66
}
67 };
68
59
60
}
6
7
8}
9
------
st.push('(');"
else if (c == ¹)') {
int main() {
while (st.top() != '(')
{
}
}
st.pop();
}
//If an operator is scanned
else {
final_string += st.top();
st.pop();
while (!st.empty() && order_of_operator (s[1]) <= order_of_operator
final_string+= st.top();
st.pop();
}
}
while (!st.empty()) {
st.push(c);
final_string+= st.top();
st.pop();
return final_string;
}
void showPostfix() {
string ans = convertInfixtoPostfix (this->infix);
cout << ans << endl;
infixData.txt
1 #include <iostream>
2
3 using namespace std;
4
infixToPostFixlmp.cpp | infixToPostfix.h
// Write your main here.
return 0;
main.cpp
+
Transcribed Image Text:Instructions Instructions Redo Programming Exercise 10 of Chapter 17 so that it uses the STL class stack to convert the infix expressions to postfix expressions. Grading When you have completed your program, click the Submit button to record your score. infixData.txt 1 A+B-C; 2 (A+B)*C; 3 (A+B)*(C-D); 4 A+((B+C)*(E-F)-G)/(H-I); 5 A+B* (C+D)-E/F*G+H; 6 infixData.txt infixToPostFixlmp.cpp| infixToPostfix.h infixToPostFixImp.cpp infixToPostfix.h 1 #include<iostream> 2 #include<string> 3 #include<fstream> 4 #include "infixToPostfix.h" 5 using namespace std; 6 int main() { 7 8 9 10 infixToPostfix InfixExp; string infix; cout << "Infix: "; cin >> infix; 11 12 13 14 15 InfixExp.getInfix(infix); 16 // InfixExp.showInfix(); cout << "Postfix: "; 22 return 0; 23 } 24 17 18 19 InfixExp.showPostfix(); 20 cout << endl; 21 main.cpp main.cpp | + + infixData.txt 1 #include<iostream> 2 #include<stack> // for using stl stack 3 using namespace std; 4 class infixToPostfix { 5 string infix; 6 public: 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 infixToPostFixImp.cpp infixToPostfix.h 32 33 34 35 36 void getInfix (string x) { this->infix = x; } void showInfix() { } //Function to return order_of_operatoredence of operators int order_of_operator (char c) if (c==¹^¹) cout << this->infix << endl; return 3; else if (c == '/' || c == '*') return 2; else if (c == '+' || c == '-') C return 1; else return -1; 메 string convertInfixtoPostfix (string s) { main.cpp for (int i = 0; i < s.size()-1; i++) { // s.size()-1 as to avoid the ";" char c = s[1]; if ((c>= 'a' && c <= ¹z¹) || (c>= 'A' && c <= 'Z') || (c >= '0' && c final_string += c; // If the scanned character is an // '(', push it to the stack. else if (c == '(') 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 61 62 63 stack<char> st; //For stack operations, we are using STL class stack accordi 64 string final_string; 65 66 } 67 }; 68 59 60 } 6 7 8} 9 ------ st.push('(');" else if (c == ¹)') { int main() { while (st.top() != '(') { } } st.pop(); } //If an operator is scanned else { final_string += st.top(); st.pop(); while (!st.empty() && order_of_operator (s[1]) <= order_of_operator final_string+= st.top(); st.pop(); } } while (!st.empty()) { st.push(c); final_string+= st.top(); st.pop(); return final_string; } void showPostfix() { string ans = convertInfixtoPostfix (this->infix); cout << ans << endl; infixData.txt 1 #include <iostream> 2 3 using namespace std; 4 infixToPostFixlmp.cpp | infixToPostfix.h // Write your main here. return 0; main.cpp +
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage