简单计算器可视化程序编程.docx

上传人:b****6 文档编号:5578500 上传时间:2022-12-28 格式:DOCX 页数:34 大小:74.44KB
下载 相关 举报
简单计算器可视化程序编程.docx_第1页
第1页 / 共34页
简单计算器可视化程序编程.docx_第2页
第2页 / 共34页
简单计算器可视化程序编程.docx_第3页
第3页 / 共34页
简单计算器可视化程序编程.docx_第4页
第4页 / 共34页
简单计算器可视化程序编程.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

简单计算器可视化程序编程.docx

《简单计算器可视化程序编程.docx》由会员分享,可在线阅读,更多相关《简单计算器可视化程序编程.docx(34页珍藏版)》请在冰豆网上搜索。

简单计算器可视化程序编程.docx

简单计算器可视化程序编程

简单计算器

一、基本功能描述二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计算

科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行

以角度、弧度两种方式实现上述部分函数

具备历史计算的记忆功能

对不正确的表达式能指出其错误原因

二、流程图

图1输入数据子函数流程图

图2

三、设计步骤

打开MicrosoftVisualC++6.0,在文件中点击新建,在弹出框内选择MFCAppWizard[exe]工程,输入工程名Calculter及其所在位置,点击确定,如图3所示。

图3新建MFCAppWizard[exe]工程

将弹出MFCAppWizard-step1对话框,选择基本对话框,点击完成,如图4所示。

图4基本对话框

这样,MFCAppWizard就建立了一个基于对话窗口的程序框架,如图5所示。

图5新建的对话框

四、界面设计

1、创建控件

在图5所示的ResoureView选项卡中打开Dialog资源组,双击IDD_CALCULATOR_DIALOG,在右边的窗口中显示出待编辑的对话框。

开始摆放控件,包括编辑框和按钮的创建。

按钮的创建以“1”为例进行介绍,其他按钮的创建可参照此进行操作。

1)在图5中Controls的“编辑框”按钮上单击鼠标左键,在对话框编辑窗口上合适的位置按下鼠标左键并拖动鼠标画出一个大小合适的编辑框。

在编辑框上单击鼠标右键,在弹出的快捷莱单中选择属性选项,此时弹出Edit属性对话框,如图6所示,在该对话框中输入ID属性。

图6Edit属性对话框

2)在图3中Controls的“Button”按钮上单击鼠标左键,在对话框上的合适的位置上按下鼠标左键并拖动鼠标画出一个大小合适的下压式按钮。

在按钮上单击鼠标右键,在弹出的快捷菜单中选择属性选项,此时也弹出PushButton属性对话框,如图7所示,在该对话框中输入控件的ID值和标题属性。

图7PushButton属性

按照上面的操作过程编辑其他按钮和编辑框等对象的属性。

表1各按钮和编辑框等对象的属性

对象

ID

标题或说明

编辑框

IDC_DC_EDIT_INPUT

结果显示

按钮

IDC_BUTTON1

1

按钮

IDC_BUTTON2

2

按钮

IDC_BUTTON3

3

按钮

IDC_BUTTON4

4

按钮

IDC_BUTTON5

5

按钮

IDC_BUTTON6

6

按钮

IDC_BUTTON7

7

按钮

IDC_BUTTON8

8

按钮

IDC_BUTTON9

9

按钮

IDC_BUTTON10

0

按钮

IDC_BUTTON11

.

按钮

IDC_BUTTON12

空格

按钮

IDC_BUTTON23

-

按钮

IDC_BUTTON26

*

按钮

IDC_BUTTON27

/

按钮

IDC_BUTTON38

sqrt

按钮

IDC_BUTTON30

%

按钮

IDC_BUTTON25

^

按钮

IDC_BUTTON31

sin

按钮

IDC_BUTTON32

cos

按钮

IDC_BUTTON33

tan

按钮

IDC_BUTTON34

cot

按钮

IDC_BUTTON40

sin~1

按钮

IDC_BUTTON41

cos~1

按钮

IDC_BUTTON35

log

按钮

IDC_BUTTON36

ln

按钮

IDC_BUTTON14

a

按钮

IDC_BUTTON15

b

按钮

IDC_BUTTON16

c

按钮

IDC_BUTTON17

d

按钮

IDC_BUTTON18

e

按钮

IDC_BUTTON19

f

按钮

IDC_BUTTON13

退格

按钮

IDC_BUTTON37

e^

按钮

IDC_BUTTON22

清空

按钮

IDOK

计算

按钮

IDC_RADIO_DEGREE

角度

按钮

IDC_RADIO_RADIAN

弧度

按钮

IDC_STATIC

计算结果

按钮

IDC_BTN_HISTREXP

历史表达式

按钮

IDCANCEL

退出

按钮

IDC_BTN_CONVERT

进制

完成后界面如图8所示。

图8计算器界面

五、计算器源程序代码

//Calculation.cpp:

implementationoftheCCalculationclass.

//MadebyGamsnin

//////////////////////////////////////////////////////////////////////

#include"stdafx.h"

#include"Calculator.h"

#include"Calculation.h"

#include"math.h"

#ifdef_DEBUG

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#definenewDEBUG_NEW

#endif

#defineFUNCNUM15//函数个数

