《qt5计算器代码(共30页).doc》由会员分享,可在线阅读,更多相关《qt5计算器代码(共30页).doc(30页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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();
2、 void widgetInitial();/初始化控件 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 fa
3、ctorial(int num);/计算阶乘 /中缀转换为后缀表达式 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 *showTe
4、xtEdit; /表达式显示栏 QLineEdit *showResultEdit; /结果显示栏 QPushButton *backBtn; /回退 QPushButton *ceBtn; /清除 QPushButton *clearBtn; /清空 QPushButton *oneBtn; QPushButton *twoBtn; QPushButton *threeBtn; QPushButton *fourBtn; QPushButton *fiveBtn; QPushButton *sixBtn; QPushButton *sevenBtn; QPushButton *eightBt
5、n; QPushButton *nineBtn; QPushButton *zeroBtn; QPushButton *pointBtn; /. QPushButton *left_bracket; /( QPushButton *right_bracket; /) /内存管理 QPushButton *clearMemoryBtn; /创建清除内存按钮 QPushButton *readMemoryBtn; /创建读内存按钮 QPushButton *setMemoryBtn; /创建设置内存按钮 /运算 QPushButton *signBtn; /+/- QPushButton *add
6、Btn; QPushButton *subBtn; QPushButton *mulBtn; QPushButton *divideBtn; QPushButton *sqrtBtn; /开方 QPushButton *squBtn; /平方 QPushButton *cubeBtn; /立方 QPushButton *logBtn; QPushButton *recipBtn; /倒数 QPushButton *factBtn; /阶乘 QPushButton *sinBtn; QPushButton *cosBtn; QPushButton *tanBtn; QPushButton *In
7、Btn; /对数 QPushButton *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 setMemory
8、BtnSlot(); void 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 fourB
9、tnSlot(); void 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
10、(); void equalBtnSlot(); 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 *p
11、arent) : QDialog(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(); showT
12、extEdit=new QLineEdit(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); one
13、Btn-setText(1); 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);
14、 sevenBtn-setText(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); read
15、MemoryBtn=new 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(t
16、his); mulBtn-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);
17、 cubeBtn=new 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-se
18、tText(cos); tanBtn=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);
19、coshBtn-setText(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(show
20、ResultEdit,30); pushButtonStyle(clearMemoryBtn); pushButtonStyle(readMemoryBtn); pushButtonStyle(setMemoryBtn); pushButtonStyle(signBtn); pushButtonStyle(sqrtBtn); pushButtonStyle(sinBtn); pushButtonStyle(backBtn); pushButtonStyle(ceBtn); pushButtonStyle(clearBtn); pushButtonStyle(squBtn); pushButto
21、nStyle(cosBtn); pushButtonStyle(left_bracket); pushButtonStyle(right_bracket); pushButtonStyle(sevenBtn); pushButtonStyle(eightBtn); pushButtonStyle(nineBtn); pushButtonStyle(divideBtn); pushButtonStyle(cubeBtn); pushButtonStyle(tanBtn); pushButtonStyle(fourBtn); pushButtonStyle(fiveBtn); pushButton
22、Style(sixBtn); pushButtonStyle(mulBtn); pushButtonStyle(logBtn); pushButtonStyle(InBtn); pushButtonStyle(oneBtn); pushButtonStyle(twoBtn); pushButtonStyle(threeBtn); pushButtonStyle(subBtn); pushButtonStyle(recipBtn); pushButtonStyle(tenxBtn); pushButtonStyle(zeroBtn); pushButtonStyle(pointBtn); pus
23、hButtonStyle(addBtn); pushButtonStyle(factBtn); pushButtonStyle(equalBtn); pushButtonStyle(expBtn); pushButtonStyle(sinhBtn); pushButtonStyle(coshBtn); pushButtonStyle(tanhBtn); pushButtonStyle(xyBtn); pushButtonStyle(modBtn); /关联信号和槽 connect(clearMemoryBtn,SIGNAL(clicked(bool),this,SLOT(clearMemory
24、BtnSlot(); connect(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,SIGN
25、AL(clicked(bool),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,
26、SIGNAL(clicked(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,SL
27、OT(eightBtnSlot(); 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(
28、bool),this,SLOT(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(clicke
29、d(bool),this,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(c
30、licked(bool),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(factB
31、tn,SIGNAL(clicked(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(); con
32、nect(tanhBtn,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(QLay
33、out:SetFixedSize); 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,
34、2,4); mainLayout-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-addWi
35、dget(coshBtn,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); m
36、ainLayout-addWidget(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,
37、6,2); mainLayout-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); mai
38、nLayout-addWidget(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); pushB
39、utton-setFont(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
40、(Qt:AlignRight); 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().length()-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-s