简单计算器可视化程序编程_第1页
简单计算器可视化程序编程_第2页
简单计算器可视化程序编程_第3页
简单计算器可视化程序编程_第4页
简单计算器可视化程序编程_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

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

文档简介

1、简单计算器、基本功能描述二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行以角度、弧度两种方式实现上述部分函数具备历史计算的记忆功能对不正确的表达式能指出其错误原因流程图输入数据子函数图1输入数据子函数流程图三、设计步骤打开MicrosoftVisualC+6.0,在文件中点击新建,在弹出框内选择MFCAppWizardexe工程,输入工程名Calculter及其所在位置,点击确定,如图3所示。新建团区文件工程工作区其它文档ATLCOMApWiaardClusterRcsourcf?Typ

2、eWizardCustomAppWiz9rd蹄口加baseProjectDevSludioAdd-iiWizard国ExtendedStoredProcWizard,fjlSAPIExtensionWizardMakefile编MFCActiveXConlrolWizard囹MFCAppWizard(dll)MhCAppWiz3rdlexeijNcwDatabaseWizardT|UtilityProject3Win32Application1Win32CunaolcApplicationyWin32Dynamic-LinkLibrayWin32StaticLibrary工程名称回:|Calc

3、ulator色置理|DiVCCalculator|行创建新的工作空间但)摩加到当前工作空间回r从属干Di:II3平台旧:Win32图3新建MFCAppWizardexe工程将弹出MFCAppWizard-step1对话框,选择基本对话框,点击完成,如图4所示。图4基本对话框这样,MFCAppWizard就建立了一个基于对话窗口的程序框架,如图5所示。图5新建的对话框四、界面设计1、创建控件在图5所示的ResoureView选项卡中打开Dialog资源组,双击IDD_CALCULATOR_DIALOG在右边的窗口中显示出待编辑的对话框。开始摆放控件,包括编辑框和按钮的创建。按钮的创建以“1为例进

4、行介绍,其他按钮的创建可参照此进行操作。1)在图5中Controls的编辑框”按钮上单击鼠标左键,在对话框编辑窗口上合适的位置按下鼠标左键并拖动鼠标画出一个大小合适的编辑框。在编辑框上单击鼠标右键,在弹出的快捷莱单中选择属性选项,此时弹出Edit属性对话框,如图6所示,在该对话框中输入ID属性。图6Edit属性对话框2)在图3中Controls的"Button按钮上单击鼠标左键,在对话框上的合适的位置上按下鼠标左键并拖动鼠标画出一个大小合适的下压式按钮。在按钮上单击鼠标右键,在弹出的快捷菜单中选择属性选项,此时也弹出PushButton属性对话框,如图7所示,在该对话框中输入控件的I

5、D值和标题属性。图7PushButton属性按照上面的操作过程编辑其他按钮和编辑框等对象的属性。表1各按钮和编辑框等对象的属性对象ID标题或说明编辑框IDC_DC_EDIT_INPUT结果显示按钮IDC_BUTTON11按钮IDC_BUTTON22按钮IDC_BUTTON313按钮IDC_BUTTON44按钮IDC_BUTTON55按钮IDC_BUTTON66按钮IDC_BUTTON77按钮IDC_BUTTON88按钮IDC_BUTTON919按钮IDC_BUTTON100按钮IDC_BUTTON11.按钮IDC_BUTTON121空格按钮IDC_BUTTON23-按钮IDC_BUTTON26

6、*按钮IDC_BUTTON27/按钮IDC_BUTTON38sqrt按钮IDC_BUTTON30%按钮IDC_BUTTON25A按钮IDC_BUTTON31sin按钮IDC_BUTTON32cos按钮IDC_BUTTON33tan按钮IDCBUTTON34cot按钮IDCBUTTON40sin1按钮IDCBUTTON41cos1按钮IDCBUTTON35log按钮IDCBUTTON36In按钮IDC_BUTTON14a按钮IDCBUTTON15b按钮IDCBUTTON16c按钮IDC_BUTTON17d按钮IDC_BUTTON18e按钮IDC_BUTTON19f按钮IDC_BUTTON13退格

