#include <stdio.h>
#include <math.h>

void Q(void);
void Q1(void);
void Q2(void);
void Q3(void);
void to_d(void);
void from_d(void);
char x1[99],x2[]="0123456789ABCDEF";
long int x,n,i,j,k,c,sum,z,y[99];

int main()
{
  Q();
 return 0;
}
void Q(void)
{
 printf("1 - Conv. between Any two System\n"
		  "2 - Addition Numbers in Any System\n"
		  "3 - Multiplication Numbers in Any System\n");
 while(1){
			 printf("Enter Choice (0 to finsh): ");
			 scanf("%s",x1);
			 if      (x1[0]=='1')  {Q1();break;}
			 else if (x1[0]=='2')  {Q2();break;}
			 else if (x1[0]=='3')  {Q3();break;}
			 else if (x1[0]=='0') {printf("\nGood Bye");break;}
			 else     printf("Choice Error.\n");
			}
}
void Q1(void)
{
 printf("\nEnter Number in Any System: ");
 scanf("%s",x1);
 while(x1[0]!='0'){
						 do{
							printf("Enter the Base of this System: ");
							scanf("%ld",&n);
							if(n>16) printf("Base Error.\n");
							}while(n>16);
						 z=n;
						 to_d();
						 if(x1[i]=='\0'){
											x=sum;
											do{
											  printf("Enter the Base of System to conv.: ");
											  scanf("%ld",&n);
											  if(n>16 || n<2) printf("Base Error.\n");
											  }while(n>16 || n<2);
											printf("(%s)%ld = (",x1,z);
											from_d();
											printf(")%ld\n",n);
											  }
						 printf("\nEnter another Number ('0' to finsh or 'M' to back Menu): ");
						 scanf("%s",x1);
						 if (x1[0]=='0') printf("\nGood Bye");
						 if (x1[0]=='M'||x1[0]=='m') printf("\n\n"),Q();
						}
}
void Q2(void)
{
 printf("\nEnter Number#1 in Any System: ");
 scanf("%s",x1);
 while(x1[0]!='0'){
						 c=1,z=0;
						 do{
							do{
							  printf("Enter the Base of this System: ");
							  scanf("%ld",&n);
							  if(n>16) printf("Base Error.\n");
							  }while(n>16);
							to_d();
							if(x1[i]=='\0') ++c,z+=sum;
							printf("\nEnter Number#%d in Any System('0' to finsh): ",c);
							scanf("%s",x1);
							}while(x1[0]!='0');
						 x=z;
						 do{
							 printf("Enter the Base of System to conv. Addition: ");
							 scanf("%ld",&n);
							 if(n>16 || n<2) printf("Base Error.\n");
							}while(n>16 || n<2);
						 printf("The Addition is (");
						 from_d();
						 printf(")%ld\n",n);

						 printf("\nEnter another Numbers to Addition\n('0' to finsh or 'M' to back Menu): ");
						 scanf("%s",x1);
						 if (x1[0]=='0') printf("\nGood Bye");
						 if (x1[0]=='M'||x1[0]=='m') printf("\n\n"),Q();
						}
}
void Q3(void)
{
 printf("\nEnter Number#1 in Any System: ");
 scanf("%s",x1);
 while(x1[0]!='0'){
						 c=1,z=1;
						 do{
							do{
							  printf("Enter the Base of this System: ");
							  scanf("%ld",&n);
							  if(n>16) printf("Base Error.\n");
							  }while(n>16);
							to_d();
							if(x1[i]=='\0') ++c,z*=sum;
							printf("\nEnter Number#%d in Any System('0' to finsh): ",c);
							scanf("%s",x1);
							}while(x1[0]!='0');
						 x=z;
						 do{
							 printf("Enter the Base of System to conv. Multiplication: ");
							 scanf("%ld",&n);
							 if(n>16 || n<2) printf("Base Error.\n");
							}while(n>16 || n<2);
						 printf("The Multiplication is (");
						 from_d();
						 printf(")%ld\n",n);

						 printf("\nEnter another Numbers to Multiplication\n('0' to finsh or 'M' to back Menu): ");
						 scanf("%s",x1);
						 if (x1[0]=='0') printf("\nGood Bye");
						 if (x1[0]=='M'||x1[0]=='m') printf("\n\n"),Q();
						}
}
void to_d(void)
{
 sum=i=k=0;
 while(x1[k++]!='\0');
 for(k;k-2>=0;k--,i++){
		 for(j=0;j<=15;j++) if(x1[i]==x2[j]) {sum+=j*pow(n,k-2);break;}
		 if(j>n-1) {printf("Number Error with this Base.\n");break;};
							 }
}
void from_d(void)
{
 for(i=0;x!=0;i++) y[i]=x%n,x/=n;
 for(j=i-1;j>=0;j--) for(k=0;k<=15;k++) if(y[j]==k) printf("%c",x2[k]);
}