#defineCONSTNUM11//常数个数

#defineE

#definePI3.14159

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

CCalculation:

:

CCalculation()

{

//初始化函数和常数

m_bDegree=0;

m_nOutputFlag=0;

opt[0]="e^";

opt[1]="ch";

opt[2]="sqr";

opt[3]="sh";

opt[4]="log";

opt[5]="cos";

opt[6]="sin";

opt[7]="tan";

opt[8]="ln";

opt[9]="abs";

opt[10]="at";

opt[11]="as";

opt[12]="ac";

opt[13]="th";

opt[14]="cot";

opt2[0]='^';

opt2[1]='/';

opt2[2]='*';

opt2[3]='%';

opt2[4]='+';

opt2[5]='-';

m_strConValue[1]="9.80665";m_strConName[1]="GG";

m_strConValue[2]="(8.988*10^9)";m_strConName[2]="EF";

m_strConValue[3]="0.02241383";m_strConName[3]="VM";

m_strConValue[4]="(1.380662*10^(-23))";m_strConName[4]="BC";

m_strConValue[5]="8.3144";m_strConName[5]="MG";

m_strConValue[6]="(6.6720*10^(-11))";m_strConName[6]="GR";

m_strConValue[7]="(6.022045*10^23)";m_strConName[7]="NA";

}

CCalculation:

:

~CCalculation()

{

}

boolCCalculation:

:

IsDigital(CStringstr)

{

while(str.GetAt(0)=='-')str.Delete(0);

for(inti=0;i

=-1)return0;

for(i=0;i<6;i++)if(str.Find(opt2[i])!

=-1)return0;

return1;

}

CStringCCalculation:

:

NtoS(doubled)

{

intdecimal,sign;

char*buffer;

buffer=_ecvt(d,16,&decimal,&sign);

CStringstr=buffer;

if(decimal>=0&&decimal<=16)str.Insert(decimal,".");

elseif(decimal>16)

{

for(inti=str.GetLength();i

str+=".0";

}

else

{

for(inti=0;i<-decimal;i++)str.Insert(0,"0");

str.Insert(0,".");

}

if(sign==1)str.Insert(0,"-");

returnstr;

}

doubleCCalculation:

:

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_"+m_strTmp+"未知符号_";

if(op=='+'){y=x1+x2;}

elseif(op=='*'){y=x1*x2;}

elseif(op=='-'){y=x1-x2;}

elseif(op=='^'){y=pow(x1,x2);}

elseif(op=='/'){y=x1/x2;}

elseif(op=='%'){y=fmod(x1,x2);}

elsereturn"ERROR_"+m_strTmp+"未知运算符_";

returnNtoS(y);

}

CStringCCalculation:

:

SingleE(CStringop,doubledx)

{

if(op=="ln")

{

returnNtoS(log(dx));

}

elseif(op=="log")

{

returnNtoS(log10(dx));

}

elseif(op=="sqr")

{

returnNtoS(sqrt(dx));

}

elseif(op=="e^")

{

returnNtoS(exp(dx));

}

elseif(op=="abs")

{

returnNtoS(fabs(dx));

}

elseif(op=="ac")

{

returnNtoS(acos(dx));

}

elseif(op=="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")

{

returnNtoS(sinh(dx));

}

elseif(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(inti=0;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;iGetAt(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)/*处理所有的一元运算*/

{

for(inti=0;i

{

intpos=strExp->Find(opt[i]);

if(pos!

=-1)

{

CStringstr=strExp->Right(strExp->GetLength()-pos-opt[i].GetLength());

doubledx=StoN(str);

if(m_strTmp==str){*strExp="ERROR"+str+"_无法识别的函数_";return;}

strExp->Delete(pos,strExp->GetLength()-pos);

*strExp+=SingleE(opt[i],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('^');

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);

}

while

(1)

{

pos=strExp->Find('%');

if(pos==-1)break;

elseCalcu(strExp,pos);

}

pos=0;

if(strExp->GetAt(0)=='-'||strExp->GetAt(0)=='+')strExp->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)的情况***********/

intnCount=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;

}

elsepos--;

/***********************************************/

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+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++;

}

}

voidCCalculation:

:

Calcu(CString*strExp,intpos)

{

charch;

for(intj=pos-1;j>=0;j--)

{

ch=strExp->GetAt(j);

if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='^')

{

if(j==0&&ch!

='-'){*strExp="ERROR_缺少参数_";return;}

if(j==0&&ch=='-')j=-1;//防止把负号误判为减号

elseif(j>0&&ch=='-'&&!

IsDigital(strExp->Mid(j-1,1)))j--;

break;

}

}

for(intk=pos+1;kGetLength();k++)

{

ch=strExp->GetAt(k);

if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='^')

{

if(ch=='-'&&k>pos+1)break;

if(ch=='-'&&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)){*strExp="ERROR_缺少运算符_";return;}

MinusMinus(strExp);

}

CStringCCalculation:

:

MainPro(CStringstrExp)

{

if(strExp.IsEmpty())return"表达式不能为空";

Macro(&strExp);

strExp.MakeLower();//表达式全部小写

/**********给表达式加上保护括号************/

strExp.Insert(0,"(");

strExp+=")";

/******************************************/

intpos=strExp.Find("");

intn

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1