如何在DEV C+中解决这个问题百分比计算?

发布于 2025-01-10 07:08:03 字数 2220 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2025-01-17 07:08:03

主要问题是数字的类型。 int 数字类型适用于整数,例如 3 和 99。int 类型不处理诸如 3.5 或 0.99 之类的数字。如果您尝试将带有小数点的数字放入 int 中,您将丢失小数点后的详细信息。这就是为什么你的成绩为零。例如,如果 Delta=20ova1=50,则您的 Zeta 评估将执行以下步骤:

  1. 20/50 * 100;
  2. 0 * 100;因为你失去了“.4”
  3. 0;

改进它的一种方法是通过执行 Zeta = 100*Delta/ova1; 来更改计算顺序,其作用是:

  1. 100*20/50;
  2. 2000/50;
  3. 40;

但这还不够,因为您仍然会丢失分数。问题的另一部分是 Zeta 不应该是 int;它应该是一个double,并且您需要转换(或“转换”)int 计算。

在代码顶部附近,从 int 行中删除 Zeta 并在下面添加此行:

double Zeta;

然后计算 Zeta 的行应变为:

Zeta = (double)100*Delta/ova1;

The primary problem is the type of numbers. The int number type is for whole numbers, like 3 and 99. The int type does not handle numbers like 3.5 or 0.99. If you try to put a number with a decimal point into an int, you will lose the details after the decimal point. That is why you are getting zero. For example, if Delta=20 and ova1=50, then your Zeta evaluation goes through these steps:

  1. 20/50 * 100;
  2. 0 * 100; because you lose the ".4"
  3. 0;

One way to improve it is to change the order of the evaluation by doing Zeta = 100*Delta/ova1; which does:

  1. 100*20/50;
  2. 2000/50;
  3. 40;

But that is not sufficient, because you will still be losing fractions. The other part of the problem is that Zeta should not be an int; it should be a double, and you need to convert (or "cast") the int calculation.

Near the top of your code, remove Zeta from the int line and add this line below:

double Zeta;

Then the line to calculate Zeta should become:

Zeta = (double)100*Delta/ova1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文