问题是要编写一个程序来从一个文件中读取多组价格和折扣,并选择一个数据结束标记,例如 0 或 -1。它还说在客户必须支付的另一个文件(输出)中打印原始价格、折扣金额和最终价格。
这是我在 input.txt 中写的:
285 30
300 25
0
这是来源:
#include <stdio.h>
int main(void)
{
FILE * in = fopen("C:\\Users\\Desktop\\c\\price.txt","r");
FILE * out = fopen("C:\\Users\\Desktop\\c\\priceout.txt","w");
double price, discountedAmount, finalPrice;
int disPercent;
fscanf(in,"%lf",&price);
while(price != 0) {
fscanf(in,"%d",&disPercent);
discountedAmount = price * disPercent / 100;
finalPrice = price - discountedAmount;
fscanf(in,"%lf",&price);
fprintf(out,"The original price is: %.2f\n",price);
fprintf(out,"The discount amount is: %.2f\n",discountedAmount);
fprintf(out,"Final Price is: %.2f",finalPrice);
}
fclose(in);
fclose(out);
return 0;
}
这是程序写入 output.txt 的内容:
The original price is: 300.00
The discount amount is: 85.50
Final Price is: 199.50
The original price is: 0.00
The discount amount is: 75.00
Final Price is: 225.00
程序不应该在 0 处停止吗?我究竟做错了什么?