Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

Have I incorporated my research into my outline effectively?

Answered: 1 week ago