C ++ / Qt计算器问题(不确定此问题是在QT方面还是与节点/指针有关)

时间:2019-03-02 00:28:59

标签: c++ qt

我正在使用Qt在C ++中创建一个八进制计算器。 我在将我的QString变量(currentOutput)输出到lineedit框时遇到麻烦。我可以将qstring文字放入setOutput函数的最后一行,但是它不发送变量。

从当我按下符号按钮时在调试模式下抛出的异常来看,看来我在堆栈中使用的节点节点未正确创建,我不知道为什么。

代码包含在下面。我知道它很忙,并且有许多更简单,更优雅的解决方案,但这是我第一次使用Qt,并且仍在学习中。 如果有人有足够的耐心来帮助我解决这个问题,我将不胜感激...

main.cpp:

#include "qtprogramwin.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtProgramWin w;
    w.show();

    return a.exec();
}

qtprogramwin.h:

#ifndef QTPROGRAMWIN_H
#define QTPROGRAMWIN_H

#include <QMainWindow>
#include <QLineEdit>
#include <string>

namespace Ui {
class QtProgramWin;
}

class QtProgramWin : public QMainWindow
{
    Q_OBJECT

public:
    explicit QtProgramWin(QWidget *parent = 0);
    ~QtProgramWin();

    struct node {
        int value;
        node* next;
        bool negative;
    };

private:
    Ui::QtProgramWin *ui;


    int currentVal = 0, condition;
        bool add = 0, sub = 0, mult = 0, div = 0;
    node* head;
    QString currentOutput;

public slots:
    void pressDigit(int);
    void pressMult();
    void pressDiv();
    void pressSub();
    void pressAdd();
    void pressClear();
    void pressSign();
    void pressEquals();
    bool isEmpty();
    void multiDigit(int);
    void clearFlags();
    void setOutput();
    bool operations();
};

#endif // QTPROGRAMWIN_H

qtprogramwin.cpp:

#include "qtprogramwin.h"
#include "ui_qtprogramwin.h"
#include <qlineedit>
#include <string>

QtProgramWin::QtProgramWin(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QtProgramWin)
{
    ui->setupUi(this);
    connect(ui->pushButton_0, SIGNAL(clicked()), this, SLOT(pressDigit(0)));
    connect(ui->pushButton_0, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_1, SIGNAL(clicked()), this, SLOT(pressDigit(1)));
    connect(ui->pushButton_1, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(pressDigit(2)));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(pressDigit(3)));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(pressDigit(4)));
    connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(pressDigit(5)));
    connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_6, SIGNAL(clicked()), this, SLOT(pressDigit(6)));
    connect(ui->pushButton_6, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_7, SIGNAL(clicked()), this, SLOT(pressDigit(7)));
    connect(ui->pushButton_7, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_add, SIGNAL(clicked()), this, SLOT(pressAdd()));
    connect(ui->pushButton_add, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_sub, SIGNAL(clicked()), this, SLOT(pressSub()));
    connect(ui->pushButton_sub, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_mult, SIGNAL(clicked()), this, SLOT(pressMult()));
    connect(ui->pushButton_mult, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_div, SIGNAL(clicked()), this, SLOT(pressDiv()));
    connect(ui->pushButton_div, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_clear, SIGNAL(clicked()), this, SLOT(pressClear()));
    connect(ui->pushButton_clear, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_equals, SIGNAL(clicked()), this, SLOT(pressEquals()));
    connect(ui->pushButton_equals, SIGNAL(clicked()), this, SLOT(setOutput()));
    connect(ui->pushButton_sign, SIGNAL(clicked()), this, SLOT(pressSign()));
    connect(ui->pushButton_sign, SIGNAL(clicked()), this, SLOT(setOutput()));

    head = (node*) malloc (sizeof(node));
    head->value = NULL;
    head->negative = false;
    head->next = nullptr;
        condition = 1;
}

QtProgramWin::~QtProgramWin()
{
    pressClear();

    delete ui;
}


void QtProgramWin::pressMult()
{
    if ((mult == true) || (div == true) || (add == true) || (sub == true))
    {
        bool success = operations();

        if (success == true)
        {
            clearFlags();
            currentOutput = QString("*");
            mult = true;
            condition = 3;
            setOutput();
        }

        else
        {
            clearFlags();
            pressClear();
            condition = 5; //error
            setOutput();
        }
    }
}

void QtProgramWin::pressDiv()
{
    if ((mult == true) || (div == true) || (add == true) || (sub == true))
    {
        bool success = operations();

        if (success == true)
        {
            clearFlags();
            currentOutput = QString("/");
            mult = true;
            condition = 3;
            setOutput();
        }

        else
        {
            clearFlags();
            pressClear();
            condition = 5; //error
            setOutput();
        }
    }
}

