Advertisement
Khoa98

câu 5

Feb 17th, 2020
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package baitap;
  7.  
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author Khoapro
  13.  */
  14. public class main {
  15.  
  16.     public static final String KHONG = "không";
  17.     public static final String MOT = "một";
  18.     public static final String HAI = "hai";
  19.     public static final String BA = "ba";
  20.     public static final String BON = "bốn";
  21.     public static final String NAM = "năm";
  22.     public static final String SAU = "sáu";
  23.     public static final String BAY = "bảy";
  24.     public static final String TAM = "tám";
  25.     public static final String CHIN = "chín";
  26.     public static final String LAM = "lăm";
  27.     public static final String LE = "lẻ";
  28.     public static final String MUOI = "mươi";
  29.     public static final String MUOIF = "mười";
  30.     public static final String MOTS = "mốt";
  31.     public static final String TRAM = "trăm";
  32.     public static final String NGHIN = "nghìn";
  33.     public static final String TRIEU = "triệu";
  34.     public static final String TY = "tỷ";
  35.  
  36.     public static final String[] number = {KHONG, MOT, HAI, BA,
  37.         BON, NAM, SAU, BAY, TAM, CHIN};
  38.  
  39.     //Hàm chính đọc số
  40.     public static ArrayList<String> readNum(String a) {
  41.         ArrayList<String> kq = new ArrayList<String>();
  42.  
  43.         //Cắt chuổi string chử số ra thành các chuổi nhỏ 3 chử số
  44.         ArrayList<String> List_Num = Split(a, 3);
  45.  
  46.         while (List_Num.size() != 0) {
  47.             //Xét 3 số đầu tiên của chuổi (số đầu tiên của List_Num)
  48.             switch (List_Num.size() % 3) {
  49.                 //3 số đó thuộc hàng trăm
  50.                 case 1:
  51.                     kq.addAll(read_3num(List_Num.get(0)));
  52.                     break;
  53.                 // 3 số đó thuộc hàng nghìn
  54.                 case 2:
  55.                     ArrayList<String> nghin = read_3num(List_Num.get(0));
  56.                     if (!nghin.isEmpty()) {
  57.                         kq.addAll(nghin);
  58.                         kq.add(NGHIN);
  59.                     }
  60.                     break;
  61.                 //3 số đó thuộc hàng triệu
  62.                 case 0:
  63.                     ArrayList<String> trieu = read_3num(List_Num.get(0));
  64.                     if (!trieu.isEmpty()) {
  65.                         kq.addAll(trieu);
  66.                         kq.add(TRIEU);
  67.                     }
  68.                     break;
  69.             }
  70.  
  71.             //Xét nếu 3 số đó thuộc hàng tỷ
  72.             if (List_Num.size() == (List_Num.size() / 3) * 3 + 1 && List_Num.size() != 1) {
  73.                 kq.add(TY);
  74.             }
  75.  
  76.             //Xóa 3 số đầu tiên để tiếp tục 3 số kế
  77.             List_Num.remove(0);
  78.         }
  79.  
  80.         return kq;
  81.     }
  82.  
  83.     public static ArrayList<String> read_3num(String a) {
  84.         ArrayList<String> kq = new ArrayList<String>();
  85.         int num = -1;
  86.         try {
  87.             num = Integer.parseInt(a);
  88.         } catch (Exception ex) {
  89.         }
  90.         if (num == 0) {
  91.             return kq;
  92.         }
  93.  
  94.         int hang_tram = -1;
  95.         try {
  96.             hang_tram = Integer.parseInt(a.substring(0, 1));
  97.         } catch (Exception ex) {
  98.         }
  99.         int hang_chuc = -1;
  100.         try {
  101.             hang_chuc = Integer.parseInt(a.substring(1, 2));
  102.         } catch (Exception ex) {
  103.         }
  104.         int hang_dv = -1;
  105.         try {
  106.             hang_dv = Integer.parseInt(a.substring(2, 3));
  107.         } catch (Exception ex) {
  108.         }
  109.  
  110.         //xét hàng trăm
  111.         if (hang_tram != -1) {
  112.             kq.add(number[hang_tram]);
  113.             kq.add(TRAM);
  114.         }
  115.  
  116.         //xét hàng chục
  117.         switch (hang_chuc) {
  118.             case -1:
  119.                 break;
  120.             case 1:
  121.                 kq.add(MUOIF);
  122.                 break;
  123.             case 0:
  124.                 if (hang_dv != 0) {
  125.                     kq.add(LE);
  126.                 }
  127.                 break;
  128.             default:
  129.                 kq.add(number[hang_chuc]);
  130.                 kq.add(MUOI);
  131.                 break;
  132.         }
  133.  
  134.         //xét hàng đơn vị
  135.         switch (hang_dv) {
  136.             case -1:
  137.                 break;
  138.             case 1:
  139.                 if ((hang_chuc != 0) && (hang_chuc != 1) && (hang_chuc != -1)) {
  140.                     kq.add(MOTS);
  141.                 } else {
  142.                     kq.add(number[hang_dv]);
  143.                 }
  144.                 break;
  145.             case 5:
  146.                 if ((hang_chuc != 0) && (hang_chuc != -1)) {
  147.                     kq.add(LAM);
  148.                 } else {
  149.                     kq.add(number[hang_dv]);
  150.                 }
  151.                 break;
  152.             case 0:
  153.                 if (kq.isEmpty()) {
  154.                     kq.add(number[hang_dv]);
  155.                 }
  156.                 break;
  157.             default:
  158.                 kq.add(number[hang_dv]);
  159.                 break;
  160.         }
  161.         return kq;
  162.     }
  163.  
  164.     public static ArrayList<String> Split(String str, int chunkSize) {
  165.         int du = str.length() % chunkSize;
  166.         //Nếu độ dài chuổi không phải bội số của chunkSize thì thêm # vào trước cho đủ.
  167.         if (du != 0) {
  168.             for (int i = 0; i < (chunkSize - du); i++) {
  169.                 str = "#" + str;
  170.             }
  171.         }
  172.         return splitStringEvery(str, chunkSize);
  173.     }
  174.  
  175.     //Hàm cắt chuổi ra thành chuổi nhỏ
  176.     public static ArrayList<String> splitStringEvery(String s, int interval) {
  177.         ArrayList<String> arrList = new ArrayList<String>();
  178.         int arrayLength = (int) Math.ceil(((s.length() / (double) interval)));
  179.         String[] result = new String[arrayLength];
  180.         int j = 0;
  181.         int lastIndex = result.length - 1;
  182.         for (int i = 0; i < lastIndex; i++) {
  183.             result[i] = s.substring(j, j + interval);
  184.             j += interval;
  185.         }
  186.         result[lastIndex] = s.substring(j);
  187.  
  188.         arrList.addAll(Arrays.asList(result));
  189.         return arrList;
  190.     }
  191.  
  192.     public static int nhap() {
  193.         Scanner input = new Scanner(System.in);
  194.         boolean check = false;
  195.         int n = 0;
  196.         while (!check) {
  197.             System.out.print(" ");
  198.             try {
  199.                 n = input.nextInt();
  200.                 check = true;
  201.  
  202.             } catch (Exception e) {
  203.                 System.out.print("Bạn hãy nhập số: ");
  204.                 input.nextLine();
  205.             }
  206.         }
  207.         return (n);
  208.     }
  209.  
  210.     public static String xuLy() {
  211.         System.out.print("Nhập số: ");
  212.         String xuLy = String.valueOf(nhap());
  213.         ArrayList<String> kq = readNum(xuLy);
  214.         String ketQua = "";
  215.         for (String n : kq) {
  216.             ketQua += n + " ";
  217.         }
  218.         return ketQua;
  219.     }
  220.  
  221.     public static void main(String[] args) {
  222.         System.out.print("Đọc số: " + xuLy());
  223.     }
  224.  
  225.     //nguồn: http://diendan.congdongcviet.com/threads/t220383::chuong-trinh-doc-so-thanh-chu-doc-moi-so-thanh-chu-de-tinh-tien.cpp
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement