我是C的新手,我在课堂上(在线)做得很好,直到最后几个课程。他们变得越来越复杂,我不是一个战略家。但是,我正在努力,我很沮丧。
我一直在研究这个问题(它已经过期了)我的最终目标是将一个数字转换为薪水的单词。
我在这里尝试做的是将double值转换为int并将int放入char数组中,这样最终“数组量”将会读取(如果支付为$ 1234.56)001234。然后我在想我可以做每个位置的ifs或case(hundThous = 0,tenThous = 0,Thous = 1等等)来转换它。我被困在这里,但需要帮助。
如何将值1234放入char数组?
另外,在上面的函数中,我称之为“checkWriter(money);” ,钱是双倍的。那是对的吗?我只是想让它调用这个函数来将转换后的double打印成单词。
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;
}
非常感谢帮助。谢谢!!!