Nguythang

convertC

Jul 14th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.c
  9.  * Author: NgT
  10.  *
  11.  * Created on July 15, 2016, 12:40 AM
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. int value(char a) {
  19.     if (a == '0') return 0;
  20.     if (a == '1') return 1;
  21.     if (a == '2') return 2;
  22.     if (a == '3') return 3;
  23.     if (a == '4') return 4;
  24.     if (a == '5') return 5;
  25.     if (a == '6') return 6;
  26.     if (a == '7') return 7;
  27.     if (a == '8') return 8;
  28.     if (a == '9') return 9;
  29.     if (a == 'A') return 10;
  30.     if (a == 'B') return 11;
  31.     if (a == 'C') return 12;
  32.     if (a == 'D') return 13;
  33.     if (a == 'E') return 14;
  34.     if (a == 'F') return 15;
  35. }
  36.  
  37. int checkBase(int n, char s[]) {
  38.     int i;
  39.     if (n == 2) {
  40.         for (i = 0; i < strlen(s) - 1; i++) {
  41.             if (s[i] == '1') continue;
  42.             if (s[i] == '0') continue;
  43.             return 1;
  44.         }
  45.     }
  46.     if (n == 8) {
  47.         for (i = 0; i < strlen(s) - 1; i++) {
  48.             if (s[i] >= '0' && s[i] <= '7') continue;
  49.             return 1;
  50.         }
  51.     }
  52.     if (n == 16) {
  53.         for (i = 0; i < strlen(s) - 1; i++) {
  54.             if (s[i] >= '0' && s[i] <= '9') continue;
  55.             if (s[i] >= 'A' && s[i] <= 'F') continue;
  56.             return 1;
  57.         }
  58.     }
  59.     return 0;
  60. }
  61.  
  62. int convert(int n) {
  63.     char s[100];
  64.     do {
  65.         printf("Enter base - %d: ", n);
  66.         gets(s);
  67.     } while (checkBase(n, s));
  68.     int i, base = 1, dec = 0;
  69.     for (i = strlen(s) - 1; i>-1; i--) {
  70.         dec += value(s[i]) * base;
  71.         base *= n;
  72.     }
  73.     return dec;
  74. }
  75.  
  76. void read(int *n) {
  77.     int a, k = 1;
  78.     char c;
  79.  
  80.     do {
  81.         if (k == 0) printf("Enter a number: ");
  82.         k = scanf("%d%c", &a, &c);
  83.         if (k == 0 || c != '\n') {
  84.             k = 0;
  85.             while (getchar() != '\n');
  86.         }
  87.     } while (k == 0);
  88.     *n = a;
  89. }
  90.  
  91. void solve() {
  92.     int a;
  93.     int result;
  94.     printf("1. Bin to Dec\n");
  95.     printf("2. Oct to Dec\n");
  96.     printf("3. Hex to Dec\n");
  97.     printf("4.  Exit\n");
  98.  
  99.  
  100.     do {
  101.         printf("Enter a number: ");
  102.         read(&a);
  103.         switch (a) {
  104.             case 1: result = convert(2);
  105.                 printf("Result: %d\n", result);
  106.                 break;
  107.             case 2: result = convert(8);
  108.                 printf("Result: %d\n", result);
  109.                 break;
  110.             case 3: result = convert(16);
  111.                 printf("Result: %d\n", result);
  112.                 break;
  113.         }
  114.     } while (a != 4);
  115. }
  116. int main(int argc, char** argv) {
  117.  
  118.     solve();
  119.     return (EXIT_SUCCESS);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment