| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1285 人关注过本帖
标题:[原创]简单计算器程序
只看楼主 加入收藏
veryleaves
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2004-11-7
收藏
 问题点数:0 回复次数:5 
[原创]简单计算器程序

#include<math.h> double kaipingfang(double *p) { return sqrt(*p); }

double nchifang(double *p,double *q) { double sum; sum=pow((*p),(*q)); return sum; }

double jiecheng(double *a) { double sum=1.0; for(;(*a)>0;(*a)--) sum=sum*(*a); return sum; }

double pailie(double *p,double *q) { double sum=1.0; int c,a,b; a=c=*p;b=*q; for(;a>=c-b+1;a--) sum=sum*a; return sum; }

double zhuhe(double *p,double *q) { double sum,a,b; a=pailie(p,q); b=jiecheng(q); sum=a/b; return sum; }

double readnumber(char f[],int *i) { double x=0.0; int k=0; while(f[*i]>='0'&&f[*i]<='9') { x=x*10+(f[*i]-'0'); (*i)++; } if(f[*i]=='.') { (*i)++; while(f[*i]>='0'&&f[*i]<='9') { x=x*10+(f[*i]-'0'); (*i)++; k++; } } while(k!=0) { x=x/10.0; k=k-1;} return x; }

double evalpost(char f[]) { double obst[100]; int top=0; int i=0; double x1, x2; while(f[i]!='=') { if(f[i]>='0'&&f[i]<='9') { obst[top]=readnumber(f,&i);top++;} else if(f[i]=='~') { x1=obst[--top]; obst[top]=-x1; top++;i++; } else if(f[i]=='A') { x2=obst[--top]; x1=obst[--top]; obst[top]=pailie(&x1,&x2); top++; i++; } else if(f[i]=='C') { x2=obst[--top]; x1=obst[--top]; obst[top]=zhuhe(&x1,&x2); top++; i++; } else if(f[i]=='!') { x1=obst[--top]; obst[top]=jiecheng(&x1); top++; i++; } else if(f[i]=='S') { x1=obst[--top]; obst[top]=kaipingfang(&x1); top++; i++; } else if(f[i]=='^') { x2=obst[--top]; x1=obst[--top]; obst[top]=nchifang(&x1,&x2); top++; i++; } else if(f[i]==' ') i++; else if(f[i]=='+') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1+x2; top++; i++; } else if(f[i]=='-') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1-x2; top++; i++; } else if(f[i]=='*') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1*x2 ; top++; i++;} else if(f[i]=='/') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1/x2; top++; i++; }

} return obst[0]; }

int is_operation(char op) { switch(op) { case'+': case'-': case'*': case'/':return 1; default:return 0; } }

int priority(char op) { switch(op) { case'=':return -1; case'{': case'[': case'(':return 0; case'+': case'-':return 1; case'*': case'/':return 2; case'A': case'C': case'!': case'S': case'^':return 3; case'~':return 4; default:return -1; } }

void postfix(char e[],char f[]) { int i=0, j=0,k=0; char opst[100],c[100]; int top, t; top=0; opst[top]='=';top++; while(e[i]!='=') { if((e[i]>='0'&&e[i]<='9')||e[i]=='.') f[j++]=e[i]; else if(e[i]=='('||e[i]=='['||e[i]=='{') { opst[top]=e[i];top++;c[k]=e[i];k++;} else if(e[i]==')'||e[i]==']'||e[i]=='}') { c[k]=e[i];k++; t=top-1; while(opst[t]!='('&&opst[t]!='['&&opst[t]!='{') { f[j++]=opst[--top];t=top-1;} top--; } else if(is_operation(e[i]))/*改*/ { f[j++]=' '; while(priority(opst[top-1])>=priority(e[i])) f[j++]=opst[--top]; opst[top]=e[i]; top++; } else if(e[i]==',') f[j++]=' '; else { printf("Input datas error\n"); break; }

i++;} while(top) f[j++]=opst[--top]; /*if(match_kuohao(c)== 待改*/}

main() { char s1[80], s2[80]; double result; printf("Plese input information:\n"); gets(s1); postfix(s1,s2); result=evalpost(s2); printf("The result is:%f " ,result); } 

[此贴子已经被作者于2004-11-07 12:05:58编辑过]

搜索更多相关主题的帖子: double sum 计算器 return 
2004-11-07 11:52
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
没有?
2004-11-07 11:56
veryleaves
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2004-11-7
收藏
得分:0 
我是上传过来的
2004-11-07 12:01
veryleaves
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2004-11-7
收藏
得分:0 
我修改一下
2004-11-07 12:02
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
其他的都没有内容,请补上
2004-11-07 12:10
veryleaves
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2004-11-7
收藏
得分:0 
现在可以了
2004-11-07 12:11
快速回复:[原创]简单计算器程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016185 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved