无法计算利润和损失,程序停止,没有输出。

huangapple go评论54阅读模式
英文:

Cannot compute profit and loss, program stops with no output

问题

以下是您代码中需要翻译的部分:

#include <stdio.h>;

int main() {
    int x, y, z, b, n;
    char k = '%';
    char r = '$';
    printf("输入商品成本价:-");
    scanf("%d", &x);
    printf("输入商品售价:-");
    scanf("%d", &y);
    if (x < y) {
        n = y - x;
        z = 100 / x;
        b = n / z;
        printf("你获得了%d%c的利润\n", b, k);
        printf("或\n");
        printf("%d %c 的利润\n", n, r);
    }
    if (x > y) {
        n = y - x;
        z = 100 / x;
        b = n / z;
        printf("你遭受了%d%c的亏损\n", b, k);
        printf("或\n");
        printf("%d %c 的亏损\n", n, r);
    }
    return 0;
}

这是您的C程序的翻译部分。如果您有任何其他问题,欢迎提出。

英文:

Question:
> If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss incurred.

Here is my code:

#include &lt;stdio.h&gt;

int main() {
    int x, y, z, b, n;
    char k = &#39;%&#39;;
    char r = &#39;$&#39;;
    printf(&quot;Enter cost of item:-&quot;);
    scanf(&quot;%d&quot;, &amp;x);
    printf(&quot;Enter selling price of item:-&quot;);
    scanf(&quot;%d&quot;, &amp;y);
    if (x &lt; y) {
    	n = y - x;
    	z = 100 / x;
    	b = n / z;
    	printf(&quot;You made %d%c profit \n&quot;, b, k);
    	printf(&quot;or\n&quot;);
    	printf(&quot;%d %c profits\n&quot;, n, r);
    }
    if (x &gt; y) {
    	n = y - x;
    	z = 100 / x;
    	b = n / z;
    	printf(&quot;You made %d%c loss \n&quot;, b, k);
    	printf(&quot;or\n&quot;);
    	printf(&quot;%d %c loss\n&quot;, n, r);
    }
  	return 0;
}

Hey I'm new in C programming knows basic only. Today I'm learning if / else and I try to solve this question.
But my code not giving any error and warning. It takes input and after its end my if not working.
please Help:(

答案1

得分: 1

这里有一个问题:

z = 100 / x;
b = n / z;

如果价格 x 大于 100,由于整数除法的语义,z 的值将为 0,这将导致除法溢出,可能导致程序停止。

以下是我的建议:

  • 避免除以零
  • 使用浮点数运算以避免过多的四舍五入。
  • 使用明确的变量名,使您的程序更易阅读和理解。
  • 检测 scanf() 的返回值,以检测无效或丢失的输入。

以下是修改后的版本:

#include <stdio.h>

int main(void) {
    int cost, sale;

    printf("输入物品成本: ");
    if (scanf("%d", &cost) != 1) {
        printf("输入错误!\n");
        return 1;
    }
    printf("输入物品售价: ");
    if (scanf("%d", &sale) != 1) {
        printf("输入错误!\n");
        return 1;
    }
    if (cost == 0) {
        printf("无成本基础,总利润为 $%d\n", sale);
    } else if (cost == sale) {
        printf("成本与售价相等:既无盈利也无损失\n");
    } else if (cost < sale) {
        int profit = sale - cost;
        int margin = profit * 100 / cost;
        printf("您赚了 %d%% 利润,共 $%d\n", margin, profit);
    } else {
        int loss = cost - sale;
        int margin = loss * 100 / cost;
        printf("您遭受了 %d%% 损失,共 $%d\n", margin, loss);
    }
    return 0;
}

希望这对你有所帮助。

英文:

There is a problem here:

    z = 100 / x;
    b = n / z;

If the price x is larger than 100, z will have the value 0 because of integer division semantics and n / z will cause undefined behavior because of division overflow, which may cause the program to stop.

Here are my recommendations:

  • avoid dividing by zero
  • use floating point arithmetics to avoid excessive rounding.
  • use explicit names to make your program easier to read and understand.
  • test the return value of scanf() to detect invalid or missing input.

Here is a modified version:

#include &lt;stdio.h&gt;

int main(void) {
    int cost, sale;

    printf(&quot;Enter cost of item: &quot;);
    if (scanf(&quot;%d&quot;, &amp;cost) != 1) {
        printf(&quot;input error!\n&quot;);
        return 1;
    }
    printf(&quot;Enter selling price of item: &quot;);
    if (scanf(&quot;%d&quot;, &amp;sale) != 1) {
        printf(&quot;input error!\n&quot;);
        return 1;
    }
    if (cost == 0) {
        printf(&quot;no cost base, total profit of $%d\n&quot;, sale);
    } else
    if (cost == sale) {
        printf(&quot;it&#39;s a wash: no profit, no loss\n&quot;);
    } else
    if (cost &lt; sale) {
        int profit = sale - cost;
        int margin = profit * 100 / cost;
        printf(&quot;You made a %d%% profit of $%d\n&quot;, margin, profit);
    } else {
        int loss = cost - sale;
        int margin = loss * 100 / cost;
        printf(&quot;You made a %d%% loss of $%d\n&quot;, margin, loss);
    }
    return 0;
}

答案2

得分: 0

以下是代码的中文翻译:

这里是完整的答案。你在计算中犯了一个错误。

#include <stdio.h>

int main() {
    float x, y, z, b, n;
    char k = '%';
    char r = '$';
    printf("输入物品的成本: ");
    scanf("%f", &x);
    printf("输入物品的销售价格: ");
    scanf("%f", &y);
    if (x <= y) {
        n = y - x;
        z = n / x * 100;

        printf("你赚了 %f %c 利润 \n", z, k);
        printf("或\n");
        printf("%f %c 利润\n", n, r);
    }
    if (x > y) {
        n = x - y;
        z = n / x * 100;
        printf("你亏了 %f%c \n", z, k);
        printf("或\n");
        printf("%f %c 亏损\n", n, r);
    }
    return 0;
}
英文:

Here. is the complete answer. for you. have have make mistake in calculation

#include &lt;stdio.h&gt;

int main() {
    float x, y, z, b, n;
    char k = &#39;%&#39;;
    char r = &#39;$&#39;;
    printf(&quot;Enter cost of item: &quot;);
    scanf(&quot;%f&quot;, &amp;x);
    printf(&quot;Enter selling price of item: &quot;);
    scanf(&quot;%f&quot;, &amp;y);
    if (x &lt;= y) {
        n = y - x;
        z = n/x*100;
      
        printf(&quot;You made %f %c profit \n&quot;, z, k);
        printf(&quot;or\n&quot;);
        printf(&quot;%f %c profits\n&quot;, n, r);
    }
    if (x &gt; y) {
        n = x-y;
        z = n/x*100;
        printf(&quot;You made %f%c loss \n&quot;, z, k);
        printf(&quot;or\n&quot;);
        printf(&quot;%f %c loss\n&quot;, n, r);
    }
    return 0;
}

huangapple
  • 本文由 发表于 2023年4月6日 19:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定