Xin chào các bạn nhé! Thật ra mình mới làm xong 1 bài tập là viết 1 chương trình có thể đọc được 1 số có tối đa 9 chữ số (còn số âm thì nó vẫn đọc ra số nguyên dương). Do hơi dài nên mình hỏi ý kiến các bạn xem chương trình mình có lỗi hay lổ hổng nào không nhé
#include <stdio.h>// *Important: Variable " const "
#include <limits.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <Windows.h>
int main()
{
int x;
printf("Type a number: ");
scanf("%d", &x);
int digit = log10((float)x) + 1;
if (digit > 9)
printf("Can not handle\n");
else
{
// Xet 9 chu so
if (digit == 9)
{
int temp = x / 100000000;
if (temp == 1)
printf("Mot tram ");
else if (temp == 2)
printf("Hai tram ");
else if (temp == 3)
printf("Ba tram ");
else if (temp == 4)
printf("Bon tram ");
else if (temp == 5)
printf("Nam tram ");
else if (temp == 6)
printf("Sau tram ");
else if (temp == 7)
printf("Bay tram ");
else if (temp == 8)
printf("Tam tram ");
else if (temp == 9)
printf("Chin tram ");
digit--;
x %= 100000000;
}
// Xet 8 chu so
if (digit == 8)
{
int temp = x / 10000000;
if (temp == 1)
printf("muoi ");
else if (temp == 2)
printf("hai muoi ");
else if (temp == 3)
printf("ba muoi ");
else if (temp == 4)
printf("bon muoi ");
else if (temp == 5)
printf("nam muoi ");
else if (temp == 6)
printf("sau muoi ");
else if (temp == 7)
printf("bay muoi ");
else if (temp == 8)
printf("tam muoi ");
else if (temp == 9)
printf("chin muoi ");
digit--;
x %= 10000000;
}
// Xet 7 chu so
if (digit == 7)
{
int temp = x / 1000000;
if (temp == 1)
printf("mot trieu ");
else if (temp == 2)
printf("hai trieu ");
else if (temp == 3)
printf("ba trieu ");
else if (temp == 4)
printf("bon trieu ");
else if (temp == 5)
printf("nam trieu ");
else if (temp == 6)
printf("sau trieu ");
else if (temp == 7)
printf("bay trieu ");
else if (temp == 8)
printf("tam trieu ");
else if (temp == 9)
printf("chin trieu ");
digit--;
x %= 1000000;
}
// Xet 6 chu so
if (digit == 6)
{
int temp = x / 100000;
if (temp == 1)
printf("mot tram ");
else if (temp == 2)
printf("hai tram ");
else if (temp == 3)
printf("ba tram ");
else if (temp == 4)
printf("bon tram ");
else if (temp == 5)
printf("nam tram ");
else if (temp == 6)
printf("sau tram ");
else if (temp == 7)
printf("bay tram ");
else if (temp == 8)
printf("tam tram ");
else if (temp == 9)
printf("chin tram ");
digit--;
x %= 100000;
}
// Xet 5 chu so
if (digit == 5)
{
int temp = x / 10000;
if (temp == 1)
printf("muoi ");
else if (temp == 2)
printf("hai muoi ");
else if (temp == 3)
printf("ba muoi ");
else if (temp == 4)
printf("bon muoi ");
else if (temp == 5)
printf("nam muoi ");
else if (temp == 6)
printf("sau muoi ");
else if (temp == 7)
printf("bay muoi ");
else if (temp == 8)
printf("tam muoi ");
else if (temp == 9)
printf("chin muoi ");
digit--;
x %= 10000;
}
// Xet 4 chu so
if (digit == 4)
{
int temp = x / 1000;
if (temp == 1)
printf("mot ngan ");
else if (temp == 2)
printf("hai ngan ");
else if (temp == 3)
printf("ba ngan ");
else if (temp == 4)
printf("bon ngan ");
else if (temp == 5)
printf("nam ngan ");
else if (temp == 6)
printf("sau ngan ");
else if (temp == 7)
printf("bay ngan ");
else if (temp == 8)
printf("tam ngan ");
else if (temp == 9)
printf("chin ngan ");
digit--;
x %= 1000;
}
//Xet 3 chu so
if (digit == 3)
{
int temp = x / 100;
if (temp == 1)
printf("mot tram ");
else if (temp == 2)
printf("hai tram ");
else if (temp == 3)
printf("ba tram ");
else if (temp == 4)
printf("bon tram ");
else if (temp == 5)
printf("nam tram ");
else if (temp == 6)
printf("sau tram ");
else if (temp == 7)
printf("bay tram ");
else if (temp == 8)
printf("tam tram ");
else if (temp == 9)
printf("chin tram ");
digit--;
x %= 100;
}
// Xet 2 chu so
if (digit == 2)
{
int temp = x / 10;
if (temp == 1)
printf("muoi ");
else if (temp == 2)
printf("hai muoi ");
else if (temp == 3)
printf("ba muoi ");
else if (temp == 4)
printf("bon muoi ");
else if (temp == 5)
printf("nam muoi ");
else if (temp == 6)
printf("sau muoi ");
else if (temp == 7)
printf("bay muoi ");
else if (temp == 8)
printf("tam muoi ");
else if (temp == 9)
printf("chin muoi ");
digit--;
}
// Xet 1 chu so
if (digit == 1)
{
int temp = x % 10;
if (temp == 1)
printf("mot");
else if (temp == 2)
printf("hai");
else if (temp == 3)
printf("ba");
else if (temp == 4)
printf("bon");
else if (temp == 5)
printf("lam");
else if (temp == 6)
printf("sau");
else if (temp == 7)
printf("bay");
else if (temp == 8)
printf("tam");
else if (temp == 9)
printf("chin");
}
}
getch();
return 0;
}
Cảm ơn nhiều nhé!