Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ PLEASE!!! Modify your Postfix Calculator to read an expression from a file that uses a mix of decimal entries and Roman Numbers. Process

IN C++ PLEASE!!!

Modify your Postfix Calculator to read an expression from a file that uses a mix of decimal entries and Roman Numbers. Process each entry and keep a running total of those that are valid. Display that total, the number of valid entries, and the number of invalid entries to complete this quiz.

A postfix expression is invalid if An operation is encountered but there are not at least two entries on the stack

The expression has been consumed but there is not exactly one entry on the stack

The expression contains any entry that cannot be parsed as an integer operation, a decimal number (which might include prefix + or -), or a Roman Number

Note that Roman Numbers are always greater than zero, and cannot have a + or - sign in front.

MY CODE THAT NEEDS TO BE MODIFIED

#include #include #include #include #include using namespace std;

// 'sum' variable to keep track of the total sum of all the expressions double sum = 0;

// i keeps track of the number of line currently read from the file int i = 1; // function to calculate the final value of each expression 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 stos;

while (stream >> linia) { if (linia == "*" || linia == "+" || linia == "-" || linia == "/" || linia == "%") { // 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 stos.push(atoi(linia.c_str())); }

// print the current expression's value cout << "Line #" << i << ": " << stos.top() << endl;

// update the sum variable sum += stos.top();

// emptying the stack stos.pop();

// increment i to update the line number i++; }

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; return 0; } }

File:

iii 156 - 10 + 81 * XxXi / 73 34 + + 88 -118 / 95 -155 / -3 47 102 + % % 166 123 LXXV % * 177 153 - + 1234 * XXIII -52 169 % 48 / + 66 * -193 CMIX -181 + 76 * + + 91 28 161 119 / / / -29 59 -167 / + 183 168 % 70 103 + / -91 76 + % -172 IV 51 VI 189 + - / -174 67 88 180 163 * * + + 110 130 54 / 11 135 - % 12 - 44 167 % * * -158 14 154 -31 + 153 + 99 + 43 191 + / - % 187 18 31 * + -79 146 173 - -105 / 64 - * + 62 5 - + -71 76 + -165 92 63 + - / 107 168 162 - 40 * + 41 / 63 140 / - -36 XXIX 128 141 XXXII / / -107 - / + % -117 94 / 165 -142 + % 175 100 * -144 * / 91 143 - 166 + 92 -67 + % 101 160 85 + 65 169 % + 180 -10 / 154 + + / LXXIX CXXXI + + 116 -143 + 29 -166 % 89 39 182 / * 93 % -87 * 113 10 - -178 - 8 34 * - -89 191 - 175 16 145 + 181 111 + * - + 164 35 + 95 + - -167 xxxii -135 178 % 30 * 21 / * + -12 -133 -24 -128 % 145 - % / - -157 139 * 85 197 % 111 * 62 / + -106 45 -114 - * 171 147 + * % 139 192 -49 157 185 45 - 83 / -41 - * 84 / 95 28 / * + + - -95 101 157 - + 29 45 + 28 + 17 104 + % -51 102 % 93 / IXIXIXCXIXIXI 139 * % 189 37 95 - -158 * 43 127 XVI + 36 -76 + - + + / 96 180 * 194 XLII % / 112 17 74 % / -149 / 172 58 * 26 122 - - 3 + -50 C++ 169 -152 XXIII * + / -101 74 18 96 153 / 172 47 / + * 78 - / / 52 / 58 70 138 * / 194 - 18 * 33 163 ICIV 130 + 37 71 * % 21 / % % 36 / -116 - 68 40 / 7 186 % * 112 33 160 * + -38 - % -121 106 xxxv - / 0 -114 CAFE / / 108 % 78 + 9 / -171 % 40 -95 41 / 110 171 + - % CI 127 / -50 C++ 169 -152 XXIII * + / -81 94 % -172 % 195 184 * 122 * XX + % 13 67 56 31 % % / 48 - 11 145 % + 53 22 166 129 - - / + 132 52 % -84 158 159 179 * / 22 / * * 75 + 183 - 35 / -96 II + 130 194 13 -4 * - / * IV 76 178 % 42 -

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

Is the person willing to deal with the consequences?

Answered: 1 week ago

Question

Was there an effort to involve the appropriate people?

Answered: 1 week ago