다음은 대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 C 소스 코드이다.
(Ch를 이용하면 컴파일 하지 않고 소스를 그대로 실행시킬 수 있다.)
메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환을 하는 핵심 함수 convertAtoI()와 convertItoA()의 소스가 자체 제작되어 포함되어 있다. 이를 이용하는 부분은 153~154째 줄에 있는
val = convertAtoI(s, srcRdx);
convertItoA((char *) ret, val, destRdx);
이다. 지원되는 진법은 2진법에서 36진법 까지이다.
- /*
- * Filename: convertRadix.c
- * Convert radix in a interactive mode.
- *
- * Compile: cl convertRadix.c
- * Execute: convertRadix
- *
- * Date: 2008/03/25
- * Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- */
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #define MAX_BUF 300
- enum { FALSE, TRUE };
- struct _TOKEN {
- char a[81];
- char b[81];
- };
- void println(char s[]) {
- printf("%s\n", s);
- }
- void print(char s[]) {
- printf("%s", s);
- }
- void printUsage() {
- println("Usage: convertRadix");
- println("Convert radix in a interactive mode, where the maximum radix is 36.");
- }
- void printAbout() {
- println(" About: Convert radix in a interactive mode.");
- }
- void printMainMenu() {
- println(" Command: (S)etup radix, (A)bout, (Q)uit or E(x)it");
- }
- void printMainPrompt() {
- print(" Prompt> ");
- }
- void printSubMenu(int srcRadix, int destRadix) {
- char sbuf[MAX_BUF];
- sprintf(sbuf, " Convert Radix_%d to Radix_%d", srcRadix, destRadix);
- println(sbuf);
- println(" SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit");
- }
- void printSubPrompt() {
- print(" Input Value>> ");
- }
- char BASE36[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- void convertItoA(char *pstr, long num, int radix) {
- char tmp[2];
- char ret[MAX_BUF];
- char arr[MAX_BUF];
- long q, r;
- int i, n;
- int isNegative = FALSE;
- if (num < 0L) {
- isNegative = TRUE;
- num = -num;
- }
- arr[0] = '\0';
- q = num;
- r = 0L;
- while (q >= (long) radix) {
- r = q % (long) radix;
- q = q / (long) radix;
- tmp[0] = BASE36[r];
- tmp[1] = '\0';
- strcat(arr, tmp);
- }
- tmp[0] = BASE36[q];
- tmp[1] = '\0';
- strcat(arr, tmp);
- if (isNegative) {
- strcat(arr, "-");
- }
- n = strlen(arr);
- for (i = 0; i < n; i++) {
- ret[i] = arr[n - i - 1];
- }
- ret[n] = '\0';
- strcpy(pstr, (char *) ret);
- }
- long convertAtoI(const char *pstr, int radix) {
- long ret = 0L;
- char arr[MAX_BUF];
- long q, r;
- int isNegative = FALSE;
- int len = strlen(pstr);
- char c;
- int i;
- long val;
- c = pstr[0];
- if (c == '-') {
- isNegative = TRUE;
- }
- else if (c >= '0' && c <= '9') {
- ret = (long) (c - '0');
- }
- else if (c >= 'A' && c <= 'Z') {
- ret = (long) (c - 'A') + 10L;
- }
- else if (c >= 'a' && c <= 'z') {
- ret = (long) (c - 'a') + 10L;
- }
- if (ret >= (long) radix) {
- println(" Invalid character!");
- return ret;
- }
- for (i = 1; i < len; i++) {
- c = pstr[i];
- ret *= radix;
- if (c >= '0' && c <= '9') {
- val = (long) (c - '0');
- }
- else if (c >= 'A' && c <= 'Z') {
- val = (long) (c - 'A') + 10L;
- }
- else if (c >= 'a' && c <= 'z') {
- val = (long) (c - 'a') + 10L;
- }
- if (val >= (long) radix) {
- println(" Invalid character!");
- return ret;
- }
- ret += val;
- }
- return ret;
- }
- void convertRadix(char *pstr, char s[], int srcRdx, int destRdx) {
- long val;
- char ret[MAX_BUF];
- val = convertAtoI(s, srcRdx);
- convertItoA((char *) ret, val, destRdx);
- strcpy(pstr, ret);
- }
- void readLine(char *pdata) {
- scanf("%s", pdata);
- }
- void readTwo(struct _TOKEN *ptoken) {
- scanf("%s", ptoken->a);
- scanf("%s", ptoken->b);
- }
- void tokenize(struct _TOKEN *ptoken, const char *line, char c) {
- int len = strlen(line);
- int i;
- int from, to;
- i = 0;
- while (line[i] != c) {
- i++;
- }
- from = i;
- while (line[i] == c) {
- i++;
- }
- to = i;
- strncpy(ptoken->a, line, to - from);
- ptoken->a[to - from] = '\0';
- while (line[i] != c) {
- i++;
- }
- from = i;
- while (line[i] == c) {
- i++;
- }
- to = i;
- strncpy(ptoken->b, line, to - from);
- ptoken->b[to - from] = '\0';
- }
- void doConvert(int srcRadix, int destRadix) {
- char sbuf[MAX_BUF];
- char line[MAX_BUF];
- char cmd[MAX_BUF];
- char srcStr[MAX_BUF], destStr[MAX_BUF];
- long tmp;
- println("");
- printSubMenu(srcRadix, destRadix);
- do {
- printSubPrompt();
- readLine((char *)cmd);
- while (strlen(cmd) <= 0) {
- readLine((char *)cmd);
- }
- if (strcmp("main()", cmd) == 0) {
- return;
- }
- else if (strcmp("exit()", cmd) == 0 || strcmp("quit()", cmd) == 0) {
- exit(0);
- }
- tmp = convertAtoI(cmd, srcRadix);
- strcpy(srcStr, cmd);
- convertRadix(destStr, srcStr, srcRadix, destRadix);
- sprintf(sbuf, " ( %s )_%d ---> ( %s )_%d", srcStr, srcRadix, destStr, destRadix);
- println(sbuf);
- println("");
- } while (TRUE);
- }
- void doStart() {
- char line[MAX_BUF];
- char cmd[MAX_BUF];
- int srcRadix = 10, destRadix = 10;
- char srcStr[MAX_BUF], destStr[MAX_BUF];
- struct _TOKEN st;
- int onlyOnce = TRUE;
- do {
- println("");
- if (onlyOnce) {
- println(" The supported maximum radix is 36.");
- onlyOnce = FALSE;
- }
- printMainMenu();
- printMainPrompt();
- readLine((char *)cmd);
- while (strlen(cmd) <= 0) {
- readLine((char *)cmd);
- }
- if (strstr("qQxX", cmd) != NULL && strlen(cmd) == 1) {
- exit(0);
- }
- else if (strstr("aA", cmd) != NULL && strlen(cmd) == 1) {
- printAbout();
- }
- else if (strstr("sS", cmd) != NULL && strlen(cmd) == 1) {
- print(" Input the source and target radices (say, 16 2): ");
- readTwo((struct _TOKEN *) &st);
- srcRadix = convertAtoI(st.a, 10);
- destRadix = convertAtoI(st.b, 10);
- doConvert(srcRadix, destRadix);
- }
- } while (TRUE);
- }
- void main(int argc, char *argv[]) {
- if (argc > 1 && strcmp("-h", argv[1]) == 0) {
- printUsage();
- exit(1);
- }
- doStart();
- }
컴파일> cl convertRadix.c
실행> convertRadix
The supported maximum radix is 36.
Command: (S)etup radix, (A)bout, (Q)uit or E(x)it
Prompt> s
Input the source and target radices (say, 16 2): 8 16
Convert Radix_8 to Radix_16
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> 1234
( 1234 )_8 ---> ( 29C )_16
Input Value>> main()
Command: (S)etup radix, (A)bout, (Q)uit or E(x)it
Prompt> s
Input the source and target radices (say, 16 2): 16 8
Convert Radix_16 to Radix_8
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> 29c
( 29c )_16 ---> ( 1234 )_8
Input Value>> exit()
'프로그래밍 > C' 카테고리의 다른 글
7비트 ASCII 코드표 만들기 예제 with C and Ch (0) | 2008.03.31 |
---|---|
진법(radix) 표 만들기 예제 with C and Ch (0) | 2008.03.29 |
황금비율(golden ratio) 구하기 with C or Ch (0) | 2008.03.24 |
현재 시각 알아내기 for C and Ch (0) | 2008.03.24 |
GMP 사용 예제 for C (0) | 2008.03.19 |