嵌入式实验之多功能计算器的实现
嵌入式_实验五_简单计算器的设计

嵌入式操作系统原理与应用实验报告实验题目:实验五、简单计算器设计院系:计算机科学与技术学院专业年级:学生姓名:学号2014 年10 月7日一、实验目标和重点使用MFC编写一个如下图所示的计算器程序可以直接从键盘输入两个数,然后按下运算按钮可以计算,可以计算三角函数二、实验内容思路:设计三个edit控件,作为被运算数和结果显示,添加加减乘除按钮,按下后进行运算;添加Check 按钮检测状态,因为三角函数都是单目运算符,故只有一个数,选择其中一个edit控件显示。
三、实验步骤及结果1)首先打开Microsoft Visual Studio,选择新建项目,基于对话框2)创建一个对话框程序,在Dialog中完成以下设计图,添加“+”、“‐”,“*”,“/”按钮button 控件并在属性里修改各个控件的caption属性、控件ID。
例如:“+”修改为ID_ADD,完成修改后,如图:3)对三个edit控件进行添加变量m_num1、m_num2、m_result,前面两个为操作数,后面一个为运算结果。
5)点击“闪电”,选择添加事件如下图6)在操作符按下的click事件下添加代码:操作符“+”添加代码:void CsimplecaculatorDlg::OnBnClickedAdd() {UpdateData();m_result=m_num1+m_num2;UpdateData(FALSE);}操作符“-”添加代码:void CsimplecaculatorDlg::OnBnClickedMinus() {UpdateData();m_result=m_num1*m_num2;UpdateData(FALSE);}操作符“-”添加代码:void CsimplecaculatorDlg::OnBnClickedPlus(){UpdateData();m_result=m_num1-m_num2;UpdateData(FALSE);}操作符“/”添加代码:void CsimplecaculatorDlg::OnBnClickedDiv(){UpdateData();if(m_num2)m_result=m_num1/m_num2;elseAfxMessageBox(_T("被除数不能为!"),0,0);UpdateData(FALSE);}7)以下为三角函数的添加代码:同时要在文件的开始添加pi的值,#define pi 3.1415926操作符“sin”按下的click事件下添加以下代码UpdateData();temp=m_num1;if(m_num1<180)m_result=sin(m_num1*pi/180.0); //Pi为圆周率,定义预处理命令else{while (temp>=180){temp-=180;}m_result=sin(temp*pi/180.0);UpdateData(FALSE);其他运算符(cos tan cot )同理添加如图:8)以下为选择简单计算器和三角函数的按钮的控件添加代码:9)在操作符“清除”按下的click事件下添加以下代码:10)完成以上操作后,为了实现三角函数的函数曲线,添加以下代码:CDC *pDC=GetDC();//CClientDC * pClientDC=myWindow.GetClientRect(&rc);//得到绘图环境CPen pen,*poldpen; //创建画笔,和用来保存旧画笔pen.CreatePen(PS_SOLID,2,RGB(m_red,0,0)); //创建一个新画笔的,特性,实线,线宽个像素poldpen=pDC->SelectObject(&pen); //选择新画笔,保存旧画笔//colorBrush.CreateSolidBrush(clRGB);//CDC*pDC=GetDC();pDC->MoveTo(110,180);pDC->LineTo(260,180);pDC->MoveTo(120,110);pDC->LineTo(120,250);pDC->MoveTo(120,200);double i,x,y;for (i=0;i<=200;i++){x=120+i;y=180-20*sin(i/20);pDC->LineTo(x,y);}pDC->SelectObject(&poldpen);// 恢复原画笔状态ReleaseDC(pDC); //释放画笔CBrush colorBrush; 11)实验结果截图:四、心得体会通过此次实验,完成了简单计算器的设计。
嵌入式实验报告-简易计算器

