Bootstrap

qt 计算器实现(中缀转后缀,后缀计算)

我先把界面贴出来


界面就是这样子的,当然也可以不是这样子的,自己设计,怎么好看怎么来

我来简介下按钮的对象 0到9 就是

pushButton9,
pushButton8等等
我先把界面的代码贴出来
其实蛮简单的,就是让其生成正确的中缀,然后进行中缀转后缀处理。
关键是如何是生成正确的中缀。处理好0到9和括号还有+-*/等直接的关系。就是一个逻辑
1.数字不能出现两个小数点
2.不能同时出现两个操作符。比如++。-+。等
3.括号要匹配
4.操作符之间的输入要正确 等
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "kuohao_stack.h"
namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QString all;//记录全部其信息
     QString only_num;//记录数字其信息
     QString x(QString s);//我测试用的,没啥用
private slots:
     void on_pushButton1_clicked();
     void on_pushButton2_clicked();
     void on_pushButton3_clicked();
     void on_pushButton4_clicked();
     void on_pushButton5_clicked();
     void on_pushButton6_clicked();
     void on_pushButton7_clicked();
     void on_pushButton8_clicked();
     void on_pushButton9_clicked();
     void on_pushButton0_clicked();
     void on_pushButton_dot_clicked();
     void on_pushButton_add_clicked();
     void on_pushButton_sub_clicked();
     void on_pushButton_mul_clicked();
     void on_pushButton_div_clicked();
     void on_pushButton_4_clicked();
     void on_pushButton_3_clicked();
     void on_pushButton_clicked();
     void on_pushButton_2_clicked();
     void on_pushButton_res_clicked();
     //void on_pushButton_close_clicked();


private:
    Ui::MainWindow *ui;
};


#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   
    ui->setupUi(this);
    ui->lineEdit->setReadOnly(true);
    ui->lineEdit->setAlignment(Qt::AlignRight);
     ui->lineEdit_2->setAlignment(Qt::AlignRight);
   // ui->lineEdit_2->setStyleSheet("{border-width:0;border-style:outset;font: 20pt}");//除边框
   // ui->lineEdit->setStyleSheet("{border-width:0;border-style:outset;font: 20pt}");
}
QString MainWindow::x(QString s)
{
   
    return s;
}
MainWindow::~MainWindow()
{
   
    delete ui;
}
void MainWindow::on_pushButton1_clicked()
{
   
    all=ui->lineEdit->text();//先保存输入的
    all+="1" ;              //然后再增加
    only_num+="1";       //这个是保持数字的变量 为了让其不出现两个小数点
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton2_clicked()
{
   
    all=ui->lineEdit->text();
    all+="2" ;
    only_num+="2";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton3_clicked()
{
   
    all=ui->lineEdit->text();
    all+="3" ;
    only_num+="3";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton4_clicked()
{
   
    all=ui->lineEdit->text();
    all+="4" ;
    only_num+="4";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton5_clicked()
{
   
    all=ui->lineEdit->text();
    all+="5" ;
    only_num+="5";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton6_clicked()
{
   
    all=ui->lineEdit->text();
    all+="6" ;
    only_num+="6";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton7_clicked()
{
   
    all=ui->lineEdit->text();
    all+="7" ;
    only_num+="7";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton8_clicked()
{
   
    all=ui->lineEdit->text();
    all+="8" ;
    only_num+="8";
    ui->lineEdit->setText(all);
    ui->lineEdit_2->setText(all);
}
void MainWindow::on_pushButton9_clicked()
{
   
    all=ui->lineEdit->text();
    all+="9" ;
    only_num+="9";
    ui->
;