Modify the postfix program below to handle both subtraction and division: Sample
ID: 3542620 • Letter: M
Question
Modify the postfix program below to handle both subtraction and division:
Sample input: ./a.out 4 10 - 6 x 12 / Sample output: -3
#include <iostream> #include <cstring> #include "stack.cpp" using namespace std; int main(int argc, char *argv[]) { char *a = argv[1]; int i, div, N = strlen(a); stack<int> save(N); for(i = 0; i<N; i++) { if(a[i] == '+') save.push(save.pop() + save.pop()); if(a[i] == '*') save.push(save.pop() * save.pop()); if(a[i] == '-') save.push((-1)*save.pop() + save.pop()); if(a[i] == '/') { div = save.pop(); save.push(save.pop() / div); } if((a[i] >= '0') && (a[i] <= '9')) save.push(a[i] -'0'); //while ((a[i] >= '0') && (a[i] <= '9')) // save.push(10*save.pop() + (a[i++]-'0')); } cout << save.pop() <<endl; } Explanation / Answer
`int main(int argc, char *argv[]) {` char *a = argv[1]; int i, div, N = strlen(a); stack save(N); cout > a; for(i = 0; i= '0') && (a[i] = '0') && (a[i]Related 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.