一目的及要求1实验目的根据计算器的原理设计一个具有加减乘除功能的简易计算器。
2实验要求(1)数字和结果用数码管显示。
(2)数字、+-*/、=、C用4X4键盘实现。
(3)计算结果正确,有出错提示。
二实验原理框图基本工作原理:本设计利用数码管和4*4矩阵式键盘实现了简易计算器的功能。
接通电源,数码管显示全0。
计算时,通过键盘输入需要计算的数字,该数字显示在数码管上,当键入等号时,计算结果显示在数码管上。
进行第二次运算时,按C键清除键盘结果。
当计算出现错误时,LED灯亮报警。
当计算结果超出数码管显示出现溢出时,报警电路也会报警。
报警输出为--。
四系统软件设计1.数据输入模块原理:通过4*4矩阵模块输入数字,在数码管上显示出来。
2.运算模块原理:四种运算同步运行,通过按键加、减、乘、除选择输出对应的计算结果,当按键等号时,将所得结果反馈给运算模块输入端。
3.输出模块原理:通过按键等号来控制显示运算对象还是运算结果,当等号按下时,输出计算结果,否则显示当前输入的数据。
当输出结果溢出是LED亮四次,同时数码管显示都为--。
五实验调试首先按清零键清零。
然后进行调试。
输入数据2,再按乘法键,输入第二个数字6,按等号键,数码管显示12;再按除法键,输入第二个数据3,按等号键,数码管显示4;再按加法键,输入第三个数据7,依次按等号键,数码管显示11;按减法键,输入第四个数据99,依次按等号键,数码管显示-88。
若输入超出显示管的最大值或者超出数码管的位数,结果溢出,LED亮报警,同时数码管显示都为--。
如输入999999加上2 ,结果就溢出,LED灯亮四次报警。
六程序#include "LPC2468.h" /* LPC24xx definitions */ #include "type.h"#include "irq.h"#include "target.h"#include "timer.h"#include "fio.h"#include "keyboard.h"#include "SPI.h"extern BYTE seg_buf[50]; // LPC2468开发板使用此数组的0~5显示六个数码管;LPC2478板使用1~6BYTE seg_copy1[7];BYTE seg_copy2[7];unsigned long Num1 =0;/*第一个输入的数字*/unsigned long Num2 =0;/*第二个输入的数字*/unsigned long Num3 =0;/*第二个输入的数字*/extern BYTE KEY; // LPC2468开发板使用此数组的0~5显示六个数码管;LPC2478板使用1~6enum {Add =1,Dec,Mut,Div,nofuntion}funtion;/******************************************************************** *********** Main Function main()********************************************************************* *********/int main (void){unsigned char counter = 0; /*计算输入的数字的个数,超过6个则报警,运算结果超过6位数也报警*/unsigned char cal_allow = 1; /*允许输入数字标志*/unsigned char input_allow = 1;/*允许输入数字标志*/unsigned char funtion_type = 0;/*运算功能*/unsigned char Ne_num = 0;/*负数标志*/DWORD value=0,i=0;TargetResetInit();enable_timer(1);SPI_Init(8); // SPI总线速率为28.8/8 = 3.6 MHz Seg_Init(); // 数码管初始化LedsInit();for(i=0;i<7;i++){seg_copy1[i]=0;seg_copy2[i]=0;seg_buf[i]=0;}counter = 0;cal_allow = 1;input_allow = 1;funtion_type = nofuntion;while ( 1 ){value = KEY;/*输入数字*/if(value>0 && value<11){if(counter < 6&&input_allow==1){if(counter == 0) seg_buf[1] = value-1;else{for(i=0;i<counter;i++){seg_buf[counter+1-i] = seg_buf[counter-i]; }seg_buf[1] = value-1;}counter++;}if(counter == 6){input_allow = 0;LedOn(1);LedOn(2);LedOn(3);LedOn(4);}}/*如果是“C”键,则清除显示,清除计算标志*/if(value == 11){for(i=0;i<7;i++){seg_copy1[i]=0;seg_copy2[i]=0;seg_buf[i]=0;}counter = 0;Num1 = 0;Num2 = 0;Num3 = 0;cal_allow = 1;input_allow = 1;Ne_num = 0;/*负数标志*/funtion_type = nofuntion;}/*如果是“+”键,则显示结果*/if(value == 13 ){if(cal_allow == 1){for(i=0;i<7;i++){seg_copy1[i] = seg_buf[i];/*备份第一次输入的数字*/seg_buf[i]=0; /*显示清零以准备第二次输入数字*/}funtion_type = Add;counter = 0; /*计数器清零允许第二次计数*/cal_allow =1; /*再等号按下前不能再按第二次*/input_allow = 1; /*允许第二次输入数据*/}else{input_allow = 0; /*禁止按下2次功能键时候输入数据*/}/*如果是“-”键,则显示结果*/if(value == 14&& cal_allow == 1){if(cal_allow == 1){for(i=0;i<7;i++){seg_copy1[i] = seg_buf[i];/*备份第一次输入的数字*/seg_buf[i]=0; /*显示清零以准备第二次输入数字*/}funtion_type = Dec;counter = 0; /*计数器清零允许第二次计数*/cal_allow =1; /*再等号按下前不能再按第二次*/input_allow = 1; /*允许第二次输入数据*/}else{input_allow = 0; /*禁止按下2次功能键时候输入数据*/ }}/*如果是“X”键,则显示结果*/if(value == 15 && cal_allow == 1){if(cal_allow == 1){for(i=0;i<7;i++)seg_copy1[i] = seg_buf[i];/*备份第一次输入的数字*/seg_buf[i]=0; /*显示清零以准备第二次输入数字*/}funtion_type = Mut;counter = 0; /*计数器清零允许第二次计数*/cal_allow =1; /*再等号按下前不能再按第二次*/input_allow = 1; /*允许第二次输入数据*/}else{input_allow = 0; /*禁止按下2次功能键时候输入数据*/ }}/*如果是“/”键,则显示结果*/if(value == 16 && cal_allow == 1){if(cal_allow == 1){for(i=0;i<7;i++){seg_copy1[i] = seg_buf[i];/*备份第一次输入的数字*/seg_buf[i]=0; /*显示清零以准备第二次输入数字*/}funtion_type = Div;counter = 0; /*计数器清零允许第二次计数*/cal_allow =1; /*再等号按下前不能再按第二次*/input_allow = 1; /*允许第二次输入数据*/}else{input_allow = 0; /*禁止按下2次功能键时候输入数据*/ }}/*如果是“=”键,则清除显示,清除计算标志*/if(value == 12){for(i=0;i<7;i++){seg_copy2[i] = seg_buf[i];/*拷贝第二次输入的数字*/}/*把输入的数字串1合成运算数字*/Num1 = seg_copy1[6]*100000+seg_copy1[5]*10000+seg_copy1[4]*1000 +seg_copy1[3]*100+seg_copy1[2]*10 +seg_copy1[1];/*把输入的数字串2合成运算数字*/Num2 = seg_copy2[6]*100000+seg_copy2[5]*10000+seg_copy2[4]*1000 +seg_copy2[3]*100+seg_copy2[2]*10 +seg_copy2[1];switch(funtion_type){case Add:{Num1 = Num1+Num2;/*计算结果存在Num1中*/break;}case Dec:{if(Num1==Num2) Num1 = 0;else if(Num1>Num2){Num3 = Num1-Num2;/*计算结果存在Num1中*/ Num1 = Num3;}else if(Num2 > Num1){Num3 = Num2-Num1;Ne_num =1;Num1 = Num3;}break;}case Mut:{Num3 = Num1*Num2;/*计算结果存在Num1中*/ Num1 = Num3;break;}case Div:{if(Num1>=Num2&&Num2!=0){Num3 = Num1/Num2;/*计算结果存在Num1中*/}Num1 = Num3;break;}default:break;}/*显示结果*/if(Num1>999999||(Ne_num ==1&&Num1>99999)||Num2 ==0) {for(i=0;i<7;i++){seg_copy1[i]=0;seg_copy2[i]=0;seg_buf[i]=10;/*显示横杠表示计算溢出错误!*/}for(i=0;i<5;i++){LedOn(1);LedOn(2);LedOn(3);LedOn(4);delayMs(1,200);LedOff(1);LedOff(2);LedOff(3);LedOff(4);delayMs(1,200);}}else{seg_buf[1] = Num1%10;seg_buf[2] = (Num1%100)/10;seg_buf[3] = (Num1%1000)/100;seg_buf[4] = (Num1%10000)/1000;seg_buf[5] = (Num1%100000)/10000;seg_buf[6] = Num1/100000;if(Ne_num ==1){seg_buf[6] = 10;/*显示负号*/}}}delayMs(1,200);}}/******************************************************************** *********** End Of File********************************************************************* ********/七实验现象(1)运行成功以后的计算机界面如图一:图一(2)正确输入6+6=12的现象如图二和图三:图二图三(3)当进行除操作且除数为0时的现象如图四:图四七实验小结在设计计算机的过程中,我们遇到了很多问题,但是都通过查资料和请教同学得到了解决。
多功能计算器

