Qt实践(一)——简易计算器

这个计算器实现了带括号的加减乘除浮点数运算,但由于时间问题,我没有添加浮点数的大数类,只是实现了一个简易计算器。除Qt设计界面外主要运用了中缀表达式转后缀表达式及后缀表达式的求值。下面上程序:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 

class MainWindow : public QMainWindow
{
    Q_OBJECT
protected:
    double result;
    QLineEdit *data;
    QPushButton *button_0;
    QPushButton *button_1;
    QPushButton *button_2;
    QPushButton *button_3;
    QPushButton *button_4;
    QPushButton *button_5;
    QPushButton *button_6;
    QPushButton *button_7;
    QPushButton *button_8;
    QPushButton *button_9;
    QPushButton *button_ce;
    QPushButton *button_point;
    QPushButton *button_lbracket;
    QPushButton *button_rbracket;
    QPushButton *button_delete;
    QPushButton *button_jia;
    QPushButton *button_jian;
    QPushButton *button_cheng;
    QPushButton *button_chu;
    QPushButton *button_result;
    QString s;

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

public:
    void layout();
    void operation();
    void init();
private slots:
    void button_0_clicked();
    void button_1_clicked();
    void button_2_clicked();
    void button_3_clicked();
    void button_4_clicked();
    void button_5_clicked();
    void button_6_clicked();
    void button_7_clicked();
    void button_8_clicked();
    void button_9_clicked();
    void button_ce_clicked();
    void button_point_clicked();
    void button_lbracket_clicked();
    void button_rbracket_clicked();
    void button_delete_clicked();
    void button_result_clicked();
    void button_cheng_clicked();
    void button_chu_clicked();
    void button_jia_clicked();
    void button_jian_clicked();
};


#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include 
#include 
#include 
#include 
#include 
using namespace std;

char infix[1000],suffix[1000];
int k=0;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
}

void MainWindow::layout()
{
    QWidget *centerWindow = new QWidget;
    this->setCentralWidget(centerWindow);

    s = "";
    init();
    data = new QLineEdit("");

    button_0 = new QPushButton("0");
    button_1 = new QPushButton("1");
    button_2 = new QPushButton("2");
    button_3 = new QPushButton("3");
    button_4 = new QPushButton("4");
    button_5 = new QPushButton("5");
    button_6 = new QPushButton("6");
    button_7 = new QPushButton("7");
    button_8 = new QPushButton("8");
    button_9 = new QPushButton("9");
    button_ce = new QPushButton("CE");
    button_point = new QPushButton(".");
    button_lbracket = new QPushButton("(");
    button_rbracket = new QPushButton(")");
    button_delete = new QPushButton("<-");
    button_jia = new QPushButton("+");
    button_jian = new QPushButton("-");
    button_cheng = new QPushButton("*");
    button_chu = new QPushButton("/");
    button_result = new QPushButton("=");

    data->setFixedHeight(30);
    button_0->setMinimumHeight(50);
    button_1->setMinimumHeight(50);
    button_2->setMinimumHeight(50);
    button_3->setMinimumHeight(50);
    button_4->setMinimumHeight(50);
    button_5->setMinimumHeight(50);
    button_6->setMinimumHeight(50);
    button_7->setMinimumHeight(50);
    button_8->setMinimumHeight(50);
    button_9->setMinimumHeight(50);
    button_ce->setMinimumHeight(50);
    button_point->setMinimumHeight(50);
    button_lbracket->setMinimumHeight(50);
    button_rbracket->setMinimumHeight(50);
    button_delete->setMinimumHeight(50);
    button_jia->setMinimumHeight(50);
    button_jian->setMinimumHeight(50);
    button_cheng->setMinimumHeight(50);
    button_chu->setMinimumHeight(50);
    button_result->setMinimumHeight(50);

    //连接信号
    connect(button_0,SIGNAL(clicked(bool)),this,SLOT(button_0_clicked()));
    connect(button_1,SIGNAL(clicked(bool)),this,SLOT(button_1_clicked()));
    connect(button_2,SIGNAL(clicked(bool)),this,SLOT(button_2_clicked()));
    connect(button_3,SIGNAL(clicked(bool)),this,SLOT(button_3_clicked()));
    connect(button_4,SIGNAL(clicked(bool)),this,SLOT(button_4_clicked()));
    connect(button_5,SIGNAL(clicked(bool)),this,SLOT(button_5_clicked()));
    connect(button_6,SIGNAL(clicked(bool)),this,SLOT(button_6_clicked()));
    connect(button_7,SIGNAL(clicked(bool)),this,SLOT(button_7_clicked()));
    connect(button_8,SIGNAL(clicked(bool)),this,SLOT(button_8_clicked()));
    connect(button_9,SIGNAL(clicked(bool)),this,SLOT(button_9_clicked()));
    connect(button_ce,SIGNAL(clicked(bool)),this,SLOT(button_ce_clicked()));
    connect(button_point,SIGNAL(clicked(bool)),this,SLOT(button_point_clicked()));
    connect(button_lbracket,SIGNAL(clicked(bool)),this,SLOT(button_lbracket_clicked()));
    connect(button_rbracket,SIGNAL(clicked(bool)),this,SLOT(button_rbracket_clicked()));
    connect(button_delete,SIGNAL(clicked(bool)),this,SLOT(button_delete_clicked()));
    connect(button_jia,SIGNAL(clicked(bool)),this,SLOT(button_jia_clicked()));
    connect(button_jian,SIGNAL(clicked(bool)),this,SLOT(button_jian_clicked()));
    connect(button_cheng,SIGNAL(clicked(bool)),this,SLOT(button_cheng_clicked()));
    connect(button_chu,SIGNAL(clicked(bool)),this,SLOT(button_chu_clicked()));
    connect(button_result,SIGNAL(clicked(bool)),this,SLOT(button_result_clicked()));

    QGridLayout * layout = new QGridLayout;

    layout->addWidget(data,1,1,1,4,Qt::Alignment());

    layout->addWidget(button_ce,2,1,Qt::Alignment());
    layout->addWidget(button_delete,2,2,Qt::Alignment());
    layout->addWidget(button_lbracket,2,3,Qt::Alignment());
    layout->addWidget(button_rbracket,2,4,Qt::Alignment());

    layout->addWidget(button_1,3,1,Qt::Alignment());
    layout->addWidget(button_2,3,2,Qt::Alignment());
    layout->addWidget(button_3,3,3,Qt::Alignment());
    layout->addWidget(button_jia,3,4,Qt::Alignment());

    layout->addWidget(button_4,4,1,Qt::Alignment());
    layout->addWidget(button_5,4,2,Qt::Alignment());
    layout->addWidget(button_6,4,3,Qt::Alignment());
    layout->addWidget(button_jian,4,4,Qt::Alignment());

    layout->addWidget(button_7,5,1,Qt::Alignment());
    layout->addWidget(button_8,5,2,Qt::Alignment());
    layout->addWidget(button_9,5,3,Qt::Alignment());
    layout->addWidget(button_cheng,5,4,Qt::Alignment());

    layout->addWidget(button_result,6,1,Qt::Alignment());
    layout->addWidget(button_0,6,2,Qt::Alignment());
    layout->addWidget(button_point,6,3,Qt::Alignment());
    layout->addWidget(button_chu,6,4,Qt::Alignment());

    layout->setMargin(12);
    centerWindow->setLayout(layout);
}

