Here is the problem I am supposed to be answering:
Suppose an expression is represented in a string (i.e., 35*4+5/(4+5)). Write a function that splits the expression into numbers, operators, and parentheses. The function takes an expression as the argument and returns a vector of strings. Each string represents a number, an operand, or a parenthesis. The header of the function is given as follows:
vector<string> split(const string &expression)
For example, split("35*4+5/(4+5)") returns a vector that contains the strings 35, * ,4 ,+ ,5 ,/ ,(, 4, +, 5, ).
I have already done four assignments for my VB.Net class and one for C++ so maybe I am just burnt out but this is all due tomorrow. Please help!! Here is what I have so far:
#include <iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
vector<string> split(const string &expression);
int main()
{
string expression;
cout << "Enter an expression: ";
getline(cin, expression);
split(expression);
cout << expression << " split into individual entities yields: " << << endl;
return 0;
}
vector<string> split(const string &expression)
{
vector<string> v; //A vector to store split items as strings
string numberString; //A numeric string
for (int i = 0; i < expression.length(); i++)
{
if (isdigit(expression[i]))
numberString.append(1, expression[i]); //Append a digit
else
{
if (numberString.size() > 0)
{
v.push_back(numberString); //Store the numeric string
numberString.erase(); //Empty the numeric string
}
if (!isspace(expression[i]))
{
string s;
s.append(1, expression[i]);
v.push_back(s); //Store an operator and parenthesis
}
}
}
//Store the last numeric string
if (numberString.size() > 0)
v.push_back(numberString);
return v;
}