7、按钮IDC_BUTTON37eA按钮IDC_BUTTON22清空按钮IDOK计算按钮IDC_RADIO_DEGREE角度按钮IDC_RADIO_RADIAN弧度按钮IDC_STATIC计算结果按钮IDC_BTN_HISTREXP历史表达式按钮IDCANCEL退出按钮IDC_BTN_CONVERT进制完成后界面如图8所小图8计算器界面五、计算器源程序代码/Calculation.cpp:implementationoftheCCalculationclass./#include"stdafx.h"#include"Calculator.h"#include

8、"Calculation/"#include"math.h"#ifdef_DEBUG#undefTHIS_FILEstaticcharTHIS_FILE=_FILE_;#definenewDEBUG_NEW#endif# defineFUNCNUM15/函数个数# defineCONSTNUM11/常数个数# definePI3.14159/Construction/Destruction/CCalculation:CCalculation()初始化函数和常数m_bDegree=0;m_nOutputFlag=0;opt0="eA"o

9、pt1="ch"opt2="sqr"opt3="sh"opt4="log"opt5="cos"opt6="sin"opt7="tan"opt8="ln"opt9="abs"opt10="at"opt11="as"opt12="ac"opt13="th"opt14="cot"opt20=w;opt21='/

10、9;opt22='*'opt23='%'opt24='+'opt25='-'m_strConName1="GG"m_strConName2="EF"m_strConName3="VM"m_strConName4="BC"m_strConName5="MG"m_strConName6="GR"m_strConName7="NA"m_strConValue1="9.80665"

