Question
Modify your Postfix Calculator to read entries from a file, one line at a time. For those entries that are valid, compute the value and
Modify your Postfix Calculator to read entries from a file, one line at a time. For those entries that are valid, compute the value and keep a running total. Display that sum and the number of invalid entries that were in the file.
We will consider a postfix expression to be invalid if:
An operation is encountered, but there are not at least two entries on the stack.
The postfix expression has been consumed, but there is not exactly one entry remaining on the stack.
CODE THAT NEEDS TO BE MODIFIED
#include
// 'sum' variable to keep track of the total sum of all the expressions long sum = 0;
// i keeps track of the number of line currently read from the file int lineCount = 1; // function to calculate the final value of each expression
int invalid_input = 0;
int value(char r) { if (r == 'I' || r == 'i') return 1; if (r == 'V' || r == 'v') return 5; if (r == 'X' || r == 'x') return 10; if (r == 'L' || r == 'l') return 50; if (r == 'C' || r == 'c') return 100; if (r == 'D' || r == 'd') return 500; if (r == 'M' || r == 'm') return 1000; return -1; } int romanToDecimal(string& str) { int res = 0; for (int i = 0; i < str.length(); i++) { int s1 = value(str[i]); if (i + 1 < str.length()) { int s2 = value(str[i + 1]); if (s1 >= s2) res = res + s1; else { res = res + s2 - s1; i++; } } else res = res + s1; } return res; }
void parseLine(const string& s) { // initialize a istringstream with the parameter string s istringstream stream(s); string linia;
// stack variable to store the operands of the input string stack
bool invalid = false;
while (stream >> linia) {
if (linia == "*" || linia == "+" || linia == "-" || linia == "/" || linia == "%") { if (stos.size() < 2) { // cout << "Error: not enough operands for operator " << linia << " on line " << lineCount << endl; invalid_input++; invalid = true; break; }
// pop out and store 2 operands in the variables a and b int b = stos.top(); stos.pop(); int a = stos.top(); stos.pop();
// Now push the apt expression onto the the stack if (linia == "*") stos.push(a * b); else if (linia == "+") stos.push(a + b); else if (linia == "-") stos.push(a - b); else if (linia == "/") stos.push(a / b); else if (linia == "%") stos.push(a % b); } // if the current linia is an operand i.e. a number else { // if linia is not a number if (!isdigit(linia[linia.length() - 1])) stos.push(romanToDecimal(linia)); else stos.push(stoi(linia)); } }
// print the current expression's value if (!invalid) { cout << "Line #" << lineCount << ": " << stos.top() << endl;
// update the sum variable sum += stos.top();
// emptying the stack if (!stos.empty()) stos.pop();
// increment i to update the line number } lineCount++; }
int main() { ifstream input; // open a file for input input.open("Postfix_1.txt");
// check if the file openend successfully if (!input) { cout << "Unable to open" << endl; } else { // 'line' variable to store the data of each line read from the file string line = "";
// read file until the end while (getline(input, line)) { // call parseLine() function to evaluate the expression // in the current 'line' parseLine(line); }
// print the value of the sum variable cout << " Sum of all expressions: " << sum;
cout << " Valid Entries = " << lineCount - invalid_input << endl << "Invalid Entries = " << invalid_input << endl; return 0; } }
Postfix_1.txt File
133 -167 - 79 196 / 13 125 139 -59 -168 163 17 / + / - + + + + -14 -46 54 128 * * % 147 2 + 10 / 45 * 191 % -188 % 171 45 * -132 % 31 % 93 + -90 - -91 % 176 - 80 147 - 89 + / 39 129 + 93 * -175 164 * 83 / % -93 163 - 4 * * 174 25 99 41 187 - 177 + / * % 105 33 % 1 / -37 27 -58 + 4 % + + 149 / -59 32 136 140 - / * 192 % 63 93 + + 167 % 21 -16 36 * - / 99 196 / 191 31 92 + * 57 - 58 198 * -57 + + 179 % 7 -102 -174 65 66 * / + - 37 193 / -17 28 103 109 * - 23 79 92 - -91 + 100 * + % + - 126 -3 + 111 18 % 35 / 163 - 119 78 + 73 124 191 + 44 57 168 - 14 / + -180 140 - * * + % 48 -38 -108 + + 131 -134 27 / 175 * 175 -90 - + % + 120 104 % 13 - 193 - 47 84 + -54 67 163 29 * % % + 159 13 * 2 100 56 47 / + * - 167 -152 * 168 192 -129 + 176 57 38 80 65 % + % / - * % 57 172 / -168 - 666 130 172 -82 -183 / - 117 - + 165 114 17 * / 154 % + 124 96 % + -133 173 + 9 107 - * 106 / 101 * -136 59 % * 159 -155 - 96 165 / 41 33 - 172 -148 - 148 38 -129 * / - 18 -137 -32 - % 67 126 103 % / 65 % -53 119 82 64 % 15 + / - % 176 51 75 68 / 46 123 57 % * * * + 122 109 74 102 116 182 - * * + % 15 110 164 43 -117 167 66 * 124 -155 + - - 64 155 % % + - + / 45 -57 * -81 * -41 - 83 122 167 113 101 / -33 -26 + - * - - 147 80 169 185 + 151 20 135 8 198 * / + * / - - * 141 122 + 156 123 * 2 -157 - 108 132 77 + 22 174 12 - 32 + % - * / 93 199 % 93 + 17 165 133 - 139 + * 93 - * 187 160 -177 5 - 117 + % 71 / 138 -153 % - * 32 1 64 72 + 108 * * 160 199 + * % -85 111 * + -136 144 - 123 149 149 * 15 171 123 % % -59 * - -184 178 % 22 170 - 60 * 25 71 * -131 192 - 57 9 - -137 -162 * % % % 71 -177 % 96 144 116 133 -69 176 42 * + % + % + 158 22 + 99 / -49 / -84 * 181 95 15 137 39 / -65 + / * + / 40 178 92 85 * 77 - + +
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started