/********************************************* * This program allows making arithmetic * * operations in any base between 2 and 36. * * ------------------------------------------ * * Ref.: "Mathématiques en Turbo-Pascal By * * M. Ducamp and A. Reverchon (2), * * Eyrolles, Paris, 1988" [BIBLI 05]. * * ------------------------------------------ * * Sample runs: * * * * ARITHMETIC OPERATIONS IN ANY BASE * * BETWEEN 2 AND 36 * * * * Enter base (2 to 36): 25 * * Enter 1st number : AM3G * * * * Enter next number: 28 * * Operator (+-* /^): + * * Result : AM50 * * Enter next number: J * * Operator (+-* /^): * * * Result : 86MD6 * * Enter next number: 2B * * Operator (+-* /^): / * * Result : 39JM * * * * ARITHMETIC OPERATIONS IN ANY BASE * * BETWEEN 2 AND 36 * * * * Enter base (2 to 36): 2 * * Enter 1st number : 11001010 * * * * Enter next number: 1100 * * Operator (+-* /^): - * * Result : 10111110 * * Enter next number: 1110 * * Operator (+-* /^): - * * Result : 10110000 * * Enter next number: 10101 * * Operator (+-* /^): + * * Result : 11000101 * * Enter next number: 10 * * Operator (+-* /^): / * * Result : 1100010 * * Enter next number: 11 * * Operator (+-* /^): * * * Result : 100100110 * * * * ARITHMETIC OPERATIONS IN ANY BASE * * BETWEEN 2 AND 36 * * * * Enter base (2 to 36): 16 * * Enter 1st number : FEAA * * * * Enter next number: FF * * Operator (+-* /^): + * * Result : FFA9 * * Enter next number: 1799 * * Operator (+-* /^): - * * Result : E810 * * Enter next number: 2 * * Operator (+-* /^): / * * Result : 7408 * * * * Note: Enter 0 to exit. * * * * C++ Version By J-P Moreau * * (www.jpmoreau.fr) * ********************************************** Explanations: ------------ The letters A,B,...,Z represent 10,11,...,36 in the base > 10. The numbers are first converted from base b to base 10 by Function DecodeBasis, then operation is made in base 10 and result is converted from base 10 to base b by Function CodeBasis. Note: assuming that a real number has a mantissa of 40 digits in base 2, the number of useful digits udg in base b is given by formula: udg = integer(40*log(2)/log(b)) b=10: udg = 12 b=16: udg = 10 b=36: udg = 7 */ #include #include #include #include #define MAXREAL 1e40 #define FALSE 0 #define TRUE 1 int b; char x[40],y[40]; float r1,r2; char op; //Convert a number from base b to base 10. The function returns //FALSE if b not in [2..36] or if string x contains invalid //characters in base b or if result y is too big. bool DecodeBasis(char *x,int b, float *y) { int lgth,i,j; float mult; char *ch; if (b<2 || b>36) { printf(" Base must be between 2 and 36 !\n"); return FALSE; } *y=0; mult=1.0; lgth=strlen(x); for (i=0; i=b) return FALSE; *y+=mult*j; if (mult>MAXREAL/b) return FALSE; mult=mult*b; } return TRUE; } //Convert a number from base 10 to base b. The function returns //FALSE if b not in [2..36] or if string x contains invalid //characters in base 10 or if number x is too big. bool CodeBasis(float x,int b, char *y) { int n; char ch[2]; if (b<2 || b>36) { printf(" Base must be between 2 and 36 !\n"); return FALSE; } strcpy(y,""); while (x>0) { n=(int) floor(x-b*int(x/b)); if (n<10) { //y=Chr(Ord("0")+n)+y; sprintf(ch,"%d",n); strcat(y,ch); } else { //y=Chr(Ord("A")+n-10) +y; switch(n) { case 10: strcat(y,"A"); break; case 11: strcat(y,"B"); break; case 12: strcat(y,"C"); break; case 13: strcat(y,"D"); break; case 14: strcat(y,"E"); break; case 15: strcat(y,"F"); break; case 16: strcat(y,"G"); break; case 17: strcat(y,"H"); break; case 18: strcat(y,"I"); break; case 19: strcat(y,"J"); break; case 20: strcat(y,"K"); break; case 21: strcat(y,"L"); break; case 22: strcat(y,"M"); break; case 23: strcat(y,"N"); break; case 24: strcat(y,"O"); break; case 25: strcat(y,"P"); break; case 26: strcat(y,"Q"); break; case 27: strcat(y,"R"); break; case 28: strcat(y,"S"); break; case 29: strcat(y,"T"); break; case 30: strcat(y,"U"); break; case 31: strcat(y,"V"); break; case 32: strcat(y,"W"); break; case 33: strcat(y,"X"); break; case 34: strcat(y,"Y"); break; case 35: strcat(y,"Z"); } } x=(float) int(x/b); } y=strrev(y); //string inversion return TRUE; } void main() { printf("\n ARITHMETIC OPERATIONS IN ANY BASE BETWEEN 2 AND 36\n\n"); do { printf(" Enter base (2 to 36): "); scanf("%d",&b); printf(" Enter 1st number : "); scanf("%s",x); } while (!DecodeBasis(x,b,&r1)); do { printf("\n Enter next number: "); scanf("%s",x); if (strcmp(x,"0")!=0) { if (DecodeBasis(x,b,&r2)) { printf(" Operator (+-* /^): "); scanf("%s",&op); switch(op) { case '+': r1+=r2; break; case '-': r1=fabs(r1-r2); break; case '*': r1*=r2; break; case '/': r1=int(r1/r2); break; case '^': r1=int(exp(r2*log(r1))); } if (CodeBasis(r1,b,y)) printf(" Result : %s\n",y); else printf(" Error in coding result.\n"); } else printf(" Number not valid or decoding error.\n"); } } while (strcmp(x,"0")!=0); printf("\n\n"); } //end of file basisop.cpp