MainWindow::~MainWindow()
{

}

void MainWindow::init()
{
    s = "";
}
//符号优先级判断
int mycmp(char a, char b) 
{
    if(b == '(')
        return 1;//左括号直接入栈
    else if((b == '*' || b == '/') &&(a == '+' || a == '-' || a == '('))
        return 1;//*、/优先级高于+、-、(,入栈
    else if((b == '+' || b == '-') && (a == '('))
        return 1;//+、-优先级高于(,入栈
    else
        return 0;
}
//中缀表达式转后缀表达式
void infix_to_suffix(char* infix, char* suffix)
{
    int i, j=0, top=0;
    char stack[1000];//存储运算符的栈

    for(i=0; infix[i]!='\0'; i++) 
    {
        if((infix[i] >= '0' && infix[i] <= '9')||infix[i] == '.' ) 
        {
            suffix[j++] = infix[i];//操作数则直接输出
        } 
        else 
        {
            if(i != 0 && ((infix[i-1] >= '0' && infix[i-1] <= '9')||infix[i] == '.') )
            {
                suffix[j++] = ' ';//操作数后补充空格分割
            }
            if(infix[i] == ')') 
            {
                //遇到右括号则一直弹出直到左括号,但左括号不输出
                while(stack[top-1] != '(') 
                {
                    suffix[j++] = stack[--top];
                    suffix[j++] = ' ';
                }
                top--;
            } 
            else if(top == 0 || mycmp(stack[top-1], infix[i])) 
            {
                //栈为空或当前操作符的优先级高于栈顶操作符,当前操作符入栈
                stack[top++] = infix[i];
            } 
            else 
            {
                //当前操作符优先级等于或低于栈顶操作符则弹出栈顶
                while(!mycmp(stack[top-1], infix[i])) 
                {
                    suffix[j++] = stack[--top];
                    suffix[j++] = ' ';
                    if(top == 0)
                        break;
                }
                stack[top++] = infix[i];//当前操作符入栈
            }
        }
    }
    //补充空格分割
    if(suffix[j-1] != ' ') 
    {
        suffix[j++] = ' ';
    }
    //如果操作符栈不为空,弹出所有操作符
    while(top != 0) 
    {
        suffix[j++] = stack[--top];
        suffix[j++] = ' ';
    }
    suffix[j] = '\0';
}
//后缀表达式求值
double suffix_value(char* suffix)
{
    int i;
    double stack[1000];
    int top = 0;
    double value = 0.0;
    double temp = 0.0;
    double exp = 0.1;
    for(i=0; suffix[i] != '\0'; i++)
    {
        if(suffix[i] >= '0' && suffix[i] <= '9')
        {
            while(suffix[i] >= '0' && suffix[i] <= '9')
            {
                value = value*10 + suffix[i] - '0';
                i++;
            }
            i--;
        }
        else if(suffix[i] == '.')
        {
            i++;
            while(suffix[i] >= '0' && suffix[i] <= '9')
            {
                temp = temp + (suffix[i] - '0') * exp;
                i++;
                exp = exp * 0.1;
            }
            i--;
            value = value + temp;
        }
        else if(suffix[i] == ' ')
        {
            //操作数入栈
            stack[top++] = value;
            value = 0;
            temp = 0;
            exp = 0.1;
        }
        else
        {
            //根据操作符,对栈顶两个操作数进行计算并得到结果
            switch(suffix[i])
            {
                case '+': value = stack[top-2] + stack[top-1];break;
                case '-': value = stack[top-2] - stack[top-1];break;
                case '*': value = stack[top-2] * stack[top-1];break;
                case '/': value = stack[top-2] / stack[top-1];break;
                default: break;
            }
            top -= 2;
        }
    }

    return stack[0];
}

