C - 舍入问题 (CS50)
C - Rounding issues (CS50)
我已经在谷歌上搜索了好几天了,但我迷路了。所以在网上做 CS50 似乎无法处理这四舍五入的数字。我的程序搞砸了像 2.10 这样的浮点数与像 100 这样的整数相乘,它会输出 209.xxxxxxxx
现在就像我说的那样,我已经阅读了无数关于我应该使用 ceilf 并包含 的帖子,但我收到了一个错误
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 和某个文件的帖子,但老实说,我不明白它的意思。
我绝不是在寻找一个彻底的解决方案,只是在寻找改进的指导。
这是我的代码,可能不像某些人希望的那样精简,但我在这里回归基础 ;)
#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);
}
}
}
}
}
基本上它应该计算出达到给定数量所需的最少硬币数量。它在大多数情况下都有效,直到浮标弄乱为止。原谅我喜欢写笔记所以学得更好
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$
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
I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.
您必须link到数学图书馆来修正错误。数学函数的实现通常放在一个单独的库中,即数学库。如果您使用 gcc
添加 -lm
到 linker 命令。
我猜你想将浮点数四舍五入到最接近的整数。 ceilf
不是那样做,而是四舍五入。如果可能,您可以使用此宏四舍五入到最接近的长度:
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
此外,您会收到一个 linking 错误,因为您没有 link 数学库。例如使用:
gcc greedy.c -lm -o greedy
我已经在谷歌上搜索了好几天了,但我迷路了。所以在网上做 CS50 似乎无法处理这四舍五入的数字。我的程序搞砸了像 2.10 这样的浮点数与像 100 这样的整数相乘,它会输出 209.xxxxxxxx
现在就像我说的那样,我已经阅读了无数关于我应该使用 ceilf 并包含 的帖子,但我收到了一个错误
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 和某个文件的帖子,但老实说,我不明白它的意思。
我绝不是在寻找一个彻底的解决方案,只是在寻找改进的指导。
这是我的代码,可能不像某些人希望的那样精简,但我在这里回归基础 ;)
#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);
}
}
}
}
}
基本上它应该计算出达到给定数量所需的最少硬币数量。它在大多数情况下都有效,直到浮标弄乱为止。原谅我喜欢写笔记所以学得更好
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$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
I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.
您必须link到数学图书馆来修正错误。数学函数的实现通常放在一个单独的库中,即数学库。如果您使用 gcc
添加 -lm
到 linker 命令。
我猜你想将浮点数四舍五入到最接近的整数。 ceilf
不是那样做,而是四舍五入。如果可能,您可以使用此宏四舍五入到最接近的长度:
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
此外,您会收到一个 linking 错误,因为您没有 link 数学库。例如使用:
gcc greedy.c -lm -o greedy