Guest User

Untitled

a guest
Nov 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. /*INVALID INPUTS:
  4. (1) if input contains characters not within the set of characters of that particular base
  5. (e.g., base 3 with 99291 is invalid because only 0-2 are valid characters for that base)
  6. (e.g., base 16 with 2233AALDDEFF is invalid because only 0-9 and A-F are valid characters for that base)
  7. (2) if input contains characters not within the set of characters valid, i.e., some characters are not from the set containing 0-9 and A-Z.
  8. (e.g., for whatever base, input 11ADDD_LO is invalid since it contains '_')
  9. (e.g., for whatever base, input 3998a is invalid since it contains a lowercase letter)
  10. (e.g., for whatever base, input 889ADFO SLDO is invalid since it contains a space)
  11.  
  12. new limit ? 2^31 -1 = 2 147 483 647 */
  13. int convertVal(char num[],long int i,long int len){
  14.        
  15.         if(i>=len) return 0;
  16.        
  17.         num[i]=(num[i]<65)?num[i]-48:num[i]-55;
  18.         convertVal(num,i+1,len);
  19.        
  20.         return 0;
  21. }
  22.  
  23. int checkIfValid(char num[], int n){
  24.     int i;
  25.     int len = strlen(num);
  26.     for(i = 0; i < len; i++){
  27.     if(num[i] < '0' || num[i] > '9' || num[i] < 'A' || num[i] > 'Z'){
  28.         printf("invalid");
  29.         return -1;}
  30.     }
  31.     return 0;
  32. }
  33. long int toDecimal(char num[],long int n,long int i,long int len){
  34.        
  35.         long int pow=1,j;
  36.        
  37.         if(i>=len) return 0;
  38.        
  39.         for(j=1;j<len-i;j++)
  40.                 pow=pow*n;
  41.                
  42.         return num[i]*pow+toDecimal(num,n,i+1,len);
  43. }
  44.  
  45. long int newBase(long int dec,long int m,long int pow){
  46.        
  47.         long int j,power=1;
  48.        
  49.         if(pow<0) return 0;
  50.        
  51.         for(j=0;j<pow;j++)
  52.                 power=power*m;
  53.                
  54.         printf("%c",(dec/power<10)?dec/power+48:dec/power+55);
  55.         newBase(dec-(dec/power)*power,m,pow-1);
  56.        
  57.         return 0;
  58. }
  59. int main(){
  60.        
  61.         char num[100];
  62.         long int n,m,i=0,pow=1;//n is the base of the number ; m is the new base ; num is the number to be converted
  63.         unsigned long long int temp,dec;
  64.         int x=1,y;
  65.         scanf("%d",&y);
  66.        
  67.         while (x<=y){
  68.         scanf("%ld %ld %s",&n,&m,num);
  69.        
  70.         long int len = strlen(num);
  71.        
  72.         convertVal(num,0,len);
  73.         checkIfValid(num,n);
  74.         dec=toDecimal(num,n,0,len);
  75.        
  76.         while(dec>=pow*m){
  77.                 pow=pow*m;
  78.                 i++;
  79.         }
  80.        
  81.         temp = newBase(dec,m,i);
  82.         printf("\n");
  83.        
  84.        
  85.         x++;
  86.         }
  87.      return 0;
  88. }
Add Comment
Please, Sign In to add comment