void MainWindow::operation()
{
    infix_to_suffix(infix, suffix);
    result =  suffix_value(suffix);
    s = QString::number(result,10.0);
}

void getnum(char num)
{
    infix[k++]=num;
}
void MainWindow::button_0_clicked()
{
    s += "0";
    data->setText(s);
    getnum('0');
}

void MainWindow::button_1_clicked()
{
    s += "1";
    data->setText(s);
    getnum('1');
}

void MainWindow::button_2_clicked()
{
    s += "2";
    data->setText(s);
    getnum('2');
}

void MainWindow::button_3_clicked()
{
    s += "3";
    data->setText(s);
    getnum('3');
}

void MainWindow::button_4_clicked()
{
    s += "4";
    data->setText(s);
    getnum('4');
}

void MainWindow::button_5_clicked()
{
    s += "5";
    data->setText(s);
    getnum('5');
}

void MainWindow::button_6_clicked()
{
    s += "6";
    data->setText(s);
    getnum('6');
}

void MainWindow::button_7_clicked()
{
    s += "7";
    data->setText(s);
    getnum('7');
}

void MainWindow::button_8_clicked()
{
    s += "8";
    data->setText(s);
    getnum('8');
}

void MainWindow::button_9_clicked()
{
    s += "9";
    data->setText(s);
    getnum('9');
}

void MainWindow::button_ce_clicked()
{
    s = "";
    data->setText("");
    //初始化数据
    init();
    memset(infix,0,1000);
    memset(suffix,0,1000);
    k = 0;
}

void MainWindow::button_point_clicked()
{
    s += ".";
    data->setText(s);
    getnum('.');
}

void MainWindow::button_lbracket_clicked()
{
    s += "(";
    data->setText(s);
    getnum('(');
}

void MainWindow::button_rbracket_clicked()
{
    s += ")";
    data->setText(s);
    getnum(')');
}

void MainWindow::button_delete_clicked()
{
    s=s.left(s.length()-1);
    data->setText(s);
    k--;
}

void MainWindow::button_jia_clicked()
{
    s += "+";
    data->setText(s);
    getnum('+');
}

void MainWindow::button_jian_clicked()
{
    s += "-";
    data->setText(s);
    getnum('-');
}

void MainWindow::button_cheng_clicked()
{
    s += "*";
    data->setText(s);
    getnum('*');
}

void MainWindow::button_chu_clicked()
{
    s += "/";
    data->setText(s);
    getnum('/');
}

void MainWindow::button_result_clicked()
{
    infix[k]='\0';
    operation();
    QString a = QString::number(result,10.0);
    data->setText(a);
}

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.setFixedSize(500,500);
    w.setWindowTitle("计算器");
    w.layout();
    return a.exec();
}

最终实现结果为:

Qt实践(一)——简易计算器_第1张图片

Qt实践(一)——简易计算器_第2张图片

你可能感兴趣的:(Qt学习,Qt,计算器)