#include<cacu.h>
#include<qfont.h>
window::window( )
{
resize(300,300); //重画窗口大小
A="0";
B="0";
c=' '; //初始化。注意c初始化为空格键
l=new QLineEdit(this);
l->setText(B);
l->setReadOnly(true); //添加文本编辑框并且设置为不可更改
b1=new QPushButton("1",this);
b2=new QPushButton("2",this);
b3=new QPushButton("3",this);
b4=new QPushButton("4",this);
b5=new QPushButton("5",this);
b6=new QPushButton("6",this);
b7=new QPushButton("7",this);
b8=new QPushButton("8",this);
b9=new QPushButton("9",this);
b0=new QPushButton("0",this);
bchen=new QPushButton("*",this);
bchu=new QPushButton("/",this);
bjia=new QPushButton("+",this);
bjian=new QPushButton("-",this);
b=new QPushButton(".",this);
bden=new QPushButton("=",this);
binit=new QPushButton("Init",this);
ll=new QLabel("Caculator",this); //为各个部件申请空间
QFont f=QFont("Times",18,QFont::Bold); //设置QLabel的格式
ll->setFont(f); //应用格式
/********以上处理各个部件在主窗口中的位置***********/
l->setGeometry(20,10,250,60);
b1->setGeometry(20,90,30,30);
b2->setGeometry(70,90,30,30);
b3->setGeometry(120,90,30,30);
b4->setGeometry(170,90,30,30);
b5->setGeometry(220,90,30,30);
b6->setGeometry(20,150,30,30);
b7->setGeometry(70,150,30,30);
b8->setGeometry(120,150,30,30);
b9->setGeometry(170,150,30,30);
b0->setGeometry(220,150,30,30);
bchen->setGeometry(20,210,30,30);
bchu->setGeometry(70,210,30,30);
bjia->setGeometry(120,210,30,30);
bjian->setGeometry(170,210,30,30);
b->setGeometry(220,210,30,30);
bden->setGeometry(20,270,30,30);
binit->setGeometry(70,270,30,30);
ll->setGeometry(120,265,200,40);
/*******以上连接各个按键和对应的槽****************/
connect(b1,SIGNAL(clicked()),this,SLOT(B1()));
connect(b2,SIGNAL(clicked()),this,SLOT(B2()));
connect(b3,SIGNAL(clicked()),this,SLOT(B3()));
connect(b4,SIGNAL(clicked()),this,SLOT(B4()));
connect(b5,SIGNAL(clicked()),this,SLOT(B5()));
connect(b6,SIGNAL(clicked()),this,SLOT(B6()));
connect(b7,SIGNAL(clicked()),this,SLOT(B7()));
connect(b8,SIGNAL(clicked()),this,SLOT(B8()));
connect(b9,SIGNAL(clicked()),this,SLOT(B9()));
connect(b0,SIGNAL(clicked()),this,SLOT(B0()));
connect(bjia,SIGNAL(clicked()),this,SLOT(Badd()));
connect(bjian,SIGNAL(clicked()),this,SLOT(Bdel()));
connect(bchen,SIGNAL(clicked()),this,SLOT(Bmul()));
connect(bchu,SIGNAL(clicked()),this,SLOT(Bdiv()));
connect(b,SIGNAL(clicked()),this,SLOT(Bpoi()));
connect(bden,SIGNAL(clicked()),this,SLOT(Bden()));
connect(binit,SIGNAL(clicked()),this,SLOT(Binit()));
}
/********以下为各个槽函数详细内容********/
void window::B1() //数字按键“1”的槽函数
{
if(B=="0") B="1"; //如果B为0直接进行替代,想象下计算器,按了“1”键总不能显示的是“01”吧..
else
{
B=l->text(); //如果不是,先获取当前文本编辑框的内容,如B=23,再按一下那么在后面追加1..
B.append('1');
}
l->setText(B); //将B再次送到文本编辑框进行显示...
}
void window::B2() //同上
{
if(B=="0") B="2";
else
{
B=l->text();
B.append('2');
}
l->setText(B);
}
void window::B3() //同上..
{
if(B=="0") B="3";
else
{
B=l->text();
B.append('3');
}
l->setText(B);
}
void window::B4()
{
if(B=="0") B="4";
else
{
B=l->text();
B.append('4');
}
l->setText(B);
}
void window::B5()
{
if(B=="0") B="5";
else
{
B=l->text();
B.append('5');
}
l->setText(B);
}
void window::B6()
{
if(B=="0") B="6";
else
{
B=l->text();
B.append('6');
}
l->setText(B);
}
void window::B7()
{
if(B=="0") B="7";
else
{
B=l->text();
B.append('7');
}
l->setText(B);
}
void window::B8()
{
if(B=="0") B="8";
else
{
B=l->text();
B.append('8');
}
l->setText(B);
}
void window::B9()
{
if(B=="0") B="9";
else
{
B=l->text();
B.append('9');
}
l->setText(B);
}
void window::B0()
{
if(B=="0") B="0";
else
{
B=l->text();
B.append('0');
}
l->setText(B);
}
void window::Bpoi() //这个是小数点键对应的slot函数,比较特殊,直接追加就可以了
{
if(B=="0") B.append('.');
else
{
B=l->text();
B.append('.');
}
l->setText(B);
}
void window::Badd() //加法键
{
double n,m;
B=l->text();
if(c==' ') //如果c为空格键,说明现在是第一次运算,那么不进行任何操作,只是将B复制给A(注意A原来是“0”)
//同时保存这一次的操作符,以便下一次计算.
{
A=B;
B="0";
c='+';
}
else
{
n=A.toDouble(); //如果上一次已经有运算符,那么将A,B都转换成浮点数
m=B.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m; //实现运算
A=QString::number(n,'f',10); //再转换回字符形式
B="0"; //将B重新初始化
c='+';
l->setText(A); //显示中间结果
}
}
void window::Bdel() //同上
{
double n,m;
B=l->text();
if(c==' ')
{
A=B;
B="0";
c='-';
}
else
{
n=A.toDouble();
m=B.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
A=QString::number(n,'f',10);
B="0";
c='-';
l->setText(A);
}
}
void window::Bmul() //乘号
{
double n,m;
B=l->text();
if(c==' ')
{
A=B;
B="0";
c='*';
}
else
{
n=A.toDouble();
m=B.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
A=QString::number(n,'f',10);
B="0";
c='*';
l->setText(A);
}
}
void window::Bdiv() //除号
{
double n,m;
B=l->text();
if(c==' ')
{
A=B;
B="0";
c='/';
}
else
{
n=A.toDouble();
m=B.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
A=QString::number(n,'f',10);
B="0";
l->setText(A);
}
}
void window::Bden() //等号键,同上类似
{
double n,m;
B=l->text();
if(c==' ')
{
A=B;
B="0";
l->setText(A);
A="0";
c=' ';
}
else
{
n=A.toDouble();
m=B.toDouble();
if(c=='+') n=n+m;
if(c=='-') n=n-m;
if(c=='*') n=n*m;
if(c=='/') n=n/m;
A=QString::number(n,'f',10);
B="0";
c=' ';
l->setText(A);
A="0";
}
}
void window::Binit() //按了Init键,所有都初始化
{
A="0";
B="0";
c=' ';
l->setText(B);
}