[C/C++] Chương trình mô phỏng thuật toán đổi cơ số bằng hình vẽ

Về thuật toán đổi các cơ số các bạn có thể xem trên mạng, tuy nhiên chương trình sau sẽ thực hiện đổi số hệ 10 sang hệ 2, 8, 16 và in ra theo đúng dạng chúng ta vẫn làm.
Thực hiện trên dev-C
đổi cơ số

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#define d 179//186
#define ng 196//205
#define gi 197//206
#define swap(type, a, b) {type temp = a; a= b; b =temp;}
void gotoxy(int x, int y)   // chuyen con tro toi (x,y)
{
    static HANDLE h = NULL;
    if(!h)
        h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD c = { x, y };
    SetConsoleCursorPosition(h,c);
}
 
void reverse(char s[])      // dao nguoc xau
{
    int i;
    for (i=0; i<strlen(s)/2; i++)
        swap(char, s[i], s[strlen(s)-i - 1]);
}
 
int main()
{
    int a, n;
    int x, y = 3, sys, i, l1, l2, count;
    char s1[100], s2[100], s[100], result[1000];
    char again;
    char *syschar = (char*) malloc(20*sizeof(char));
    begin:
    x = 4, sys = 2, i, l1 = 0, l2 = 0, count =0;
    printf("Enter number want convert : ");
    scanf("%d", &n);
    a = n;
    do
    {
        printf("Enter number system (2,8,16): ");
        scanf("%d", &sys);
    }while (sys != 2 && sys != 8 && sys != 16);
    if (sys == 2) strcpy(syschar, "01"); //syschar = "01";
    else if (sys == 8) strcpy(syschar, "01234567");
    else if (sys == 16) strcpy(syschar, "0123456789ABCDEF");
 
    gotoxy(x, y);
    printf("%d",n);
    while (n>0)
    {
        sprintf(s1,"%-2d", n);              // chuyen n sang xau s1
        sprintf(s2,"%-2d",n/sys);           // chuyen n/sys sang xau s2
        result[count] = syschar[n%sys]; // cong phan du vao kq
        l1 = strlen(s1);                   
        l2 = strlen(s2);
        gotoxy(x + l1, y);     
        printf(" %c %d",d, sys);        // in | va co so
        gotoxy(2, ++y);
        if (count>0) printf("%c", d);    // in mu ten
        gotoxy(x, y);
        for (i=0; i<l1 + 3 + l2; i++)    // in -------
            if (i==l1+1) printf("%c", gi);
            else printf("%c", ng);
        gotoxy(2, ++y);
        if (count==0) printf("%c",30);  // in duong thang dung cua mui ten
        else printf("%c", d);
        gotoxy(x, y);
        for (i = 0; i<l1-2; i++)     // in khoang trong truoc so du
            printf(" ");
        sprintf(s,"%-2d %c %s", n%sys, d, s2);  // in so du va thuong
        printf("%-2d %c %s", n%sys, d, s2);
        x += l1-2 + strlen(s) - l2;             // tang x den vi tri moi
        n /=sys;
        count++;
    }
    y+=7;
    result[count] = '\0';
    reverse(result);
    printf("nn%d(10) = %s(%d)",a, result, sys);
    printf("nDo you want continue? (y/n)");
    fflush(stdin);
    again = getchar();
    again = toupper(again);
    if (again == 'Y') goto begin;
    free(syschar);
    return 0;
}

Thực hiện trên terminal Linux
đổi cơ số

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define d 124//179//186
#define ng 45//196//205
#define gi 124//197//206
#define gotoxy(type, x, y) printf("%c[%d;%df",0x1B,x, y)
#define swap(type, a, b) {type temp = a; a= b; b =temp;}
void reverse(char s[])      // dao nguoc xau
{
    int i, l = strlen(s)/2;
    for (i=0; i<l; i++)
        swap(char, s[i], s[strlen(s)-i - 1]);
}
 
int main()
{
    int a, n;
    int x = 4, y, sys, i, l1, l2, count;
    char s1[100], s2[100], s[100], result[1000];
    char again;
    char *syschar = (char*) malloc(20*sizeof(char));
    do
    {
        y = 5; sys = 2; l1 = 0; l2 = 0; count =0;
        printf("Enter number want convert : ");
        scanf("%d", &n);
        a = n;
        do
        {
            printf("Enter number system (2,8,16): ");
            scanf("%d", &sys);
        }while (sys != 2 && sys != 8 && sys != 16);
        if (sys == 2) strcpy(syschar, "01"); //syschar = "01";
        else if (sys == 8) strcpy(syschar, "01234567");
        else if (sys == 16) strcpy(syschar, "0123456789ABCDEF");
         
        gotoxy(int, x, y);
        printf("%d",n);
        while (n>0)
        {
            sprintf(s1,"%-2d", n);              // chuyen n sang xau s1
            sprintf(s2,"%-2d",n/sys);           // chuyen n/sys sang xau s2
            result[count] = syschar[n%sys]; // cong phan du vao kq
            l1 = strlen(s1);                   
            l2 = strlen(s2);
            gotoxy(int, x , y + l1);       
            printf(" %c %d",d, sys);        // in | va co so
            gotoxy(int, ++x, 2);   
            if (count>0) printf("%c", d);    // in mu ten
            gotoxy(int, x, y);
            for (i=0; i<l1 + 3 + l2; i++)    // in -------
                if (i==l1+1) printf("%c", gi);
                else printf("%c", ng);
            gotoxy(int, ++x, 2);   
            if (count==0) printf("%c",'^'); // in duong thang dung cua mui ten
            else printf("%c", d);
            gotoxy(int, x, y);
            for (i = 0; i<l1-2; i++)     // in khoang trong truoc so du
                printf(" ");
            sprintf(s,"%-2d %c %s", n%sys, d, s2);  // in so du va thuong
            printf("%-2d %c %s", n%sys, d, s2);
            y += l1-2 + strlen(s) - l2;             // tang x den vi tri moi
            n /=sys;
            count++;
        }
        x += 7;
        result[count] = '\0';
        reverse(result);
        printf("nn%d(10) = %s(%d)",a, result, sys);
        printf("nDo you want continue? (y/n)");
        fgets (s,256,stdin);
        scanf("%c", &again);
        again = toupper(again);
    }while (again == 'Y');// goto begin;
    return 0;
}

Đọc thêm:
Nguyên tắc để chuyển đổi giữa các hệ cơ số
Hiển thị số hệ 2, hệ 8, hệ 16 của số thập phân