1

Suppose I have a string exp in the following format:

123+456*789-1011+1213

I want to store all the numbers in vector numbers, and all the operations in vector op.

vector<long long>numbers // {123, 456, 789, 1011, 1213}
vector<char>op // {+, *, -, +}

for (int i = 0; i <  exp.size(); i++){
    if (exp[i]=="+" || exp[i]=="-" || exp[i]=="*"){
        op.push_back(exp[i]);
    }else{
        ...
    }
}

How do I store the numbers, and convert them from char to long long?

user2109581
  • 187
  • 2
  • 2
  • 11
  • 1
    *How do I store the numbers, and convert them from char to long long?* -- If this is a homework assignment, that sounds like that this is the problem you're supposed to solve on your own. So expected is an attempt, or at least state what approach you tried. In general, you need to *parse* the expression, and parsing is a big topic. – PaulMcKenzie Jul 02 '16 at 16:49

2 Answers2

1

You will need to parse the input expression to extract the numbers and operator.
There are many ways by which this can be done, however following is my approach.

  1. Traverse through all the character and push the values in Operator vector which is not a digit and replace it with space.

  2. Now extract the numbers from the expression and convert it to numbers and push the values in Number vector.

To know how to split a string you can check the following links:

  1. Split a string - Stack overflow
  2. Split a string - cplusplus.com

Use stol or strtol or string stream to convert string to long value.


#include <iostream>
#include <string>
#include <vector>
#include <sstream>

int main()
{
    std::string exp = "123+456*789-1011+1213";
    std::vector<long> vecNums;
    std::vector<char> vecOper;
    for (decltype(exp.size()) i = 0; i < exp.size(); ++i) {
        if (!isdigit(exp[i])) {
            vecOper.push_back(exp[i]);
            exp[i] = ' ';
        }
    }
    std::istringstream iss(exp);
    while (iss) {
        std::string substr;
        long num;
        std::getline(iss, substr, ' ');
        if (substr.size() != 0) {
            // Using strtol function
            // num = strtol(substr.c_str(), NULL, 10);
            // Using stol function
            num = stol(substr);
            vecNums.push_back(num);
        }
        //
        // Or use string stream to convert string to long
        //
        //long num;
        //iss >> num;
        //vecNums.push_back(num);
    }
    std::cout << "Numbers: " << std::endl;
    for (auto &i : vecNums) {
        std::cout << i << " ";
    }
    std::cout << "\nOperators: " << std::endl;
    for (auto &i : vecOper)
        std::cout << i << " ";
    return 0;
}
Community
  • 1
  • 1
ani627
  • 5,578
  • 8
  • 39
  • 45
0

If you're going to use iostreams:

void parse_string(const string& s) {
    using num_t = long long;
    using op_t = char;
    istringstream sstr(s);
    vector<num_t> numbers;
    vector<op_t> ops;

    (void)sstr.peek();  //set eofbit if s.empty() 
    while (!sstr.eof() && !sstr.fail()) {
        num_t num;
        sstr >> num;
        if (!sstr.fail()) {
            numbers.push_back(num);
            if (!sstr.eof()) {
                op_t op;
                sstr >> op;
                if (!sstr.fail()) {
                    ops.push_back(op);
                }
            }
        }
    }
    //assert(ops.size() + 1 == numbers.size());

    //copy(begin(numbers), end(numbers), ostream_iterator<num_t>(cout, " "));
    //copy(begin(ops), end(ops), ostream_iterator<op_t>(cout, " "));
}

Error checking code as been removed (validate operators are correct, exceptions).

David Thomas
  • 778
  • 5
  • 10
  • check for failure instead of eof – Cheers and hth. - Alf Jul 02 '16 at 18:04
  • @Cheersandhth.-Alf Hmm... Edited per your suggestion adding the checks for fail() which does catch badly formatted input (which I stated was not my intent). The tests for eof() still allow the code to detect a clean parsing from a failed parsing. Said differently, attempting to read past eof() to force a fail() condition seems to obfuscate the end of input with parsing errors on the last token. – David Thomas Jul 02 '16 at 22:55