引言 (2)1.计算器的发展和分类 (2)计算器的发展 (2)计算器的分类 (3)2.设计方法 (4)3.硬件系统设计 (4)单片机 (4)4*4矩阵式键盘 (5)7段LED数码管 (6)程序下载接口 (6)数码管驱动芯片 (6)单片机时钟电路 (6)单片机复位电路 (7)4.软件设计 (10)汇编语言和C语言的特点及选择 (10)键扫程序设计 (11)算术运算程序设计 (11)显示原理 (12)5.硬件调试 (12)常见故障 (12)6.软件调试 (13)总结 (13)参考文献 (15)附录 (16)引言当今社会,计算器作为一种快速、通用的计算工具方便了用户的使用。
单片机由于其微小的体积和极低的成本,广泛的应用于家用电器、工业控制等领域中。
在工业生产中,单片微型计算机是微型计算机的一个重要分支,也是颇具生命里的机种。
单片机的出现是计算机制造技术高速发展的产物,它是嵌入式控制系统的核心。
单片微型计算机简称单片机,特别使用于控制领域,故又称为微控制器。
本文是基于单片机控制的多功能计算器的设计。
设计以单片机为控制核心,运用C语言编写程序,采用矩阵键盘式输入数据、在液晶模块LCD1602上显示计算过程以及结果,该计算器能进行加、减、乘、除运算,此外该计算器还具有计时和倒计时的功能。
这样一个简易的计算器实现了多功能,相比当前一些计算器来说,在功能上增加了却没有增加产品的成本,采用的技术也更趋向于国产化。
1.计算器的发展和分类计算器的发展计算器一般由运算器、控制器、存储器、键盘、显示器、电源和一些可选外围设备及电子配件通过人工或机器设备组成。
低档计算器的运算器、控制器由数字逻辑电路实现简单的串行运算,其随机存储器只有一、二个单元,供累加存储用。
高档计算器由微处理器和只读存储器实现各种复杂的运算程序,有较多的随机存储单元以存放输入程序和数据。
键盘是计算器的输入部件,一般采用接触式或传感式。
为减小计算器的尺寸,一键常常有多种功能。
嵌入式系统之基于QT的简单计算器

