标签:blog io os ar for sp div log amp
#include <stdio.h> #include <string.h> #include <malloc.h> #include<assert.h> #define MAX_DATA_LEN 1000 #define TRUE 1 #define FALSE 0 typedef struct{ int intNum; int dotPos; }IntDotInfo; 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) { if(strlen(str)==findIntNum(str)) return 0; else return strlen(str)-findIntNum(str)-1; } 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); if(IntNum1 < IntNum2) return 1; else if((IntNum1==IntNum2) && (sub1[0]<sub2[0])) return 1; return 0; } int isPositive(char* s) { if(s[0]==‘-‘) return FALSE; else return TRUE; } int getIntDotInfo(char* sub1,char* sub2,IntDotInfo *data1,IntDotInfo *data2) { if(sub1==NULL || sub2 == NULL) return -1; data1->intNum=findIntNum(sub1); data1->dotPos=findDotNum(sub1); data2->intNum=findIntNum(sub2); data2->dotPos=findDotNum(sub2); return 1; } int eliminateZeroMSB(char *dataIn,char* dataOut) { } int everyBitMinus(int *a,int *b, int *c, int bitLen,int borrow) { int i; for(i=0;i<=bitLen;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; } } char* bigDataMinus(char* sub1,char* sub2) { int i,k,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; IntDotInfo data1={0}; IntDotInfo data2={0}; 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‘; getIntDotInfo(sub1,sub2,&data1,&data2); intNum1=data1.intNum; dotPos1=data1.dotPos; intNum2=data2.intNum; dotPos2=data2.dotPos; 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))//没有小数 { everyBitMinus(&a[0],&b[0],&c[0],resultLenMax-1-0+1,0); } 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; } c[dotPos2]=‘.‘-‘0‘; everyBitMinus(&a[dotPos2+1-shift-1],&b[dotPos2+1],&c[dotPos2+1],(resultLenMax-1)-(dotPos2+1)+1,borrow); } else if(dotPos2==0)//被减数有小数,减数没有小数 { shift=dotPos1; for(i=0;i<=dotPos1-1;i++) { c[i]=a[i]; } c[dotPos1]=‘.‘-‘0‘; everyBitMinus(&a[dotPos1+1],&b[dotPos1+1-shift-1],&c[dotPos1+1],(resultLenMax-1)-(dotPos1+1)+1,borrow); } else if(dotPos1-dotPos2==0)//2个数都有小数,且小数位数相等 { everyBitMinus(&a[0],&b[0],&c[0],(dotPos1-1)-0+1,borrow); c[dotPos1]=‘.‘-‘0‘; everyBitMinus(&a[dotPos1+1],&b[dotPos1+1],&c[dotPos1+1],(resultLenMax-1)-(dotPos1+1)+1,borrow); } 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; } everyBitMinus(&a[0],&b[shift],&c[shift],(dotPos2-1)-(shift)+1,borrow); c[dotPos2]=‘.‘-‘0‘; everyBitMinus(&a[dotPos2+1-shift],&b[dotPos2+1],&c[dotPos2+1],(resultLenMax-1+shift)-(dotPos2+1)+1,borrow); } else if(dotPos1-dotPos2>0)//2个数都有小数,被减数位数多 { 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; } everyBitMinus(&a[shift],&b[0],&c[shift],(dotPos1-1)-(shift)+1,borrow); c[dotPos1]=‘.‘-‘0‘; everyBitMinus(&a[dotPos1+1],&b[dotPos1+1-shift],&c[dotPos1+1],(resultLenMax-1+shift)-(dotPos1+1)+1,borrow); } for(i=resultLenMax-1+shift;i>=0;i--) { if(c[i]!=0)//||( c[i]==0 && c[i-1]+‘0‘==‘.‘)) break; } for(i,k=0;i>=0;i--,k++) { sub[k]=c[i]+‘0‘; } sub[k]=‘\0‘; //printf("%s\n",sub); return sub; } //carry int everyBitAdd(int* s1,int* s2, int *sum, int Len, int carry) { int i; for(i=0;i<=Len-1;i++) { sum[i] = (s1[i] + s2[i] + carry) % 10; carry = (s1[i] + s2[i] + carry) / 10; } return carry; } char* alignNZero(int n) { int i; char* s; s=(char *)malloc(n * sizeof(char)); for(i=0;i<n;i++) s[i]=‘0‘; s[i]=‘\0‘; return s; } char* bigDataAdd(char* sub1,char* sub2) { int i,k,len_str,resultLenMax; int carry=0; int intNum1=0,intNum2=0,dotPos1=0,dotPos2=0,intBitNum,dotBitNum; int a[1001]={0},b[1001]={0},c[1001]={0}; char* sum; IntDotInfo data1={0}; IntDotInfo data2={0}; char dotbuf1[512] = {0}; char buf1[512] = {0}; char dotbuf2[512] = {0}; char buf2[512] = {0}; getIntDotInfo(sub1,sub2,&data1,&data2); intNum1=data1.intNum; dotPos1=data1.dotPos; intNum2=data2.intNum; dotPos2=data2.dotPos; intBitNum=(intNum1>intNum2) ? intNum1 : intNum2; dotBitNum=(dotPos1>dotPos2) ? dotPos1 : dotPos2; resultLenMax=intBitNum+dotBitNum+1; sum=(char *)malloc(resultLenMax * sizeof(char)); if(NULL==sum) { printf("malloc ERROR!\n"); return NULL; } memset(sum,0,resultLenMax); //先小数对齐 if(dotPos1<=dotPos2) { memset(dotbuf1, 0, sizeof(dotBitNum)); sprintf(dotbuf1,"%s%s",sub1,alignNZero(dotBitNum-dotPos1)); sprintf(dotbuf2,"%s",sub2); } else if(dotPos1>dotPos2) { memset(dotbuf2, 0, sizeof(dotBitNum)); sprintf(dotbuf2,"%s%s",sub2,alignNZero(dotBitNum-dotPos2)); sprintf(dotbuf1,"%s",sub1); } //再整数对齐 if(intNum1<=intNum2) { memset(buf1, 0, sizeof(intBitNum)); sprintf(buf1,"%s%s",alignNZero(intBitNum-intNum1),dotbuf1); sprintf(buf2,"%s",dotbuf2); } else if(intNum1>intNum2) { memset(buf2, 0, sizeof(intBitNum)); sprintf(buf2,"%s%s",alignNZero(intBitNum-intNum2),dotbuf2); sprintf(buf1,"%s",dotbuf1); } //printf("buf1=%s,buf2=%s\n",buf1,buf2); len_str=dotBitNum+intBitNum+1; for(i=0;i<=len_str-1;++i) a[i]=buf1[len_str-1-i]-‘0‘; for(i=0;i<=len_str-1;++i) b[i]=buf2[len_str-1-i]-‘0‘; for(i=0;i<=dotBitNum-1;i++) carry=everyBitAdd(&a[0],&b[0],&c[0],dotBitNum,carry); printf("dot carry=%d\n",carry); c[dotBitNum]=‘.‘-‘0‘; for(i=dotBitNum+1;i<=dotBitNum+1+intBitNum-1;i++) carry=everyBitAdd(&a[dotBitNum+1],&b[dotBitNum+1],&c[dotBitNum+1],intBitNum,carry); printf("int carry=%d\n",carry); for(i=dotBitNum+1+intBitNum-1;i>=0;i--) { if(c[i]!=0)//||( c[i]==0 && c[i-1]+‘0‘==‘.‘)) break; } for(i,k=0;i>=0;i--,k++) { sum[k]=c[i]+‘0‘; } sum[k]=‘\0‘; free(carry); return sum; } char* sub(char* s1, char* s2) { int key=0; char* result; int p1,p2; result=(char *)malloc(2 * MAX_DATA_LEN * sizeof(char)); if(NULL==result) { printf("malloc ERROR!\n"); return NULL; } memset(result,0,2 * MAX_DATA_LEN); p1=isPositive(s1); p2=isPositive(s2); if(p1 && p2) { key=valueCompare(s1,s2); if(key==1) { sprintf(result,"-%s",bigDataMinus(s2,s1)); } else { result=bigDataMinus(s1,s2); } } else if(!p1 && !p2) { key=valueCompare(s1+1,s2+1); if(key==1) { result=bigDataMinus(s2+1,s1+1); } else { sprintf(result,"-%s",bigDataMinus(s1+1,s2+1)); } } else if(p1 && !p2) { result=bigDataAdd(s1,s2+1); } else if(!p1 && p2) { result=bigDataAdd(s1+1,s2); } return result; } void unit_test() { printf("sub(sub(333.3333333333333,-8888.8888)=%s\n",sub("333.3333333333333","-8888.8888")); assert(strcmp(sub("333.3333333333333","-8888.8888"), "9222.2221333333333") == 0); return ; assert(strcmp(sub("1","0"), "1") == 0); assert(strcmp(sub("100", "200"), "-100") == 0); assert(strcmp(sub("2222222.2222222","1111111.1111111"), "1111111.1111111") == 0); assert(strcmp(sub("2222222.2222222","-1111111.1111111"), "3333333.3333333") == 0); assert(strcmp(sub("10000000", "1.234"), "9999998.766") == 0); assert(strcmp(sub("1111","234"), "877") == 0); assert(strcmp(sub("22","3.333333333"), "18.666666667") == 0); assert(strcmp(sub("2222222.2222222","33"), "2222189.2222222") == 0); assert(strcmp(sub("333.333","44.44444444"), "288.88855556") == 0); assert(strcmp(sub("333.3333333333333","44.44"), "288.8933333333333") == 0); assert(strcmp(sub("222.2233333","-1.1"), "223.3233333") == 0); } void main() { unit_test(); }
标签:blog io os ar for sp div log amp
原文地址:http://www.cnblogs.com/x113/p/4019674.html