[英]C program: Simple math program
Want to print for example Grade: 4/5 80 percent Program ask a user how many math problems they want to solve and prints out the number of "wrongs/the number of rights" and their grade. 例如要打印等级:4/5 80%程序会询问用户他们要解决多少数学问题,并打印出“错误/权利的数量”及其等级。 I think I dont have my math right at the end of the code cause its printing out for example: Grade: 4/5 -7446528 percent
我想我在代码末尾没有正确的数学运算,例如导致打印出来:成绩:4/5 -7446528%
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++
# include <stdio.h>
int main()
{
int NumberOfTimes, AddAns, SubAns, AddCorrect=0, SubCorrect=0, CorrectAnsAdd, CorrectAnsSub, TotalCorrect, TotalWrong, Add;
int i,a,b,c,d,e,f,g;
float percent;
printf("\n");
printf("-------------------MATH QUIZ------------------------\n");
printf("Enter the number of Math problems you want to solve:"); //enters the # of problems the program produces
scanf("%d", &NumberOfTimes);
printf("\n");
srand(time(NULL));
//Random number generator
for (i=0;i<NumberOfTimes;++i)
{
b = rand() %3 + 1;
c = rand() %3 + 1;
a = rand() %2 + 1;
//Random addition problems
if (a == 1)
{
printf("%d + %d = ", b,c);
scanf("%d", &AddAns);
d = b + c;
if (AddAns == d)
{
printf(" +Correct\n");
AddCorrect = AddCorrect + 1;
}
//Random subtraction problems
else
{
printf(" +Wrong, it was %d\n", d);
AddIncorrect = AddIncorrect + 1;
}
}
if (a == 2)
{
printf("%d - %d = ", b,c);
scanf("%d", &SubAns);
g = b - c;
//Produces right or wrong answers
if (SubAns == g)
{
printf(" +Correct\n");
SubCorrect = SubCorrect + 1;
}
else
{
printf(" +Wrong, it was %d\n", g);
SubIncorrect = SubIncorrect + 1;
}
}
}
//Producing the output to wrong/right numbers and grade percentage
TotalCorrect = AddCorrect + SubCorrect;
printf("\n");
printf("Grade: %d/%d\n",TotalCorrect,NumberOfTimes);
printf("\n");
percent=NumberOfTimes/TotalCorrect;
printf("%d percent \n", percent);
return 0;
}
There are a few things going on. 发生了一些事情。 First, to print a
float
with printf
, use %f
. 首先,要使用
printf
打印float
,请使用%f
。 %d
is for int
s. %d
用于int
s。
Secondly, when you calculate percent
, you're accidentally using integer division. 其次,当您计算
percent
,您不小心使用了整数除法。 Since NumberOfTimes
and TotalCorrect
are both integers, NumberOfTimes/TotalCorrect
performs integer division and produces an int
. 由于
NumberOfTimes
和TotalCorrect
都是整数,因此NumberOfTimes/TotalCorrect
执行整数除法并产生一个int
。 It's only converted to a float
after the whole initializing expression is evaluated. 只有在对整个初始化表达式求值之后 ,才将其转换为
float
。 Use this instead: 使用此代替:
percent = (float)TotalCorrect / NumberOfTimes;
// OR, if you want an actual percent:
percent = 100.0f*TotalCorrect/NumberOfTimes;
Then, using %f
: 然后,使用
%f
:
printf("%f percent\n", percent); // "80.000000 percent"
Note that this will display the percentage out to many decimal places; 请注意,这会将百分比显示到许多小数位; if you want a cleaner display without a decimal point, you could just calculate the percent as an
int
: 如果您想要一个没有小数点的更干净的显示,则可以将百分比计算为
int
:
// multiply before dividing to avoid integer division problems
int percent = 100*TotalCorrect/NumberOfTimes;
printf("%d percent\n", percent); // "80 percent"
Hope this helps! 希望这可以帮助!
In C, dividing two integers won't get you a floating point number, even if the assignment is to a float. 在C语言中,即使分配为浮点数,将两个整数相除也不会得到浮点数。 It's just how the language works.
这就是语言的工作方式。 You'll have to cast NumberOfTimes and TotalCorrect as floats.
您必须将NumberOfTimes和TotalCorrect强制转换为浮点数。
So replace percent=NumberOfTimes/TotalCorrect;
因此,替换
percent=NumberOfTimes/TotalCorrect;
with 同
percent=(float)TotalCorrect/(float)NumberOfTimes * 100;
Furthermore, you're trying to print a float as an integer in the line 此外,您尝试在行中将浮点数打印为整数
printf("%d percent \n", percent);
which is giving you a wonky result. 这给你一个奇怪的结果。 Instead, try:
相反,请尝试:
printf("%d percent \n", (int)percent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.