void QtProgramWin::pressAdd()
{
    if ((mult == true) || (div == true) || (add == true) || (sub == true))
    {
        bool success = operations();

        if (success == true)
        {
                clearFlags();
            currentOutput = QString("+");
            mult = true;
            condition = 3;
            setOutput();
        }

        else
        {
            clearFlags();
            pressClear();
            condition = 5; //error
            setOutput();
        }
    }
}

void QtProgramWin::pressSub()
{
    if ((mult == true) || (div == true) || (add == true) || (sub == true))
    {
        bool success = operations();

        if (success == true)
        {
            clearFlags();
            currentOutput = QString("-");
            mult = true;
            condition = 3;
            setOutput();
        }

        else
        {
            clearFlags();
            pressClear();
            condition = 5; //error
            setOutput();
        }
    }
}

bool QtProgramWin::operations()
{
    if ((head->next != nullptr) && (head->next->next != nullptr))
    {
        int numAnswer;
        int num1 = head->next->next->value;
        if (head->next->next->negative = true)
            num1 *= (-1);

        int num2 = head->next->value;
        if (head->next->negative = true)
            num2 *= (-1);

        node* temp = head->next->next;
        head->next->next = nullptr;
        delete temp;
        head->next->negative = false;

        if (add == true)
        {
            numAnswer = abs(num1 + num2);
        }
        else if (sub == true)
        {
            numAnswer = abs(num1 - num2);
        }
        else if (mult == true)
        {
            numAnswer = abs(num1 * num2);
        }
        else if (div == true)
        {
            if (num2 != 0)
            {
                numAnswer = abs(num1 / num2);
            }
            else
            {
                return false;
            }
        }

        if (numAnswer < 0)
            head->next->negative = true;

        head->next->value = numAnswer;
    }
    else
    {
        return false;
    }
    return true;
}

void QtProgramWin::pressSign()
{
    if (head->next->negative == false)                   //exception being thrown here
        head->next->negative = true;
    else
        head->next->negative = false;

    setOutput();
}

void QtProgramWin::pressEquals()
{
    if ((mult == true) || (div == true) || (add == true) || (sub == true))
    {
        bool success = operations();

        if (success == true)
        {
            clearFlags();
            condition = 4;
            setOutput();
        }

        else
        {
            clearFlags();
            pressClear();
            condition = 5; //error
            setOutput();
        }
    }
}

void QtProgramWin::pressClear()
{
    clearFlags();

    while (head->next != nullptr)
    {
        node* temp = head->next;
        head->next = temp->next;
        delete temp;
    }

    condition = 1;
    setOutput();
}

void QtProgramWin::pressDigit(int num)
{
    if (head->next == nullptr)
    {
        node* temp = (node*) malloc (sizeof(node));
        temp->next = nullptr;
        temp->value = num;
        temp->negative = false;
        head->next = temp;

        condition = 2;   //1 = nothing/default, 2 = digit, 3 = sign, 4 = '='
        setOutput();
    }

    else
    {
        if (condition == 2)
        {
            multiDigit(num);
            condition = 2;
            setOutput();
        }
        else if (condition == 3)
        {
            node* temp = new node;
            temp->next = nullptr;
            temp->value = num;
            temp->negative = false;
            head->next = temp;
            condition = 2;
            setOutput();
        }
        else if (condition == 4)
        {
            pressClear();
            condition = 1;
            clearFlags();
            pressDigit(num);
        }
    }
}

bool QtProgramWin::isEmpty()
{
    if (head->next == nullptr)
       return true;
    return false;
}

void QtProgramWin::multiDigit(int temp)
{
    temp += ((head->next->value) * 8);

    head->next->value = temp;
}

void QtProgramWin::clearFlags()
{
    mult = false;
    div = false;
    add = false;
    sub = false;
}

void QtProgramWin::setOutput()
{
    if (condition == 1)  //last was a clear. output empty string.
    {
        currentOutput = QString();
    }

    //If condition is 3, string was set in function. jump to setText()

    if ((condition == 2) || (condition == 4))   //4 is equals
    {
        currentOutput = QString();
        int number = head->next->value, digit;

        while (number != 0)
        {
            digit = number % 8;
            currentOutput.prepend(QString("%1").arg(digit));
            number = ((number - digit) / 8);
        }

        if (head->next->negative == true)
            currentOutput.prepend("-");
    }

    if (condition == 5)  // error
    {
        currentOutput = QString("Error. Press Clear and Try Again.");
    }

    ui->display->setText(currentOutput);  
}

0 个答案:

没有答案