#include <stdio.h> #include <string.h> int compare(int*,int*); void swap(int* ,int* ); void deal(int*,int*,int,int); int main() { char s1[100],s2[100]; int oper1[100]={0},oper2[100]={0},m,n,c,k,i; gets(s1); gets(s2); m=strlen(s1); n=strlen(s2); (m<n? k=n : k=m); c=k; for(i=0;i<m;i++) oper1[k--]=s1[m-1-i]-'0'; for(k=c,i=0;i<n;i++) oper2[k--]=s2[n-1-i]-'0'; if(compare(oper1,oper2)==3) puts("0"); if(compare(oper1,oper2)==1)deal(oper1,oper2,c,k); if(compare(oper1,oper2)==2) { swap(oper1,oper2); printf("-"); deal(oper1,oper2,c,k); } } void deal(int* oper1,int* oper2,int c,int k) { for(int i=c;i>=0;i--) { oper1[i]-=oper2[i]; if(oper1[i]<0) { oper1[i]+=10; oper1[i-1]--; } } for(int i=0;i<c;i++) if(oper1[i]==0&&oper1[i+1])k=i+1; for(int i=k;i<=c;i++) printf("%d",oper1[i]); } void swap(int* oper1,int* oper2) { for(int i=0;i<100;i++) {oper1[i]=oper1[i]+oper2[i]; oper2[i]=oper1[i]-oper2[i]; oper1[i]=oper1[i]-oper2[i]; } } int compare(int* oper1,int* oper2) { for(int i=0;i<100;i++) { if(oper1[i]>oper2[i]) return 1;//操作数oper1大 if(oper1[i]<oper2[i]) return 2;//操作数oper2大 } return 3;//操作数oper1和操作数oper2一样大 }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:732 |
WU-C语言程序设计教程(第三版)课后习题11.12 (C++代码)(想学链表的小伙伴可以看看)浏览:964 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)from DQM浏览:773 |
1642题解浏览:784 |
字符逆序 (C语言代码)浏览:675 |
单词个数统计 (C语言代码)浏览:1046 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:661 |
矩阵转置 (C语言代码)浏览:855 |
数列问题 (C语言代码)浏览:1068 |
小O的数字 (C语言代码)浏览:1490 |