[C / C ] 仿真算法通过改变基数
关于算法的改变,你可以在网上看到的数字, 然而,该方案将在系统变更后实施 10 给我们 2, 8, 16 并根据我们做正确的打印出来的表格.
执行对开发-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; }
实现对Linux终端
#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; }
阅读更多:
准则基本系统之间切换
我们看一些 2, 往来 8, 往来 16 小数
简单问题的代码爆炸鸟瞰, 请不要张贴这样的事情一遍
比较, 这个问题很简单,但这样它显示的是视觉形象如上述,我觉得并不难,但它不是简单的.
你可以安装一个几行的算法完成. 这是所有代码的模拟算法, 重申其算法是模拟NHE.