Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #define Max 101
- #define printp printf(" "); //in khoang trang
- #define printd printf("\n");//xuong dong
- #define printdonvi printf(" (VND)");
- //co the thay VND bang don vi tien te khac (i.e USD, EUR)
- void printc(char c); //Ham in ra chu tuong ung voi so
- void num2vitext(char s[], int n); //Ham chuyen so thanh chu (Viet khong dau)
- int chuanhoa(char s[]);
- int bang0(char s[]);
- int main()
- {
- char t[Max];
- char ex = 'Y';
- while (ex != 27)
- {
- system("cls");
- printf("Nhap so tien (dang so): ");
- fflush(stdin);
- gets(t);
- printf("========================================\n");
- if (!chuanhoa(t))
- printf("Du lieu khong hop le!");
- else
- {
- printf("So tien (dang chu) la: ");
- num2vitext(t, strlen(t));
- printdonvi;
- }
- printf("\n========================================\n");
- printf("Nhan ESC de thoat, Enter de tiep tuc: ");
- ex = getch();
- }
- return 0;
- }
- void printc(char c)
- {
- switch (c)
- {
- case '0': printf("khong"); break;
- case '1': printf("mot"); break; //laf mootj chuws khoong phair moots
- case '2': printf("hai"); break;
- case '3': printf("ba"); break;
- case '4': printf("bon"); break;
- case '5': printf("nam"); break;
- case '6': printf("sau"); break;
- case '7': printf("bay"); break;
- case '8': printf("tam"); break;
- case '9': printf("chin"); break;
- }
- return;
- }
- void num2vitext(char s[], int n)
- {
- //Neu chi co mot chu so, in ra binh thuong
- if (n == 1)
- {
- printc(s[0]);
- return;
- }
- //Neu co 2 chu so
- if (n == 2)
- {
- if (s[0] == '1') printf("muoi");
- else
- {
- printc(s[0]);
- printf(" muoi");
- }
- //---------------------------------
- if (s[1] == '0') return;
- else
- if (s[1] == '5') printf(" lam");
- else
- if (s[1] == '4')
- {
- if (s[0] >= '2') printf(" tu");
- else
- {
- printp;
- printc(s[1]);
- }
- }
- else
- if (s[1] == '1')
- {
- if (s[0] == '1')
- {
- printp;
- printc(s[1]);
- }
- else printf(" mot"); //moots
- }
- else
- {
- printp;
- printc(s[1]);
- }
- return;
- }
- //Neu co 3 chu so
- if (n == 3)
- {
- printc(s[0]);
- printf(" tram");
- //----------------------------------------
- if (s[1] == '0')
- {
- if (s[2] == '0') return;
- else
- {
- printf(" le ");
- printc(s[2]);
- }
- }
- else
- {
- char s0[3];
- memmove(s0, s+1, 2);
- printp;
- num2vitext(s0, 2);
- }
- return;
- }
- //Neu tu 10 chu so tro len, tach ra thanh cac nhom co 9 so
- if (n > 9)
- {
- char s1[n - 9 + 1], s2[9 + 1];
- memmove(s1, s, n - 9);
- memmove(s2, s + n - 9, 9);
- num2vitext(s1, n-9);
- printf(" ti ");
- num2vitext(s2, 9);
- return;
- }
- if (n > 3)
- {
- char s3[4], s4[7];
- if (n > 6)
- {
- memmove(s3, s, n-6);
- memmove(s4, s+n-6, 6);
- if (bang0(s3));
- else
- {
- num2vitext(s3, n-6);
- printf(" trieu");
- }
- if (!bang0(s4))
- {
- printp;
- num2vitext(s4, 6);
- }
- }
- if (n <= 6)
- {
- memmove(s3, s, n-3);
- memmove(s4, s+n-3, 3);
- if (bang0(s3));
- else
- {
- num2vitext(s3, n-3);
- printf(" nghin");
- }
- if (!bang0(s4))
- {
- printp;
- num2vitext(s4, 3);
- }
- }
- return;
- }
- else
- printf("\nCo loi xay ra!?");
- return;
- }
- int chuanhoa(char s[])
- {
- if (s[0] == '\0') return 0;
- if (s[0] == '0') return 0;
- int i;
- for (i = 0; i < strlen(s); i++)
- if ((s[i] <48) || (s[i] > 57)) return 0;
- return 1;
- }
- int bang0(char s[])
- {
- int i;
- for (i = 0; i < strlen(s); i++)
- if (s[i] != '0') return 0;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement