Advertisement
soosle

Đổi số sang chữ

Mar 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #define Max 101
  5. #define printp printf(" "); //in khoang trang
  6. #define printd printf("\n");//xuong dong
  7. #define printdonvi printf(" (VND)");
  8. //co the thay VND bang don vi tien te khac (i.e USD, EUR)
  9.  
  10.  
  11. void printc(char c); //Ham in ra chu tuong ung voi so
  12. void num2vitext(char s[], int n); //Ham chuyen so thanh chu (Viet khong dau)
  13. int chuanhoa(char s[]);
  14. int bang0(char s[]);
  15.  
  16.  
  17. int main()
  18. {
  19.     char t[Max];
  20.     char ex = 'Y';
  21.    
  22.     while (ex != 27)
  23.     {
  24.         system("cls");
  25.         printf("Nhap so tien (dang so): ");
  26.         fflush(stdin);
  27.         gets(t);
  28.         printf("========================================\n");
  29.         if (!chuanhoa(t))
  30.             printf("Du lieu khong hop le!");
  31.         else
  32.         {
  33.             printf("So tien (dang chu) la: ");
  34.             num2vitext(t, strlen(t));
  35.             printdonvi;
  36.         }
  37.         printf("\n========================================\n");
  38.         printf("Nhan ESC de thoat, Enter de tiep tuc: ");
  39.         ex = getch();
  40.     }
  41.     return 0;
  42. }
  43.  
  44. void printc(char c)
  45. {
  46.     switch (c)
  47.     {
  48.         case '0': printf("khong"); break;
  49.         case '1': printf("mot"); break; //laf mootj chuws khoong phair moots
  50.         case '2': printf("hai"); break;
  51.         case '3': printf("ba"); break;
  52.         case '4': printf("bon"); break;
  53.         case '5': printf("nam"); break;
  54.         case '6': printf("sau"); break;
  55.         case '7': printf("bay"); break;
  56.         case '8': printf("tam"); break;
  57.         case '9': printf("chin"); break;   
  58.     }
  59.     return;
  60. }
  61.  
  62. void num2vitext(char s[], int n)
  63. {
  64.     //Neu chi co mot chu so, in ra binh thuong
  65.     if (n == 1)
  66.     {
  67.         printc(s[0]);
  68.         return;
  69.     }
  70.     //Neu co 2 chu so
  71.     if (n == 2)
  72.     {
  73.         if (s[0] == '1') printf("muoi");
  74.         else
  75.         {
  76.             printc(s[0]);
  77.             printf(" muoi");
  78.         }
  79.         //---------------------------------
  80.         if (s[1] == '0') return;
  81.         else
  82.         if (s[1] == '5') printf(" lam");
  83.         else
  84.         if (s[1] == '4')
  85.         {
  86.             if (s[0] >= '2') printf(" tu");
  87.             else
  88.             {
  89.                 printp;
  90.                 printc(s[1]);
  91.             }
  92.         }
  93.         else
  94.         if (s[1] == '1')
  95.         {
  96.             if (s[0] == '1')
  97.             {
  98.                 printp;
  99.                 printc(s[1]);
  100.             }
  101.             else printf(" mot"); //moots
  102.         }
  103.         else
  104.         {
  105.             printp;
  106.             printc(s[1]);
  107.         }
  108.         return;
  109.     }
  110.     //Neu co 3 chu so
  111.     if (n == 3)
  112.     {
  113.         printc(s[0]);
  114.        
  115.         printf(" tram");
  116.         //----------------------------------------
  117.         if (s[1] == '0')
  118.         {
  119.             if (s[2] == '0') return;
  120.             else
  121.             {
  122.                 printf(" le ");
  123.                 printc(s[2]);
  124.             }
  125.         }
  126.         else
  127.         {
  128.             char s0[3];
  129.             memmove(s0, s+1, 2);
  130.             printp;
  131.             num2vitext(s0, 2);
  132.         }
  133.         return;
  134.     }
  135.     //Neu tu 10 chu so tro len, tach ra thanh cac nhom co 9 so
  136.     if (n > 9)
  137.     {
  138.         char s1[n - 9 + 1], s2[9 + 1];
  139.         memmove(s1, s, n - 9);
  140.         memmove(s2, s + n - 9, 9);
  141.         num2vitext(s1, n-9);
  142.         printf(" ti ");
  143.         num2vitext(s2, 9);
  144.         return;
  145.     }
  146.    
  147.     if (n > 3)
  148.     {
  149.         char s3[4], s4[7];
  150.         if (n > 6)
  151.         {
  152.             memmove(s3, s, n-6);
  153.             memmove(s4, s+n-6, 6);
  154.             if (bang0(s3));
  155.             else
  156.             {
  157.                 num2vitext(s3, n-6);
  158.                 printf(" trieu");
  159.             }
  160.             if (!bang0(s4))
  161.             {
  162.                 printp;
  163.                 num2vitext(s4, 6);
  164.             }
  165.         }
  166.        
  167.         if (n <= 6)
  168.         {
  169.             memmove(s3, s, n-3);
  170.             memmove(s4, s+n-3, 3);
  171.             if (bang0(s3));
  172.             else
  173.             {
  174.                 num2vitext(s3, n-3);
  175.                 printf(" nghin");
  176.             }
  177.             if (!bang0(s4))
  178.             {
  179.                 printp;
  180.                 num2vitext(s4, 3);
  181.             }
  182.         }
  183.         return;
  184.     }
  185.     else
  186.     printf("\nCo loi xay ra!?");
  187.     return;
  188. }
  189. int chuanhoa(char s[])
  190. {
  191.     if (s[0] == '\0') return 0;
  192.     if (s[0] == '0') return 0;
  193.     int i;
  194.     for (i = 0; i < strlen(s); i++)
  195.         if ((s[i] <48) || (s[i] > 57)) return 0;
  196.        
  197.     return 1;
  198. }
  199.  
  200. int bang0(char s[])
  201. {
  202.     int i;
  203.     for (i = 0; i < strlen(s); i++)
  204.         if (s[i] != '0') return 0;
  205.     return 1;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement