Qt課題之科學(xué)計(jì)算器_第1頁
Qt課題之科學(xué)計(jì)算器_第2頁
Qt課題之科學(xué)計(jì)算器_第3頁
Qt課題之科學(xué)計(jì)算器_第4頁
Qt課題之科學(xué)計(jì)算器_第5頁
已閱讀5頁,還剩31頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、QT作業(yè) 科學(xué)計(jì)算器QT課程作業(yè)設(shè)計(jì)題目:科學(xué)計(jì)算器小組成員:陳啟敬 201113010409劉陽 201113010405何亞洲 201113010407王勇 201113010404黃昭宇 201113010421一、 功能設(shè)計(jì)要求: 特別聲明:本程序系QT5編譯制作科學(xué)計(jì)算器兩種計(jì)算器模式:一個(gè)普通模式,一個(gè)科學(xué)模式。參照windows7 的“計(jì)算器”程序說明:在查看菜單下有兩個(gè)菜單項(xiàng):“標(biāo)準(zhǔn)型”、“科學(xué)型”。當(dāng)用戶選擇標(biāo)準(zhǔn)型的時(shí)候,只有一個(gè)顯示區(qū),顯示用戶當(dāng)前鍵入的數(shù)據(jù)或計(jì)算結(jié)果;當(dāng)用戶選擇科學(xué)型的時(shí)候,有兩個(gè)顯示區(qū),其中最上面有個(gè)顯示用戶當(dāng)前鍵入的數(shù)據(jù) 或計(jì)算的結(jié)果,也就是標(biāo)準(zhǔn)型的顯

2、示區(qū),它的下面(MC 按鈕的左邊)有一個(gè)顯示區(qū)用來顯示當(dāng)前用戶臨時(shí)保存在Memory 中的數(shù)據(jù)。在編輯菜單下有“復(fù)制”和“粘貼”兩個(gè)菜單項(xiàng),其功能與 windows的計(jì)算器一樣。幫助菜單下有“關(guān)于計(jì)算器”菜單項(xiàng),點(diǎn)擊該菜單項(xiàng)時(shí) 彈出一個(gè)對話框,顯示版權(quán)信息(包含開發(fā)者名字、學(xué)號)。功能介紹:對于計(jì)算器我們都很熟悉,在平常的生活中也經(jīng)常用到。本次設(shè)計(jì)的科學(xué)計(jì)算器不但可以計(jì)算普通的加減乘除運(yùn)算,還可以對于三角函數(shù),乘方,對數(shù)等一些運(yùn)算。二、 詳細(xì)的設(shè)計(jì)介紹1 界面設(shè)計(jì) 界面設(shè)計(jì)我們是使用Qt creator進(jìn)行設(shè)計(jì)的,首先創(chuàng)建QMainWindow(parent).ui,這個(gè)界面為QWidget。

3、然后在左側(cè)的Widget Box找相應(yīng)的控件添加到QMainWindow(parent)里。然后添加相應(yīng)的按鈕,lineEdit控件作為算式顯示窗口和結(jié)果顯示窗口,pushButton作為相應(yīng)數(shù)字和功能按鈕等,并將它們的名稱屬性修改。相應(yīng)的屬性修改: 動(dòng)作編輯器的設(shè)置:信號槽設(shè)置:設(shè)計(jì)出的界面如下: 標(biāo)準(zhǔn)型: 科學(xué)型:實(shí)現(xiàn)版權(quán)信息: 界面的相關(guān)代碼:#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>namespace Uiclass MainWindow;class MainWindow : public Q

4、MainWindow Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); MainWindow();private slots: /存儲(chǔ)槽口 void on_btnMjian_clicked(); void on_btnMjia_clicked(); void on_btnMS_clicked(); void on_btnMR_clicked(); void on_btnMC_clicked();void about();void copy();void paste(); /標(biāo)準(zhǔn)型和科學(xué)型槽口 void on_action_S_

5、triggered(); void on_action_T_triggered(); /0-9槽函數(shù)的定義 void on_btn1_clicked(); void on_btn2_clicked(); void on_btn3_clicked(); void on_btn4_clicked(); void on_btn5_clicked(); void on_btn6_clicked(); void on_btn7_clicked(); void on_btn8_clicked(); void on_btn9_clicked(); void on_btn0_clicked(); void o

