The input from the keyboard is the following line: + * - 5 8 7 + 12 879 (more specifically, this is a direct Polish entry)

I need to write them into an array (in the same order) I'm going to store it as an array of strings. (Maybe there are better ideas? You can use any containers). It would be easier if the numbers were only in the range of 0-9.

PS In fact, this is crawling, then you need to calculate this very Polish record. Since it is straight, you need to go from right to left. Therefore, I need to store a string somewhere.

  • do not need to parse and store the input, write immediately a calculator - vasily-vm

2 answers 2

If the entered tokens are separated by spaces, then you can use the following approach

#include <iostream> #include <sstream> #include <string> #include <vector> #include <iterator> #include <algorithm> int main() { std::string input; std::getline( std::cin, input ); std::istringstream is( input ); std::vector<std::string> tokens; std::copy( std::istream_iterator<std::string>( is ), std::istream_iterator<std::string>(), std::back_inserter( tokens ) ); for ( const auto &s : tokens ) std::cout << s << ' '; std::cout << std::endl; return 0; } 

If you enter a string

 + * - 5 8 7 + 12 879 

then the conclusion will fit her

 + * - 5 8 7 + 12 879 

If in the future you need to process the created vector in the reverse order, then you can use its reverse iterators, or immediately reverse the vector using the standard std::reverse algorithm. For example,

 std::reverse( tokens.begin(), tokens.end() ); 

    Without further ado, I would use a structure with int and char fields, write to the stack right away and calculate.

    Something like this (written on the knee, suboptimally and without error handling, to clarify the idea). To store in an array of strings, I believe, is inappropriate. Stack is a natural choice for reverse polish recording ...

     struct Item { int i = 0; char c = 0; }; int main() { string input; getline(cin,input); stack<Item> st; for(int i = 0, end = input.length(); i < end; ++i) { switch(input[i]) { case ' ': break; case '+': case '-': case '*': case '/': { Item it; it.c = input[i]; st.push(it); // cout << "Push " << it.c << endl; break; } default: if (!isdigit(input[i])) { cerr << "Wrong symbol\n"; return 1; } Item it; char * pos; it.i = strtol(&input[i],&pos,10); i += pos-&input[i]; st.push(it); // cout << "Push " << it.i << endl; } } while(!st.empty()) { int i1 = st.top().i; st.pop(); if (st.empty()) { cout << "Result: " << i1 << endl; break; } int i2 = st.top().i; st.pop(); char op = st.top().c; st.pop(); switch(op) { case '+': i1 += i2; break; case '-': i1 -= i2; break; case '*': i1 *= i2; break; case '/': i1 /= i2; break; default: cerr << "Wrong expression\n"; return 1; } Item it; it.i = i1; st.push(it); } return 0; } 

    PS Yes, here the numbers are considered positive; just did not bother negative, so as not to complicate.

    • @ minusator: Why minus? - VladD
    • one
      @VladD Apparently, because I took time out to write the code :) - Harry