Qt计算器.docx
《Qt计算器.docx》由会员分享,可在线阅读,更多相关《Qt计算器.docx(22页珍藏版)》请在冰豆网上搜索。
Qt计算器
Linux界面设计
——Qt简单计算器
课程名称:
计算机基础综合
课题分类:
Qt界面设计
班级:
11计算机科学与技术2班
姓名:
段秉芮
学号:
1145532211
完成时间:
2014年01月01
Qt计算器
一.摘要
当今社会是信息社会,科技经济高速发展的社会,为了更方便人们的工作生活和加速人们处理信息的速度,计算器应运而生。
然而,电脑已经成为当代人生活的必需品,运行在电脑上的计算器成为了人们的生活学习绝佳伴侣。
本设计是用C++编写的,基于Qt图形用户界面应用程序框架的计算器。
设计出两个电脑端和安卓手机端两个应用程序,并实现了一定的计算功能。
二.建立项目如下所示:
首先在linux下softcenter安装QtCreator
或者在terminal中输入命令安装;
sudoapt-getinstalllibqt4-devlibqt4-debuglibqt4-guilibqt4-sqlqt4-dev-toolsqt4-docqt4-designerqt4-qtconfig
比如qt4-dev-tools包含了QtAssistant及QtLinguist等工具,因此不需要单独安装这两个工具。
其它的,qt4-doc是帮助文档,包含了Qt中各个类库的详细说明以及丰富的例子程序,可以使用QtAssistant工具来打开阅读。
qt4-qtconfig是配置Qt环境的一个对话框,一般默认就行了,很少有必要去更改。
qt4-demos包含很多可以运行起来的可执行文件以及源代码。
qt4-designer是用来设计GUI界面的设计器
sudoapt-getinstalllibqt4-debug
//sudoapt-getinstalllibqt4-dbg
安装完成打开。
1.创建工程
选择File->New File or Project,然后如下所示,之后next。
之后在Details时候,选项Baseclass选中QWidget。
然后next至finish。
二.程序代码如下:
1.建立calculator.h
右键calculator项目,选择addnew。
。
。
如下所示:
calculator.h代码修改如下:
#ifndefCALCULATOR_H
#defineCALCULATOR_H
#include
#include
#include
#include
classCalculator:
publicQWidget
{
Q_OBJECT
public:
Calculator(QWidget*parent=0);
~Calculator();
private:
QTextEdit*screenTextEdit;
QPushButton*button1;
QPushButton*button2;
QPushButton*button3;
QPushButton*button4;
QPushButton*button5;
QPushButton*button6;
QPushButton*button7;
QPushButton*button8;
QPushButton*button9;
QPushButton*button0;
QPushButton*buttonx;
QPushButton*buttondivide;
QPushButton*buttonadd;
QPushButton*buttonsub;
QPushButton*buttondot;
QPushButton*buttonequal;
QPushButton*buttonclear;
QPushButton*buttonback;
QPushButton*buttonLArc;
QPushButton*buttonRArc;
boolisEnter;
doublesum;
inti;
intj;
doubleresult;
boolisPoint;
boolisLArc;
charsign;
boolisTwoOptrsFlag;
boolisEqualClicked;
intf1[7];
intf2[7];
QVectorvectorOpnd;
QVectorvectorOptr;
QVectorvectorExp;
boolisTwoOptr(QPushButton*button);
voidinitConnect();
voidinitVector();
doubleoperate(doublea,chartheat,doubleb);
charprecedeSign(charoptr1,charoptr2);
intchar2num(charoptr);
voidinitData();
charbutton2char(QPushButton*button);
voidgetDigital(chardigital);
voidgetSign(charch);
boolpickSign(charch);
boolpushVector(charoptr);
voidgetResult();
privateslots:
voidgetButton();
voidclearScreen();
voidmyBackSpace();
};
#endif//CALCULATOR_H
2.main.cpp修改如下:
只需要将
Widgetw;
w.show();
修改为
Calculatorcal;
cal.show();
3.calculator.cpp
建立calculator.cpp文件的过程和建立calculator.h的过程一样,只是选择C++SourceFile选项。
calculator.cpp代码如下:
#include"calculator.h"
#include
Calculator:
:
Calculator(QWidget*parent)
:
QWidget(parent)
{
initData();
setWindowTitle("计算器");
setWindowIcon(QIcon(":
/calculator.png"));
screenTextEdit=newQTextEdit;
screenTextEdit->setFixedHeight(50);
screenTextEdit->setFont(QFont("Courier10Pitch",18,QFont:
:
Bold));
screenTextEdit->setReadOnly(true);
button1=newQPushButton("1");
button2=newQPushButton("2");
button3=newQPushButton("3");
button4=newQPushButton("4");
button5=newQPushButton("5");
button6=newQPushButton("6");
button7=newQPushButton("7");
button8=newQPushButton("8");
button9=newQPushButton("9");
button0=newQPushButton("0");
buttonx=newQPushButton("×");
buttonadd=newQPushButton("+");
buttonsub=newQPushButton("-");
buttonback=newQPushButton("back");
buttondivide=newQPushButton("/");
buttondot=newQPushButton(".");
buttonLArc=newQPushButton("(");
buttonRArc=newQPushButton(")");
buttonequal=newQPushButton("=");
buttonclear=newQPushButton("clear");
initConnect();
QGridLayout*gridLayout=newQGridLayout;
gridLayout->addWidget(screenTextEdit,0,0,2,5);
gridLayout->addWidget(button1,2,0,1,1);
gridLayout->addWidget(button2,2,1,1,1);
gridLayout->addWidget(button3,2,2,1,1);
gridLayout->addWidget(buttonadd,2,3,1,1);
gridLayout->addWidget(buttonsub,2,4,1,1);
gridLayout->addWidget(button4,3,0,1,1);
gridLayout->addWidget(button5,3,1,1,1);
gridLayout->addWidget(button6,3,2,1,1);
gridLayout->addWidget(buttonx,3,3,1,1);
gridLayout->addWidget(buttondivide,3,4,1,1);
gridLayout->addWidget(button7,4,0,1,1);
gridLayout->addWidget(button8,4,1,1,1);
gridLayout->addWidget(button9,4,2,1,1);
gridLayout->addWidget(buttonLArc,4,3,1,1);
gridLayout->addWidget(buttonRArc,4,4,1,1);
gridLayout->addWidget(button0,5,0,1,1);
gridLayout->addWidget(buttondot,5,1,1,1);
gridLayout->addWidget(buttonback,5,2,1,1);
gridLayout->addWidget(buttonclear,5,3,1,1);
gridLayout->addWidget(buttonequal,5,4,1,1);
setLayout(gridLayout);
initVector();
}
voidCalculator:
:
initConnect()
{
connect(button0,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button1,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button2,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button3,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button4,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button5,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button6,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button7,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button8,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(button9,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttonx,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttonadd,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttondivide,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttondot,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttonsub,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttonRArc,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttonLArc,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
connect(buttonback,&QPushButton:
:
clicked,this,&Calculator:
:
myBackSpace);
connect(buttonclear,&QPushButton:
:
clicked,this,&Calculator:
:
clearScreen);
connect(buttonequal,&QPushButton:
:
clicked,this,&Calculator:
:
getButton);
}
boolCalculator:
:
isTwoOptr(QPushButton*button)
{
if(button==buttonadd||button==buttonsub
||button==buttonx||button==buttondivide)
returntrue;
else
returnfalse;
}
voidCalculator:
:
myBackSpace()
{
QTextCursorcursor=screenTextEdit->textCursor();
cursor.deletePreviousChar();
if(!
isEqualClicked)
vectorExp.pop_back();
}
voidCalculator:
:
getButton()
{
QPushButton*button=qobject_cast(sender());
if(isTwoOptrsFlag&&isTwoOptr(button))
{
}
else{
vectorExp.append(button2char(button));
isEqualClicked=false;
isTwoOptrsFlag=false;
if(!
isEnter)
screenTextEdit->append("");
screenTextEdit->insertPlainText(button->text());
isEnter=true;
if(button==buttonadd||button==buttonsub
||button==buttonx||button==buttondivide)
isTwoOptrsFlag=true;
}
if(button==buttonequal)
{
isEqualClicked=true;
getResult();
}
}
voidCalculator:
:
getResult()
{
while(!
vectorExp.isEmpty())
{
charch=vectorExp.first();
vectorExp.removeFirst();
getDigital(ch);
getSign(ch);
}
}
voidCalculator:
:
getDigital(chardigital)
{
if(digital=='0'||digital=='1'||digital=='2'
||digital=='3'||digital=='4'||digital=='5'
||digital=='6'||digital=='7'||digital=='8'
||digital=='9')
{
if(isPoint)
i*=10;
j=0;
sum=sum*10+(digital-'0');
isLArc=false;
}
elseif(digital=='.')
{
isPoint=true;
isLArc=false;
}
}
voidCalculator:
:
getSign(charch)
{
if(isLArc&&ch!
='('&&ch!
=')')
{
sign=ch;
}
elseif(ch=='+'||ch=='-'
||ch=='*'||ch=='/'
||ch=='='||ch=='('
||ch==')')
{
if(ch=='(')
isLArc=true;
else
isLArc=false;
j++;
if(j<=1)
{
if(sign=='-')
sum=sum*(-1);
sign='';
vectorOpnd.append(sum/i);
isLArc=false;
i=1;
sum=0;
isPoint=false;
}
pickSign(ch);
}
}
boolCalculator:
:
pickSign(charch)
{
chartheta;
doublea,b;
if(ch=='=')
{
while(vectorOptr.last()!
='#')
{
do{
theta=vectorOptr.last();
vectorOptr.pop_back();
}while(theta=='('||theta==')');
if(theta!
='#')//void8(,9),8()
{
b=vectorOpnd.last();vectorOpnd.pop_back();
a=vectorOpnd.last();vectorOpnd.pop_back();
vectorOpnd.append(operate(a,theta,b));
}
elseif(theta=='#')
vectorOptr.append('#');
}
result=vectorOpnd.last();
screenTextEdit->insertPlainText(QString("%1").arg(result));
vectorOpnd.clear();
vectorOptr.clear();
initVector();
isEnter=false;
returntrue;
}
pushVector(ch);
returntrue;
}
boolCalculator:
:
pushVector(charoptr)
{
chartheta;
doublea,b;
if(optr==')')
{
do{
theta=vectorOptr.last();
vectorOptr.pop_back();
}while(theta=='('||theta==')');
if(theta!
='#')//void8(,9),8()
{
b=vectorOpnd.last();vectorOpnd.pop_back();
a=vectorOpnd.last();vectorOpnd.pop_back();
vectorOpnd.append(operate(a,theta,b));
}
else
{
vectorOptr.append('#');
}
returntrue;
}
switch(precedeSign(vectorOptr.last(),optr))
{
case'<':
vectorOptr.append(optr);
break;
case'=':
vectorOptr.pop_back();
break;
case'>':
theta=vectorOptr.last();
vectorOptr.pop_back();
b=vectorOpnd.last();
vectorOpnd.pop_back();
a=vectorOpnd.last();
vectorOpnd.pop_back();
vectorOpnd.append(operate(a,theta,b));
vectorOptr.append(optr);
break;
}
returntrue;
}
vo