6、n_btnPoint_clicked();/點(diǎn) void additiveOperatorClicked(QString);/加法器 void multiplicativeOperatorClicked(QString);/乘法器 /void kuohaoOperatorClicked(QString);/括號運(yùn)算 void on_btnBackspace_clicked();/退格 void on_btnClear_clicked();/初始化 void on_btnPlus_clicked();/符號的改變 void on_btnCE_clicked();/清屏 void on_btnSq

7、rt_clicked();/開方 void on_btnPercent_clicked();/百分號 void on_btn_1_x_clicked();/ 1/x void on_btnEql_clicked();/等于 void clickbtn1(QString);/數(shù)字 void clickbtn2(QString);/一元操作運(yùn)算 void on_btnAdd_clicked();/加 void on_btnSubtract_clicked();/減 void on_btnMultiply_clicked();/乘 void on_btnDivide_clicked();/除 voi

8、d on_btnPi_clicked();/按鍵Pi void on_btnLn_clicked();/按鍵Ln void on_btnSinh_clicked();/按鍵sinh void on_btnSin_clicked();/按鍵Sin void on_btnX_2_clicked();/按鍵X2 void on_btnN_class_clicked();/按鍵N! void on_btnCosh_clicked();/按鍵Cosh void on_btnCos_clicked();/按鍵Cos void on_btnX_y_clicked();/按鍵xy void on_btnX_y

9、_clicked();/按鍵xy void on_btnTanh_clicked();/按鍵tanh void on_btnTan_clicked();/按鍵tan void on_btnX_3_clicked();/按鍵x3 void on_btnX_3_clicked();/按鍵3x void on_btnExp_clicked();/按鍵e void on_btnMod_clicked();/按鍵mod void on_btnLog_clicked();/按鍵log void on_btn10_x_clicked();/按鍵10x void on_btnLkuo_clicked();/按

10、鍵 ( void on_btnRkuo_clicked();/按鍵 )private: Ui:MainWindow *ui; QString B;/標(biāo)記等號狀態(tài) QString C;/標(biāo)記一元操作狀態(tài) QString D;/標(biāo)記add運(yùn)算符狀態(tài) QString E;/標(biāo)記mul運(yùn)算符狀態(tài) QString F;/標(biāo)記x_y,x_yQString K;/標(biāo)記括號狀態(tài)QString M; /QString G;/標(biāo)記x_y void abortOperation();/錯(cuò)誤判斷 double sumInMemory;/記憶存儲(chǔ) double sumSoFar;/加 double factorSoFa

11、r;/乘 double SoFar;/冪 double kuohao;/括號 QString pendingAdditiveOperator; QString pendingMultiplicativeOperator; QString pendingOperator1; QString kuoOperator;/括號操作 bool calculate(double rightOperand, const QString &pendingOperator);#endif / MAINWINDOW_H2 計(jì)算功能的設(shè)計(jì)2.1關(guān)于計(jì)算機(jī)的初始化和相關(guān)版本的代碼:#include "

12、;mainwindow.h"#include "ui_mainwindow.h"#include "QMessageBox"#include <math.h>/#define exp=2.71828/#define pi=3.14159MainWindow:MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui:MainWindow) ui->setupUi(this); ui->groupBox->hide();/科學(xué)計(jì)算框默認(rèn)隱藏 layout(

