[C / C ] The simulation algorithm by changing the base figure
About algorithm change the number you can see on the net, however, the program would be the change of system 10 to Us 2, 8, 16 and in accordance with the format we still do.
Perform on dev-C
#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; }
Implementation on Linux terminal
#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; }
Read more:
Principles to switch between the base system
Show us some 2, Contacts 8, Contacts 16 of decimal
the simple math that code looked about to explode eye, please do not post things like this all over again
Quite, the problem is simple, but it is showing the visual image above, I think not difficult but not simple.
The algorithm you can install a few lines is finished. Here is the post code simulation algorithms, reiterated its algorithm it is SIMULATION offline.