#include <stdio.h> #include <string.h> #include <malloc.h> #define MAX_DATA_LEN 1000 int findIntNum(char* str)//整数个数 { int i=0,dotPos=0; for(i=0;i<=strlen(str)-1;++i) { if((str[i])==‘.‘) { return i; } } return i; } int findDotNum(char* str) { //找到小数点,从右往左第几位,0开始.小数个数 if(strlen(str)==findIntNum(str)) return 0; else return strlen(str)-findIntNum(str)-1; } //第一个为minuend,第二个为subtractor int valueCompare(char *sub1,char* sub2) { int len_str1,len_str2; int IntNum1=0,IntNum2=0; len_str1=strlen(sub1); len_str2=strlen(sub2); IntNum1=findIntNum(sub1); IntNum2=findIntNum(sub2); //printf("len_str1=%d,len_str2=%d,IntNum1=%d,IntNum2=%d\n",len_str1,len_str2,IntNum1,IntNum2); if(IntNum1 < IntNum2) return 1; else if((IntNum1==IntNum2) && (sub1[0]<sub2[0])) return 1; return 0; } char* minus(char* sub1,char* sub2) { int t,i,len_str1,len_str2,resultLenMax; int borrow=0; int IntNum1=0,IntNum2=0,dotPos1=0,dotPos2=0,shift=0; int a[1001]={0},b[1001]={0},c[1001]={0}; char* sub; len_str1=strlen(sub1); for(i=0;i<=len_str1-1;++i) a[i]=sub1[len_str1-1-i]-‘0‘; len_str2=strlen(sub2); for(i=0;i<=len_str2-1;++i) b[i]=sub2[len_str2-1-i]-‘0‘; IntNum1=findIntNum(sub1); IntNum2=findIntNum(sub2); dotPos1=findDotNum(sub1); dotPos2=findDotNum(sub2); resultLenMax=(IntNum1>IntNum2?IntNum1:IntNum2) + (dotPos1>dotPos2?dotPos1:dotPos2) + 1; sub=(char *)malloc(resultLenMax * sizeof(char)); if(NULL==sub) { printf("malloc ERROR!\n"); return; } memset(sub,0,resultLenMax); if((dotPos1==0) && (dotPos2==0))//没有小数 { for(i=0;i<=resultLenMax-1;i++) { c[i]=((a[i]-b[i]-borrow)>=0) ? (a[i]-b[i]-borrow):(a[i]-b[i]-borrow+10); borrow=((a[i]-b[i]-borrow)>=0) ? 0 : 1; } } else if(dotPos1==0)//被减数没小数,减数有小数 { shift=dotPos2; for(i=0;i<=dotPos2-1;i++) { c[i]=((0-b[i]-borrow)>=0) ? (0-b[i]-borrow):(0-b[i]-borrow+10); borrow=((0-b[i]-borrow)>=0) ? 0 : 1; //printf("\nc[%d]=%c\n",i,c[i]+‘0‘); } c[dotPos2]=‘.‘-‘0‘; for(i=dotPos2+1;i<=resultLenMax-1;i++) { c[i]=((a[i-shift-1]-b[i]-borrow)>=0) ? (a[i-shift-1]-b[i]-borrow):(a[i-shift-1]-b[i]-borrow+10); borrow=((a[i-shift-1]-b[i]-borrow)>=0) ? 0 : 1; } } else if(dotPos2==0)//被减数有小数,减数没有小数 { shift=dotPos1; for(i=0;i<=dotPos1-1;i++) { c[i]=a[i]; } c[dotPos1]=‘.‘-‘0‘; for(i=dotPos1+1;i<=resultLenMax-1;i++) { c[i]=((a[i]-b[i-shift-1]-borrow)>=0) ? (a[i]-b[i-shift-1]-borrow):(a[i]-b[i-shift-1]-borrow+10); borrow=((a[i]-b[i-shift-1]-borrow)>=0) ? 0 : 1; } } else if(dotPos1-dotPos2==0)//2个数都有小数,且小数位数相等 { for(i=0;i<=dotPos1-1;i++) { c[i]=((a[i]-b[i]-borrow)>=0) ? (a[i]-b[i]-borrow):(a[i]-b[i]-borrow+10); borrow=((a[i]-b[i]-borrow)>=0) ? 0 : 1; } c[dotPos1]=‘.‘-‘0‘; for(i=dotPos1+1;i<=resultLenMax-1;i++) { c[i]=((a[i]-b[i]-borrow)>=0) ? (a[i]-b[i]-borrow):(a[i]-b[i]-borrow+10); borrow=((a[i]-b[i]-borrow)>=0) ? 0 : 1; } } else if(dotPos1-dotPos2<0)//2个数都有小数,减数位数多 { shift=dotPos2-dotPos1; for(i=0;i<=shift-1;i++) { c[i]=((0-b[i]-borrow)>=0) ? (0-b[i]-borrow):(0-b[i]-borrow+10); borrow=((0-b[i]-borrow)>=0) ? 0 : 1; } for(i=shift;i<=dotPos2-1;i++) { c[i]=((a[i-shift]-b[i]-borrow)>=0) ? (a[i-shift]-b[i]-borrow):(a[i-shift]-b[i]-borrow+10); borrow=((a[i-shift]-b[i]-borrow)>=0) ? 0 : 1; } c[dotPos2]=‘.‘-‘0‘; for(i=dotPos2+1;i<=resultLenMax-1+shift;i++) { c[i]=((a[i-shift]-b[i]-borrow)>=0) ? (a[i-shift]-b[i]-borrow):(a[i-shift]-b[i]-borrow+10); borrow=((a[i-shift]-b[i]-borrow)>=0) ? 0 : 1; } } else if(dotPos1-dotPos2>0) { shift=dotPos1-dotPos2; for(i=0;i<=shift-1;i++) { c[i]=((a[borrow]-0-borrow)>=0) ? (a[borrow]-0-borrow):(a[borrow]-0-borrow+10); borrow=((a[borrow]-0-borrow)>=0) ? 0 : 1; } for(i=shift;i<=dotPos1-1;i++) { c[i]=((a[i]-b[i-shift]-borrow)>=0) ? (a[i]-b[i-shift]-borrow):(a[i]-b[i-shift]-borrow+10); borrow=((a[i]-b[i-shift]-borrow)>=0) ? 0 : 1; } c[dotPos1]=‘.‘-‘0‘; for(i=dotPos1+1;i<=resultLenMax-1+shift;i++) { c[i]=((a[i]-b[i-shift]-borrow)>=0) ? (a[i]-b[i-shift]-borrow):(a[i]-b[i-shift]-borrow+10); borrow=((a[i]-b[i-shift]-borrow)>=0) ? 0 : 1; } } for(i=resultLenMax-1+shift;i>=0;i--) sub[resultLenMax-1+shift-i]=c[i]+‘0‘; //最高位借完为0没处理 // while(i++) // if(sub[i]==0)sub[i]==‘\0‘; //printf("%s\n",sub); return sub; } int main() { int key=0; char str1[MAX_DATA_LEN],str2[MAX_DATA_LEN]; char* result; printf("Please input two big data:\n"); result=(char *)malloc(2 * MAX_DATA_LEN * sizeof(char)); if(NULL==result) { printf("malloc ERROR!\n"); return; } memset(result,0,2 * MAX_DATA_LEN); scanf("%s",str1);//输入有效性判断 scanf("%s",str2); key=valueCompare(str1,str2); printf("The minus result of the two big data is:"); if(key==1) { printf("-"); result=minus(str2,str1); } else { result=minus(str1,str2); } printf("%s\n",result);
free(result);
return 0; }
时间: 2024-10-03 23:48:42