英文:
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 <stdio.h>
int main() {
int x, y, z, b, n;
char k = '%';
char r = '$';
printf("Enter cost of item:-");
scanf("%d", &x);
printf("Enter selling price of item:-");
scanf("%d", &y);
if (x < y) {
n = y - x;
z = 100 / x;
b = n / z;
printf("You made %d%c profit \n", b, k);
printf("or\n");
printf("%d %c profits\n", n, r);
}
if (x > y) {
n = y - x;
z = 100 / x;
b = n / z;
printf("You made %d%c loss \n", b, k);
printf("or\n");
printf("%d %c loss\n", 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 <stdio.h>
int main(void) {
int cost, sale;
printf("Enter cost of item: ");
if (scanf("%d", &cost) != 1) {
printf("input error!\n");
return 1;
}
printf("Enter selling price of item: ");
if (scanf("%d", &sale) != 1) {
printf("input error!\n");
return 1;
}
if (cost == 0) {
printf("no cost base, total profit of $%d\n", sale);
} else
if (cost == sale) {
printf("it's a wash: no profit, no loss\n");
} else
if (cost < sale) {
int profit = sale - cost;
int margin = profit * 100 / cost;
printf("You made a %d%% profit of $%d\n", margin, profit);
} else {
int loss = cost - sale;
int margin = loss * 100 / cost;
printf("You made a %d%% loss of $%d\n", 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 <stdio.h>
int main() {
float x, y, z, b, n;
char k = '%';
char r = '$';
printf("Enter cost of item: ");
scanf("%f", &x);
printf("Enter selling price of item: ");
scanf("%f", &y);
if (x <= y) {
n = y - x;
z = n/x*100;
printf("You made %f %c profit \n", z, k);
printf("or\n");
printf("%f %c profits\n", n, r);
}
if (x > y) {
n = x-y;
z = n/x*100;
printf("You made %f%c loss \n", z, k);
printf("or\n");
printf("%f %c loss\n", n, r);
}
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论