C-舍入问题(CS50) [英] C - Rounding issues (CS50)
问题描述
我已经在谷歌上搜索了好几天了,我迷路了.因此,在线进行CS50似乎无法解决这一数字舍入问题.我的程序搞砸了将 2.10 之类的浮点数与 100 之类的整数相乘,它会输出 209.xxxxxxxx
I have been Googling this for days now and I am lost. So doing CS50 online and can't seem to get a handle on this rounding of numbers. My program is messing up multiplying floats like 2.10 with integers like 100 it would output 209.xxxxxxxx
现在,就像我说我已经阅读了许多关于应该使用 ceilf 并包含 的帖子一样,但是我遇到了错误
Now like I say I have read countless posts on that I should use ceilf and include but I am getting an error
greedy.c :(.text + 0x74):对'ceilf'的未定义引用 collect2:错误:ld返回1退出状态make:*** [贪婪]错误1 亚当@贝多芬:〜/projects/atom/edx/pSet1/greedy $
greedy.c:(.text+0x74): undefined reference to `ceilf' collect2: error: ld returned 1 exit status make: *** [greedy] Error 1 adam@beethoven:~/projects/atom/edx/pSet1/greedy$
我看过有关-lm和某个文件的帖子,但是老实说我不明白这是什么意思.
I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.
我绝不是在寻找一种彻底的解决方案,而只是寻求改进的指导.
I am in no way looking for an outright solution, just guidance in improving.
这是我的代码,可能不像某些人希望的那样精简,但我在这里回到了基础;)
Here is my code, probably not as streamlined as some would like but I am back to basics here ;)
#include <stdio.h>
#include <math.h>
int main() {
// Initialize Variables
int coinsTotal = 0,
quarter = 25,
dime = 10,
nickel = 5,
penny = 1,
cents;
float changeDue;
do {
printf("How much change are you owed? (Format = 0.00)($): ");
scanf("%f", &changeDue );
// Convert to cents
cents = changeDue * 100;
} while(cents <= 0);
while (cents >= quarter) {
cents = cents - quarter;
coinsTotal = coinsTotal + 1;
} if (cents == 0) {
printf("The miminum number of coins is: %d\n", coinsTotal);
} else {
while (cents >= dime) {
cents - dime;
coinsTotal = coinsTotal + 1;
} if (cents == 0) {
printf("The minimum number of coins is: %d\n", coinsTotal);
} else {
while (cents >= nickel) {
cents = cents - nickel;
coinsTotal = coinsTotal + 1;
} if (cents == 0) {
printf("The minimum number of coins is: %d\n", coinsTotal);
} else {
while (cents >= penny) {
cents = cents - penny;
coinsTotal = coinsTotal + 1;
} if (cents == 0) {
printf("The minimum number of coins is: %d\n", coinsTotal);
}
}
}
}
}
基本上,它应该计算出一定数量所需的最少硬币数量.在大多数情况下,它会起作用,直到浮子弄乱为止.打扰一下我喜欢写的笔记,这样我会学得更好.
Basically it should work out the minimum number of coins needed to make a given amount. It works in most cases until the floats mess up. Excuse the notes I like to write what I did so I learn better.
Update尝试使用-lm与GCC进行编译,但仍然失败. 亚当@贝多芬:〜/projects/atom/edx/pSet1/greedy $ gcc -o foo -lm greedy.c/tmp/cc3qHAK7.o:在函数
main': greedy.c:(.text+0x6e): undefined reference to
ceilf'collect2中:错误:ld返回1退出 状态adam @ beethoven:〜/projects/atom/edx/pSet1/greedy $
Update Tried to compile with GCC using -lm but still failed. adam@beethoven:~/projects/atom/edx/pSet1/greedy$ gcc -o foo -lm greedy.c /tmp/cc3qHAK7.o: In function
main': greedy.c:(.text+0x6e): undefined reference to
ceilf' collect2: error: ld returned 1 exit status adam@beethoven:~/projects/atom/edx/pSet1/greedy$
解决方案我没有使用 make 命令,而是使用了gcc并添加了
-lm标志在命令末尾 gcc -o foo greedy.c -lm
SOLUTION Instead of using the make command I used gcc and added
the -lm flag At the end of the command gcc -o foo greedy.c -lm
推荐答案
我看过有关-lm和某个文件的帖子,但是老实说我不明白这是什么意思.
I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.
您必须链接到数学库以纠正错误.数学函数的实现通常放在一个单独的库中,即数学库.如果使用gcc
,请在链接器命令中添加-lm
.
You have to link to the math library to fix the error. Math functions implementations are usually put as a separate library, the math library. If you use gcc
add -lm
to the linker command.
这篇关于C-舍入问题(CS50)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!