qt5计算器代码_第1页
qt5计算器代码_第2页
qt5计算器代码_第3页
qt5计算器代码_第4页
qt5计算器代码_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Qt计算器源代码caculator.h头文件#ifndef CACULATOR_H#define CACULATOR_H#include #include #include #include #include #include #include #include #include #include math.hnamespace Ui class Caculator;class Caculator : public QDialog Q_OBJECTpublic: explicit Caculator(QWidget *parent = 0); Caculator(); void widgetI

2、nitial();/初始化控件 void widgetLayout();/设置窗口布局 void pushButtonStyle(QPushButton *pushButton);/设置按钮样式 void lineEditStyle(QLineEdit *lineEdit,int height);/设置输入框样式 void setNumber(int num);/设置数字 void setOperation(int oper);/设置+、-、*、/、.、)等符号 void caculate(int cacu);/计算x2、x3、sin、cos、tan等 long factorial(int n

3、um);/计算阶乘 /中缀转换为后缀表达式 int toPostfix(const char *S,char OPS,int &len); /计算后缀表达式 int evaluation (char B, int len, double &result,bool &flag);protected: void closeEvent(QCloseEvent *event);/窗口关闭事件 void keyPressEvent(QKeyEvent *key);/按键事件private: Ui:Caculator *ui; /计算器相关控件 QLineEdit *showTextEdit; /表达式显

4、示栏 QLineEdit *showResultEdit; /结果显示栏 QPushButton *backBtn; /回退 QPushButton *ceBtn; /清除 QPushButton *clearBtn; /清空 QPushButton *oneBtn; QPushButton *twoBtn; QPushButton *threeBtn; QPushButton *fourBtn; QPushButton *fiveBtn; QPushButton *sixBtn; QPushButton *sevenBtn; QPushButton *eightBtn; QPushButto

5、n *nineBtn; QPushButton *zeroBtn; QPushButton *pointBtn; /. QPushButton *left_bracket; /( QPushButton *right_bracket; /) /内存管理 QPushButton *clearMemoryBtn; /创建清除内存按钮 QPushButton *readMemoryBtn; /创建读内存按钮 QPushButton *setMemoryBtn; /创建设置内存按钮 /运算 QPushButton *signBtn; /+/- QPushButton *addBtn; QPushBut

6、ton *subBtn; QPushButton *mulBtn; QPushButton *divideBtn; QPushButton *sqrtBtn; /开方 QPushButton *squBtn; /平方 QPushButton *cubeBtn; /立方 QPushButton *logBtn; QPushButton *recipBtn; /倒数 QPushButton *factBtn; /阶乘 QPushButton *sinBtn; QPushButton *cosBtn; QPushButton *tanBtn; QPushButton *InBtn; /对数 QPus

7、hButton *tenxBtn; QPushButton *expBtn; QPushButton *sinhBtn; QPushButton *coshBtn; QPushButton *tanhBtn; QPushButton *xyBtn; /xy QPushButton *modBtn; /取模 QPushButton *equalBtn; double sumMemory;private slots: /按钮所关联的槽函数 void clearMemoryBtnSlot(); void readMemoryBtnSlot(); void setMemoryBtnSlot(); vo

8、id signBtnSlot(); void sqrtBtnSlot(); void sinBtnSlot(); void backBtnSlot(); void ceBtnSlot(); void clearBtnSlot(); void squBtnSlot(); void cosBtnSlot(); void sevenBtnSlot(); void eightBtnSlot(); void nineBtnSlot(); void divideBtnSlot(); void cubeBtnSlot(); void tanBtnSlot(); void fourBtnSlot(); voi

9、d fiveBtnSlot(); void sixBtnSlot(); void mulBtnSlot(); void logBtnSlot(); void InBtnSlot(); void oneBtnSlot(); void twoBtnSlot(); void threeBtnSlot(); void subBtnSlot(); void recipBtnSlot(); void tenxBtnSlot(); void zeroBtnSlot(); void pointBtnSlot(); void addBtnSlot(); void factBtnSlot(); void equa