13、)->setSizeConstraint(QLayout:SetFixedSize); sumInMemory = 0.0;/變量初始化 sumSoFar = 0.0; factorSoFar = 0.0; SoFar=0.0; B="0" C="0" K="0" ui->lineEdit->setText("0"); ui->lineEdit->setReadOnly(true);/將文本編輯框設(shè)置為不可更改 ui->lineEdit->setAlignment(Qt:A

14、lignRight);/設(shè)置成右對齊 ui->lineEdit_2->setReadOnly(true);/將文本編輯框設(shè)置為不可更改 QFont font = ui->lineEdit->font();/設(shè)置字體 font.setPointSize(font.pointSize() + 6); ui->lineEdit->setFont(font);MainWindow:MainWindow() delete ui;void MainWindow:about()/關(guān)于計(jì)算器(A) QMessageBox:about(this, tr("關(guān)于計(jì)算器

15、(A)"), tr("<b>版本號:</b> v1.0.0.0<li>" "<b>開發(fā)者:</b> 陳啟敬 201113010409<li>" "<div align=right>劉陽 201113010405</div>" "<div align=right>何亞洲 201113010407</div>" "<div align=right>王勇 20111301

16、0404</div>" "<div align=right>黃昭宇 201113010421</div>");2.2.數(shù)字按鍵、等號等按鈕代碼: void MainWindow:on_btnEql_clicked()/按鍵=號 if (!ui->lineEdit_2->text().contains("=") double operand = ui->lineEdit->text().toDouble(); QString A=ui->lineEdit->text(); i

17、f (!pendingOperator1.isEmpty() calculate(operand,pendingOperator1); operand = SoFar; SoFar = 0.0; pendingOperator1.clear(); if (!pendingMultiplicativeOperator.isEmpty() if (!calculate(operand, pendingMultiplicativeOperator) abortOperation(); return; operand = factorSoFar; factorSoFar = 0.0; pendingM

18、ultiplicativeOperator.clear(); if (!pendingAdditiveOperator.isEmpty() if (!calculate(operand, pendingAdditiveOperator) abortOperation(); return; pendingAdditiveOperator.clear(); else sumSoFar = operand; ui->lineEdit->setText(QString:number(sumSoFar); if(K !="1") if(C != "1"

19、) ui->lineEdit_2->setText(ui->lineEdit_2->text()+A+"="+QString:number(sumSoFar); else ui->lineEdit_2->setText(ui->lineEdit_2->text()+"="+QString:number(sumSoFar); else ui->lineEdit_2->setText(ui->lineEdit_2->text()+"="+QString:number(

20、sumSoFar); sumSoFar = 0.0; B="1" C="0" /0-9槽函數(shù)void MainWindow:on_btn0_clicked()/0號鍵的槽函數(shù) clickbtn1("0");void MainWindow:on_btn1_clicked()/1號鍵的槽函數(shù) clickbtn1("1");void MainWindow:on_btn2_clicked()/2號鍵的槽函數(shù) clickbtn1("2");void MainWindow:on_btn3_clicked()

21、/3號鍵的槽函數(shù) clickbtn1("3");void MainWindow:on_btn4_clicked()/4號鍵的槽函數(shù) clickbtn1("4");void MainWindow:on_btn5_clicked()/5號鍵的槽函數(shù) clickbtn1("5");void MainWindow:on_btn6_clicked()/6號鍵的槽函數(shù) clickbtn1("6");void MainWindow:on_btn7_clicked()/7號鍵的槽函數(shù) clickbtn1("7"

22、);void MainWindow:on_btn8_clicked()/8號鍵的槽函數(shù) clickbtn1("8");void MainWindow:on_btn9_clicked()/9號鍵的槽函數(shù) clickbtn1("9");void MainWindow:on_btnPoint_clicked()/Point鍵的槽函數(shù) if (!ui->lineEdit->text().contains(".") ui->lineEdit->setText(ui->lineEdit->text() + tr

23、(".");void MainWindow:on_btnBackspace_clicked()/backspace槽函數(shù)的定義 QString operand = ui->lineEdit->text(); operand.chop(1); if (operand.isEmpty() operand = "0" ui->lineEdit->setText(operand);void MainWindow:on_btnCE_clicked()/按鍵CE=清楚屏幕 ui->lineEdit->setText("0

24、");2.3.計(jì)算槽的相關(guān)代碼:bool MainWindow:calculate(double rightOperand, const QString &pendingOperator)/計(jì)算槽,棧 if(pendingOperator=tr("+") sumSoFar+=rightOperand; else if(pendingOperator=tr("-") sumSoFar-=rightOperand; else if(pendingOperator=tr("*") factorSoFar*=rightOpe

25、rand; else if(pendingOperator=tr("/") if (rightOperand=0.0) return false; factorSoFar /= rightOperand; else if(pendingOperator=tr("x_y") SoFar=pow(SoFar,rightOperand); else if(pendingOperator=tr("x_y") if(rightOperand<=0.0) return false; SoFar=pow(rightOperand,1.0/So

26、Far); return true;void MainWindow:clickbtn1(QString btn)/按鍵數(shù)字 if(ui->lineEdit->text() = "0") ui->lineEdit->setText(btn); else if(!pendingOperator1.isEmpty() if(F!="0") ui->lineEdit->setText(btn); F = "0" else ui->lineEdit->setText(ui->lineEdit

27、->text()+btn); else if(!pendingMultiplicativeOperator.isEmpty() if(D!="0") ui->lineEdit->setText(btn); D = "0" else ui->lineEdit->setText(ui->lineEdit->text()+btn); else if(!pendingAdditiveOperator.isEmpty() if(E!="0") ui->lineEdit->setText(bt

28、n); E = "0" else ui->lineEdit->setText(ui->lineEdit->text()+btn); else if(B!="1") ui->lineEdit->setText(ui->lineEdit->text()+btn); else ui->lineEdit->setText(btn); B="0" 2.4.一元操作運(yùn)算的相關(guān)代碼:void MainWindow:clickbtn2(QString btn)/一元操作運(yùn)算 double o

29、perand = ui->lineEdit->text().toDouble(); double result = 0.0; if(btn=tr("Sqrt") if(operand<0.0) abortOperation(); return; result=sqrt(operand); else if(btn=tr("x_3") if(operand<0.0) abortOperation(); return; result=pow(operand,1.0/3.0); else if(btn=tr("pi")

30、 result=3.14159; else if(btn=tr("exp") result=2.71828; else if(btn=tr("x_2") result=pow(operand, 2.0); else if(btn=tr("10_x") result=pow(10.0, operand); else if(btn=tr("x_3") result=pow(operand, 3.0); else if(btn=tr("%") result=operand/100.0; else if

31、(btn=tr("1/x") if(operand=0.0) abortOperation(); return; result=1.0/operand; else if(btn=tr("ln") if(operand<0.0) abortOperation(); return; result=log(operand); QString str = QString:number(result,'f',4); result=str.toDouble(); else if(btn=tr("log10") if(oper

32、and<0.0) abortOperation(); return; result=log10(operand); else if(btn=tr("n!") int i,j=1; if(operand>0.0) for(i=2;i<=operand;i+) j*=i; result=j; else if(operand<0.0) abortOperation(); return; else result=1; else if(btn=tr("sinh") result=sinh(operand); else if(btn=tr(&

33、quot;sin") double a=operand/57.1158; result=sin(a); QString str = QString:number(result,'f',2); result=str.toDouble(); else if(btn=tr("cosh") result=cosh(operand); else if(btn=tr("cos") double a=operand/57.1158; result=cos(a); QString str = QString:number(result,'

34、;f',2); result=str.toDouble(); else if(btn=tr("tanh") result=tanh(operand); else if(btn=tr("tan") double a=operand/57.1158; result=tan(a); QString str = QString:number(result,'f',2); result=str.toDouble(); ui->lineEdit->setText(QString:number(result);void MainWi

35、ndow:abortOperation()/abortOperation的定義 on_btnClear_clicked(); ui->lineEdit->setText(tr("Error");2.5.計(jì)算器運(yùn)算實(shí)現(xiàn)代碼:void MainWindow:additiveOperatorClicked(QString clickedOperator)/加法器 double operand = ui->lineEdit->text().toDouble(); double A = ui->lineEdit->text().toDouble()

36、; if (!pendingOperator1.isEmpty() calculate(operand,pendingOperator1); operand = SoFar; ui->lineEdit->setText(QString:number(SoFar); SoFar = 0.0; /pendingOperator1.clear(); if (!pendingMultiplicativeOperator.isEmpty() if (!calculate(operand, pendingMultiplicativeOperator) abortOperation(); ret

37、urn; ui->lineEdit->setText(QString:number(factorSoFar); operand = factorSoFar; factorSoFar = 0.0; /pendingMultiplicativeOperator.clear(); if (!pendingAdditiveOperator.isEmpty() ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(A)+clickedOperator); if (!calculate(operand

38、, pendingAdditiveOperator) abortOperation(); return; ui->lineEdit->setText(QString:number(sumSoFar); else sumSoFar = operand; if(!pendingOperator1.isEmpty() ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(A)+clickedOperator); else if(!pendingMultiplicativeOperator.isE

39、mpty() ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(A)+clickedOperator); else if(K!="0") ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(operand)+clickedOperator); else ui->lineEdit_2->setText(QString:number(operand)+clickedOpera

40、tor); pendingMultiplicativeOperator.clear(); pendingOperator1.clear(); E = "1" pendingAdditiveOperator = clickedOperator;void MainWindow:multiplicativeOperatorClicked(QString clickedOperator)/乘法器 double operand = ui->lineEdit->text().toDouble(); double A = ui->lineEdit->text().

41、toDouble(); if (!pendingOperator1.isEmpty() calculate(operand,pendingOperator1); operand = SoFar; ui->lineEdit->setText(QString:number(SoFar); SoFar = 0.0; /pendingOperator1.clear(); if (!pendingMultiplicativeOperator.isEmpty() ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString

42、:number(A)+clickedOperator); if (!calculate(operand, pendingMultiplicativeOperator) abortOperation(); return; ui->lineEdit->setText(QString:number(factorSoFar); else factorSoFar = operand; if(!pendingOperator1.isEmpty() ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(

43、A)+clickedOperator); else if(!pendingAdditiveOperator.isEmpty() ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(A)+clickedOperator); else if(K!="0") ui->lineEdit_2->setText(ui->lineEdit_2->text()+QString:number(operand)+clickedOperator); else ui->li

44、neEdit_2->setText(QString:number(operand)+clickedOperator); pendingOperator1.clear(); pendingMultiplicativeOperator = clickedOperator; D = "1"/MC,MR,MS,M+,M-槽的定義void MainWindow:on_btnMC_clicked()/按鍵MC=memory clear sumInMemory = 0.0; ui->lineEdit_2->setText("M=0");/edit_2

45、顯示存儲(chǔ)中的數(shù)據(jù) ui->lineEdit->setText("0");void MainWindow:on_btnMR_clicked()/按鍵MR=memory read C = "1" ui->lineEdit->setText(QString:number(sumInMemory); QString M=QString:number(sumInMemory); if(!pendingAdditiveOperator.isEmpty()|!pendingMultiplicativeOperator.isEmpty() ui-

46、>lineEdit_2->setText(ui->lineEdit_2->text()+M); else ui->lineEdit_2->setText("M="+QString:number(sumInMemory);void MainWindow:on_btnMS_clicked()/按鍵MS=memory save on_btnEql_clicked(); sumInMemory = ui->lineEdit->text().toDouble(); ui->lineEdit_2->setText("

47、M="+QString:number(sumInMemory);/edit_2顯示存儲(chǔ)中的數(shù)據(jù)void MainWindow:on_btnMjia_clicked()/按鍵M+ on_btnEql_clicked(); sumInMemory += ui->lineEdit->text().toDouble(); ui->lineEdit_2->setText("M="+QString:number(sumInMemory);/edit_2顯示存儲(chǔ)中的數(shù)據(jù)void MainWindow:on_btnMjian_clicked()/按鍵M-

48、on_btnEql_clicked(); sumInMemory-=ui->lineEdit->text().toDouble(); ui->lineEdit_2->setText("M="+QString:number(sumInMemory);/edit_2顯示存儲(chǔ)中的數(shù)據(jù)void MainWindow:on_btnAdd_clicked()/+號鍵 additiveOperatorClicked("+"); C = "0" if(B != "0") ui->lineEdit_2-

49、>setText(ui->lineEdit->text()+"+"); void MainWindow:on_btnSubtract_clicked()/-號鍵 additiveOperatorClicked("-"); C = "0" if(B != "0") ui->lineEdit_2->setText(ui->lineEdit->text()+"-"); /else ui->lineEdit_2->setText(ui->lin

50、eEdit->text()+"-");void MainWindow:on_btnMultiply_clicked()/*號鍵 multiplicativeOperatorClicked("*"); C = "0" if(B != "0") ui->lineEdit_2->setText(ui->lineEdit->text()+"*"); void MainWindow:on_btnDivide_clicked()/號鍵 multiplicativeOperatorClicked("/"); C = "0" if(B != "0") ui->lineEdit_2->setText(ui->lineEdit->text()+"/"); void MainWindow:on_btnPlus_clicked()/按鍵

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論