C中的百分比计算问题
Percentage calculation problems in C
我正在制作一个程序,根据条件计算有多少及格分数。但是,当我尝试计算及格分数的百分比时,即 if (passGrade >= 0) { printf("Percent of passing grades are %d! \n", (inputGrade/passGrade)*100); return 0;
程序没有显示结果,而是显示为零,我确实调试了程序,并且 passGrade 是多少我在这一行中输入的总成绩显示了它的值 printf("You have entered %d passing grades! \n", passGrade);
但是当涉及到下一行时,它只显示了一个 -1。我确实在网上查找问题,但没有任何显示。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int inputGrade, passGrade, percentGradePass;
inputGrade = 0;
passGrade = 0;
/* printf("Please type a grade (-1) to exit: ");
scanf("%i", &inputGrade);
printf("You typed: %i \n", inputGrade); */
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
scanf("%d", &inputGrade);
printf("You typed: %d \n", inputGrade);
if (inputGrade == -1)
{
inputGrade = -1;
}
else if (inputGrade >= 70 && inputGrade <= 100) {
passGrade = passGrade + 1;
}
else {
inputGrade = 0;
}
}
printf("You have entered %d passing grades! \n", passGrade);
if (passGrade >= 0) {
printf("Percent of passing grades are %d! \n", (inputGrade/passGrade)*100);
return 0;
}
}
不胜感激。
'the program does not show the result' 是什么意思?
当您的程序达到“(inputGrade/passGrade)*100)”时
1/ 你应该检查 'passGrade' 是否为 0,因为如果它是 '0' 就会出现 run-time 错误。
2/ 而且,由于 while 循环在 'inputGrade == -1' 时结束,所以您需要将其转换为 'plus',然后再除以
3/ 最后,因为 'scanf("%d", &inputGrade);' 表示 inputGrade = 'input' 所以,你不需要在你的 'if' 案例中写 'inputGrade = -1;'
您的代码存在许多问题。有些问题很关键,而另一些则代表“strange/unnecessary”代码。
内联注释示例:
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
scanf("%d", &inputGrade); // ALWAYS, ALWAYS... check the return value of scanf
printf("You typed: %d \n", inputGrade);
if (inputGrade == -1)
{
inputGrade = -1; // Unnecessary! inputGrade is already -1 so no need to
// assign it again
}
else if (inputGrade >= 70 && inputGrade <= 100) {
passGrade = passGrade + 1;
}
else {
inputGrade = 0; // Unnecessary! inputGrade will get a new value from scanf
// in the next loop
}
}
所以你的循环可以简单地是:
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
if (scanf("%d", &inputGrade) != 1)
{
// Invalid input, terminate program
exit(1);
}
printf("You typed: %d \n", inputGrade);
if (inputGrade >= 70 && inputGrade <= 100)
{
passGrade = passGrade + 1;
}
}
另一个潜在的问题是:
(inputGrade/passGrade)*100
因为两个变量的类型都是int
,除法的结果将被截断为最接近的整数,例如6/10 将导致 0 而不是 0.6,整个表达式将因此变为 0。为避免这种情况,您可以先进行乘法(即 (100 *inputGrade)/passGrade
)或使用浮点类型而不是整数类型。
也就是说,你的算法似乎存在基本的误解。你说:
I try calculating the percentage of passing scores
计算你想要的百分比
100 * NumberOfPassingGrades / TotalNumberOfGrades
但这不是你在做什么。不是在计算中使用inputGrade
,而是需要计算成绩的数量并在计算中使用它。
类似于:
int totalGrade = 0;
while (1)
{
printf("Please type a grade (-1) to exit: ");
if (scanf("%d", &inputGrade) != 1)
{
// Invalid input, terminate program
exit(1);
}
if (inputGrade != -1)
{
// Stop the loop
break;
}
printf("You typed: %d \n", inputGrade);
if (inputGrade >= 70 && inputGrade <= 100)
{
passGrade = passGrade + 1;
}
totalGrade = totalGrade + 1;
}
printf("You have entered %d passing grades! \n", passGrade);
printf("You have entered %d grades in total \n", totalGrade);
if (totalGrade > 0)
{
printf("Percent of passing grades are %d! \n", (100 * passGrade)/totalGrade);
}
我正在制作一个程序,根据条件计算有多少及格分数。但是,当我尝试计算及格分数的百分比时,即 if (passGrade >= 0) { printf("Percent of passing grades are %d! \n", (inputGrade/passGrade)*100); return 0;
程序没有显示结果,而是显示为零,我确实调试了程序,并且 passGrade 是多少我在这一行中输入的总成绩显示了它的值 printf("You have entered %d passing grades! \n", passGrade);
但是当涉及到下一行时,它只显示了一个 -1。我确实在网上查找问题,但没有任何显示。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int inputGrade, passGrade, percentGradePass;
inputGrade = 0;
passGrade = 0;
/* printf("Please type a grade (-1) to exit: ");
scanf("%i", &inputGrade);
printf("You typed: %i \n", inputGrade); */
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
scanf("%d", &inputGrade);
printf("You typed: %d \n", inputGrade);
if (inputGrade == -1)
{
inputGrade = -1;
}
else if (inputGrade >= 70 && inputGrade <= 100) {
passGrade = passGrade + 1;
}
else {
inputGrade = 0;
}
}
printf("You have entered %d passing grades! \n", passGrade);
if (passGrade >= 0) {
printf("Percent of passing grades are %d! \n", (inputGrade/passGrade)*100);
return 0;
}
}
不胜感激。
'the program does not show the result' 是什么意思?
当您的程序达到“(inputGrade/passGrade)*100)”时
1/ 你应该检查 'passGrade' 是否为 0,因为如果它是 '0' 就会出现 run-time 错误。
2/ 而且,由于 while 循环在 'inputGrade == -1' 时结束,所以您需要将其转换为 'plus',然后再除以
3/ 最后,因为 'scanf("%d", &inputGrade);' 表示 inputGrade = 'input' 所以,你不需要在你的 'if' 案例中写 'inputGrade = -1;'
您的代码存在许多问题。有些问题很关键,而另一些则代表“strange/unnecessary”代码。
内联注释示例:
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
scanf("%d", &inputGrade); // ALWAYS, ALWAYS... check the return value of scanf
printf("You typed: %d \n", inputGrade);
if (inputGrade == -1)
{
inputGrade = -1; // Unnecessary! inputGrade is already -1 so no need to
// assign it again
}
else if (inputGrade >= 70 && inputGrade <= 100) {
passGrade = passGrade + 1;
}
else {
inputGrade = 0; // Unnecessary! inputGrade will get a new value from scanf
// in the next loop
}
}
所以你的循环可以简单地是:
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
if (scanf("%d", &inputGrade) != 1)
{
// Invalid input, terminate program
exit(1);
}
printf("You typed: %d \n", inputGrade);
if (inputGrade >= 70 && inputGrade <= 100)
{
passGrade = passGrade + 1;
}
}
另一个潜在的问题是:
(inputGrade/passGrade)*100
因为两个变量的类型都是int
,除法的结果将被截断为最接近的整数,例如6/10 将导致 0 而不是 0.6,整个表达式将因此变为 0。为避免这种情况,您可以先进行乘法(即 (100 *inputGrade)/passGrade
)或使用浮点类型而不是整数类型。
也就是说,你的算法似乎存在基本的误解。你说:
I try calculating the percentage of passing scores
计算你想要的百分比
100 * NumberOfPassingGrades / TotalNumberOfGrades
但这不是你在做什么。不是在计算中使用inputGrade
,而是需要计算成绩的数量并在计算中使用它。
类似于:
int totalGrade = 0;
while (1)
{
printf("Please type a grade (-1) to exit: ");
if (scanf("%d", &inputGrade) != 1)
{
// Invalid input, terminate program
exit(1);
}
if (inputGrade != -1)
{
// Stop the loop
break;
}
printf("You typed: %d \n", inputGrade);
if (inputGrade >= 70 && inputGrade <= 100)
{
passGrade = passGrade + 1;
}
totalGrade = totalGrade + 1;
}
printf("You have entered %d passing grades! \n", passGrade);
printf("You have entered %d grades in total \n", totalGrade);
if (totalGrade > 0)
{
printf("Percent of passing grades are %d! \n", (100 * passGrade)/totalGrade);
}