1、首先,打开Vs 2010,如图。
3、点击下一步,注意勾选空项目,点击下一步,点击完成。
4、点击左侧的源文件,右击选择“添加—>项目”,选择C++文件,命名为简单计算器,因为是C程序,注意后缀名要加上.c,点击确定完成文件新建工作。
5、输入以下代码,好了,一个简单的计算器便做好了
这种运算比较麻烦,不过4种运算符号优先级相同应该简单写,我这里有个算法,能进行简单的四则运算,delphi的,供参考
Function Math_Evaluate(S0:string):Extended
Function Evaluate(S0:String):ExtendedForward
Procedure CleanUp(var s0:string)
Var
I:integer
Begin
S0:=LowerCase(s0)
I:=Pos(' ',s0)
While I>0 Do
Begin
Delete(S0,I,1)
I:=Pos(' ',S0)
End
End
Function GetFirstOpp(Tot:IntegerS0:String):Integer
Const
Sopps:String=('+-*/^')
Var
I:Integer
Begin
If Tot=0 Then Tot:=Length(S0)
For I:=1 To 5 Do
Begin
Result:=Pos(Sopps[i],S0)
If ((I<3) And (Result>0)) Then
If ((Result=1) Or (Pos(S0[Result-1],Sopps)>0)) Then
Result:=0
If Result>0 Then
If Result<Tot Then
Exit
End
If Result>Tot Then
Result:=0
End
Function SpecialF(P1:IntegerS0:String):Extended
Var
Operstr:String
Arg:Extended
Begin
Result:=0
Operstr:=Copy(S0,1,P1-1)
If S0[Length(S0)]<>')' Then
Exit
Operstr:=LowerCase(Operstr)
Arg:=Evaluate(Copy(S0,P1+1,Length(S0)-P1-1))
if Operstr ='sin' Then
Result:=Sin(Arg)
Else if Operstr ='cos' Then
Result:=Cos(Arg)
Else if Operstr ='tan' Then
Result:=Sin(Arg)/Cos(Arg)
Else if Operstr ='arctan' Then
Result:=Arctan(Arg)
Else if Operstr ='log' Then
Result:=Ln(Arg)/Ln(10)
Else if Operstr ='ln' Then
Result:=Ln(Arg)
Else if Operstr ='exp' Then
Result:=Exp(Arg)
Else if Operstr ='sqrt' Then
Result:=Sqrt(Arg)
{enter additional functions here}
Else Exit
End
Function GetValue(S0:String):Extended
Begin
Result:=0
If Length(S0)<1 Then Exit
If Length(S0)=1 Then
Result:=StrToFloat(S0)
Else
Case s0[1] Of
'x':Result:=1
'y':Result:=1
'z':Result:=1
Else Result:=StrToFloat(S0)
End
End
Procedure MatchBracket(Var I:IntegerS0:String)
Var
J,Len:Integer
Begin
J:=1
Len:=Length(S0)
Repeat Inc(I)
If I>Len Then Exit
If S0[I]='(' Then Inc(J)
If S0[I]=')' Then Dec(J)
If J<0 Then Exit
Until J=0
End
Function Calculate(P1:IntegerS0:String):Extended
Var
V1,V2:Extended
Begin
Result:=0
V1:=Evaluate(Copy(S0,1,P1-1))
V2:=Evaluate(Copy(S0,P1+1,Length(s0)-P1))
Case S0[P1] Of
'+': Result:=V1+V2
'-': Result:=V1-V2
'/': Result:=V1/V2
'*': Result:=V1*V2
'^': Result:=Exp(V2*Ln(V1))
Else Exit
End
End
Function Evaluate(S0:string):Extended
Var
P1,P2,Q1:Integer
Begin
P1:=Pos('(',S0)
P2:=P1
If P2>0 Then
MatchBracket(P2,S0)
If P1=1 Then
Begin
If P2=Length(S0) Then
Begin
Delete(S0,P2,1)
Delete(S0,1,1)
Result:=Evaluate(S0)
End Else
Result:=Calculate(P2+1,S0)
Exit
End
Q1:=GetFirstOpp(P1,S0)
If (P1+Q1=0) Then
Begin
Result:=GetValue(S0)
Exit
End
If Q1<>0 Then
Result:=Calculate(Q1,S0)
Else If Length(S0)>P2 Then
Result:=Calculate(P2+1,S0)
Else
Result:=SpecialF(P1,S0)
End
Begin
Try
CleanUp(S0)
Result:=Evaluate(S0)
Except
Result:=0
End
End
5 8
评论
分享
举报收起
gongfang2012
推荐于2017-04-23
展开全部
#include<stdio.h>
int main(void)
{
int data1,data2
char op
printf("Please input data1 op data2:")
scanf("%d %c %d",&data1,&op,&data2)//注意输入时 *** 作数与运算符之间要用空格隔开。
switch(op)
{
case '+':
printf("\n%d+%d=%d",data1,data2,data1+data2)
break
case '-':
printf("\n%d-%d=%d",data1,data2,data1-data2)
break
case '*':
printf("\n%d*%d=%d",data1,data2,data1*data2)
case '/':
if (data2==0)
printf("Error! chu shu wei 0")
else
printf("\n%d/%d=%d",data1,data2,data1/data2)
break
case '%':
if (data2==0)
printf("Error! chu shu wei 0")
else
printf("\n%d%%%d=%d",data1,data2,data1%data2)
break
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)