该代码假设计算出租车的价格。 例如,如果我乘坐2个行李箱10公里,它应该打印27.2 我得到13.00
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define START_PRICE 10.20;
#define PRICE_PER_KM 1.30;
#define PRICE_PER_SUITCASE 2.00;
void main()
{
double km;
int suitcase;
double finalPrice;
printf("Please enter number of kilometers: ");
scanf("%lf", &km);
printf("Please enter number of suitcases: ");
scanf("%d", &suitcase);
finalPrice = km*PRICE_PER_KM + suitcase*PRICE_PER_SUITCASE + START_PRICE;
printf("The total price is: %.2lf\n", finalPrice);
}
答案 0 :(得分:4)
不要在预处理器过度扩展的行末尾添加分号。
所以在这里删除分号:
#define START_PRICE 10.20;
#define PRICE_PER_KM 1.30;
#define PRICE_PER_SUITCASE 2.00;
这样
finalPrice = km*PRICE_PER_KM + suitcase*PRICE_PER_SUITCASE + START_PRICE;
不会扩展到
finalPrice = km*1.30; + suitcase*2.00; + 10.20;;
而是扩展为
finalPrice = km*1.30 + suitcase*2.00 + 10.20;
答案 1 :(得分:0)
如果您打开编译器上的设置,您将看到它:
错误:无效的语句[-Werror = unused-value] |
此外,您应该尊重最低标准并将您的void main更改为int main(void){retunr 0}。 并且总是检查scanf的返回。
这是你的最终计划:
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#define START_PRICE 10.20
#define PRICE_PER_KM 1.30
#define PRICE_PER_SUITCASE 2.00
int main(void){
double km;
int suitcase;
double finalPrice;
printf("Please enter number of kilometers: ");
if(scanf("%lf", &km) == 1)
printf("Please enter number of suitcases: ");
if(scanf("%d", &suitcase) == 1)
finalPrice = km * PRICE_PER_KM + suitcase * PRICE_PER_SUITCASE + START_PRICE;
printf("The total price is: %.2lf\n", finalPrice);
return 0;
}
输出:
Please enter number of kilometers: 10 Please enter number of suitcases: 5 The total price is: 33.20