可运行QT计算器源码_蓝石头_新浪博客

========================================================================

main.cpp

========================================================================

#include<qapplication.h>
#include"mainwindow.h"
int main(int argc,char **argv)
{
    QApplication   a(argc,argv);
    window *w = new window();
    w->show();
    return a.exec();
}

=======================================================================

mainwindow.h

=======================================================================

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include<QpushButton>
#include<QLineEdit>
#include<QWidget>
#include<QString>
#include<QLabel>
class window:public QWidget
{
    Q_OBJECT
public:
    window();
public slots:
    void Button1Click();
    void Button2Click();
    void Button3Click();
    void Button4Click();
    void Button5Click();
    void Button6Click();
    void Button7Click();
    void Button8Click();
    void Button9Click();
    void Button0Click();
    void ButtonAddC();
    void ButtonDelC();
    void ButtonDivC();
    void ButtonMulC();
    void ButtonResult();

private:
    QLineEdit *lineEdit;
    QPushButton *Button[15];

    QString firstNum,secondNum;
    char oper;
    int index;
};


#endif // MAINWINDOW_H

=========================================================================

mainwindows.cpp

=========================================================================

#include "mainwindow.h"

#include<qfont.h>
window::window( )
{
firstNum="0";
secondNum="0";
oper=' ';
lineEdit=new QLineEdit(this);
index = 0;

Button[10]=new QPushButton("*",this);
Button[11]=new QPushButton("/",this);
Button[12]=new QPushButton("+",this);
Button[13]=new QPushButton("-",this);
Button[14]=new QPushButton("=",this);
for(int i = 0; i < 10; i++)
{
    QString temp;
    temp = QString::number(i,'f',0);
    Button[i] = new QPushButton(temp,this);
}
lineEdit->setGeometry(20,10,250,30);
for(int i = 1; i <= 3; i++)
{
    for(int j = 1; j <= 5; j++)
    {
        Button[index]->setGeometry(20+(j-1)*50,10+i*60,30,30);
        index++;
    }
}

connect(Button[1],SIGNAL(clicked()),this,SLOT(Button1Click()));
connect(Button[2],SIGNAL(clicked()),this,SLOT(Button2Click()));
connect(Button[3],SIGNAL(clicked()),this,SLOT(Button3Click()));
connect(Button[4],SIGNAL(clicked()),this,SLOT(Button4Click()));
connect(Button[5],SIGNAL(clicked()),this,SLOT(Button5Click()));
connect(Button[6],SIGNAL(clicked()),this,SLOT(Button6Click()));
connect(Button[7],SIGNAL(clicked()),this,SLOT(Button7Click()));
connect(Button[8],SIGNAL(clicked()),this,SLOT(Button8Click()));
connect(Button[9],SIGNAL(clicked()),this,SLOT(Button9Click()));
connect(Button[0],SIGNAL(clicked()),this,SLOT(Button0Click()));
connect(Button[12],SIGNAL(clicked()),this,SLOT(ButtonAddC()));
connect(Button[13],SIGNAL(clicked()),this,SLOT(ButtonDelC()));
connect(Button[10],SIGNAL(clicked()),this,SLOT(ButtonMulC()));
connect(Button[11],SIGNAL(clicked()),this,SLOT(ButtonDivC()));

connect(Button[14],SIGNAL(clicked()),this,SLOT(ButtonResult()));

}

void window::Button1Click()
{
    if(secondNum == "0")
        secondNum = "1";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('1');
    }
    lineEdit->setText(secondNum);
}
void window::Button2Click()
{
    if(secondNum=="0")
        secondNum="2";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('2');
    }
    lineEdit->setText(secondNum);
}
void window::Button3Click()
{
    if(secondNum=="0")
        secondNum="3";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('3');
    }
    lineEdit->setText(secondNum);
}