嵌入式系统之基于QT的简单计算器QT是一种跨平台的应用程序开发框架,它提供了一种编写图形用户界面(GUI)程序的简便方法。
在嵌入式系统中,使用QT可以快速开发出各种功能强大、界面友好的应用程序。
对于一个简单计算器来说,主要包括用户界面设计和计算逻辑两部分。
首先,我们需要设计一个界面来展示计算器的按钮和显示结果的区域。
在QT中,可以使用QGridLayout布局管理器来创建按钮布局,同时将其连接到对应的信号槽函数上,以实现按钮的点击响应。
```cpp//main.qmlimport QtQuick 2.12import QtQuick.Window 2.12import QtQuick.Controls 2.12Windowvisible: truewidth: 400height: 600title: "Simple Calculator"RowLayoutspacing: 10GridLayoutid: buttonGridrows: 4columns: 4anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter Buttontext: "7"onClicked: calculator.append("7")}Buttontext: "8"onClicked: calculator.append("8")}// ... more buttons ...Buttontext: "="onClicked: calculator.calculate}// ... more buttons ...}TextFieldid: resultFieldwidth: 200height: 50placeholderText: "0"readOnly: true}}```在这个示例中,我们使用了QT的QML语言来创建用户界面。
嵌入式QT实验报告

嵌入式QT实验报告一、实验目的本次实验的目的是设计并实现一个简单的计算器应用程序,基于嵌入式QT进行开发。
通过这个实验,可以熟悉QT的控件使用、界面设计和信号与槽的连接等知识点。
二、实验原理计算器应用程序的主要功能是实现基本的数学运算,包括加、减、乘、除。
用户通过界面上的按钮输入数字和运算符,程序根据用户的输入进行相应的计算,并将结果显示在界面上。
三、系统设计1.界面设计计算器的界面主要分为两个部分,一个是数字输入框,用于用户输入数字;另一个是运算符按钮区域,用于用户选择运算符。
2.数字和运算符输入用户通过按钮选择数字和运算符,并将其显示在数字输入框中,需要使用QT的信号与槽机制来连接按钮和数字输入框。
3.计算功能根据用户选择的运算符,进行相应的数学运算,并将结果显示在数字输入框中。
需要使用switch语句来判断用户选择的运算符,并进行相应的计算。
四、实验步骤1.创建QT应用程序项目在QT Creator中创建一个新的QT Widget应用程序项目,命名为"Calculator",选择嵌入式QT版本。
2.设计界面使用QT Designer设计计算器的界面,添加一个数字输入框和若干个按钮,按钮包括数字按钮和运算符按钮。
需要注意布局和按钮的大小等细节。
3.连接信号与槽4.实现计算功能在mainwindow.cpp文件中实现计算器功能的代码。
首先声明一个私有变量operand用于保存上一次的操作数;然后根据用户选择的按钮,进行相应的计算。
五、实验结果(插入计算器界面截图)六、实验总结通过本次实验,我成功地设计并实现了一个简单的计算器应用程序。
在实验过程中,我学会了使用QT Designer设计界面,了解了QT的信号与槽机制,能够连接按钮和输入框等控件;同时,也巩固了基本的C++编程知识,熟悉了switch语句的使用。
在实验过程中,我遇到了一些问题,比如界面的布局和按钮的大小等细节问题,通过查阅文档和调试,最终解决了这些问题。
ram嵌入式计算器报告

