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

I don't see anything wrong with your implementation except for this trivial piece missing in the main() to see the result:

vector<string> result = split(expression);

  cout << expression << " split into individual entities yields: ";
  for(vector<string>::iterator it = result.begin(); it != result.end(); ++it)
    cout << "'" << (*it) << "', ";
  cout << endl;

When I compile and run it, I get this output:

Enter an expression: 43+4-5-(4*5)
43+4-5-(4*5) split into individual entities yields: '43', '+', '4', '-', '5', '-', '(', '4', '*', '5', ')',

Thank you! I was so lost but now I UNDERSTAND it and that is all I wanted. You are the greatest. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.