【发布时间】:2012-04-13 19:37:32
【问题描述】:
我是 C 的新手,我在课堂(在线)上表现还不错,直到最后几个程序。他们变得越来越复杂,我不是一个战略家。但是,我正在努力,我很沮丧。
我已经研究这个有一段时间了(它已经过期了),我的终极目标是将一个数字转换成单词来支付薪水。
我在这里要做的是将双精度值转换为 int 并将 int 放入 char 数组中,这样最终“数组金额”将读取(如果 pay 是 $1234.56)001234。然后我在想我可以为每个位置(hundThous = 0、tenThous = 0、Tous = 1 等)做 ifs 或 case 来转换它。不过,我被困在这里,需要帮助。
如何将值 1234 放入 char 数组中?
另外,在上面的函数中,我调用了“checkWriter(money);” ,其中钱是双倍的。那是对的吗?我只是希望它调用这个函数来将转换后的双精度字打印出来。
void checkWriter(double z)
{
double v;
int w, y, cents;
int b, c, x, length;
char array[SIZE];
char amount[SIZE]; /*size = 7, our largest value will be in the hundred thousands*/
v = 100 * z; /*make z a whole number*/
w = ((int)v); /*convert z to an integer w*/
cents = w % 100; /*cents equals the remainder of w/100*/
y = (w - cents)/100; /*y equals the converted integer minus cents, divided by 100*/
sprintf(array, "%-6d", y); /* ATTEMPTING TO PUT y INTO array (saw this on google) */
printf("%s\n\n", array); /* Just wanted to see if it worked. It didn't. I got -2big
number.*/
length = strnlen(array); /*find length of the value in array*/
array[length] = '\0'; /* affix terminating null character to array */
b = SIZE - length - 1; /* b is amount of zeroes needed */
for(c = 0; c < length - 1; c++) { /* loops, plugging zeroes in amount until b=c,
then attaches array to amount */
if(b == c) {
amount[c] = '\0';
strcat(amount, array);
}
else {
amount[c] = '0';
}
}
printf("%s\n\n", amount); /* Checking to see if it worked. Nope. All zeroes. And
sometimes extra symbols at the end*/
return;
}
非常感谢帮助。谢谢!!!
【问题讨论】: