一道编程题目不会做,求解答!!!!!
题目如下:编写一个程序,从键盘上读入一个小于1000的数字,然后创建并输出一个字符串,说明该整数的值。例如,输入941,程序产生的字符串是“Ninehundredandf...
题目如下:编写一个程序,从键盘上读入一个小于1000的数字,然后创建并输出一个字符串,说明该整数的值。例如,输入941,程序产生的字符串是“Nine hundred and forty one"。
然后书本给出的答案是:
/* Exercise 6.1 Convert an integer to words */
#include <stdio.h>
#include <string.h>
int main(void)
{
char *unit_words[] = {"zero", "one","two","three","four","five","six","seven","eight","nine"};
char *teen_words[] = {"ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char *ten_words[] = {"error", "error","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char hundred[] = " hundred";
char and[] = " and ";
char value_str[50] = "";
int value = 0; /* Integer to be converted */
int digits[] = {0,0,0}; /* Stores digits of value entered */
int i = 0;
printf("Enter a positive integer less than 1000: ");
scanf("%d",&value);
if(value >= 1000)
value = 999;
else if(value < 1)
value = 1;
while(value > 0)
{
digits[i++] = value%10;
value /= 10;
}
if(digits[2] > 0)
{
strcat(strcat(value_str,unit_words[digits[2]]), hundred);
if(digits[1]>0 || digits[0]>0)
strcat(value_str, and);
}
if(digits[1] > 0)
{
if(digits[1] == 1)
strcat(value_str,teen_words[digits[0]]);
else
{
strcat(value_str,ten_words[digits[1]]);
if(digits[0] > 0)
strcat(strcat(value_str, " "), unit_words[digits[0]]);
}
}
else
if(digits[0] > 0)
strcat(value_str, unit_words[digits[0]]);
printf("\n%s\n", value_str);
return 0;
}
然而我用C-Free编译了这一段代码,却出现了如下的画面:
书本到底哪里出错了?为什么要这样写?char *unit_words[]是什么意思? 展开
然后书本给出的答案是:
/* Exercise 6.1 Convert an integer to words */
#include <stdio.h>
#include <string.h>
int main(void)
{
char *unit_words[] = {"zero", "one","two","three","four","five","six","seven","eight","nine"};
char *teen_words[] = {"ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char *ten_words[] = {"error", "error","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char hundred[] = " hundred";
char and[] = " and ";
char value_str[50] = "";
int value = 0; /* Integer to be converted */
int digits[] = {0,0,0}; /* Stores digits of value entered */
int i = 0;
printf("Enter a positive integer less than 1000: ");
scanf("%d",&value);
if(value >= 1000)
value = 999;
else if(value < 1)
value = 1;
while(value > 0)
{
digits[i++] = value%10;
value /= 10;
}
if(digits[2] > 0)
{
strcat(strcat(value_str,unit_words[digits[2]]), hundred);
if(digits[1]>0 || digits[0]>0)
strcat(value_str, and);
}
if(digits[1] > 0)
{
if(digits[1] == 1)
strcat(value_str,teen_words[digits[0]]);
else
{
strcat(value_str,ten_words[digits[1]]);
if(digits[0] > 0)
strcat(strcat(value_str, " "), unit_words[digits[0]]);
}
}
else
if(digits[0] > 0)
strcat(value_str, unit_words[digits[0]]);
printf("\n%s\n", value_str);
return 0;
}
然而我用C-Free编译了这一段代码,却出现了如下的画面:
书本到底哪里出错了?为什么要这样写?char *unit_words[]是什么意思? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询