1
    // FILE: calc.h
#include <iostream>
#include <stack> // Uses STL
#include <string> // Uses STL
using namespace std;

void evaluate_stack_tops(stack<double> & numbers, stack<char> & operations);

double read_and_evaluate(string line)
{
    const char RIGHT_PARENTHESIS = ')';
    stack<double> numbers; // local stack object
    stack<char> operations; // local stack object
    double number;
    char symbol;
    size_t position = 0;
    while (position < line.length())
    {

        if (isdigit(line[position]))
        {
            number = line[position++] - '0'; // get value
            numbers.push(number);
        }
        else if (strchr("+-*/", line[position]) != NULL)
        {
            symbol = line[position++];
            operations.push(symbol);
        }
        else if (line[position] == RIGHT_PARENTHESIS)
        {
            position++;
            evaluate_stack_tops(numbers, operations);
        }
        else
            position++;
    }
    if (!operations.empty())
        evaluate_stack_tops(numbers, operations);
    return numbers.top();
}

void evaluate_stack_tops(stack<double> & numbers, stack<char> & operations)
{
    double operand1, operand2;
    operand2 = numbers.top();
    numbers.pop();
    operand1 = numbers.top();
    numbers.pop();
    switch (operations.top())
    {
    case '+': numbers.push(operand1 + operand2); break;
    case '-': numbers.push(operand1 - operand2); break;
    case '*': numbers.push(operand1 * operand2); break;
    case '/': numbers.push(operand1 / operand2); break;
    }
    operations.pop();
}


// FILE: Use_Stack.cpp
#include <iostream>   
using namespace std;
#include "calc.h"

int main()
{
    double answer;
    string line;
    cout << "Type a fully parenthesized arithmetic expression (SINGLE DIGITS ONLY!):\n";
    getline(cin, line);
    answer = read_and_evaluate(line);
    cout << "That evaluates to " << answer << endl;
    system("pause");
    return 0;
}

Everything works and i can input simple things like "2 4 3 * + 7 – 2 +" but if i wanted to input something like "123 60 +" it would not work. i separated it in two header files. Can someone give me a hint on how to accept multi-digit integers?

emanuel
  • 11
  • 3
  • possible duplicate of [How to parse a string to an int in C++?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – Jonathan Potter Oct 13 '14 at 03:49

2 Answers2

0

One way to solve the problem is where you discover an number, instead of assuming it is only one digit long, use a loop to collect all the other digits that are part of the number. The loop would terminate when it encounters a non-digit, or a space.

A better way to do it would be to tokenize the input string using stringstream. In this scenario, you would put the entire line of input into a string and then use a while loop similar to the following:

stringstream ss(line);
string token;
while (ss >> token) {
    // do stuff with token
}
Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49
0

RPN works by having a stack of values which you manipulate. Given input "13", the manipulation needed to go from top=1 to top=13 is fairly straightforward: top = 10 * top + digit

MSalters
  • 173,980
  • 10
  • 155
  • 350