c++实现带界面简单计算器

说明

  • 编辑器使用vs2010,界面设计采用qt4.8.6
  • 本计算器只简单实现带括号的四则运算

界面部分

采用qt直接拖拽,设计如下

c++实现带界面简单计算器

相关代码

几乎是自动生成

#ifndef UI_MYCLASS_H
#define UI_MYCLASS_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MyClassClass
{
public:
    QWidget *centralWidget;
    QVBoxLayout *verticalLayout;
    QLabel *label;
    QLineEdit *line;
    QGridLayout *gridLayout;
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    QPushButton *btn4;
    QPushButton *btn5;
    QPushButton *btn6;
    QPushButton *btn7;
    QPushButton *btn8;
    QPushButton *btn9;
    QPushButton *btn0;
    QPushButton *btnend;
    QPushButton *btnclear;
    QPushButton *btnl;
    QPushButton *btnr;
    QPushButton *btnadd;
    QPushButton *btnminus;
    QPushButton *btn;
    QPushButton *btnmul;
    QPushButton *btndivide;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *MyClassClass)
    {
        if (MyClassClass->objectName().isEmpty())
            MyClassClass->setObjectName(QString::fromUtf8("MyClassClass"));
        MyClassClass->resize(377, 385);
        centralWidget = new QWidget(MyClassClass);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        verticalLayout = new QVBoxLayout(centralWidget);
        verticalLayout->setSpacing(6);
        verticalLayout->setContentsMargins(11, 11, 11, 11);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        label = new QLabel(centralWidget);
        label->setObjectName(QString::fromUtf8("label"));

        verticalLayout->addWidget(label);

        line = new QLineEdit(centralWidget);
        line->setObjectName(QString::fromUtf8("line"));

        verticalLayout->addWidget(line);

        gridLayout = new QGridLayout();
        gridLayout->setSpacing(6);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        btn1 = new QPushButton(centralWidget);
        btn1->setObjectName(QString::fromUtf8("btn1"));

        gridLayout->addWidget(btn1, 0, 0, 1, 1);

        btn2 = new QPushButton(centralWidget);
        btn2->setObjectName(QString::fromUtf8("btn2"));

        gridLayout->addWidget(btn2, 0, 1, 1, 1);

        btn3 = new QPushButton(centralWidget);
        btn3->setObjectName(QString::fromUtf8("btn3"));

        gridLayout->addWidget(btn3, 0, 2, 1, 1);

        btn4 = new QPushButton(centralWidget);
        btn4->setObjectName(QString::fromUtf8("btn4"));

        gridLayout->addWidget(btn4, 1, 0, 1, 1);

        btn5 = new QPushButton(centralWidget);
        btn5->setObjectName(QString::fromUtf8("btn5"));

        gridLayout->addWidget(btn5, 1, 1, 1, 1);

        btn6 = new QPushButton(centralWidget);
        btn6->setObjectName(QString::fromUtf8("btn6"));

        gridLayout->addWidget(btn6, 1, 2, 1, 1);

        btn7 = new QPushButton(centralWidget);
        btn7->setObjectName(QString::fromUtf8("btn7"));

        gridLayout->addWidget(btn7, 2, 0, 1, 1);

        btn8 = new QPushButton(centralWidget);
        btn8->setObjectName(QString::fromUtf8("btn8"));

        gridLayout->addWidget(btn8, 2, 1, 1, 1);

        btn9 = new QPushButton(centralWidget);
        btn9->setObjectName(QString::fromUtf8("btn9"));

        gridLayout->addWidget(btn9, 2, 2, 1, 1);

        btn0 = new QPushButton(centralWidget);
        btn0->setObjectName(QString::fromUtf8("btn0"));

        gridLayout->addWidget(btn0, 3, 0, 1, 1);

        btnend = new QPushButton(centralWidget);
        btnend->setObjectName(QString::fromUtf8("btnend"));

        gridLayout->addWidget(btnend, 3, 1, 1, 1);

        btnclear = new QPushButton(centralWidget);
        btnclear->setObjectName(QString::fromUtf8("btnclear"));

        gridLayout->addWidget(btnclear, 3, 2, 2, 1);

        btnl = new QPushButton(centralWidget);
        btnl->setObjectName(QString::fromUtf8("btnl"));

        gridLayout->addWidget(btnl, 4, 0, 1, 1);

        btnr = new QPushButton(centralWidget);
        btnr->setObjectName(QString::fromUtf8("btnr"));

        gridLayout->addWidget(btnr, 4, 1, 1, 1);

        btnadd = new QPushButton(centralWidget);
        btnadd->setObjectName(QString::fromUtf8("btnadd"));

        gridLayout->addWidget(btnadd, 5, 0, 1, 1);

        btnminus = new QPushButton(centralWidget);
        btnminus->setObjectName(QString::fromUtf8("btnminus"));

        gridLayout->addWidget(btnminus, 5, 1, 1, 1);

        btn = new QPushButton(centralWidget);
        btn->setObjectName(QString::fromUtf8("btn"));

        gridLayout->addWidget(btn, 5, 2, 2, 1);

        btnmul = new QPushButton(centralWidget);
        btnmul->setObjectName(QString::fromUtf8("btnmul"));

        gridLayout->addWidget(btnmul, 6, 0, 1, 1);

        btndivide = new QPushButton(centralWidget);
        btndivide->setObjectName(QString::fromUtf8("btndivide"));

        gridLayout->addWidget(btndivide, 6, 1, 1, 1);


        verticalLayout->addLayout(gridLayout);

        MyClassClass->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(MyClassClass);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 377, 26));
        MyClassClass->setMenuBar(menuBar);
        mainToolBar = new QToolBar(MyClassClass);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        MyClassClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(MyClassClass);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        MyClassClass->setStatusBar(statusBar);

        retranslateUi(MyClassClass);

        QMetaObject::connectSlotsByName(MyClassClass);
    } // setupUi

    void retranslateUi(QMainWindow *MyClassClass)
    {
        MyClassClass->setWindowTitle(QApplication::translate("MyClassClass", "\350\256\241\347\256\227\345\231\250", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("MyClassClass", "\350\257\267\350\276\223\345\205\245\344\270\200\344\270\252\344\273\245#\347\273\223\345\260\276\347\232\204\347\256\227\345\274\217", 0, QApplication::UnicodeUTF8));
        btn1->setText(QApplication::translate("MyClassClass", "1", 0, QApplication::UnicodeUTF8));
        btn2->setText(QApplication::translate("MyClassClass", "2", 0, QApplication::UnicodeUTF8));
        btn3->setText(QApplication::translate("MyClassClass", "3", 0, QApplication::UnicodeUTF8));
        btn4->setText(QApplication::translate("MyClassClass", "4", 0, QApplication::UnicodeUTF8));
        btn5->setText(QApplication::translate("MyClassClass", "5", 0, QApplication::UnicodeUTF8));
        btn6->setText(QApplication::translate("MyClassClass", "6", 0, QApplication::UnicodeUTF8));
        btn7->setText(QApplication::translate("MyClassClass", "7", 0, QApplication::UnicodeUTF8));
        btn8->setText(QApplication::translate("MyClassClass", "8", 0, QApplication::UnicodeUTF8));
        btn9->setText(QApplication::translate("MyClassClass", "9", 0, QApplication::UnicodeUTF8));
        btn0->setText(QApplication::translate("MyClassClass", "0", 0, QApplication::UnicodeUTF8));
        btnend->setText(QApplication::translate("MyClassClass", "#", 0, QApplication::UnicodeUTF8));
        btnclear->setText(QApplication::translate("MyClassClass", "\345\210\240\351\231\244", 0, QApplication::UnicodeUTF8));
        btnl->setText(QApplication::translate("MyClassClass", "(", 0, QApplication::UnicodeUTF8));
        btnr->setText(QApplication::translate("MyClassClass", ")", 0, QApplication::UnicodeUTF8));
        btnadd->setText(QApplication::translate("MyClassClass", "+", 0, QApplication::UnicodeUTF8));
        btnminus->setText(QApplication::translate("MyClassClass", "-", 0, QApplication::UnicodeUTF8));
        btn->setText(QApplication::translate("MyClassClass", "\350\256\241\347\256\227", 0, QApplication::UnicodeUTF8));
        btnmul->setText(QApplication::translate("MyClassClass", "*", 0, QApplication::UnicodeUTF8));
        btndivide->setText(QApplication::translate("MyClassClass", "/", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class MyClassClass: public Ui_MyClassClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MYCLASS_H

功能方面

界面部分

c++实现带界面简单计算器

相关代码

main.cpp

#include "myclass.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyClass w;
    w.show();
    return a.exec();
}

myclass.cpp

#include "myclass.h"
#include "QMessageBox"
using namespace std;

MyClass::MyClass(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    connect(ui.btn,SIGNAL(clicked()),this,SLOT(Onbtn()));
    connect(ui.btn1,SIGNAL(clicked()),this,SLOT(Onbtn1()));
    connect(ui.btn2,SIGNAL(clicked()),this,SLOT(Onbtn2()));
    connect(ui.btn3,SIGNAL(clicked()),this,SLOT(Onbtn3()));
    connect(ui.btn4,SIGNAL(clicked()),this,SLOT(Onbtn4()));
    connect(ui.btn5,SIGNAL(clicked()),this,SLOT(Onbtn5()));
    connect(ui.btn6,SIGNAL(clicked()),this,SLOT(Onbtn6()));
    connect(ui.btn7,SIGNAL(clicked()),this,SLOT(Onbtn7()));
    connect(ui.btn8,SIGNAL(clicked()),this,SLOT(Onbtn8()));
    connect(ui.btn9,SIGNAL(clicked()),this,SLOT(Onbtn9()));
    connect(ui.btn0,SIGNAL(clicked()),this,SLOT(Onbtn0()));
    connect(ui.btnadd,SIGNAL(clicked()),this,SLOT(Onbtnadd()));
    connect(ui.btnminus,SIGNAL(clicked()),this,SLOT(Onbtnminus()));
    connect(ui.btnmul,SIGNAL(clicked()),this,SLOT(Onbtnmul()));
    connect(ui.btndivide,SIGNAL(clicked()),this,SLOT(Onbtndivide()));
    connect(ui.btnend,SIGNAL(clicked()),this,SLOT(Onbtnend()));
    connect(ui.btnl,SIGNAL(clicked()),this,SLOT(Onbtnl()));
    connect(ui.btnr,SIGNAL(clicked()),this,SLOT(Onbtnr()));
    connect(ui.btnclear,SIGNAL(clicked()),this,SLOT(Onbtnclear()));
}

MyClass::~MyClass()
{

}
template<class t>
class stack
{
private:
    int data[100];
    int count;
public:
    void Stack();
    int empty();
    int full();
    int get_top();
    void push(t x);
    void pop();
};

template<class t>
void stack<t>::Stack()
{
    count=0;
}
template<class t>
int stack<t>::empty()
{
    if(count==0)
        return 1;
    else
        return 0;
}
template<class t>
int stack<t>::full()
{
    if(count==100)
        return 1;
    else
        return 0;
}
template<class t>
int stack<t>::get_top()
{
    if(empty()==0)
        return data[count-1];
}
template<class t>
void stack<t>::push(t x)
{
    if(full()==0)
    {
        data[count]=x;
        count++;
    }
}
template<class t>
void stack<t>::pop()
{
    if(empty()==0)
        count--;
}

int numbers(string nu,int i)//用来判断是数字还是运算符 
{
    if(nu[i]>='0'&&nu[i]<='9')
        return 1;
    else
        return 0;
}
int priority(char py)//比较运算符的优先级 
{
    if(py=='+'||py=='-')
        return 1;
    else if(py=='*'||py=='/')
        return 2;
    else if(py=='#')
        return -1;
    else if(py=='('||py==')')
        return 0;
}
int calculate(string ch)//运算 
{
    stack<int> num;
    stack<char> sym;
    char character;
    int number,fn;//fn用于十位以上的数字
    int f=0;//用来区分符号和数字的转换的标记
    int i=0;//用来遍历表达式 
    num.Stack();
    sym.Stack();
    sym.push('#');
    while(sym.empty()==0)
    {
        if(numbers(ch,i)==1)//如果是数字 
        {
            if(f==0)
            {
                num.push(ch[i]-48);
                i++;
                f=1;
            }
            else
            {
                fn=num.get_top()*10+ch[i]-48;
                num.pop();
                num.push(fn);
                i++;
                f=1;
            }
            
        }
        else//不是数字 
        {
            f=0;
            character=sym.get_top();
            if(priority(ch[i])==priority(character)&&ch[i]==')')//括号的判断 
            {
                sym.pop();
                i++;
            }
            else if(ch[i]=='(')
            {
                sym.push(ch[i]);
                i++;
            }
            else if(priority(ch[i])==priority(character)&&ch[i]=='#')//#的判断 
            {
                sym.pop();
            }
            else if(priority(ch[i])>=priority(character))//比顶优先就入栈 
            {
                sym.push(ch[i]);
                i++;
            }
            else//比顶差就运算 
            {
                if(character=='-')
                {
                    number=num.get_top();
                    num.pop();
                    number=num.get_top()-number;
                    num.pop();
                    num.push(number);
                }
                else if(character=='+')
                {
                    number=num.get_top();
                    num.pop();
                    number=num.get_top()+number;
                    num.pop();
                    num.push(number);
                }
                else if(character=='*')
                {
                    number=num.get_top();
                    num.pop();
                    number=num.get_top()*number;
                    num.pop();
                    num.push(number);
                }
                else if(character=='/')
                {
                    number=num.get_top();
                    num.pop();
                    number=num.get_top()/number;
                    num.pop();
                    num.push(number);
                }
                sym.pop();
                if(priority(ch[i])>priority(sym.get_top()))//只有当前遍历到的运算符比顶大时才将其入栈,并向下遍历 
                {
                    sym.push(ch[i]);
                    i++;
                }
                else
                    continue;

            }
        }
    } 
    return num.get_top();//输出运算结果 
}
int MyClass::Onbtn1()
{  
    expression+="1";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtn2()
{
    expression+="2";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtn3()
{
    expression+="3";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtn4()
{
    expression+="4";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtn5()
{
    expression+="5";  
    ui.line->setText(expression);
    return 0;
}
int MyClass::Onbtn6()
{
    expression+="6";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtn7()
{
    expression+="7";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtn8()
{
    expression+="8";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtn9()
{
    expression+="9";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtn0()
{
    expression+="0";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtnadd()
{
    expression+="+";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtnminus()
{
    expression+="-";  
    ui.line->setText(expression); 
    return 0;
}
int MyClass::Onbtnmul()
{
    expression+="*";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtndivide()
{
    expression+="/";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtnend()
{
    expression+="#";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtnl()
{
    expression+="(";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtnr()
{
    expression+=")";  
    ui.line->setText(expression);  
    return 0;
}
int MyClass::Onbtnclear()
{
    ui.line->setText("");
    expression="";  
    return 0;
}
int MyClass::Onbtn()
{
    QString line=ui.line->text();
    string ch;
    int c;

    ch = string((const char *)line.toLocal8Bit());//将QString转化为string

    c=calculate(ch);//计算
    ch= to_string(static_cast<long long>(c));//将返回的int转化为string

    line= QString(QString::fromLocal8Bit(ch.c_str()));//将string转化为QString

    QMessageBox::information(this,"result",line);//显示结果
    return 0;
}

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QtGui/QMainWindow>
#include "ui_myclass.h"

class MyClass : public QMainWindow
{
    Q_OBJECT

public:
    MyClass(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~MyClass();
    private slots:
        int Onbtn();
        int Onbtn1();
        int Onbtn2();
        int Onbtn3();
        int Onbtn4();
        int Onbtn5();
        int Onbtn6();
        int Onbtn7();
        int Onbtn8();
        int Onbtn9();
        int Onbtn0();
        int Onbtnadd();
        int Onbtnminus();
        int Onbtnmul();
        int Onbtndivide();
        int Onbtnend();
        int Onbtnl();
        int Onbtnr();
        int Onbtnclear();
private:
    Ui::MyClassClass ui;
    QString expression;
};

#endif // MYCLASS_H
上一篇:QT之布局


下一篇:Qt内存管理分析与代码展示