开发者

Problem pushing a float to stack for console Calculator

开发者 https://www.devze.com 2023-03-21 05:12 出处:网络
I\'m writing a C++ console based calculator. The way it\'ll work is - the readEquation consists of an iterator that reads through the sum, and puts and pushes all numbers to the stack.

I'm writing a C++ console based calculator.

The way it'll work is - the readEquation consists of an iterator that reads through the sum, and puts and pushes all numbers to the stack. -when the read equation comes across an operator e.g. '+', it calls the add() function, which takes two parameters: add(float topOfStack, nextNumber()); -nextNumber handles BODMAS, if the next number is being divided or multiplied, it will do solve those out first, otherwise it will return the number. -the previous sum is popped from the stack, and the result of the addition is pushed to the stack.

this is the code i've written so far: I get some开发者_如何学C kind of error in this line this->numberStack->push(f); acccording to the debugger - but it doesn't tell me what the problem is. I'd appreciate any help with this.

Thanks

Code:

    #include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>


using namespace std;

class Calculator
{
    public:
        Calculator();
        virtual ~Calculator();
        void readEquation();
        float nextInteger(int position);
        string equation;
    protected:
        float add(float firstNumber, float secondNumber);
        float subtract (float firstNumber, float secondNumber);
        float multiply (float number, float multiple);
        float divide (float numerator, float denominator);
        float squareRoot(float number);
        float square(float number);
    private:
        void temporaryPrintStack();
        float answer();
        stack<float>* numberStack;
};



Calculator::Calculator()
{
    //c
}

Calculator::~Calculator()
{
    //dtor
}

float Calculator::add(float numOne,float numTwo)
{
    return numOne+numTwo;
}

float Calculator::subtract(float numOne,float numTwo)
{
    return numOne-numTwo;
}

float Calculator::multiply(float number, float multiple)
{
    return number*multiple;
}

float Calculator::divide(float numerator, float denominator)
{
    return numerator/denominator;
}

float Calculator::square(float number)
{
    return number*number;
}

float Calculator::squareRoot(float number)
{
    return 0;
}

float Calculator::answer()
{
     return this->numberStack->top();
}

void Calculator::temporaryPrintStack()
{
    for(int index = 0; index <=  (int) this->numberStack->size();index++)
    {
        cout<<this->numberStack->top()<<", ";
        this->numberStack->pop();
    }
}

void Calculator::readEquation()
{
    int position=0;
    stringstream stringStream;
    string::iterator iterator;
    for(iterator = this->equation.begin();iterator!=this->equation.end();iterator++,position++)
    {
        if(*iterator>='0' && *iterator<='9' || *iterator=='.')//can this be replaced by isdigit()?
        {
            stringStream<<*iterator;
        }
        if(*iterator=='+')
        {
            string number;
            stringStream>>number;
            cout<<number<<" pushed to stack."<<endl;
            float f = atof(number.c_str());
            this->numberStack->push(f);
            //this->numberStack->push(this->add(this->numberStack->top(),this->nextInteger(position)));
            this->temporaryPrintStack();
        }

        if(*iterator=='=')
        {
            this->answer();
        }
    }
}

int main()
{
    Calculator *c=new Calculator();
    std::cout<<"Enter your sum in this form 2+2/1=";
    getline(cin,c->equation,'=');
    c->readEquation();
    system("pause");
    delete c;
    return 0;
}


stack<float>* numberStack;

This declares that your class will hold a pointer to a stack<float>. It does not allocate that object. You need to do that yourself (probably in the constructor), and also take care of deleting that object in your destructor.

// in ctor
numberStack = new stack<float>;

// in dtor
delete numberStack;

Alternatively, don't use a pointer, but a plain object.


Looks like you forget to initialize your stack in your Calculator constructor. Actually you can simply use:

stack<float> numberStack;
0

精彩评论

暂无评论...
验证码 换一张
取 消