怎样编程简单的计算器程序

怎样编程简单的计算器程序,第1张

这种运算比较麻烦,不过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:=V1V2;

'^': Result:=Exp(V2Ln(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;

#include <stdioh>

void main()

{

int a,b;

float f1,f2;

printf("Enter one int\n");

scanf("%d",&a);

printf("a=%d\n",a);

}

1、求1+2+3+………+100。(循环)

答案

#include<stdioh>

void main()

{

int i,sum=0;

for(i=1;i<=100;i++)

sum=sum+i;

printf("%d",sum);

}

2、 求123………10。(循环)

答案

void main()

{

int i=0,j=1;

for (i=2;i<=10;i++)

{

j=i;

}

printf("%d",j);

return 0;

}

3、 输入三个数字,输出他们的最大值。(if)

答案

#include<stdioh>

void main()

{int a,b,c,d;

scanf("%d,%d,%d",&a,&b,&c);

d=max(a,b,c);

printf("max=%d",d);

getch();/暂停看运行结果/

}

int max(int x,int y,int z)

{int u;

if(x>=y&&x>=z)

u=x;

else if(y>=x&&y>=z)

u=y;

else

u=z;

return(u);

4用起泡法对十个数据排序(数组实现)

答案

#include<stdioh>

main ( )

{  int i,j,t;

static int a[10]={5,7,4,2,3,6,1,0,9,8};

for(j=0;j<9;j++)

{   for(i=0;i<9-j;i++)

{  if(a>a)

{ t=a;a=a;a=t ;

}

}

}

for(i=0;i<10;i++)

printf("%2d",a);

}

5、输入十个数字,逆序输出。(数组实现)

答案

#include<stdioh>

main()

{int a[10],i=0;

for(i=0;i<=9;i++)

scanf("%f",&a);

printf("\n");

for(i=9;i>=0;i--)

printf("%f",a);

}

6输入两个数,交换他们的值并输出。(元素交换)

答案

#include<stdioh>

int main ()

{

int m,n,temp;

scanf("%d%d",&m,&n);

if (m<n)

{

temp=m;

m=n;

n=temp;

}

printf("%d",m);

return 0;

}

7输出99乘法表。(双层循环)

答案

#include <stdioh>

void main()

{

int i=1;

for(i; i<=9; i++)

{

int j=1;

for(j;j<=i;j++)

{

printf("%d%d=%d ", i, j, ij);

}

printf("\n");

}

}

8输入一行字符,将所有的小写字母转换成大写字母,大写字母转换成小写字母,其余字符不变。输出转变后的这行字符。

答案

#include "stdioh"

void main()

{

char a[n];

int i;

scanf("%s",a);

printf("大写为:");

for(i=0;i<=n;i++)

{

if(a<='z'&&a>='a')

a=a-32;

printf("%c",a);

}

printf("\n小写为:");

for(i=0;i<=3;i++)

{

a=a+32;

printf("%c",a);

}

}

9、 编写一个简单计算器程序,要求能够完成两个数的+,-,,/四种运算。输出运算式及运算结果。(switch)

62

#include"stdioh"

main()

{char c;int i=0,j=0,k=0,l=0;

while((c=getchar())!=’\n’)

{if(c>=65&&c<=90||c>=97&&c<=122) i++;

else if(c>=48&&c<=57) j++;

else if(c==32) k++;

else l++;}

printf("i=%d,j=%d,k=%d,l=%d\n",i,j,k,l);

}

66

#include"mathh"

main()

{int x=100,a,b,c;

while(x>=100&&x<1000) {a=001x;b=10(001x-a);c=x-100a-10b;

if(x==(pow(a,3)+pow(b,3)+pow(c,3))) printf("%5d",x);x++;}

}

67

main()

{int m,i,j,s;

for(m=6;m<10000;m++)

{s=1;

for(i=2;i<m;i++)

if(m%i==0) s=s+i;

if(m-s==0)

{printf("%5d its fastors are 1 ",m);for(j=2;j<m;j++) if(m%j==0)

printf("%d ",j);printf("\n");}

}

}

main()

{int m,i,j,s;

for(m=6;m<1000;m++)

{s=m-1;

for(i=2;i<m;i++)

if(m%i==0) s=s-i;

if(s==0)

{printf("%5d its fastors are 1 ",m);for(j=2;j<m;j++) if(m%j==0)

printf("%d ",j);printf("\n");}

}

}

68

main()

{int i=1,n;double t,x=1,y=2,s,sum=0;

scanf("%ld",&n);

while(i<=n) {s=y/x;sum=sum+s;t=y;y=y+x;x=t;i++;}

printf("%f\n",sum);

}

11,

#include<stdioh>

void main()

{

char c;

while((c=getchar())!='\n')

{

c=c+4;

if(c>'Z'+4||c>'z')

c=c-26;

}

printf("%c",c);

}

printf("\n");

}

12,P111  55  56(switch)

55

#include <stdioh>

main()

{int x,y;

printf("输入x:");

scanf("%d",&x);

if(x<1)

{ y=x;

printf("x=%3d, y=x=%d\n",x,y);

}

else if (x<10)

{ y=2x-1;

printf("x=%3d, y=2x-1=%d\n",x,y);

}

else

{ y=3x-11;

printf("x=%3d, y=3x-11=%d\n",x,y);

}

}

56

#include <stdioh>

main()

{ float score;

char grade;

case 2:

printf("请输入学生成绩:");

scanf("%f",&score);

while(score>100||(score<0)

{ printf("\n输入有误,请重新输入:");

scanf("%f",&score);

}

switch((int)(score/10))

{ case 10:

case 9: grade=’A’;break;

case 8: grade=’B’;break;

case 7: grade=’C’;break;

case 6: grade=’D’;break;

case 5:

case 4:

case 3:

case 1:

case 0: grade=’E’;

}

printf("成绩是%51f,相应的等级是%c。\n",score,grade);

}

13(一元二次方程求根)    (求闰年)

55

#include<stdioh>

void main()

{

int year,leap;

scanf("%d",&year);

if(year%4==0)

{

if(year%100==0)

{

if(year%400==0)

leap=1;

else

leap=0;

}

else

leap=1;

}

else

leap=0;

if(leap)

printf("%d is",year);

Else

printf("%d is not",year);

printf("a leap year\n")

}

56

14

217

输出50个学生中成绩高于80分者的学号和成绩

218

输出2000——2500年每一年是否闰年

#include<stdioh>

void main()

{

int year;

year=2000;

go: if(((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0))

printf("%d is run nian",year);

if(year<=2500)

year=year++;

if(year>2500)

goto end;

goto go;

end:   getch();

}

以上就是关于怎样编程简单的计算器程序全部的内容,包括:怎样编程简单的计算器程序、c语言简单程序编程、简单编程!!急需!!c语言!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9802418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存