Question: C++ Assignment Suppose an expression is represented in a string (example: 35*4+5/(4+5)). Write a function that splits the expression into numbers, operators and parantheses. The
C++ Assignment
Suppose an expression is represented in a string (example: 35*4+5/(4+5)). Write a function that splits the expression into numbers, operators and parantheses. The function takes an expression as the argument and returns a vector of strings. Each string represents a number, an operand or a paranthesis. The header of the function is given as follows:
Vectorsplit(const string&expression)
For example split 35*4+5/(4+5) returns a vector that contains the strings 35, *, 4, +, 5, /,(, 4, +, 5, ). Write a test program that prompts the user to enter and expression and displays the split items in reverse order. Only one item can display on each line. After the user enters their expression, the expression must redisplay. Example, 35*4+5/(4+5) split into individual entities in a reverse order yields:
Here is my code so far.
#include
#include
#include
using namespace std;
vector
vector
string num = "";
for (int i = 0; i if (line[i] >= '0' && line[i] <= '9') { num = num + line[i]; } else if (line[i] != ' ') { string s = ""; s = s + line[i]; data.push_back(s); } else { if (num.size() > 0) { data.push_back(num); num = ""; } } } return data; } vector vector string num = ""; for (int i = line.size() - 1; i >= 0; i--) { if (line[i] >= '0' && line[i] <= '9') { num = line[i] + num; } else if (line[i] != ' ') { string s = ""; s = s + line[i]; data.push_back(s); } else { if (num.size() > 0) { data.push_back(num); num = ""; } } } if (num.size() > 0) data.push_back(num); return data; } int main() { vector string line; cout << "Enter an expression :"; getline(cin, line); data = split(line); for (int i = 0; i cout << data[i] << ","; } cout << endl; data = splitreverse(line); for (int i = 0; i cout << data[i] << ","; } system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
