Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1319213
  • 博文数量: 177
  • 博客积分: 3640
  • 博客等级: 中校
  • 技术积分: 1778
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-27 16:51
文章分类

全部博文(177)

文章存档

2014年(1)

2013年(10)

2012年(3)

2011年(163)

分类: LINUX

2011-05-24 21:15:37

#ifndef CACU_H
#define CACU_H

#include
#include
#include
#include
#include
class window:public QWidget //继承Qwidget类
{
Q_OBJECT //宏声明
public:
window();
public slots: //自定义槽
void B1();
void B2();
void B3();
void B4();
void B5();
void B6();
void B7();
void B8();
void B9();
void B0(); //定义按钮0—9槽口
void Badd(); //加号槽口
void Bdel(); //减号槽口
void Bdiv(); //除号槽口
void Bmul(); //乘号槽口
void Bpoi(); //小数点槽口
void Bden(); //等号键槽口
void Binit(); //初始化槽口
private:
QLineEdit *l ;
QPushButton *b1,*b2,*b3,*b4,*b5,*b6,*b7,*b8,*b9,*b0,*bjia,*bjian,*bchen,*bchu,*b,*bden,*binit;
//定义各个按钮
QString A,B;
//A和B是分别用来保存操作数的,保存成QString主要为了操作方便,
//因为QString类可以轻易地实现向浮点型的转换..
char c; //c用于保存运算符..
QLabel *ll; //修饰性的,“Made By Eden”就是这个Label
};
#endif //CACU_H


cacu.cpp
#include
#include
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("Made By Eden",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);
}


main.cpp
#include
#include
int main(int argc,char **argv)
{
QApplication a(argc,argv);
window w;
a.setMainWidget(&w);
w.show();
return a.exec();
}
阅读(4803) | 评论(0) | 转发(3) |
0

上一篇:PATH and LDFLAGS and CFLAGS

下一篇:Qt Event

给主人留下些什么吧!~~