10、lBtnSlot(); void expBtnSlot(); void sinhBtnSlot(); void coshBtnSlot(); void tanhBtnSlot(); void xyBtnSlot(); void modBtnSlot(); void left_bracketSlot(); void right_bracketSlot();#endif / CACULATOR_Hcaculator.cpp源文件#include caculator.h#include ui_caculator.hCaculator:Caculator(QWidget *parent) : QDia

11、log(parent), ui(new Ui:Caculator) ui-setupUi(this); this-widgetInitial(); this-widgetLayout();Caculator:Caculator() delete ui;/初始化控件void Caculator:widgetInitial() this-setWindowFlags(Qt:WindowCloseButtonHint); this-setWindowTitle(计算器); this-setFixedSize(this-width(),this-height(); showTextEdit=new Q

12、LineEdit(this); showTextEdit-setText(); showResultEdit=new QLineEdit(this); showResultEdit-setText(0); backBtn=new QPushButton(this); backBtn-setText(); ceBtn=new QPushButton(this); ceBtn-setText(CE); clearBtn=new QPushButton(this); clearBtn-setText(C); oneBtn=new QPushButton(this); oneBtn-setText(1

13、); twoBtn=new QPushButton(this); twoBtn-setText(2); threeBtn=new QPushButton(this); threeBtn-setText(3); fourBtn=new QPushButton(this); fourBtn-setText(4); fiveBtn=new QPushButton(this); fiveBtn-setText(5); sixBtn=new QPushButton(this); sixBtn-setText(6); sevenBtn=new QPushButton(this); sevenBtn-set

14、Text(7); eightBtn=new QPushButton(this); eightBtn-setText(8); nineBtn=new QPushButton(this); nineBtn-setText(9); zeroBtn=new QPushButton(this); zeroBtn-setText(0); pointBtn=new QPushButton(this); pointBtn-setText(.); clearMemoryBtn=new QPushButton(this); clearMemoryBtn-setText(MC); readMemoryBtn=new

15、 QPushButton(this); readMemoryBtn-setText(MR); setMemoryBtn=new QPushButton(this); setMemoryBtn-setText(MS); signBtn=new QPushButton(this); signBtn-setText(302261); addBtn=new QPushButton(this); addBtn-setText(+); subBtn=new QPushButton(this); subBtn-setText(-); mulBtn=new QPushButton(this); mulBtn-

16、setText(*); divideBtn=new QPushButton(this); divideBtn-setText(/); right_bracket=new QPushButton(this); right_bracket-setText(); left_bracket=new QPushButton(this); left_bracket-setText(); sqrtBtn=new QPushButton(this); sqrtBtn-setText(); squBtn=new QPushButton(this); squBtn-setText(x); cubeBtn=new

17、QPushButton(this); cubeBtn-setText(x); logBtn=new QPushButton(this); logBtn-setText(log); recipBtn=new QPushButton(this); recipBtn-setText(1/x); factBtn=new QPushButton(this); factBtn-setText(n!); sinBtn=new QPushButton(this); sinBtn-setText(sin); cosBtn=new QPushButton(this); cosBtn-setText(cos); t

18、anBtn=new QPushButton(this); tanBtn-setText(tan); InBtn=new QPushButton(this); InBtn-setText(In); tenxBtn=new QPushButton(this); tenxBtn-setText(10x); expBtn=new QPushButton(this); expBtn-setText(Exp); sinhBtn=new QPushButton(this); sinhBtn-setText(sinh); coshBtn=new QPushButton(this); coshBtn-setTe

19、xt(cosh); tanhBtn=new QPushButton(this); tanhBtn-setText(tanh); xyBtn=new QPushButton(this); xyBtn-setText(xy); modBtn=new QPushButton(this); modBtn-setText(Mod); equalBtn=new QPushButton(this); equalBtn-setText(=); sumMemory=0.0; /设置样式 lineEditStyle(showTextEdit,30); lineEditStyle(showResultEdit,30

20、); pushButtonStyle(clearMemoryBtn); pushButtonStyle(readMemoryBtn); pushButtonStyle(setMemoryBtn); pushButtonStyle(signBtn); pushButtonStyle(sqrtBtn); pushButtonStyle(sinBtn); pushButtonStyle(backBtn); pushButtonStyle(ceBtn); pushButtonStyle(clearBtn); pushButtonStyle(squBtn); pushButtonStyle(cosBtn

21、); pushButtonStyle(left_bracket); pushButtonStyle(right_bracket); pushButtonStyle(sevenBtn); pushButtonStyle(eightBtn); pushButtonStyle(nineBtn); pushButtonStyle(divideBtn); pushButtonStyle(cubeBtn); pushButtonStyle(tanBtn); pushButtonStyle(fourBtn); pushButtonStyle(fiveBtn); pushButtonStyle(sixBtn)

22、; pushButtonStyle(mulBtn); pushButtonStyle(logBtn); pushButtonStyle(InBtn); pushButtonStyle(oneBtn); pushButtonStyle(twoBtn); pushButtonStyle(threeBtn); pushButtonStyle(subBtn); pushButtonStyle(recipBtn); pushButtonStyle(tenxBtn); pushButtonStyle(zeroBtn); pushButtonStyle(pointBtn); pushButtonStyle(

23、addBtn); pushButtonStyle(factBtn); pushButtonStyle(equalBtn); pushButtonStyle(expBtn); pushButtonStyle(sinhBtn); pushButtonStyle(coshBtn); pushButtonStyle(tanhBtn); pushButtonStyle(xyBtn); pushButtonStyle(modBtn); /关联信号和槽 connect(clearMemoryBtn,SIGNAL(clicked(bool),this,SLOT(clearMemoryBtnSlot(); co

24、nnect(readMemoryBtn,SIGNAL(clicked(bool),this,SLOT(readMemoryBtnSlot(); connect(setMemoryBtn,SIGNAL(clicked(bool),this,SLOT(setMemoryBtnSlot(); connect(signBtn,SIGNAL(clicked(bool),this,SLOT(signBtnSlot(); connect(sqrtBtn,SIGNAL(clicked(bool),this,SLOT(sqrtBtnSlot(); connect(sinBtn,SIGNAL(clicked(bo

25、ol),this,SLOT(sinBtnSlot(); connect(backBtn,SIGNAL(clicked(bool),this,SLOT(backBtnSlot(); connect(ceBtn,SIGNAL(clicked(bool),this,SLOT(ceBtnSlot(); connect(clearBtn,SIGNAL(clicked(bool),this,SLOT(clearBtnSlot(); connect(squBtn,SIGNAL(clicked(bool),this,SLOT(squBtnSlot(); connect(cosBtn,SIGNAL(clicke

26、d(bool),this,SLOT(cosBtnSlot(); connect(left_bracket,SIGNAL(clicked(bool),this,SLOT(left_bracketSlot(); connect(right_bracket,SIGNAL(clicked(bool),this,SLOT(right_bracketSlot(); connect(sevenBtn,SIGNAL(clicked(bool),this,SLOT(sevenBtnSlot(); connect(eightBtn,SIGNAL(clicked(bool),this,SLOT(eightBtnSl

27、ot(); connect(nineBtn,SIGNAL(clicked(bool),this,SLOT(nineBtnSlot(); connect(divideBtn,SIGNAL(clicked(bool),this,SLOT(divideBtnSlot(); connect(cubeBtn,SIGNAL(clicked(bool),this,SLOT(cubeBtnSlot(); connect(tanBtn,SIGNAL(clicked(bool),this,SLOT(tanBtnSlot(); connect(fourBtn,SIGNAL(clicked(bool),this,SL

28、OT(fourBtnSlot(); connect(fiveBtn,SIGNAL(clicked(bool),this,SLOT(fiveBtnSlot(); connect(sixBtn,SIGNAL(clicked(bool),this,SLOT(sixBtnSlot(); connect(mulBtn,SIGNAL(clicked(bool),this,SLOT(mulBtnSlot(); connect(logBtn,SIGNAL(clicked(bool),this,SLOT(logBtnSlot(); connect(InBtn,SIGNAL(clicked(bool),this,

29、SLOT(InBtnSlot(); connect(oneBtn,SIGNAL(clicked(bool),this,SLOT(oneBtnSlot(); connect(twoBtn,SIGNAL(clicked(bool),this,SLOT(twoBtnSlot(); connect(threeBtn,SIGNAL(clicked(bool),this,SLOT(threeBtnSlot(); connect(subBtn,SIGNAL(clicked(bool),this,SLOT(subBtnSlot(); connect(recipBtn,SIGNAL(clicked(bool),

30、this,SLOT(recipBtnSlot(); connect(tenxBtn,SIGNAL(clicked(bool),this,SLOT(tenxBtnSlot(); connect(zeroBtn,SIGNAL(clicked(bool),this,SLOT(zeroBtnSlot(); connect(pointBtn,SIGNAL(clicked(bool),this,SLOT(pointBtnSlot(); connect(addBtn,SIGNAL(clicked(bool),this,SLOT(addBtnSlot(); connect(factBtn,SIGNAL(cli

31、cked(bool),this,SLOT(factBtnSlot(); connect(equalBtn,SIGNAL(clicked(bool),this,SLOT(equalBtnSlot(); connect(expBtn,SIGNAL(clicked(bool),this,SLOT(expBtnSlot(); connect(sinhBtn,SIGNAL(clicked(bool),this,SLOT(sinhBtnSlot(); connect(coshBtn,SIGNAL(clicked(bool),this,SLOT(coshBtnSlot(); connect(tanhBtn,

32、SIGNAL(clicked(bool),this,SLOT(tanhBtnSlot(); connect(xyBtn,SIGNAL(clicked(bool),this,SLOT(xyBtnSlot(); connect(modBtn,SIGNAL(clicked(bool),this,SLOT(modBtnSlot();/设置窗口布局void Caculator:widgetLayout() QGridLayout *mainLayout = new QGridLayout(this);/网格布局 mainLayout-setSizeConstraint(QLayout:SetFixedS

33、ize); mainLayout-addWidget(showTextEdit , 0,0 ,1 ,7); mainLayout-addWidget(showResultEdit,1,0,1,7); mainLayout-addWidget(clearMemoryBtn,2,0); mainLayout-addWidget(readMemoryBtn,2,1); mainLayout-addWidget(setMemoryBtn,2,2); mainLayout-addWidget(signBtn,2,3); mainLayout-addWidget(sqrtBtn,2,4); mainLay

34、out-addWidget(sinBtn,2,5); mainLayout-addWidget(sinhBtn,2,6); mainLayout-addWidget(backBtn,3,0); mainLayout-addWidget(ceBtn,3,1); mainLayout-addWidget(clearBtn,3,2); mainLayout-addWidget(divideBtn,3,3); mainLayout-addWidget(squBtn,3,4); mainLayout-addWidget(cosBtn,3,5); mainLayout-addWidget(coshBtn,

35、3,6); mainLayout-addWidget(sevenBtn,4,0); mainLayout-addWidget(eightBtn,4,1); mainLayout-addWidget(nineBtn,4,2); mainLayout-addWidget(mulBtn,4,3); mainLayout-addWidget(cubeBtn,4,4); mainLayout-addWidget(tanBtn,4,5); mainLayout-addWidget(tanhBtn,4,6); mainLayout-addWidget(fourBtn,5,0); mainLayout-add

36、Widget(fiveBtn,5,1); mainLayout-addWidget(sixBtn,5,2); mainLayout-addWidget(subBtn,5,3); mainLayout-addWidget(logBtn,5,4); mainLayout-addWidget(InBtn,5,5); mainLayout-addWidget(xyBtn,5,6); mainLayout-addWidget(oneBtn,6,0); mainLayout-addWidget(twoBtn,6,1); mainLayout-addWidget(threeBtn,6,2); mainLay

37、out-addWidget(addBtn,6,3); mainLayout-addWidget(recipBtn,6,4); mainLayout-addWidget(tenxBtn,6,5); mainLayout-addWidget(modBtn,6,6); mainLayout-addWidget(zeroBtn,7,0); mainLayout-addWidget(pointBtn,7,1); mainLayout-addWidget(left_bracket,7,2); mainLayout-addWidget(right_bracket,7,3); mainLayout-addWi

38、dget(factBtn,7,4); mainLayout-addWidget(expBtn,7,5); mainLayout-addWidget(equalBtn,7,6);/按钮样式void Caculator:pushButtonStyle(QPushButton *pushButton) QFont font(宋体,12); font.setBold(true); QString styleStr=border:1px solid gray;border-radius:5px; pushButton-setStyleSheet(styleStr); pushButton-setFont

39、(font); pushButton-setFixedSize(45,35);/输入框样式void Caculator:lineEditStyle(QLineEdit *lineEdit,int height) QFont font(宋体,14); font.setBold(true); QString styleStr=border:1px solid gray;border-radius:10px; lineEdit-setStyleSheet(styleStr); lineEdit-setReadOnly(true); lineEdit-setAlignment(Qt:AlignRigh

40、t); lineEdit-setFont(font); lineEdit-setMinimumHeight(height);/设置数字void Caculator:setNumber(int num) showTextEdit-setText(showTextEdit-text().trimmed() + QString:number(num);/设置+、-、*、/、.、)等符号void Caculator:setOperation(int oper) if(!showTextEdit-text().isEmpty() int endIndex=showTextEdit-text().leng

41、th()-1; QChar endChar=showTextEdit-text().at(endIndex); if(endChar!=+&endChar!=-&endChar!=*&endChar!=/&endChar!=.&endChar!=( &endChar!=%&endChar!=) switch (oper) case 1: showTextEdit-setText(showTextEdit-text() + +); break; case 2: showTextEdit-setText(showTextEdit-text() + -); break; case 3: showTe

42、xtEdit-setText(showTextEdit-text() + *); break; case 4: showTextEdit-setText(showTextEdit-text() + /); break; case 5: showTextEdit-setText(showTextEdit-text() + .); break; case 6: showTextEdit-setText(showTextEdit-text() + ); break; case 7: showTextEdit-setText(showTextEdit-text() + ); break; case 8

43、: showTextEdit-setText(showTextEdit-text() + %); break; default: break; /计算x2、x3、sin、cos、tan等void Caculator:caculate(int cacu) double result; if(!showTextEdit-text().isEmpty() QString text=showTextEdit-text(); int endIndex=text.length()-1; QChar endChar=text.at(endIndex); if(endChar!=+&endChar!=-&en

44、dChar!=*& endChar!=/&endChar!=.&endChar!=(&endChar!=) &endChar!=%&endChar!=) double text_to_double=text.toDouble(); switch (cacu) case 1:/开方 if(text_to_double=0) result=sqrt(text_to_double); showTextEdit-setText(sqrt(+QString:number(text_to_double)+); else QMessageBox:warning(this,警告,被开方数不能为负!); ret

45、urn; break; case 2:/平方 result=pow(text_to_double,2.0); showTextEdit-setText(square(+QString:number(text_to_double)+); break; case 3:/立方 result=pow(text_to_double,3.0); showTextEdit-setText(cube(+QString:number(text_to_double)+); break; case 4:/log10 result=log10(text_to_double); showTextEdit-setText(log(+QString:number(text_to_double)+); break; case 5:/1/x result=pow(text_to_double,-1.0); showTextEdit-setText(reciproc(+QString:number(text_to_double)+); break; case 6:/阶乘 result=factorial(int)te

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论