[加减乘除计算器]设计说明书12205235栗晋鹏[二零一五年六月二十六日]加减乘除计算器目录1. .............................................................................................................................. 需求分析 ..................................................................2..1.1编写目的 ........................................................2. 1.2冃^景1.3要求 ..1.4工作原理2. 概要设计 ............................................................3..2.1功能模块图 ......................................................3.2.2计算器功能流程图 ............................................... 4.2.3设计效果图 ...................................................... 6.3. 详细设计 ............................................................ 6..3.1界面布置及“清零” ............................................. 7.3.2捕获键值 ........................................................ 9.3.5结果运算 ....................................................... 1.34. ............................................................................................................................ 调试总结 (18)4.1问题调试 ....................................................... 1.84.2心得体会 (20)5. ................................................................................................................................ 系统实现 (20)1.需求分析1.1编写目的1) 掌握ARM9嵌入式系统的程序设计方法;2) 理论联系实际,进一步提高软件开发技术;3) 培养自己分析问题解决问题的能力;4) 提高实践论文撰写能力。
多功能的计算方法

多功能的计算方法
多功能计算器是一种可以进行多种复杂计算的电子设备。
它的按键布局和功能可能会因品牌和型号的不同而有所差异,但一般来说,多功能计算器可以执行以下操作:
1. 基本算术运算:如加、减、乘、除等。
2. 代数运算:如解方程、求导数等。
3. 三角函数运算:如正弦、余弦、正切等。
4. 指数和对数运算:如自然对数、常用对数等。
5. 统计运算:如求平均值、求和、求方差等。
6. 概率运算:如组合数、概率计算等。
7. 绘制函数图像:有些多功能计算器还可以绘制函数图像,方便用户直观地了解函数的性质和变化规律。
使用多功能计算器可以大大提高计算效率和精度,特别是对于需要进行复杂计算的领域,如科学、工程、金融等。
嵌入式计算器的程序设计

