#include #include #include char s1[16]="0123456789ABCDEF";//auxiliar strings char s2[16]="0123456789abcdef"; int num(char c,int p)//return the value of the digit { int i=0; while (i=k+1;i--) { s[i]=s[i-1]; } s[i]=c; } void cut_0(char *s)//cuts the zeroes from the left side { while ((s[0]=='0') && (strlen(s)!=1)) { int j; for (j=0;j<=strlen(s)-1;j++) { s[j]=s[j+1]; } } } void addition(char* a,char* b,char* c,int p) { int carry=0; int k=strlen(a)-1,l=strlen(b)-1; while ((k>=0) && (l>=0))//while both number are digit on the current poisition { insert(s1[(num(a[k],p)+num(b[l],p)+carry)%p],c,0);//insert on the first position the suitable digit carry=(num(a[k],p)+num(b[l],p)+carry)/p; k--; l--; } if ((k<0) && (l>=0))//if the second number has more digits than the first one { while (l>=0) { insert(s1[(num(b[l],p)+carry)%p],c,0); carry=(num(b[l],p)+carry)/p; l--; } } else if ((k>=0) && (l<0))//if the first number has more digits than the firt one { while (k>=0) { insert(s1[(num(a[k],p)+carry)%p],c,0); carry=(num(a[k],p)+carry)/p; k--; } } if (carry==1)//if exists carry then inserts it { insert('1',c,0); } } void subtraction(char* a,char* b,char* c,int p,int *neg) { if ((strlen(a)=0) && (l>=0))//while both number has digit in the current position { if (num(a[k],p)-num(b[l],p)+carry>=0) { insert(s1[num(a[k],p)-num(b[l],p)+carry],c,0); carry=0; } else { insert(s1[num(a[k],p)-num(b[l],p)+carry+p],c,0); carry=-1; } k--; l--; } if ((k>=0) && (l<0))//if the minuend has more digits { while (k>=0) { if (num(a[k],p)+carry>=0) { insert(s1[num(a[k],p)+carry],c,0); carry=0; } else { insert(s1[num(a[k],p)+carry+p],c,0); carry=-1; } k--; } } cut_0(c); } void multiplication(char *a,char *b,char *c,int p) { int i; for (i=strlen(b)-1;i>=0;i--)//for every digits from the second number will be executed the multiplication { char temp[strlen(a)+strlen(b)]; temp[0]='\0'; int carry=0,j; for (j=0;j=0;j--) { insert(s1[(num(b[i],p)*num(a[j],p)+carry)%p],temp,0); carry=(num(b[i],p)*num(a[j],p)+carry)/p; } char d[200]; d[0]='\0'; insert((char)carry+'0',temp,0); addition(temp,c,d,p);//addition of the subresult for (j=0;j<=strlen(d);j++) { c[j]=d[j]; } } cut_0(c); } void division(char* a,char* b,char* q,char *r,int p) { int i,carry=0; for (i=0;iq)//if p>q then the used algorithm is the successive division by the destination base { char b[100]; b[0]='\0'; if ((a[0]=='0') && (a[1]=='\0')) { b[0]='0'; b[1]='\0'; } while ((a[0]!='0') || (a[1]!='\0'))//while the dividend is not zero { char quo[100],rem; quo[0]='\0'; division(a,&s1[q],quo,&rem,p); insert(rem,b,0); int i; for (i=0;i<=strlen(quo);i++) { a[i]=quo[i]; } } int i; for (i=0;i<=strlen(b);i++) { a[i]=b[i]; } } else if (p