다음은 대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 자바 소스 코드이다.
메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환의 핵심은 Integer 클래스와 Long 클래스의 정적 메소드
Integer.parseInt(String, int);
Long.toString(long, int);
을 이용하였으며, 지원되는 진법은 2진법부터 36진법까지이다.
- /*
- * Filename: ConvertRadixApp.java
- * Convert radix in a interactive mode.
- *
- * Compile: javac -d . ConvertRadixApp.java
- * Execute: java ConvertRadixApp
- *
- * Date: 2008/03/25
- * Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- */
- import java.io.*;
- import java.util.*;
- public class ConvertRadixApp {
- public static void printUsage() {
- System.out.println("Usage: java ConvertRadixApp");
- System.out.println("Convert radix in a interactive mode, where the maximum radix is 36.");
- }
- public void printAbout() {
- System.out.println(" About: Convert radix in a interactive mode.");
- }
- public void printMainMenu() {
- System.out.println(" Command: (S)et radix, (A)bout, (Q)uit or E(x)it");
- }
- public void printMainPrompt() {
- System.out.print(" Prompt> ");
- }
- public void printSubMenu(int srcRadix, int destRadix) {
- System.out.println(" Convert Radix_" + srcRadix + " to Radix_" + destRadix);
- System.out.println(" SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit");
- }
- public void printSubPrompt() {
- System.out.print(" Input Value>> ");
- }
- public String convertRadix(String s, int srcRdx, int destRdx) {
- long val;
- String ret = "";
- try {
- val = Integer.parseInt(s, srcRdx);
- ret = Long.toString(val, destRdx);
- return ret.toUpperCase();
- }
- catch (NumberFormatException nfx) {
- System.out.println(" Error: " + nfx.getMessage() + " cantains some invalid character.");
- ret = "????";
- }
- finally {
- return ret.toUpperCase();
- }
- }
- public void doConvert(BufferedReader r, int srcRadix, int destRadix) {
- String line;
- String cmd;
- String srcStr = "", destStr = "";
- System.out.println();
- printSubMenu(srcRadix, destRadix);
- try {
- do {
- printSubPrompt();
- while ((cmd = r.readLine()) == null) {
- }
- if ("main()".equals(cmd)) {
- return;
- }
- else if ("exit()".equals(cmd) || "quit()".equals(cmd)) {
- System.exit(0);
- }
- try {
- Integer.parseInt(cmd, srcRadix);
- srcStr = cmd;
- destStr = convertRadix(srcStr, srcRadix, destRadix);
- System.out.println(" ( " + srcStr + " )_" + srcRadix + " ---> ( " + destStr + " )_" + destRadix);
- System.out.println();
- }
- catch (NumberFormatException nfx) {
- }
- } while (true);
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- public void doStart() {
- String line;
- String cmd;
- int srcRadix = 10, destRadix = 10;
- String srcStr = "", destStr = "";
- boolean onlyOnce = true;
- BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
- try {
- do {
- System.out.println();
- if (onlyOnce) {
- System.out.println(" The supported maximum radix is 36.");
- onlyOnce = false;
- }
- printMainMenu();
- printMainPrompt();
- while ((cmd = r.readLine()) == null) {
- }
- if ("qQxX".contains(cmd) && cmd.length() == 1) {
- System.exit(0);
- }
- else if ("aA".contains(cmd) && cmd.length() == 1) {
- printAbout();
- }
- else if ("sS".contains(cmd) && cmd.length() == 1) {
- System.out.print(" Input the source and target radices (say, 16 2): ");
- line = r.readLine();
- StringTokenizer st = new StringTokenizer(line, " ,\t");
- while (st.countTokens() < 2) {
- System.out.print(" Input the source and target radices (say, 16 2): ");
- line = r.readLine();
- st = new StringTokenizer(line, " ,\t");
- }
- srcRadix = Integer.parseInt(st.nextToken());
- destRadix = Integer.parseInt(st.nextToken());
- doConvert(r, srcRadix, destRadix);
- }
- } while (true);
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- public static void main(String[] args) {
- if (args.length > 0 && "-h".equals(args[0])) {
- printUsage();
- System.exit(1);
- }
- ConvertRadixApp app = new ConvertRadixApp();
- app.doStart();
- }
- }
컴파일> javac -d . ConvertRadixApp.java
실행> java ConvertRadixApp
The supported maximum radix is 36.
Command: (S)et radix, (A)bout, (Q)uit or E(x)it
Prompt> s
Input the source and target radices (say, 16 2): 10 16
Convert Radix_10 to Radix_16
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> 256
( 256 )_10 ---> ( 100 )_16
Input Value>> main()
Command: (S)et radix, (A)bout, (Q)uit or E(x)it
Prompt> s
Input the source and target radices (say, 16 2): 16 10
Convert Radix_16 to Radix_10
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> FFFF
( FFFF )_16 ---> ( 65535 )_10
Input Value>> exit()
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
'프로그래밍 > Java' 카테고리의 다른 글
7비트 ASCII 코드표 만들기 예제 with Java (0) | 2008.03.30 |
---|---|
진법(radix) 표 만들기 예제 with Java (0) | 2008.03.29 |
황금비율(golden ratio) 구하기 with Java (0) | 2008.03.24 |
현재 시각 알아내기 for Java (0) | 2008.03.24 |
조립제법(Horner의 방법) 예제 for Java (0) | 2008.03.13 |