如何在DEV C+中解决这个问题百分比计算?
我在 DEV C+ 中遇到有关百分比计算的问题。
我想展示程序以显示百分比差异。
程序总是给我零,而不是给出计算出的答案。
这是我的代码。
#include "stdio.h"
#include <string>
using namespace std;
int main()
{
int book1, baht1, book2, baht2, book3, baht3, book4, baht4, ova1, book5, baht5, book6, baht6, book7, baht7, book8, baht8, ova2, Delta, Zeta;
printf("\nPlease enter monthly plan order.");
printf("\n\n\n\nPlease enter the number of 'The Devotion of Suspect X' = ");
scanf("%d", &book1);
baht1 = book1 * 185;
printf("\n\nPlease enter the number of 'Norwegian Wood' = ");
scanf("%d", &book2);
baht2 = book2 * 200;
printf("\n\nPlease enter the number of 'Beautiful World, Where Are You' = ");
scanf("%d", &book3);
baht3 = book3 * 250;
printf("\n\nPlease enter the number of 'Fascism of Love and Fantasy' = ");
scanf("%d", &book4);
baht4 = book4 * 450;
printf("\n\n_____________________________\n");
printf("\n\nPlease enter this month's total sales.");
printf("\n\n\n\nPlease enter the number of 'The Devotion of Suspect X' = ");
scanf("%d", &book5);
baht5 = book5 * 225;
printf("\n\nPlease enter the number of 'Norwegian Wood' = ");
scanf("%d", &book6);
baht6 = book6 * 325;
printf("\n\nPlease enter the number of 'Beautiful World, Where Are You' = ");
scanf("%d", &book7);
baht7 = book7 * 385;
printf("\n\nPlease enter the number of 'Fascism of Love and Fantasy' = ");
scanf("%d", &book8);
baht8 = book8 * 600;
printf("\n\n_____________________________\n");
printf("\n_____________________________\n");
printf("\n\nAlright.");
printf("\n\nLet's see your work.");
ova1 = (baht1 + baht2 + baht3 + baht4);
printf("\n\n\nBudget Price = %d Bahts\n", ova1);
ova2 = (baht5 + baht6 + baht7 + baht8);
printf("\n\nMonthly Sales Price = %d Bahts\n", ova2);
Delta = (ova2 - ova1);
if (Delta >0)
printf("\n\n\nYour profit : %d Bahts\n", Delta);
else
printf("\n\n\nYour loss : %d Bahts\n", Delta);
Zeta = ((Delta)/(ova1) * 100;
if (Zeta >0)
printf("\n\nProfit were = %f percents\n", Zeta);
else
printf("\n\nLoss were = %f percents\n", Zeta);
return 0;
}
我的代码有什么问题吗?
我英语和编程都不擅长。
抱歉我的英语很蹩脚,还有任何语法错误。
感谢您的帮助(如果有的话)。
I've got problem about percentage calculation in DEV C+.
I want to show the program to show the difference in percentage.
Instead of giving the calculated answer, the programs always give me zero.
Here's my code.
#include "stdio.h"
#include <string>
using namespace std;
int main()
{
int book1, baht1, book2, baht2, book3, baht3, book4, baht4, ova1, book5, baht5, book6, baht6, book7, baht7, book8, baht8, ova2, Delta, Zeta;
printf("\nPlease enter monthly plan order.");
printf("\n\n\n\nPlease enter the number of 'The Devotion of Suspect X' = ");
scanf("%d", &book1);
baht1 = book1 * 185;
printf("\n\nPlease enter the number of 'Norwegian Wood' = ");
scanf("%d", &book2);
baht2 = book2 * 200;
printf("\n\nPlease enter the number of 'Beautiful World, Where Are You' = ");
scanf("%d", &book3);
baht3 = book3 * 250;
printf("\n\nPlease enter the number of 'Fascism of Love and Fantasy' = ");
scanf("%d", &book4);
baht4 = book4 * 450;
printf("\n\n_____________________________\n");
printf("\n\nPlease enter this month's total sales.");
printf("\n\n\n\nPlease enter the number of 'The Devotion of Suspect X' = ");
scanf("%d", &book5);
baht5 = book5 * 225;
printf("\n\nPlease enter the number of 'Norwegian Wood' = ");
scanf("%d", &book6);
baht6 = book6 * 325;
printf("\n\nPlease enter the number of 'Beautiful World, Where Are You' = ");
scanf("%d", &book7);
baht7 = book7 * 385;
printf("\n\nPlease enter the number of 'Fascism of Love and Fantasy' = ");
scanf("%d", &book8);
baht8 = book8 * 600;
printf("\n\n_____________________________\n");
printf("\n_____________________________\n");
printf("\n\nAlright.");
printf("\n\nLet's see your work.");
ova1 = (baht1 + baht2 + baht3 + baht4);
printf("\n\n\nBudget Price = %d Bahts\n", ova1);
ova2 = (baht5 + baht6 + baht7 + baht8);
printf("\n\nMonthly Sales Price = %d Bahts\n", ova2);
Delta = (ova2 - ova1);
if (Delta >0)
printf("\n\n\nYour profit : %d Bahts\n", Delta);
else
printf("\n\n\nYour loss : %d Bahts\n", Delta);
Zeta = ((Delta)/(ova1) * 100;
if (Zeta >0)
printf("\n\nProfit were = %f percents\n", Zeta);
else
printf("\n\nLoss were = %f percents\n", Zeta);
return 0;
}
Is there's anything wrong about my code?
I'm not good at both English and programming.
Sorry for my broken English and any grammartical incorrect.
Thanks for your help (if there's any).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要问题是数字的类型。
int
数字类型适用于整数,例如 3 和 99。int
类型不处理诸如 3.5 或 0.99 之类的数字。如果您尝试将带有小数点的数字放入int
中,您将丢失小数点后的详细信息。这就是为什么你的成绩为零。例如,如果Delta=20
和ova1=50
,则您的Zeta
评估将执行以下步骤:改进它的一种方法是通过执行
Zeta = 100*Delta/ova1;
来更改计算顺序,其作用是:但这还不够,因为您仍然会丢失分数。问题的另一部分是
Zeta
不应该是int
;它应该是一个double
,并且您需要转换(或“转换”)int
计算。在代码顶部附近,从
int
行中删除Zeta
并在下面添加此行:然后计算
Zeta
的行应变为:The primary problem is the type of numbers. The
int
number type is for whole numbers, like 3 and 99. Theint
type does not handle numbers like 3.5 or 0.99. If you try to put a number with a decimal point into anint
, you will lose the details after the decimal point. That is why you are getting zero. For example, ifDelta=20
andova1=50
, then yourZeta
evaluation goes through these steps:One way to improve it is to change the order of the evaluation by doing
Zeta = 100*Delta/ova1;
which does:But that is not sufficient, because you will still be losing fractions. The other part of the problem is that
Zeta
should not be anint
; it should be adouble
, and you need to convert (or "cast") theint
calculation.Near the top of your code, remove
Zeta
from theint
line and add this line below:Then the line to calculate
Zeta
should become: