2 Replies - 942 Views - Last Post: 16 May 2014 - 07:55 PM Rate Topic: -----

#1 leo255  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-November 13

Trying to create a simple post-fix calculator

Posted 16 May 2014 - 05:39 PM

I'm trying to create a simple post-fix calculator - This is NOT homework, it is preparation for a test next week. It's supposed to take an expression from the user, and then calculate it. Before each number, there will be a '#' symbol, a '!' symbol before each operator, and a '.' to end the expression. I don't need to error-check at all.

So far, I'm getting a seg fault after I enter my expression into the console. I'm hoping that I'm not tremendously off with my logic.

Thanks for looking.

#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

class Stack{

    public:

    Stack(){
        this->size = 0;
    }
    Stack & push(double c){

        if (size == MAX_SIZE) die("Push Overflow");
        data[size] = c;
        size++;
        return *this;

    }
    double pop(){

        return data[--size];

    }

    double top() const{

        return data[size-1];

    }

    unsigned getSize() const{return size;}

    bool die(const string & msg){

        cerr << "Fatal Error: " << msg << endl;
        exit(EXIT_FAILURE);

    }

    void show(){

        for (int i = 0; i < size; i++){
            cout << data[i] << endl;
    }}

    private:

    static const unsigned MAX_SIZE = 50;
    double data[MAX_SIZE];
    unsigned size;

};

int main(){

    Stack s1;
    bool inputComplete = false;

    double num1 =0;
    double num2 = 0;
    double answer = 0;
    char c, ch;

    while(!inputComplete){

        cin >> ch;

        if (ch == '!'){

            cin >> c;

            num1 = s1.pop();
            num2 = s1.pop();

            switch(c){

                case '+':
                answer = num2 - num1;
                break;

                case '-':
                answer = num2 - num1;
                break;

                case '/':
                answer = num2/num1;
                break;

                case '^':
                answer = pow(num2, num1);
                break;

                case '*':
                answer = num2 * num1;
                break;

            }
            s1.push(answer);
        }

        else if (c == '#'){

            double number;
            cin >> number;

            s1.push(number);

        }

        else if (c == '.'){

            inputComplete == true;

        }
    }

    s1.show();

    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to create a simple post-fix calculator

#2 leo255  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-November 13

Re: Trying to create a simple post-fix calculator

Posted 16 May 2014 - 05:57 PM

Just as an example,

# 5 # 3 ! - # 4 ! *.

Should output 8.
Was This Post Helpful? 0
  • +
  • -

#3 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Trying to create a simple post-fix calculator

Posted 16 May 2014 - 07:55 PM

You should maybe have a prompt message like "##" or something to indicate for input.
And you might want to think about using getline and then processing the string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1