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

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 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 stos;

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

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

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

Compute the derivative p'(y) of the function p(y) = 2y-2/ 8y + 4.

Answered: 1 week ago