怎样编程简单的计算器程序?
这种运算比较麻烦,不过4种运算符号优先级相同应该简单写,我这里有个算法,能进行简单的四则运算,delphi的,供参考
Function Math_Evaluate(S0:string):Extended;
Function Evaluate(S0:String):Extended;Forward;
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:Integer;S0: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:Integer;S0: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:Integer;S0: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:Integer;S0: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;
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;
}