#define __STDC_WANT_LIB_EXT1__ 1//说明:出入数字,输出对应英文
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int main(void)
{
char again=””Y””;
do
{
int num;//数字
char result[100] = “”;//结果
size_t result_len = sizeof(result);
char connect1[10] = ” hundred”;//连接1
char connect2[10] = ” and “;//连接2
char a[10][7] = {
” “,”One”,”Two”,”Three”,”Four”,”Five”,”Six “,”Seven “,”Eight “,”Nine “//百位
};
char b[10][8] = {
” “,” “,”twenty “,”thirty “,”forty “,”fifty “,”sixty “,”seventy “,”eighty “,”ninty “//十位
};
char c[10][7] = {
” “,”one”,”two”,”three”,”four”,”five”,”six “,”seven “,”eight “,”nine “//个位
};
while (true)//检查输入能否在0-1000
{
printf(“\nEnter a number less than 1000.\n”);
scanf(“%d”, &num);
if ((num < 0) || (num>999))
printf(“You are kidding.Enter again.”);
else
break;
}
//形成最终结果
int g = num / 100;
num = num – g * 100;
if (strcat_s(result, result_len, a[g]))
{
printf(“Error”);
return 1;
}
if (a[g][0] != “” “”)
strcat_s(result, result_len, connect1);
if ((num > 0) && (g != 0))
strcat_s(result, result_len, connect2);
g = num / 10;
if (strcat_s(result, result_len, b[g]))
{
printf(“Error”);
return 2;
}
num = num – g * 10;
g = num;
if (strcat_s(result, result_len, c[g]))
{
printf(“Error”);
return 3;
}
printf(“%s”, result);//输出结果
printf(“\nDo you want to try again? Y or N\n”);//判断能否重复操作
scanf(“%c”, &again);
} while (toupper(again) ==””Y””);
getch();
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int main(void)
{
char again=””Y””;
do
{
int num;//数字
char result[100] = “”;//结果
size_t result_len = sizeof(result);
char connect1[10] = ” hundred”;//连接1
char connect2[10] = ” and “;//连接2
char a[10][7] = {
” “,”One”,”Two”,”Three”,”Four”,”Five”,”Six “,”Seven “,”Eight “,”Nine “//百位
};
char b[10][8] = {
” “,” “,”twenty “,”thirty “,”forty “,”fifty “,”sixty “,”seventy “,”eighty “,”ninty “//十位
};
char c[10][7] = {
” “,”one”,”two”,”three”,”four”,”five”,”six “,”seven “,”eight “,”nine “//个位
};
while (true)//检查输入能否在0-1000
{
printf(“\nEnter a number less than 1000.\n”);
scanf(“%d”, &num);
if ((num < 0) || (num>999))
printf(“You are kidding.Enter again.”);
else
break;
}
//形成最终结果
int g = num / 100;
num = num – g * 100;
if (strcat_s(result, result_len, a[g]))
{
printf(“Error”);
return 1;
}
if (a[g][0] != “” “”)
strcat_s(result, result_len, connect1);
if ((num > 0) && (g != 0))
strcat_s(result, result_len, connect2);
g = num / 10;
if (strcat_s(result, result_len, b[g]))
{
printf(“Error”);
return 2;
}
num = num – g * 10;
g = num;
if (strcat_s(result, result_len, c[g]))
{
printf(“Error”);
return 3;
}
printf(“%s”, result);//输出结果
printf(“\nDo you want to try again? Y or N\n”);//判断能否重复操作
scanf(“%c”, &again);
} while (toupper(again) ==””Y””);
getch();
return 0;
}
解决方案:4分
scanf(" %c", &again);//"和%之间添加一个空格?
解决方案:8分
最后的scanf前加一个 getchar(); 接收掉前面输入时留下的换行符
同时char b[10][8] 这里改成char[10][9], 不然”seventy “就溢出了
修改后代码:
同时char b[10][8] 这里改成char[10][9], 不然”seventy “就溢出了
修改后代码:
int main(void) { char again=""Y""; do { int num;//数字 char result[100] = "";//结果 size_t result_len = sizeof(result); char connect1[10] = " hundred";//连接1 char connect2[10] = " and ";//连接2 char a[10][7] = { " ","One","Two","Three","Four","Five","Six ","Seven ","Eight ","Nine "//百位 }; char b[10][9] = { " "," ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninty "//十位 }; char c[10][7] = { " ","one","two","three","four","five","six ","seven ","eight ","nine "//个位 }; while (true)//检查输入能否在0-1000 { printf("\nEnter a number less than 1000.\n"); scanf("%d", &num); if ((num < 0) || (num>999)) printf("You are kidding.Enter again."); else break; } //形成最终结果 int g = num / 100; num = num - g * 100; if (strcat_s(result, result_len, a[g])) { printf("Error"); return 1; } if (a[g][0] != "" "") strcat_s(result, result_len, connect1); if ((num > 0) && (g != 0)) strcat_s(result, result_len, connect2); g = num / 10; if (strcat_s(result, result_len, b[g])) { printf("Error"); return 2; } num = num - g * 10; g = num; if (strcat_s(result, result_len, c[g])) { printf("Error"); return 3; } printf("%s", result);//输出结果 printf("\nDo you want to try again? Y or N\n");//判断能否重复操作 getchar(); scanf("%c", &again); } while (toupper(again) ==""Y""); getch(); return 0; }
解决方案:8分
scanf 读到空格会暂停,后面的就回留到缓冲区。
scanf 读到enter会暂停,enter留在缓冲区,假如后面还有定义的变量,会赋值给后面的变量。
ag.
scanf(“%d”,&a);
scanf(“%c”,&c);
输入a回车,c就会是回车字符
scanf 读到enter会暂停,enter留在缓冲区,假如后面还有定义的变量,会赋值给后面的变量。
ag.
scanf(“%d”,&a);
scanf(“%c”,&c);
输入a回车,c就会是回车字符