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 split(string line) {

vector data;

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 splitreverse(string line) {

vector data;

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

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!