| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1486 人关注过本帖
标题:任意位整数的四则运算!SOS!
只看楼主 加入收藏
joung
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2004-6-1
收藏
 问题点数:0 回复次数:6 
任意位整数的四则运算!SOS!

设计一个处理任意位数的整数计算的程序。它能完成任意位数的整数的输入,存储与输出。提供对这种整数作加、减、乘、除、乘方等的函数,提供若干变量,使人可以利用前面的计算结果进行后面的计算。

如何分析!谢谢!

搜索更多相关主题的帖子: SOS 整数 运算 
2004-06-01 11:28
jzh2004
Rank: 1
等 级:新手上路
帖 子:341
专家分:0
注 册:2004-4-30
收藏
得分:0 
任意位?不知道内存够不够,还要乘、除、乘方,不知道现在的微机速度够不够。

我的主页 http://www.
2004-06-02 00:12
joung
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2004-6-1
收藏
得分:0 

写了一段减法的,有错,请帮改一下。谢谢!

#include <stdio.h> #include <string.h>

int main (int argc,char *argv[]); void reverse_string(char *str); int check_string(const char *str);

int main(int argc,char *argv[]) { char left[1024]; char right[1024]; char result[1024]; int left_len,right_len,max_len; int carry=0; int i,miss; printf("请输入减数:"); scanf("%s",left); if(check_string(left)!=0) { printf("减数有错,请重新输入!\n"); exit(-1); } printf("请输入被减数:"); scanf("%s",right); if(check_string(right)!=0) { printf("被减数有错,请重新输入!\n"); exit(-1); } reverse_string(left); reverse_string(right); left_len=str(left); right_len=strlen(right); max_len=(left_len>right_len)?left_len:right_len; for(i=max_len;i>0;i--) { sum-=left[i]-0; carry=miss/10; result[i]=miss%10-'0'; } if(carry) { result[i--]=carry-'0'; } result[i]=0; reverse_string(result); printf("两数相减的差为:%s.\n",result); return 0; }

void reverse_string(char *str) { char temp; int len,harf_len,i; len=strlen(str); harf_len=len/2; for(i=harf_len;i>0;i--) { temp=str[i]; str[i]=str[len-1-i]; str[len-1-i]=temp; } }

int check_string(const char *str) { int len,i; len=strlen(str); for(i=len;i>0;i--) { if(str[i]>'0'||str[i]<'9') { return -1; } } return 0; }

2004-06-03 13:07
chengstone
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:562
专家分:226
注 册:2004-4-3
收藏
得分:0 

qq:69558139
2004-07-04 16:09
mikewolf
Rank: 1
等 级:新手上路
帖 子:175
专家分:0
注 册:2004-7-3
收藏
得分:0 

/*帮你整理一下风格,便于大家看清楚*/ #include<math.h> #include<graphics.h> #include<ctype.h> #include<stdio.h> typedef struct list { double data; struct list *next; }NODE; typedef NODE *LLIST; LLIST operater=NULL; LLIST number=NULL; LLIST oper=NULL; LLIST ber=NULL; int empty(LLIST operater) { if(operater==NULL) return 1; else return 0;

}

LLIST stackin(LLIST operater,double b) {

LLIST ptr; ptr=(LLIST)malloc(sizeof(NODE)); ptr->data=b; ptr->next=operater; operater=ptr; return operater; }

LLIST stackout(LLIST number,double *num) {

LLIST ptr=number; if(number!=NULL) { number=number->next; *num=ptr->data; free(ptr); return number; }

}

int max(char sum) {

switch(sum) { case '+': case '-':return 2; case '*': case '%': case '/':return 3; case '(': case ')':return 1; case '!':return 4; case '^':return 5; case '?':return 4; default:return 0; }

}

double tatal(double num1,double num2,char sum) {

int i; double s; switch(sum) { case '+': return(num2+num1); case '-': return(num2-num1); case '*': return(num2*num1); case '/': return(num2/num1); case '%': return((int)num2%(int)num1); case '!': return Stairs((int)num2); case '?': return sqrt(num2); case '^': for(i=0;i<num1;i++) { if(i==0) s=num2; else s*=num2; } return s;

}

}

Contrary(double num1,double num2,char sum)

{

double s;

int i;

switch(sum)

{

case '-':return(num1-num2);

case '/':return(num1/num2);

case '%':return((int)num1%(int)num2);

case '^':for(i=0;i<num2;i++)

if(i==0)s=num1;

else s*=num1;

return s;

}

}

Stairs(int num2) { if(num2<=1) return 1; else return num2*Stairs(num2-1);

}

outdata(LLIST operater) { LLIST ber=operater; while(ber!=NULL) { printf("%lf ",ber->data); ber=ber->next; } printf("\n");

}

