将浮点数推入控制台计算器堆栈时出现问题

发布于 2024-11-24 13:35:41 字数 3275 浏览 2 评论 0原文

我正在编写一个基于 C++ 控制台的计算器。

它的工作方式是 - readEquation 由一个迭代器组成,该迭代器读取总和,并将所有数字放入堆栈。 -当读取的方程遇到运算符“+”时,它会调用add()函数,该函数有两个参数:add(float topOfStack, nextNumber()); -nextNumber 处理 BODMAS,如果下一个数字被除或乘,它将首先解决这些问题,否则将返回该数字。 - 将前一个和从堆栈中弹出,并将相加的结果压入堆栈。

这是我到目前为止编写的代码: 根据调试器,我在这行 this->numberStack->push(f); 中遇到某种错误 - 但它没有告诉我问题是什么。我将不胜感激任何帮助。

感谢

码:

    #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;
}

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 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2024-12-01 13:35:41
stack<float>* numberStack;

这声明您的类将保存一个指向stack的指针。它分配该对象。您需要自己执行此操作(可能在构造函数中),并负责在析构函数中删除该对象。

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

// in dtor
delete numberStack;

或者,不要使用指针,而使用普通对象。

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.

风吹雨成花 2024-12-01 13:35:41

看起来您忘记在计算器构造函数中初始化堆栈。
实际上你可以简单地使用:

stack<float> numberStack;

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

stack<float> numberStack;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文