嵌入式计算器的程序设计
嵌入式计算器是一种常见且常用的电子计算机设备,其内部集成了数字电路和微处理器芯片,可以完成加减乘除、取模、开根号等基本运算,也可以进行科学计算、统计计算、矩阵计算等高级运算。
嵌入式计算器具有体积小、价格便宜、使用方便等特点,广泛应用于各种电子产品中,例如电子手表、计算器、智能手机等。
在嵌入式计算器的程序设计中,需要熟练掌握计算机编程语言,例如C语言、汇编语言等,同时还需要掌握计算机硬件结构、内存管理、寄存器、输入输出等知识。
在程序设计过程中,需要考虑计算精度、运算速度、内存占用等因素,以保证程序的高效性和可靠性。
嵌入式计算器的程序设计分为两个阶段,编写和编译。
编写阶段需要使用计算机编程语言编写程序代码,根据需要添加注释和调试语句,以便于程序的测试和修改。
编写好程序代码后,需要进行编译,将程序代码转化为可执行的二进制代码,通常使用交叉编译器完成。
在编写嵌入式计算器的程序时,需要遵循一些编程规范,例如变量命名规范、函数参数传递规范、代码缩进规范等,以便于后续的代码维护和升级。
同时,需要进行充分的测试和调试,确保程序的正确性和稳定性。
总之,嵌入式计算器的程序设计需要掌握一定的计算机编程知识和技能,对于计算机科学专业的学生和从事相关工作的人员来说,掌握这些知识和技能是必要的。
通过有效的程序设计和优化,可以提高嵌入式计算器的运算速度和稳定性,从而满足日常工作和生活中的各种计算需求。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/**************************************************************************** **** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).** All rights reserved.** Contact: Nokia Corporation (qt-info@)**** This file is part of the examples of the Qt Toolkit.**** $QT_BEGIN_LICENSE:LGPL$** Commercial Usage** Licensees holding valid Qt Commercial licenses may use this file in** accordance with the Qt Commercial License Agreement provided with the** Software or, alternatively, in accordance with the terms contained in** a written agreement between you and Nokia.**** GNU Lesser General Public License Usage** Alternatively, this file may be used under the terms of the GNU Lesser** General Public License version 2.1 as published by the Free Software** Foundation and appearing in the file LICENSE.LGPL included in the** packaging of this file. Please review the following information to** ensure the GNU Lesser General Public License version 2.1 requirements** will be met: /licenses/old-licenses/lgpl-2.1.html.**** In addition, as a special exception, Nokia gives you certain additional** rights. These rights are described in the Nokia Qt LGPL Exception** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.**** GNU General Public License Usage** Alternatively, this file may be used under the terms of the GNU** General Public License version 3.0 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file. Please review the following information to** ensure the GNU General Public License version 3.0 requirements will be** met: /copyleft/gpl.html.**** If you have questions regarding the use of this file, please contact** Nokia at qt-info@.** $QT_END_LICENSE$******************************************************************************/ #include <QtGui>#include <math.h>#include "button.h"#include "calculator.h"//! [0]Calculator::Calculator(QWidget *parent): QDialog(parent){sumInMemory = 0.0;sumSoFar = 0.0;factorSoFar = 0.0;waitingForOperand = true;waitingForE = false;//! [0]//! [1]display = new QLineEdit("0");//! [1] //! [2]display->setReadOnly(true);display->setAlignment(Qt::AlignRight);display->setMaxLength(15);QFont font = display->font();font.setPointSize(font.pointSize() + 8);display->setFont(font);//! [2]//! [4]for (int i = 0; i < NumDigitButtons; ++i) {digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));}Button *pointButton = createButton(tr("."), SLOT(pointClicked()));Button *changeSignButton = createButton(tr("\261"), SLOT(changeSignClicked()));Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked()));Button *clearButton = createButton(tr("Clear"), SLOT(clear()));Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory()));Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory()));Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory()));Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory()));Button *divisionButton = createButton(tr("\367"), SLOT(multiplicativeOperatorClicked()));Button *timesButton = createButton(tr("\327"), SLOT(multiplicativeOperatorClicked()));Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));Button *powerButton = createButton(tr("x\262"), SLOT(unaryOperatorClicked()));Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));Button *sinhButton = createButton(tr("sinh"), SLOT(unaryOperatorClicked()));Button *coshButton = createButton(tr("cosh"), SLOT(unaryOperatorClicked()));Button *tanhButton = createButton(tr("tanh"), SLOT(unaryOperatorClicked()));Button *sinButton = createButton(tr("sin"), SLOT(unaryOperatorClicked()));Button *cosButton = createButton(tr("cos"), SLOT(unaryOperatorClicked()));Button *tanButton = createButton(tr("tan"), SLOT(unaryOperatorClicked()));Button *mi2Button = createButton(tr("x^2"), SLOT(unaryOperatorClicked()));Button *miyButton = createButton(tr("x^y"), SLOT(multiplicativeOperatorClicked()));Button *mi3Button = createButton(tr("x^3"), SLOT(unaryOperatorClicked()));Button *factorButton = createButton(tr("n!"), SLOT(unaryOperatorClicked()));Button *genyButton = createButton(tr("x^1/y"), SLOT(multiplicativeOperatorClicked()));Button *gen3Button = createButton(tr("x^1/3"), SLOT(unaryOperatorClicked()));Button *expButton = createButton(tr("Exp"), SLOT(expOperatorClicked()));Button *modButton = createButton(tr("Mod"), SLOT(multiplicativeOperatorClicked()));Button *logButton = createButton(tr("log"), SLOT(unaryOperatorClicked()));Button *mi10Button = createButton(tr("10^x"), SLOT(unaryOperatorClicked()));Button *equalButton = createButton(tr("="), SLOT(equalClicked()));//! [4]//! [5]QGridLayout *mainLayout = new QGridLayout;//! [5] //! [6]mainLayout->setSizeConstraint(QLayout::SetFixedSize);mainLayout->addWidget(display, 0, 0, 1, 10);mainLayout->addWidget(backspaceButton, 1, 0, 1, 4);mainLayout->addWidget(clearButton, 1, 4, 1, 3);mainLayout->addWidget(clearAllButton, 1, 7, 1, 3);mainLayout->addWidget(clearMemoryButton, 2, 0);mainLayout->addWidget(readMemoryButton, 3, 0);mainLayout->addWidget(setMemoryButton, 4, 0);mainLayout->addWidget(addToMemoryButton, 5, 0);for (int i = 1; i < NumDigitButtons; ++i) {int row = ((9 - i) / 3) + 2;int column = ((i - 1) % 3) + 1;mainLayout->addWidget(digitButtons[i], row, column);}mainLayout->addWidget(digitButtons[0], 5, 1);mainLayout->addWidget(pointButton, 5, 2);mainLayout->addWidget(changeSignButton, 5, 3);mainLayout->addWidget(divisionButton, 2, 4);mainLayout->addWidget(timesButton, 3, 4);mainLayout->addWidget(minusButton, 4, 4);mainLayout->addWidget(plusButton, 5, 4);mainLayout->addWidget(squareRootButton, 2, 5);mainLayout->addWidget(powerButton, 3, 5);mainLayout->addWidget(reciprocalButton, 4, 5);mainLayout->addWidget(equalButton, 5, 5);mainLayout->addWidget(sinhButton, 2, 6);mainLayout->addWidget(coshButton, 3, 6);mainLayout->addWidget(tanhButton, 4, 6);mainLayout->addWidget(expButton, 5, 6);mainLayout->addWidget(sinButton, 2, 7);mainLayout->addWidget(cosButton, 3, 7);mainLayout->addWidget(tanButton, 4, 7);mainLayout->addWidget(modButton, 5, 7);mainLayout->addWidget(mi2Button, 2, 8);mainLayout->addWidget(miyButton, 3, 8);mainLayout->addWidget(mi3Button, 4, 8);mainLayout->addWidget(logButton, 5, 8);mainLayout->addWidget(factorButton, 2, 9);mainLayout->addWidget(genyButton, 3, 9);mainLayout->addWidget(gen3Button, 4, 9);mainLayout->addWidget(mi10Button, 5, 9);setLayout(mainLayout);setWindowTitle(tr("Calculator"));}//! [6]//! [7]void Calculator::digitClicked(){Button *clickedButton = qobject_cast<Button *>(sender());int digitValue = clickedButton->text().toInt();if (display->text() == "0" && digitValue == 0.0)return;if (waitingForOperand) {display->clear();waitingForOperand = false;}display->setText(display->text() + QString::number(digitValue)); }//! [7]int factor(int n){int result = 1;while(n!=1){result=result*n;n--;}return result;}//! [8]void Calculator::unaryOperatorClicked()//! [8] //! [9]{Button *clickedButton = qobject_cast<Button *>(sender());QString clickedOperator = clickedButton->text();double operand = display->text().toDouble();double result = 0.0;if (clickedOperator == tr("Sqrt")) {if (operand < 0.0) {abortOperation();return;}result = sqrt(operand);} else if (clickedOperator == tr("x\262")) {result = pow(operand, 2.0);} else if (clickedOperator == tr("1/x")) {if (operand == 0.0) {abortOperation();return;}result = 1.0 / operand;} else if (clickedOperator == tr("sin")) {result = sin(operand);} else if (clickedOperator == tr("cos")) {result = cos(operand);} else if (clickedOperator == tr("tan")) {result = tan(operand);} else if (clickedOperator == tr("sinh")) { result = sinh(operand);} else if (clickedOperator == tr("cosh")) { result = cosh(operand);} else if (clickedOperator == tr("tanh")) { result = tanh(operand);} else if (clickedOperator == tr("x^2")) { result = operand * operand;} else if (clickedOperator == tr("x^3")) { result = operand * operand * operand;} else if (clickedOperator == tr("n!")) {if (operand < 0.0){abortOperation();return;}result = factor(operand);} else if (clickedOperator == tr("log")) {if(operand <= 0.0) {abortOperation();return;}result = log(operand)/log(10);} else if (clickedOperator == tr("10^x")) { result = pow(10, operand);} else if (clickedOperator == tr("x^1/3")) { result = pow(operand, 1.0/3.0);}display->setText(QString::number(result));waitingForOperand = true;waitingForE = false;}//! [9]//! [10]void Calculator::additiveOperatorClicked()//! [10] //! [11]{Button *clickedButton = qobject_cast<Button *>(sender());QString clickedOperator = clickedButton->text();double operand = display->text().toDouble();//! [11] //! [12]if (!pendingMultiplicativeOperator.isEmpty()) {//! [12] //! [13]if (!calculate(operand, pendingMultiplicativeOperator)) { abortOperation();return;}display->setText(QString::number(factorSoFar));operand = factorSoFar;factorSoFar = 0.0;pendingMultiplicativeOperator.clear();}//! [13] //! [14]if (!pendingAdditiveOperator.isEmpty()) {//! [14] //! [15]if (!calculate(operand, pendingAdditiveOperator)) {abortOperation();return;}display->setText(QString::number(sumSoFar));} else {sumSoFar = operand;}//! [15] //! [16]pendingAdditiveOperator = clickedOperator;//! [16] //! [17]waitingForOperand = true;waitingForE = false;}//! [17]//! [18]void Calculator::multiplicativeOperatorClicked(){Button *clickedButton = qobject_cast<Button *>(sender());QString clickedOperator = clickedButton->text();double operand = display->text().toDouble();if (!pendingMultiplicativeOperator.isEmpty()) {if (!calculate(operand, pendingMultiplicativeOperator)) { abortOperation();return;}display->setText(QString::number(factorSoFar));} else {factorSoFar = operand;}pendingMultiplicativeOperator = clickedOperator;waitingForOperand = true;waitingForE = false;}//! [18]void Calculator::expOperatorClicked(){Button *clickedButton = qobject_cast<Button *>(sender());QString clickedOperator = clickedButton->text();double operand = display->text().toDouble();if(!waitingForE){beforeE = operand;display->setText(QString::number(beforeE)+tr("e+0"));waitingForE = true;}}//! [20]void Calculator::equalClicked(){double operand = display->text().toDouble();if (!pendingMultiplicativeOperator.isEmpty()) {if (!calculate(operand, pendingMultiplicativeOperator)) { abortOperation();return;}operand = factorSoFar;factorSoFar = 0.0;pendingMultiplicativeOperator.clear();}if (!pendingAdditiveOperator.isEmpty()) {if (!calculate(operand, pendingAdditiveOperator)) { abortOperation();return;}pendingAdditiveOperator.clear();} else {sumSoFar = operand;}display->setText(QString::number(sumSoFar));sumSoFar = 0.0;waitingForOperand = true;}//! [20]//! [22]void Calculator::pointClicked(){if (waitingForOperand)display->setText("0");if (!display->text().contains("."))display->setText(display->text() + tr("."));waitingForOperand = false;}//! [22]//! [24]void Calculator::changeSignClicked(){QString text = display->text();double value = text.toDouble();if (value > 0.0) {text.prepend(tr("-"));} else if (value < 0.0) {text.remove(0, 1);}display->setText(text);}//! [24]//! [26]void Calculator::backspaceClicked(){if (waitingForOperand)return;QString text = display->text();text.chop(1);if (text.isEmpty()) {text = "0";waitingForOperand = true;}display->setText(text);}//! [26]//! [28]void Calculator::clear(){if (waitingForOperand)return;display->setText("0");waitingForOperand = true;}//! [28]//! [30]void Calculator::clearAll(){sumSoFar = 0.0;factorSoFar = 0.0;pendingAdditiveOperator.clear();pendingMultiplicativeOperator.clear();display->setText("0");waitingForOperand = true;}//! [30]//! [32]void Calculator::clearMemory(){sumInMemory = 0.0;}void Calculator::readMemory(){display->setText(QString::number(sumInMemory));waitingForOperand = true;}void Calculator::setMemory(){equalClicked();sumInMemory = display->text().toDouble();}void Calculator::addToMemory(){equalClicked();sumInMemory += display->text().toDouble();}//! [32]//! [34]Button *Calculator::createButton(const QString &text, const char *member){Button *button = new Button(text);connect(button, SIGNAL(clicked()), this, member);return button;}//! [34]//! [36]void Calculator::abortOperation(){clearAll();display->setText(tr("####"));}//! [36]//! [38]bool Calculator::calculate(double rightOperand, const QString &pendingOperator) {if (pendingOperator == tr("+")) {sumSoFar += rightOperand;} else if (pendingOperator == tr("-")) {sumSoFar -= rightOperand;} else if (pendingOperator == tr("\327")) {factorSoFar *= rightOperand;} else if (pendingOperator == tr("\367")) {if (rightOperand == 0.0)return false;factorSoFar /= rightOperand;} else if (pendingOperator == tr("x^y")) {factorSoFar = pow(factorSoFar, rightOperand);} else if (pendingOperator == tr("x^1/y")) {if (rightOperand == 0.0)return false;factorSoFar = pow(factorSoFar, 1.0/rightOperand);} else if (pendingOperator == tr("Mod")) {if (rightOperand == 0.0)return false;factorSoFar = (int)factorSoFar % (int)rightOperand;}return true;}//! [38]。