outoper(LLIST operater) { LLIST oper=operater; while(oper!=NULL) { printf("%c",oper->data); oper=oper->next; } printf("\n");

}

char *filter(char *c) { int j=0,i=0; char *s; while(c[i]!='\0'&&c[i]!='\n') { if(c[i]!=' '&&max(c[i])||isdigit(c[i])||c[i]=='.') { if(c[i]=='!'||c[i]=='?') { if(isdigit(c[i-1])||c[i-1]==')') s[j++]=c[i]; } else if(c[i]=='.') { if(isdigit(c[i-1])) s[j++]=c[i]; } else s[j++]=c[i];

} i++; } s[j]='\0'; return s; }

Resolve(char *s,double *a,char *t) { int k=1,i=0,l=0; while(s[i]!='\0'&&s[i]!='\n') { char p[50]={0}; int j=0; if(max(s[i])) { if(s[i]=='('&&s[i+1]=='-') { i+=2; while(isdigit(s[i])||s[i]=='.') { p[j++]=s[i++]; } a[k++]=0-atof(p); i++; } else t[l++]=s[i++]; } else{ while(isdigit(s[i])||s[i]=='.') { p[j++]=s[i++]; } a[k++]=atof(p);

} } t[l]='\0'; a[0]=k-1; }

Calculation() { int x,n=0,j=0,i=0,kern=1; char b[20],c[20],*s; double a[20]; double num,num1,num2,sum; cleardevice(); setcolor(BLUE); outtextxy(200,200,"please input Express type: "); setcolor(WHITE); rectangle(160,235,440,260); gotoxy(22,16); while((c[n++]=getch())!=13) { if(c[n-1]==8) { if(n==1) { n--; } else{ gotoxy(wherex()-1,16); printf(" "); n--; printf("%c",c[n]); n--; } } else{ printf("%c",c[n-1]); } s=filter(c); Resolve(s,a,b); while(b[i]!='\0'&&b!='\n') { if(b[i]!='(') { if(kern<=a[0]) number=stackin(number,a[kern++]); } if(b[i]=='!'||b[i]=='?') { number=stackout(number,&num); number=stackin(number,tatal(0,num,b[i])); } else if(operater==NULL||b[i]=='(') operater=stackin(operater,(double)b[i]); else if(b[i]==')') { if((char)operater->data=='(') operater=stackout(operater,&sum); else{ while((char)operater->data!='(') { number=stackout(number,&num); ber=stackin(ber,num); operater=stackout(operater,&sum); if(oper==NULL) oper=stackin(oper,sum); else{ if(max((char)sum)<=

max((char)oper->data)&&ber->next!=NULL) { ber=stackout(ber,&num1); ber=stackout(ber,&num2); oper=stackout(oper,&num); ber=stackin(ber,

tatal(num1,num2,(char)num)); } oper=stackin(oper,sum); } } number=stackout(number,&num1); ber=stackin(ber,num1); operater=stackout(operater,&sum); ber=stackout(ber,&num1); ber=stackout(ber,&num2); oper=stackout(oper,&num); if((char)num=='-'|| (char)num=='/'|| (char)num=='%'|| (char)num=='^') number=stackin(number,

Contrary(num1,num2,(char)num)); else{ number=stackin(number,

tatal(num1,num2,(char)num)); }

}

}

else{ if(max(b[i])<=max((char)operater->data)

&&number->next!=NULL) { number=stackout(number,&num1); number=stackout(number,&num2); operater=stackout(operater,&sum); num=tatal(num1,num2,(char)sum); number=stackin(number,num); } operater=stackin(operater,(double)b[i]); } i++; } if(kern<=a[0]) number=stackin(number,a[kern]); while(operater!=NULL) { number=stackout(number,&num1); number=stackout(number,&num2); operater=stackout(operater,&sum); number=stackin(number,tatal(num1,num2,(char)sum)); } number=stackout(number,&num2); printf("=%lf\n",num2); outtextxy(180,300,"\nCalculation End! Thank you!\n"); }

int main(void) { int gd=DETECT,gm=0; char key=1; registerbgidriver(EGAVGA_driver); initgraph(&gd,&gm,"");

while(key!=27) { Calculation(); key=getch(); key=getch(); }

closegraph();

getch(); return 0; }

2004-07-13 16:01
mikewolf
Rank: 1
等 级:新手上路
帖 子:175
专家分:0
注 册:2004-7-3
收藏
得分:0 

看完这个程序,我只想说,太乱呢!不好维护!

看来,我应当自己写一个!

2004-07-13 16:04
chengstone
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:562
专家分:226
注 册:2004-4-3
收藏
得分:0 
呵呵

qq:69558139
2004-07-13 18:01
快速回复:任意位整数的四则运算!SOS!
数据加载中...
 
   



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

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