




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、简单计算器一、基本功能描二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计算 科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行以角度、弧度两种方式实现上述部分函数具备历史计算的记忆功能对不正确的表达式能指岀其错误原因二、流程图输入数据子函数图1输入数据子函数流程图三、设计步骤打开MicrosoftVisualC+6. 0,在文件中点击新建,在弹出框内选择MFCAppWizardoxe工程,输入工程名Calculter 及其所在位置,点击确定,如图 3 所示。文件 rg 工作区|其它文椅|ATLCOM AppWizard 品Cluster
2、 Resource Type Wizard 超 Custom AppWizard Database Project DevStudio Add-in Wizard 窗 Extended Stored Proc Wizard filSAPI Extension Wizard MakefileMFC ActiveX Controlwizard 豳 MFC AppWizsrd (dll)JS:New Database WizardUtility ProjectWin32 Application Win32 Console Application Win32 Dynamic-Link Library
3、Win32 Static LibraryCalculator位置D:VCCalculator工Q创建新的工作空闾(BJr忝加到当前工作空闾回厂从展于回:工程名称:平台回回 Win32I 图 3 新建 MFCAppWizardexeX程 将弹出MFCAppWizard-stcp 1对话框,选择基本对话框,点击完成,如图4所示。图4基本对话框 这样,MFCAppWizard就建立了一个基于对话窗口的程序框架,如图5所示。图5新建的对话框四、界面设计1、创建控件在图5所示的RosoureView选项卡中打开Dialog资源组,双击IDD CALCULATOR DIALOG,在右边的窗口中 显示出待编
4、辑的对话框。开始摆放控件,包括编辑框和按钮的创建。按钮的创建以“1”为例进行介绍,其 他按钮的创建可参照此进行操作。1)在图5中Controls的“编辑框”按钮上单击鼠标左键,在对话框编辑窗口上合适的位置按下鼠标左键 并拖动鼠标画出一个大小合适的编辑框。在编辑框上单击鼠标右键,在弹出的快捷莱单中选择属性选项,此时弹出Edit属性对话框,如图6所示,在该对话框中输入ID属性。图6Edit属性对话框2)在图3中Controls的“Button”按钮上单击鼠标左键,在对话框上的合适的位置上按下鼠标左键并拖 动鼠标画出一个大小合适的下压式按钮。在按钮上单击鼠标右键,在弹出的快捷菜单中选择属性选项,此
5、时也弹岀PushButton属性对话框,如图7所示,在该对话框中输入控件的ID值和标题属性。图 7PushButton 属性按照上面的操作过程编辑其他按钮和编辑框等对象的属性。表1各按钮和编辑框等对象的属性对象ID标题或说明编辑框IDC DC EDIT INPUT结果显示按钮IDC BUTTON11按钮IDC BUTT0N22按钮IDC BUTT0N33按钮IDC BUTT0N44按钮IDC BUTT0N55按钮IDC BUTT0N66按钮IDC BUTT0N77按钮IDC BUTT0N88按钮IDC BUTT0N99按钮IDC BUTT0N100按钮IDC BUTTON11按钮IDC BUT
6、T0N12空格按钮IDC BUTT0N23按钮IDC BUTT0N26*按钮IDC BUTT0N27/按钮IDC BUTT0N38sqrt按钮IDC BUTT0N30%按钮IDC BUTT0N25A按钮IDC BUTT0N31sin按钮IDC BUTT0N32cos按钮IDC BUTT0N33tan按钮IDC BUTT0N34cot按钮IDC BUTT0N40sin、l按钮IDC BUTT0N41cosl按钮IDC BUTT0N35log按钮IDC BUTT0N36In按钮IDC BUTT0N14a按钮IDC BUTT0N15b按钮IDC BUTT0N16c按钮IDC BUTT0N17d按钮I
7、DC BUTT0N18e按钮IDC BUTT0N19f按钮IDC BUTT0N13退格按钮IDC BUTT0N37e按钮IDC BUTTON22清空按钮ID0K计算按钮IDC RADIO DEGREE角度按钮IDC RADIO RADIAN弧度按钮IDC STATIC计算结果按钮IDC BTN HISTREXP历史表达式按钮IDCANCEL退出按钮IDC BTN CONVERT进制完成后界面如图8所示。图8计算器界面五、计算器源程序代码/Calculation cpp:implementationofthoCCalculationclass/MadobyGamsnin/I/I/I/#inclu
8、de,rstdafx. h#includoCalculator h#includoCalculation.h#include,rmath. httifdef DEBUG#undefTHIS. FILEstaticcharTHIS_FILE=_FILE_;#definenewDEBUG NEWftendiff i neFUNCNUM 15/ 函数个数f i neCONSTNUM 11 / 常数个数#defineE#definePI3. 14159/Construction/Dostruction/CCalculation::CCalculationO初始化函数和常数m bDegree=0;m n
9、()utputFlag=O;opt0=e*w;optl=ch;opt 2 = r,sqrw;opt3=sh;opt4=Tog;opt5=cos;opt6=sin;opt 7=wtanr,;opt 8=Tn;opt9=abs;opt10=at;optll=as;opt12=ac;opt13=nthw; opt14=McotM; opt20=,; opt2l=,/r; opt22=*; opt23=%; opt24=+; opt25 = ,-;ms t rConVa 1 uo 1 =,f9. 80665;m_st rConName 1 =H GG,rm_strConValuo2=,r (8. 98
10、8*10*9)H;m_strConName2 =nEF,rmstrConValuo3=,f0. 02241383,r;m_strConName3 = ,VM,rm_strConValuo4=,r(l. 380662*10 (-23) ; m_strConName4 = HBC,r m_strConValuo5=,r8. 3144,r;m_strConName5 =MGin_strConValue6=,r (6. 6720*10 (-11) ;m_strConName6 =HGR,rm_strConValuo7=,r(6. 022045*1023) ”;m_strConName7=wNA,rCC
11、alculation::CCalculation() boolCCalculation::IsDigital(CStringstr)while(str. GetAt (0)=,-f)str. Delete(0);for(inti=0;iFUNCNUM;i+)if(str. Find(opti)!=-l)return0;for(i=0;i=0&decimal16)for (inti=str. GetLengthO ; idocimal; i+)str+=0; str+=w.0M;elsefor(inti=0;i-decimal;i+) str. Insert(0,M0M);str. Insert
12、 (0,w);if (sign=l)str. Insert (0,;returnstr;)doubleCCalculation::StoN(CStringstr)char*stopstring;doublex;x=strtod(str GetBuffer(0),&stopstring);m strTmp=stopstring;returnx;CStringCCalculation::TwoE(CStringstrExp)doub1gx2,y f x1=StoN(strExp);strExp=m strTmp;charop=strExp GetAt(0);strExp. Delete(0);x2
13、=StoN(strExp);i f (m_strTmp!二)returnw ERR0R_R +m_s t rTmp+ 未知符号; if (op=, + ,) y=xl+x2;elseif(op=,*f)y=xl*x2;)elseif(op=,-1)y=xl-x2;elseif (op=, J y=pow(xltx2) ;elseif (op=V,) y=xl/x2;)elseif(op=,%1)y=fmod(xl,x2) ;elsoroturnERRORstrTmp+,r未知运算符_;returnNtoS(y);CStringCCalculation::Sing1eE(CS t r i ngo
14、p,doub1cdx)if (op=Tn)returnNtoS(log (dx);elseif (op=wlog,r)returnNtoS(loglO (dx);elseif (op=,sqr,r)returnNtoS(sqrt (dx);elseif (op=UreturnNtoS(exp(dx);elseif (op=,rabs,r)returnNtoS(fabs(dx);)elseif(op=wac)returnNtoS(acos(dx);elseif (op=r,as)returnNtoS(asin(dx);)elseif(op=at)returnNtoS(atan (dx);)if(
15、m bDcgree)dx=dx*PI/180;if (op=tarT)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)returnNtoS(sinh (dx);)elseif(op=ch)returnNtoS(cosh (dx);)elseif(op=th)returnNtoS(sinh(dx)/cosh(dx); ) elseif(op=exp)returnNtoS(p
16、ow(10,dx);returnERROR” +op+未知函数;intCCalculation::LocateLBra(CStringstrExp)in tie n二strExp GetLcngthO ;intpos=-l;for(inti=0;i=0&strExp. GetAt(i)=f) r)break;if (strExp. GetAt (i)=,()pos=i;)returnpos;voidCCalculation::DelBracket(CString*strExp)intpos=LocateLBra(*strExp);int i tlon=strExp-GetLength();if
17、 (pos=-l) *strExp=lfERROR 无效表达式return;for(i=pos;iGetAt(i)=r)r)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=l,ERROF函数表达式为空_;if (IsDigital(*strExp)return; jwhile(l)/*处理所有
18、的一元运算*/for(inti=0;iFind(opti);if(pos!=-l)CStringstr=strExp-Right(strExp-GetLcngth() -pos-opti GetLength(); doubledx=StoN(str);if (m strTmp=str) *strExp=wERROFw+str + 无法识别的函数 return; strExp-Dolete(post strExp-GetLongth() -pos); *strExp+=SingleE(optirdx)+m strTmp;MinusMinus(strExp);if(pos=l)charch=str
19、Exp-GetAt(pos-1);if(ch=48&chFind(r);if(pos=-l)break;elseCalcu(strExp,pos);whiled)pos=strExp-Find(/);if(pos=-l)break;elseCalcu(strExp,pos);whiled)pos=strExp-Find(*);if(pos=-l)break;elseCalcu(strExp,pos);whiled)pos=strExp-Find(r%r);if(pos=-l)break;elseCalcu(strExp,pos);pos=0;if(strExp-GetAt(0)=r-* |st
20、rExp-GetAt(0)=,+r)strExp-Insert(0,0); while(l)inttmpos=strExp-Right (strExp-GotLength()-pos) Find(*-);if(tmpos=-1)break;elsepos+=tmpos;if(pos=0&strExp-GotAt(pos+l)=,-f|pos0)if(strExp-GetAt(pos+l)=,+,)strExp-Dolete(pos+1);pos=0; continue;/*处理连减 (如:1) 的情况*/intnCount=0;while(l)if(+pos0)if(strExp-GetAt(
21、pos)! = r-r) break;elsenCount+;elsebreak;if(nCount0)strExp-Delete(pos-nCount-1,nCount+1);if(nCount%2=0)strExp-Insert(pos-nCount-1t);elseif(pos-nCount-l0)strExp-Insert (pos-nCount-1.+); pos=0;continue;elsepos;/*/if(pos0&strExp-GetAt(pos-l)= +)pos+;continue;Calcu(strExp,pos);)elsepos+;pos=0;while(l)po
22、s=strExp-Find(+);/inttmpos=strExp-Right (strExp-GetLength()-pos). Find(, + ,);if(pos=-l)break;/elsepos+=tmpos;if(pos=0&strExp-GotAt(pos+l)=,+f|pos0)/*处理连加 (咖J: +1) 的情况*/intnCount=0;while(l)if(+pos0)if(strExp-GetAt(pos)!=r+r)break; elsenCount+;elsebreak;if(nCount0)strExp-Delete(pos-nCount-1,nCount+1)
23、;strExp-Insert(pos-nCount-1.+); pos=0;continue;elsepos-;/*/Calcu(strExp,pos);)elsepos+;voidCCalculation::Calcu(CString*strExp,intpos)charch;for(intj=pos-l;j=0; j)ch=strExp-GetAt(j); if(ch=+ | |ch=*-r I |ch=* |ch=7 I |ch=% I |ch=) if (j=0&ch!二)*strExp=MERROR 缺少参数,r; return;if (j=0&ch=r-) j=-l; 防止把负号误
24、判为减号elseif (j0&ch=,-r&! IsDigital (strExp-Mid(j-1,1) j; break;)for(intk=pos+l;kGetLcngth();k+)ch=strExp-GotAt(k); if(ch=+ | |ch=*-r I |ch=* |ch=7 I |ch=% I |ch=) if(ch=1&kpos+1)break;if(ch=f-f&kO&IsDigital(strExp-Mid(k+l,l)k+;elsebreak;)CStringstrExp2=strExp-Mid(j+1,k-j-1);*strExp=strExp-Left(j+1)+T
25、woE(strExp2)+strExp-Right(strExp-GetLength()-k); if (strExpFindC#IN) !=T) *strExp=wERROR 结果有溢出或值域越界return;)if (!SynRes(strExp) *strExp=,rERROR 缺少运算符 11 ; return; MinusMinus(strExp);CStringCCalculation::MainPro(CStringstrExp)i f (strExp. IsEmpty () return表达式不能为空;Macro(&strExp);strExp. MakeLower () ;
26、/表达式全部小写/*给 表达式力口上保护括号*/strExp Insert(0,H();strExp+=H)M; */ intpos=strExp Find(HW);intn二BraCheck (strExp);CStringstr;str. Format (F abs (n);if(n=l)strExp+=)”;elseif(n=-l)strExp. Insert(0.();else i f (n0) return,f 缺少 w+str+f 个右括号;elseif (n0) return r 缺少 w+str+w 个左括号; while(pos!=-l)/去掉表达式中的空格符strExp.
27、Delete (pos); pos=strExp. Find(T);0ct2Dec UstrExp); 将表达式中的八进制数转换成十进制Hcx2Dec (ftstrExp) ; /将表达式中的十六进制数转换成十进制Bin2Dec (ftstrExp); 将表达式中的二进制数转换成十进制 while(!IsDigitai(strExp)DelBrackct(&strExp);if(!SynRes(&strExp)returnstrExp;if(!SynRes(&strExp)returnstrExp;elsereturnModiResult(strExp);voidCCalculation::M
28、acro(CString*strExp)intpos;for(inti=0;iFind (in strConNamoi);while (pos !=T)strExp-Delete(postm strConNamef i GctLongthO); strExp-Insert(pos.m strConValuei); if(pos=l)charch=strExp-GetAt(pos-1);if(ch=48&chFind (in strConNamoi);)CStringCCalculation::ModiResult(CStringstrRes)if (strRes. Find(#IN) !=-l
29、)return结果有溢出或值域越界;/* 去掉保 护括号 */if (strRes. Get At (0)=, (/) strRes. Delete (0);if (strRes. GetAt (strRes. GetLongth()-l)=r)r)strRes. Delete(strRes. GetLength()-l); */intpos=strRes Find(. ):CStringstr=WM;if(pos!=-l)if (pos=0) strRes=,OM+strRes;elseif (strRes. GetAt (0) =r -rtrRes. GetAt (l)=r. r) str
30、Res. Insert (lt 0);if(pos16)strRes Delete(pos);strRes. Insert (lf );str. Format (,r%dwf pos-1);str=HEH+str;pos=strRes Find(H);if (pos=01|pos=l&strRes. GetAt(0)=,0*)for(inti=pos+l; i4)str. Format (,rE-%d i-2);strRes. Dolete(post iT);strRes. Insert(l,);)strRes=strRes. Left(pos+16) +str;/截取小数点后 16 位ret
31、urnstrRes;boolCCalculation::SynRes(CString*strExp)i ntpos=strExp-F i nd (w ERROR,r);if (pos!=-l)pos=strExp-ReverseFind(r ); strExp-Dclete(pos,strExp-GotLength()-pos); pos=strExp-ReverseFind(r ); strExp-Dclete(O,pos+1);returnO;returnl;voidCCalculation::MinusMinus(CString*strExp)intpos=strExp-Find (7H
32、);if (pos!=-l)strExp-Dclete(pos,2); strExp-Insert(pos,M+H);if(strExp-GetAt(0)=r+,)strExp-Delete(0);intCCalculation:BraCheck(CStringstr)intlb=0, rb=0, len=str. GetLongthO ;for(inti=0;iDclete(pos,2); for (i=pos-l;i=0;i)ch=strExp-GetAt(i);if(ch=56|ch=57|ch=97&ch=48&chMid(it1); strExp-Dolete(i); elsebrc
33、ak;)if (i=pos-l) *strExp= ERROR 缺少二元运算符 w;return; indcx=i;pos=strTmp Find(”);if(pos!=-l)strDF=strTmp. Right(strTmp GetLongth()-pos-1); strTmp. Delete(post strTmp GetLength()-pos):)strTmp. MakoReverseO ;len=strTmp GetLengthO ;for(i=0;ilen;i+)ch二strTmp. GetAt (i); dx+=(ch-48)*pow (8,i);)len=strDF. Get
34、Lcngth ();for(i=0;iInsert(index+1r strTmp); pos=strExp-Find(xo);voidCCalculation:: llex2Dec(CString*strExp)intlen,i,indox,pos=strExp-Find(xh); CStringstrTmp,strDF;charch; doublcdx;while(pos!=-l)dx=0; strTmp=w,r;strDF=w,r; strExp-Delete(pos,2); for(i=pos-l;i=0;i-) ch=strExp-GetAt(i);if (ch=48&ch=97&c
35、hMid(it1); strExp-Dolete(i); elsebreak;)if (i=pos-l) *strExp=nERROR 缺少二元运算符 return; indcx=i;pos=0;for (i=0; KstrTmp. GetLengthO ; i+)if(strTmp. GetAt(i)=f. r)pos+;if (posl) *strExp=ERROR 缺少二元运算符 return; )pos=strTmp Find(”);if(pos!=-l)strDF=strTmp. Right(strTmp GetLongth()-pos-1); strTmp. Delete(post
36、 strTmp GetLength()-pos):)strTmp. MakoReverseO ;len=strTmp GetLengthO;for(i=0;i=48&ch=97&ch= 102)/该数在 af 之间dx+=(ch-87)*pow(16 Ti);)len=strDF. GetLcngth();for (i=0;i=48&ch=97&chInsert(index+1,strTmp); pos=strExp-Find(xh);voidCCalculation::Bin2Dec(CString*strExp) intlon.i9 indox,pos=strExp-Find(xb); C
37、StringstrTmp,strDF;charch; doublcdx;while(pos!=-l)dx=0; strTmp=w,r;strDF=w,r;strExp-Delete(pos,2);for(i=pos-l;i=0;i)ch=strExp-GetAt(i):if(ch=50&ch=97&chMid(i,1); strExp-Delete(i);clsebreak;)if (i=pos-l) *strExp=MERR0R 缺少二元运算符return;index=i;pos=strTmp Find();if(pos!=-l)strDF=strTmp Right(strTmp GctLo
38、ngth()-pos-1); strTmp. Delete(poststrTmp GotLength()-pos);)strTmp. MakeRoverseO ;lcn=strTmp. GctLengthO ;for(i=0;ilen;i+)ch二strTmp. GetAt (i);dx+=(ch-48)*pow (2,i);len=strDF. GetLcngthO ;for(i=O;iInsert(index+1,strTmp); pos=strExp-Find(xb);voidCCalculation::Dec2Hcx(CString*strExp/*strExp 须为数字*/) boo
39、lbMinus=0;if (strExp-GetAt(0)=r-*)bMinus=l;strExp-Dclete(O);intpos=strExp-Find(/);CStringstrf strDec;intnDecint;doublcdDec;if(pos!=-l)strDec=strExp-Left(pos); nDecInt=atoi(strDec. GetBuffer(0); strDec=strExp-Right(strExp-GotLength()-pos);elsenDecInt=atoi(strExp-GetBuffer(0);strExp-Empty();while (nDe
40、dnt !=0)intnTmp=nDecInt%16; if(nTmp=10)str=ffaw; elseif(nTmp=ll)str=b; elseif(nTmp=12)str=c; elseif(nTmp=13)str=d; elseif (nTmp=14) str=U elseif(nTmp=15)str=f; elsestr Format (dnTmp); nDocInt/=16; strExp-Insert(0,str);)*strExp+=w;if(pos!=-l)dDec=StoN (strDcc);intnCount=0;while(dDcc!=O)dDec*=16;intnT
41、mp=dDcc;if(nTmp=10)str=a,r;elseif (nTmp=ll) str=,r b;elseif (nTmp= 12)str=,rcH;elseif(nTmp=13);elseif (nTmp=14) str=reM;elseif (nTmp= 15) str=,rfw ; elsestr Format (H%d,r ,nTmp); *strExp+=str Left(pos); dDec-=nTmp; if(+nCount=17)break;)if(bMinus)strExp-Insert(0,-R);if (strExp-Find(-1M) !=-l&bMinus!=l)*strExp=w太大无法表示;voidCCalculation::Dec20ct(CString*strExp)boolbMinus=0;if(strExp-GetAt(0)=r-1)bMinus=l;strExp-Delete(0);intpos=strExp-Find(1);CStringstrf strDec;intnDecInt;doublcdDec;if(pos!=-l)strDec=strExp-Left(pos); nDecInt=atoi(strDe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 7.5 解直角三角形说课稿-2025-2026学年初中数学苏科版2012九年级下册-苏科版2012
- 人教版二年级上册数学全册集体备课教学设计(配2025年秋改版教材)
- 二年级品德下册 主题三“我们一起做”说课稿 首师大版
- 探究与实践 看云识天气教学设计-2025-2026学年初中地理湘教版2024七年级上册-湘教版2024
- 《第一单元 唱歌 大雨和小雨》(教学设计)-2023-2024学年人教版(2012)音乐一年级上册
- 农药的田间药效试验说课稿-2025-2026学年中职专业课-植物保护技术-农林类-农林牧渔大类
- 蒲城县医院知识培训课件
- 蒜苔农业知识培训课件
- 葡萄酒酒标基础知识培训
- 2025年林业时政试题及答案解析
- 中级注册安全工程师《法律法规》试题及答案
- 2025年汽车转向系统行业需求分析及创新策略研究报告
- 2025年四川省成都市高新区事业单位招聘考试综合类面试真题模拟试卷
- 2025年秋统编版语文二年级上册全册课件(课标版)
- 七下期末人教版数学试卷
- 2025新疆巴音郭楞州和硕县面向社会招聘社区工作者7人笔试参考题库附答案解析
- 2025年六安市裕安区石婆店镇公开招考村级后备干部8名笔试备考试题及答案解析
- 2025年事业单位考试题库及参考答案
- 2025全国农业(水产)行业职业技能大赛(水生物病害防治员)选拔赛试题库(含答案)
- 石油钻采设备与工具专业标准分类
- GB/T 39725-2020信息安全技术健康医疗数据安全指南
评论
0/150
提交评论