Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdbool.h>
  4. #include<stdlib.h>
  5. int baseToInt(char* str, int base);
  6. int mypow(int n, int k);
  7.  
  8. void main()
  9. {
  10.     int base;
  11.     char *str;
  12.     char* value;
  13.     printf("**************************************************************\n");
  14.     printf("Select input format (2-binary, 8-octal, 10-decimal, 16 hexadecimal, 0-exit):");
  15.     scanf("%d", &base);
  16.     str = (char*)malloc(32 * sizeof(char));
  17.     if (base != 16)
  18.     {
  19.         printf("\nEnter value in the specified format:");
  20.         if (str == NULL)
  21.             printf("Not enough memory\n"); // if string null print not enough and finish.
  22.         scanf("%s", str);
  23.     }
  24.     if (base == 16)
  25.     {
  26.         printf("\nEnter value in the specified format:");
  27.         if (str == NULL)
  28.             printf("Not enough memory\n"); // if string null print not enough and finish.
  29.         scanf("%s", str + '\0');
  30.     }
  31.    
  32.     value = baseToInt(str,base);
  33.     printf("\nEntered value is : %d",value);
  34.     free(str);
  35.  
  36. }
  37.  
  38. int baseToInt(char* str, int base)
  39. {
  40.     char *num;
  41.     num = (char*)malloc(32 * sizeof(char));
  42.     if (base == 2)
  43.     {
  44.         if (str == 0)
  45.             num = 0;
  46.         num = *str % 2 + 10 * (baseToInt(*str / 2, base));
  47.    
  48.     }
  49.     if (base == 8)
  50.     {
  51.         if (str == 0)
  52.             num == 0;
  53.         num = *str % 8;
  54.         *str /= 8;
  55.         baseToInt(*str, base);
  56.  
  57.     }
  58.  
  59.     if (base == 10)
  60.     {
  61.         num = str;
  62.        
  63.     }
  64.     if (base == 16)
  65.     {
  66.  
  67.  
  68.  
  69.     }
  70.     return num;
  71. }
  72. /*
  73. int mypow(int n,int k)
  74. {
  75.     if (k == 0)
  76.         return 1;
  77.         return power(n, k - 1) * n;
  78. }
  79. */
  80.  
  81.  
  82.  
  83. /*
  84. **************************************************************
  85. Select input format (2-binary, 8-octal, 10-decimal, 16 hexadecimal, 0-exit):
  86. Enter value in the specified format:
  87. Entered value is:
  88. Binary form:
  89. Octal form:
  90. Decimal form:
  91. Hexadecimal form:
  92. FINISH
  93. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement