Qt设计报告----------------计算器实现
学院:xxxxxxxxxxxxxxxxxxxx 姓名:xxxxxxxxxxxxxxxx 学号:xxxxxxxxxxxxxxxxxxxx 邮箱:xxxxxxxxxxxx@https://www.sodocs.net/doc/f6310528.html, 联系方式:xxxxxxxxxxxxxxxxxxx
myWidget.h文件代码如下:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include
#include
#include
class myWidget:public QWidget
{
Q_OBJECT
private:
QString Q1,Q2;//Q1和Q2是分别用来保存操作数QString Q3;//用于保存操作数
char c; //c用于保存运算符..
QLineEdit*lineEdit;
//第一行按钮
QPushButton *bMAdd; //保存数据按钮
QPushButton *bMSub; //读取数据按钮
QPushButton *bClear; //清理屏幕按钮
QPushButton *bAdd; //’+‘按钮
//第二行按钮
QPushButton *b1; //’1‘按钮
QPushButton *b2; //‘2’按钮
QPushButton *b3; //’3‘按钮
QPushButton *bSub; //’-‘按钮
//第三行按钮
QPushButton *b4; //’4‘按钮
QPushButton *b5; //’5‘按钮
QPushButton *b6; //’6‘按钮
QPushButton *bMul; //’*‘按钮
//第四行按钮
QPushButton *b7; //’7‘按钮
QPushButton *b8; //‘8’按钮
QPushButton *b9; //‘9’按钮
QPushButton *bDiv; //‘/’按钮
//第五行按钮
QPushButton *bPoi; //‘.’按钮
QPushButton *b0; //‘0’按钮
QPushButton *bEqual; //‘=’按钮
public:
myWidget(){
Q1="0";
Q2="0";
Q3="0";
c=' ';
this->setMinimumSize(300,290);
this->setMaximumSize(300,290);
QWidget *editwindow=new QWidget(this);//编辑窗口
editwindow->setGeometry(40,20,220,50);
lineEdit=new QLineEdit("欢迎使用就算器",editwindow);//显示输入框lineEdit->setGeometry(0,0,220,50);
lineEdit->setReadOnly(1);//设置为只读,不允许直接修改文本框
QWidget *buttonwindow=new QWidget(this);
buttonwindow->setGeometry(0,90,300,200);
bMAdd=new QPushButton("M+",buttonwindow);//第一行按钮bMAdd->setGeometry(40,0,40,20);
bMSub=new QPushButton("M-",buttonwindow);
bMSub->setGeometry(100,0,40,20);
bClear=new QPushButton("clear",buttonwindow);
bClear->setGeometry(160,0,40,20);
bAdd=new QPushButton("+",buttonwindow);
bAdd->setGeometry(220,0,40,20);
b1=new QPushButton("1",buttonwindow);//第二行按钮
b1->setGeometry(40,40,40,20);
b2=new QPushButton("2",buttonwindow);
b2->setGeometry(100,40,40,20);
b3=new QPushButton("3",buttonwindow);
b3->setGeometry(160,40,40,20);
bSub=new QPushButton("-",buttonwindow);
bSub->setGeometry(220,40,40,20);
b4=new QPushButton("4",buttonwindow);//第三行按钮
b4->setGeometry(40,80,40,20);
b5=new QPushButton("5",buttonwindow);
b5->setGeometry(100,80,40,20);
b6=new QPushButton("6",buttonwindow);
b6->setGeometry(160,80,40,20);
bMul=new QPushButton("*",buttonwindow);
bMul->setGeometry(220,80,40,20);
b7=new QPushButton("7",buttonwindow);//第四行按钮
b7->setGeometry(40,120,40,20);
b8=new QPushButton("8",buttonwindow);
b8->setGeometry(100,120,40,20);
b9=new QPushButton("9",buttonwindow);
b9->setGeometry(160,120,40,20);
bDiv=new QPushButton("/",buttonwindow);
bDiv->setGeometry(220,120,40,20);
b0=new QPushButton("0",buttonwindow);//第四行按钮
b0->setGeometry(40,160,40,20);
bPoi=new QPushButton(".",buttonwindow);
bPoi->setGeometry(100,160,40,20);
bEqual=new QPushButton("=",buttonwindow);
bEqual->setGeometry(160,160,40,20);
//信号连接
connect(bClear,SIGNAL(clicked()),this,SLOT(sClear()));
connect(b1,SIGNAL(clicked()),this,SLOT(s1()));
connect(b2,SIGNAL(clicked()),this,SLOT(s2()));
connect(b3,SIGNAL(clicked()),this,SLOT(s3()));
connect(b4,SIGNAL(clicked()),this,SLOT(s4()));
connect(b5,SIGNAL(clicked()),this,SLOT(s5()));
connect(b6,SIGNAL(clicked()),this,SLOT(s6()));
connect(b7,SIGNAL(clicked()),this,SLOT(s7()));
connect(b8,SIGNAL(clicked()),this,SLOT(s8()));
connect(b9,SIGNAL(clicked()),this,SLOT(s9()));
connect(b0,SIGNAL(clicked()),this,SLOT(s0()));
connect(bAdd,SIGNAL(clicked()),this,SLOT(sAdd()));
connect(bSub,SIGNAL(clicked()),this,SLOT(sSub()));
connect(bMul,SIGNAL(clicked()),this,SLOT(sMul()));
connect(bDiv,SIGNAL(clicked()),this,SLOT(sDiv()));
connect(bPoi,SIGNAL(clicked()),this,SLOT(sPoi()));
connect(bEqual,SIGNAL(clicked()),this,SLOT(sEqual()));
connect(bMAdd,SIGNAL(clicked()),this,SLOT(sMAdd()));
connect(bMSub,SIGNAL(clicked()),this,SLOT(sMSub()));
}
public slots: //自定义槽
void s1() //数字按键“1”的槽函数1-9同理
{
if(Q2=="0") Q2="1"; //如果Q2为0替代0
else //如果不是,获取当前文本编辑框内容,按一下按钮在文本框内容
{
Q2=lineEdit->text();
Q2.append('1');
}
lineEdit->setText(Q2); //将q2在文本编辑框中显示}
void s2()
{
if(Q2=="0") Q2="2";
else
{
Q2=lineEdit->text();
Q2.append('2');
}
lineEdit->setText(Q2);
}
void s3()
{
if(Q2=="0") Q2="3";
else
{
Q2=lineEdit->text();
Q2.append('3');
}
lineEdit->setText(Q2);
}
void s4()
{
if(Q2=="0") Q2="4";
else
{
Q2=lineEdit->text();
Q2.append('4');
}
lineEdit->setText(Q2);
}
void s5()
{
if(Q2=="0") Q2="5";
else
{
Q2=lineEdit->text();
Q2.append('5');
lineEdit->setText(Q2); }
void s6()
{
if(Q2=="0") Q2="6";
else
{
Q2=lineEdit->text();
Q2.append('6');
}
lineEdit->setText(Q2); }
void s7()
{
if(Q2=="0") Q2="7";
else
{
Q2=lineEdit->text();
Q2.append('7');
}
lineEdit->setText(Q2); }
void s8()
{
if(Q2=="0") Q2="8";
else
{
Q2=lineEdit->text();
Q2.append('8');
}
lineEdit->setText(Q2); }
void s9()
{
if(Q2=="0") Q2="9";
else
{
Q2=lineEdit->text();
Q2.append('9');
}
lineEdit->setText(Q2); }
void s0()
if(Q2=="0") Q2="0";
else
{
Q2=lineEdit->text();
Q2.append('0');
}
lineEdit->setText(Q2); //将q2在文本编辑框中显示
} //定义按钮0—9槽
void sPoi() //小数点槽
{
if(Q2=="0") Q2="0.";
else
{
Q2=lineEdit->text();
Q2.append('.');
}
lineEdit->setText(Q2);
}
void sAdd()//加号槽
{
double n,m;
Q2=lineEdit->text();
if(c==' ') //如果c为空格键,说明现在是第一次运算,那么不进行任何操作,只是将Q2复制给Q1同时保存这一次的操作符,以便下一次计算.
{
Q1=Q2;
Q2="0";
c='+';
}
else
{
n=Q1.toDouble(); //如果上一次已经有运算符,那么将Q1,Q2都转换成浮点数
m=Q2.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m; //实现运算
Q1=QString::number(n,'f',10); //再转换回字符形式
Q2="0"; //将Q2重新初始化
c='+';
lineEdit->setText(Q1); //显示中间结果
}
void sSub() //减号槽{
double n,m;
Q2=lineEdit->text();
if(c==' ')
{
Q1=Q2;
Q2="0";
c='-';
}
else
{
n=Q1.toDouble();
m=Q2.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
Q1=QString::number(n,'f',10);
Q2="0";
c='-';
lineEdit->setText(Q1);
}
}
void sDiv() //除号槽{
double n,m;
Q2=lineEdit->text();
if(c==' ')
{
Q1=Q2;
Q2="0";
c='/';
}
else
{
n=Q1.toDouble();
m=Q2.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
Q1=QString::number(n,'f',10);
Q2="0";
c='/';
lineEdit->setText(Q1);
}
}
void sMul() //乘号槽{
double n,m;
Q2=lineEdit->text();
if(c==' ')
{
Q1=Q2;
Q2="0";
c='*';
}
else
{
n=Q1.toDouble();
m=Q2.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
Q1=QString::number(n,'f',10);
Q2="0";
c='*';
lineEdit->setText(Q1);
}
}
void sEqual() //等号键槽{
double n,m;
Q2=lineEdit->text();
if(c==' '){
Q1=Q2;
Q2="0";
lineEdit->setText(Q1);
c='=';
}
else{
n=Q1.toDouble();
m=Q2.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
Q1=QString::number(n,'g',10);
Q2="0";
c=' ';
lineEdit->setText(Q1);
Q1="0";
}
}
void sClear()
{
Q1="0";
Q2="0";
c=' ';
lineEdit->setText(Q2);
}
void sMAdd()
{
Q3=lineEdit->text();
}
void sMSub()
{
Q2=Q3;
lineEdit->setText(Q2);
}
};
#endif
calculate.cpp文件代码如下:
#include
#include"myWidget.h"
int main(int argc,char**argv)
{
QApplication a(argc,argv);
myWidget*mainwindow=new myWidget();//主窗口
a.setActiveWindow(mainwindow);
mainwindow->show();//显示窗口
return a.exec();
}