void window::Button4Click()
{
    if(secondNum=="0")
        secondNum="4";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('4');
    }
    lineEdit->setText(secondNum);
}
void window::Button5Click()
{
    if(secondNum=="0")
        secondNum="5";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('5');
    }
    lineEdit->setText(secondNum);
}
void window::Button6Click()
{
    if(secondNum=="0")
        secondNum="6";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('6');
    }
    lineEdit->setText(secondNum);
}

void window::Button7Click()
{
    if(secondNum=="0")
        secondNum="7";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('7');
    }
    lineEdit->setText(secondNum);
}

void window::Button8Click()
{
    if(secondNum=="0")
        secondNum="8";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('8');
    }
    lineEdit->setText(secondNum);
}
void window::Button9Click()
{
    if(secondNum=="0")
        secondNum="9";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('9');
    }
    lineEdit->setText(secondNum);
}
void window::Button0Click()
{
    if(secondNum=="0")
        secondNum="0";
    else
    {
        secondNum=lineEdit->text();
        secondNum.append('0');
    }
    lineEdit->setText(secondNum);
}


void window::ButtonAddC()
{
    double n,m;
    secondNum=lineEdit->text();
    if(oper==' ')
    {
        firstNum=secondNum;
        secondNum="0";
        oper='+';
    }
    else
    {
        n=firstNum.toDouble();
        m=secondNum.toDouble();
        if(oper=='+') n=n+m;
        if(oper=='-') n=n-m;
        if(oper=='*') n=n*m;
        if(oper=='/') n=n/m;
        firstNum=QString::number(n,'f',2);
        secondNum="0";
        oper='+';
        lineEdit->setText(firstNum);
    }
}

void window::ButtonDelC()
{
    double n,m;
    secondNum=lineEdit->text();
    if(oper==' ')
    {
        firstNum=secondNum;
        secondNum="0";
        oper='-';
    }
    else
    {
        n=firstNum.toDouble();
        m=secondNum.toDouble();
        if(oper=='+') n=n+m;
        if(oper=='-') n=n-m;
        if(oper=='*') n=n*m;
        if(oper=='/') n=n/m;
        firstNum=QString::number(n,'f',2);
        secondNum="0";
        oper='-';
        lineEdit->setText(firstNum);
    }
}
void window::ButtonMulC()
{
    double n,m;
    secondNum=lineEdit->text();
    if(oper==' ')
    {
        firstNum=secondNum;
        secondNum="0";
        oper='*';
    }
    else
    {
        n=firstNum.toDouble();
        m=secondNum.toDouble();
        if(oper=='+') n=n+m;
        if(oper=='-') n=n-m;
        if(oper=='*') n=n*m;
        if(oper=='/') n=n/m;
        firstNum=QString::number(n,'f',2);
        secondNum="0";
        oper='*';
        lineEdit->setText(firstNum);
    }
}

void window::ButtonDivC()
{
    double n,m;
    secondNum=lineEdit->text();
    if(oper==' ')
    {
        firstNum=secondNum;
        secondNum="0";
        oper='/';
    }
    else
    {
        n=firstNum.toDouble();
        m=secondNum.toDouble();
        if(oper=='+') n=n+m;
        if(oper=='-') n=n-m;
        if(oper=='*') n=n*m;
        if(oper=='/') n=n/m;
        firstNum=QString::number(n,'f',2);
        secondNum="0";
        lineEdit->setText(firstNum);
    }
}
void window::ButtonResult()
{
    double n,m;
    secondNum=lineEdit->text();
    if(oper==' ')
    {
        firstNum=secondNum;
        secondNum="0";
        lineEdit->setText(firstNum);
        firstNum="0";
        oper=' ';
    }
    else
    {
        n=firstNum.toDouble();
        m=secondNum.toDouble();
        if(oper=='+') n=n+m;
        if(oper=='-') n=n-m;
        if(oper=='*') n=n*m;
        if(oper=='/') n=n/m;
        firstNum=QString::number(n,'f',2);
        secondNum="0";
        oper=' ';
        lineEdit->setText(firstNum);
        firstNum="0";
    }
}

==============================================================================

程序截图如下

==============================================================================

郑重声明:资讯 【可运行QT计算器源码_蓝石头_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——