【问题标题】:How to solve this problem in DEV C+ percentage calculation?在DEV C+百分比计算中如何解决这个问题?
【发布时间】:2022-11-11 08:10:36
【问题描述】:

我对 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;
}

我的代码有什么问题吗?

我不擅长英语和编程。

对不起,我的英语不好和任何语法错误。

感谢您的帮助(如果有的话)。

【问题讨论】:

    标签: c++ dev-c++


    【解决方案1】:

    主要问题是数字的类型。 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;
        

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2011-03-20
      • 2021-04-12
      • 2017-09-16
      相关资源
      最近更新 更多