11、m_strConValue2="(8.988*10A9)"m_strConValue3="0.02241383"m_strConValue4="(1.380662*10A(-23)”;m_strConValue5="8.3144"m_strConValue6="(6.6720*10A(-11)”;m_strConValue7="(6.022045*10A23)"CCalculation:CCalculation()boolCCalculation:IsDigital(CStringstr)whi

12、le(str.GetAt(0)='-')str.Delete(0);for(inti=0;i<FUNCNUM;i+)if(str.Find(opti)!=-1)return0;for(i=0;i<6;i+)if(str.Find(opt2i)!=-1)return0;return1;CStringCCalculation:NtoS(doubled)intdecimal,sign;char*buffer;buffer=_ecvt(d,16,&decimal,&sign);CStringstr=buffer;if(decimal>=0&&a

13、mp;decimal<=16)str.Insert(decimal,".");elseif(decimal>16)for(inti=str.GetLength();i<decimal;i+)str+="0"str+=".0"elsefor(inti=0;i<-decimal;i+)str.Insert(0,"0");str.Insert(0,".");if(sign=1)str.Insert(0,"-");returnstr;doubleCCalcul

14、ation:StoN(CStringstr)char*stopstring;doublex;x=strtod(str.GetBuffer(0),&stopstring);m_strTmp=stopstring;returnx;CStringCCalculation:TwoE(CStringstrExp)doublex2,y,x1=StoN(strExp);strExp=m_strTmp;charop=strExp.GetAt(0);strExp.Delete(0);x2=StoN(strExp);if(m_strTmp!="")return"ERROR_&

15、quot;+m_strTmp+"未知符号if(op='+')y=x1+x2;elseif(op='*')y=x1*x2;elseif(op='-')y=x1-x2;elseif(op=w)y=pow(x1,x2);elseif(op='/')y=x1/x2;elseif(op='%')y=fmod(x1,x2);elsereturn"ERROR_"+m_strTmp+"未知运算符_"returnNtoS(y);CStringCCalculation:SingleE(

16、CStringop,doubledx)if(op="ln")returnNtoS(log(dx);elseif(op="log")returnNtoS(log10(dx);elseif(op="sqr")returnNtoS(sqrt(dx);elseif(op="eA")returnNtoS(exp(dx);elseif(op="abs")returnNtoS(fabs(dx);elseif(op="ac")returnNtoS(acos(dx);elseif(op=&qu

17、ot;as")returnNtoS(asin(dx);elseif(op="at")returnNtoS(atan(dx);if(m_bDegree)dx=dx*PI/180;if(op="tan")returnNtoS(tan(dx);elseif(op="sin")returnNtoS(sin(dx);elseif(op="cos")returnNtoS(cos(dx);elseif(op="cot")returnNtoS(1/tan(dx);elseif(op="sh&

18、quot;)returnNtoS(sinh(dx);else if(op="ch")returnNtoS(cosh(dx);elseif(op="th")returnNtoS(sinh(dx)/cosh(dx);elseif(op="exp")returnNtoS(pow(10,dx);return"ERROR"+op+"_未知函数_"intCCalculation:LocateLBra(CStringstrExp)intlen=strExp.GetLength();intpos=-1;for(

19、inti=0;i<len;i+)if(pos>=0&&strExp.GetAt(i)=')')break;if(strExp.GetAt(i)='(')pos=i;returnpos;voidCCalculation:DelBracket(CString*strExp)intpos=LocateLBra(*strExp);inti,len=strExp->GetLength();if(pos=-1)*strExp="ERROR_无效表达式_"return;for(i=pos;i<len;i+)if(st

20、rExp->GetAt(i)=')')break;CStringstr;str=strExp->Mid(pos+1,i-pos-1);MultiE(&str);strExp->Delete(pos,i-pos+1);strExp->Insert(pos,str);voidCCalculation:MultiE(CString*strExp)if(strExp->IsEmpty()*strExp="ERROR_函数表达式为空_"if(IsDigital(*strExp)return;while(1)/*处理所有的一元运算*

21、/for(inti=0;i<FUNCNUM;i+)intpos=strExp->Find(opti);if(pos!=-1)CStringstr=strExp->Right(strExp->GetLength()-pos-opti.GetLength();doubledx=StoN(str);if(m_strTmp=str)*strExp="ERROR"+str+"_无法识别的函数_"return;strExp->Delete(pos,strExp->GetLength()-pos);*strExp+=SingleE(

22、opti,dx)+m_strTmp;MinusMinus(strExp);if(pos>=1)charch=strExp->GetAt(pos-1);if(ch>=48&&ch<=57)*strExp="ERROR_缺少二元运算符_"return;break;if(i=FUNCNUM)break;/按运算符优先级处理二元运算intpos=-1;while(1)pos=strExp->Find('A');if(pos=-1)break;elseCalcu(strExp,pos);while(1)pos=strExp

23、->Find('/');if(pos=-1)break;elseCalcu(strExp,pos);while(1)pos=strExp->Find('*');if(pos=-1)break;elseCalcu(strExp,pos);while(1)pos=strExp->Find('%');if(pos=-1)break;elseCalcu(strExp,pos);pos=0;if(strExp->GetAt(0)='-'|strExp->GetAt(0)='+')strExp-&

24、gt;Insert(0,"0");while(1)inttmpos=strExp->Right(strExp->GetLength()-pos).Find('-');if(tmpos=-1)break;elsepos+=tmpos;if(pos=0&&strExp->GetAt(pos+1)='-'|pos>0)if(strExp->GetAt(pos+1)='+')strExp->Delete(pos+1);pos=0;continue;/*处理连减(如:1)的情况*/in

25、tnCount=0;while(1)if(+pos>0)if(strExp->GetAt(pos)!='-')break;elsenCount+;elsebreak;if(nCount>0)strExp->Delete(pos-nCount-1,nCount+1);if(nCount%2=0)strExp->Insert(pos-nCount-1,"-");elseif(pos-nCount-1>0)strExp->Insert(pos-nCount-1,"+");pos=0;continue;e

26、lsepos-;/*/if(pos>0&&strExp->GetAt(pos-1)='+')pos+;continue;Calcu(strExp,pos);elsepos+;pos=0;while(1)pos=strExp->Find('+');/inttmpos=strExp->Right(strExp->GetLength()-pos).Find('+');if(pos=-1)break;/elsepos+=tmpos;if(pos=0&&strExp->GetAt(pos+

27、1)='+'|pos>0)/*处理连加(如:+1)的情况*/intnCount=0;while(1)if(+pos>0)if(strExp->GetAt(pos)!='+')break;elsenCount+;elsebreak;if(nCount>0)strExp->Delete(pos-nCount-1,nCount+1);strExp->Insert(pos-nCount-1,"+");pos=0;continue;elsepos-;/*/Calcu(strExp,pos);elsepos+;void

28、CCalculation:Calcu(CString*strExp,intpos)charch;for(intj=pos-1;j>=0;j-)ch=strExp->GetAt(j);if(ch='+'|ch='-'|ch='*'|ch='/'|ch='%'11ch=w)if(j=0&&ch!='-')*strExp="ERROR_缺少参数_"return;if(j=0&&ch='-')j=-1;/防止把负号误判为减号el

29、seif(j>0&&ch='-'&&!IsDigital(strExp->Mid(j-1,1)j-;break;for(intk=pos+1;k<strExp->GetLength();k+)ch=strExp->GetAt(k);if(ch='+'|ch='-'|ch='*'|ch='/'|ch='%'11ch=w)if(ch='-'&&k>pos+1)break;if(ch='-'

30、&&k>0&&IsDigital(strExp->Mid(k+1,1)k+;elsebreak;CStringstrExp2=strExp->Mid(j+1,k-j-1);*strExp=strExp->Left(j+1)+TwoE(strExp2)+strExp->Right(strExp->GetLength()-k);if(strExp->Find("#IN")!=-1)*strExp="ERROR_结果有溢出或值域越界_"return;if(!SynRes(strExp)*

31、strExp="ERROR_缺少运算符_"return;MinusMinus(strExp);CStringCCalculation:MainPro(CStringstrExp)if(strExp.IsEmpty()return"表达式不能为空"Macro(&strExp);*/strExp.MakeLower();/表达式全部小写/*给表达式加上保护括号strExp.Insert(0,"(");strExp+=")"/*/intpos=strExp.Find("");intn=BraC

32、heck(strExp);CStringstr;str.Format("%d",abs(n);if(n=1)strExp+=")"elseif(n=-1)strExp.Insert(0,"(");elseif(n>0)return"缺少"+str+"个右括号"elseif(n<0)return"缺少"+str+"个左括号"while(pos!=-1)/去掉表达式中的空格符strExp.Delete(pos);pos=strExp.Find(&q

33、uot;");Oct2Dec(&strExp);/将表达式中的八进制数转换成十进制Hex2Dec(&strExp);/将表达式中的十六进制数转换成十进制Bin2Dec(&strExp);/将表达式中的二进制数转换成十进制while(!IsDigital(strExp)DelBracket(&strExp);if(!SynRes(&strExp)returnstrExp;if(!SynRes(&strExp)returnstrExp;elsereturnModiResult(strExp);voidCCalculation:Macro(C

34、String*strExp)intpos;for(inti=0;i<CONSTNUM;i+)pos=strExp->Find(m_strConNamei);while(pos!=-1)strExp->Delete(pos,m_strConNamei.GetLength();strExp->Insert(pos,m_strConValuei);if(pos>=1)charch=strExp->GetAt(pos-1);if(ch>=48&&ch<=57|ch=41)*strExp="缺少二元运算符"return;

35、pos=strExp->Find(m_strConNamei);CStringCCalculation:ModiResult(CStringstrRes)if(strRes.Find("#IN")!=-1)return"结果有溢出或值域越界"/*去掉保护括号*/if(strRes.GetAt(0)='(')strRes.Delete(0);if(strRes.GetAt(strRes.GetLength()-1)=')')strRes.Delete(strRes.GetLength()-1);/*/intpos=st

36、rRes.Find(".");CStringstr=""if(pos!=-1)if(pos=0)strRes="0"+strRes;elseif(strRes.GetAt(0)='-'&&strRes.GetAt(1)='.')strRes.Insert(1,"0");if(pos>16)strRes.Delete(pos);strRes.Insert(1,".");str.Format("%d",pos-1);str=&

37、quot;E"+str;pos=strRes.Find(".");if(pos=0|pos=1&&strRes.GetAt(0)='0')for(inti=pos+1;i<strRes.GetLength();i+)if(strRes.GetAt(i)!='0')break;if(i>4)str.Format("E-%d",i-2);strRes.Delete(pos,i-1);strRes.Insert(1,".");strRes=strRes.Left(pos+

38、16)+str;/截取小数点后16位returnstrRes;boolCCalculation:SynRes(CString*strExp)intpos=strExp->Find("ERROR");if(pos!=-1)pos=strExp->ReverseFind('_');strExp->Delete(pos,strExp->GetLength()-pos);pos=strExp->ReverseFind('_');strExp->Delete(0,pos+1);return0;return1;void

39、CCalculation:MinusMinus(CString*strExp)intpos=strExp->Find("-");if(pos!=-1)strExp->Delete(pos,2);strExp->Insert(pos,"+");if(strExp->GetAt(0)='+')strExp->Delete(0);intCCalculation:BraCheck(CStringstr)intlb=0,rb=0,len=str.GetLength();for(inti=0;i<len;i+)if

40、(str.GetAt(i)='(')lb+;elseif(str.GetAt(i)=')')rb+;returnlb-rb;voidCCalculation:Oct2Dec(CString*strExp)intlen,i,index,pos=strExp->Find("xo");CStringstrTmp,strDF;charch;doubledx;while(pos!=-1)dx=0;strTmp=""strDF=""strExp->Delete(pos,2);for(i=pos-1;i&

41、gt;=0;i-)ch=strExp->GetAt(i);if(ch=56|ch=57|ch>=97&&ch<=102)*strExp="ERROR_八进制数越界_"return;if(ch>=48&&ch<=55|ch=46)strTmp.Insert(0,strExp->Mid(i,1);strExp->Delete(i);elsebreak;if(i=pos-1)*strExp="ERROR_缺少二元运算符_"return;index=i;pos=strTmp.Find(&q

42、uot;.");if(pos!=-1)strDF=strTmp.Right(strTmp.GetLength()-pos-1);strTmp.Delete(pos,strTmp.GetLength()-pos);strTmp.MakeReverse();len=strTmp.GetLength();for(i=0;i<len;i+)ch=strTmp.GetAt(i);dx+=(ch-48)*pow(8,i);len=strDF.GetLength();for(i=0;i<len;i+)ch=strDF.GetAt(i);dx+=(ch-48)*pow(8,-i-1);s

43、trTmp=NtoS(dx);strExp->Insert(index+1,strTmp);pos=strExp->Find("xo");voidCCalculation:Hex2Dec(CString*strExp)intlen,i,index,pos=strExp->Find("xh");CStringstrTmp,strDF;charch;doubledx;while(pos!=-1)dx=0;strTmp=""strDF=""strExp->Delete(pos,2);for(i=p

44、os-1;i>=0;i-)ch=strExp->GetAt(i);if(ch>=48&&ch<=57|ch>=97&&ch<=102|ch=46)strTmp.Insert(0,strExp->Mid(i,1);strExp->Delete(i);elsebreak;if(i=pos-1)*strExp="ERROR_缺少二元运算符_"return;index=i;pos=0;for(i=0;i<strTmp.GetLength();i+)if(strTmp.GetAt(i)='.

45、')pos+;if(pos>1)*strExp="ERROR_缺少二元运算符_"return;pos=strTmp.Find(".");if(pos!=-1)strDF=strTmp.Right(strTmp.GetLength()-pos-1);strTmp.Delete(pos,strTmp.GetLength()-pos);strTmp.MakeReverse();len=strTmp.GetLength();for(i=0;i<len;i+)ch=strTmp.GetAt(i);if(ch>=48&&ch

46、<=57)/该数在09之间dx+=(ch-48)*pow(16,i);elseif(ch>=97&&ch<=102)/该数在af之间dx+=(ch-87)*pow(16,i);len=strDF.GetLength();for(i=0;i<len;i+)ch=strDF.GetAt(i);if(ch>=48&&ch<=57)/该数在09之间dx+=(ch-48)*pow(16,-i-1);elseif(ch>=97&&ch<=102)/该数在af之间dx+=(ch-87)*pow(16,-i-1);

47、strTmp=NtoS(dx);strExp->Insert(index+1,strTmp);pos=strExp->Find("xh");voidCCalculation:Bin2Dec(CString*strExp)intlen,i,index,pos=strExp->Find("xb");CStringstrTmp,strDF;charch;doubledx;while(pos!=-1)dx=0;strTmp=""strDF=""strExp->Delete(pos,2);for(i=

48、pos-1;i>=0;i-)ch=strExp->GetAt(i);if(ch>=50&&ch<=57|ch>=97&&ch<=102)*strExp="ERROR_二进制数越界_"return;if(ch='0'|ch='1'|ch=46)strTmp.Insert(0,strExp->Mid(i,1);strExp->Delete(i);elsebreak;if(i=pos-1)*strExp="ERROR_缺少二元运算符_"return;

49、index=i;pos=strTmp.Find(".");if(pos!=-1)strDF=strTmp.Right(strTmp.GetLength()-pos-1);strTmp.Delete(pos,strTmp.GetLength()-pos);strTmp.MakeReverse();len=strTmp.GetLength();for(i=0;i<len;i+)ch=strTmp.GetAt(i);dx+=(ch-48)*pow(2,i);len=strDF.GetLength();for(i=0;i<len;i+)ch=strDF.GetAt(i)

50、;dx+=(ch-48)*pow(2,-i-1);strTmp=NtoS(dx);strExp->Insert(index+1,strTmp);pos=strExp->Find("xb");voidCCalculation:Dec2Hex(CString*strExp/*strExp须为数字*/)boolbMinus=0;if(strExp->GetAt(0)='-')bMinus=1;strExp->Delete(0);intpos=strExp->Find('.');CStringstr,strDec;int

51、nDecInt;doubledDec;if(pos!=-1)strDec=strExp->Left(pos);nDecInt=atoi(strDec.GetBuffer(0);strDec=strExp->Right(strExp->GetLength()-pos);elsenDecInt=atoi(strExp->GetBuffer(0);strExp->Empty();while(nDecInt!=0)intnTmp=nDecInt%16;if(nTmp=10)str="a"elseif(nTmp=11)str="b"e

52、lseif(nTmp=12)str="c"elseif(nTmp=13)str="d"elseif(nTmp=14)str="e"elseif(nTmp=15)str="f"elsestr.Format("%d",nTmp);nDecInt/=16;strExp->Insert(0,str);*strExp+="."if(pos!=-1)dDec=StoN(strDec);intnCount=0;while(dDec!=0)dDec*=16;intnTmp=dDec;if(nTmp=10)str="a"elseif(nTmp=11)str="b"elseif(nTmp=12)str="c"elseif(nTmp=13)str="d"elseif(nTmp=14)str="e"elseif(nTmp=15)str="f"elsestr.Format("%d",nTmp);*

温馨提示

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

评论

0/150

提交评论