Why?为什么好像两个程序一模一样,但是却无法输出同样的结果
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
float price, area, yir, mir; // 单价、面积、年利率、月利率
float interest, loan; // 利息、贷款总额
float ave_repay, down_payment; // 月均还款、首期付款
float total_price, total_repay; // 房款总额、还款总额
int ratio, time; // 按揭成数、按揭年数
printf("请输入单价(元/平方):");
scanf("%f", &price);
printf("请输入面积:");
scanf("%f", &area);
printf("请输入按揭成数:");
scanf("%d", &ratio);
printf("请输入按揭年数:");
scanf("%d", &time);
printf("请输入当前基准年利率:");
scanf("%f", &yir);
mir = yir / 100 / 12; // 年利率需要除以100得到,因为用户输入是5.4(%),而不是0.054
time = time * 12;
total_price = price * area;
loan = total_price * ratio / 10; // 这里不能写成(ratio / 10),否则结果会变成0
ave_repay = loan * mir * pow((1 + mir), time) / (pow((1 + mir), time) - 1);
interest = ave_repay * time - loan;
total_repay = loan + interest;
down_payment = total_price * (1 - (float)ratio / 10); // 强制转换ratio为浮点型
printf("========== 报告结果 ==========\n");
printf("房款总额:%.2f元\n", total_price);
printf("首期付款:%.2f元\n", down_payment);
printf("贷款总额:%.2f元\n", loan);
printf("还款总额:%.2f元\n", total_repay);
printf("支付利息:%.2f元\n", interest);
printf("月均还款:%.2f元\n", ave_repay);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
float price, area, yir, mir; //单价,面积,年利率、月利率
float interest, loan; //利息,贷款总额
float ave_repay, down_payment; //月均还款、首期付款
float total_price, total_repay; //房贷总额、还款总额
int ratio, time; //按揭成数、按揭年数
printf("请输入单位(元/平方):"); // 6000
scanf("%f", &price);
printf("请输入面积:"); // 120
scanf("%f", &area);
printf("请输入按揭成数:"); // 7
scanf("%f", &ratio);
printf("请输入按揭年数:"); // 20
scanf("%f", &time);
printf("请输入当前基准年利率:"); // 5.4
scanf("%f", &yir);
time = time * 12;
mir = yir / 100 / 12;
total_price = price * area;
loan = total_price * ratio / 10;
ave_repay = loan * mir * pow((1 + mir), time) / (pow((1 + mir), time) - 1);
interest = time * ave_repay - loan;
total_repay = interest + loan;
/*down_payment = total_price - loan;*/
down_payment = total_price * (1 - (float)ratio / 10);
printf("============报告结果============\n");
printf("房贷总额:%.2f元\n", total_price);
printf("首期付款:%.2f元\n", down_payment);
printf("贷款总额:%.2f元\n", loan);
printf("还贷总额:%.2f元\n", total_repay);
printf("支付利息:%.2f元\n", interest);
printf("月均还款:%.2f元\n", ave_repay);
system("pause");
return 0;
}
您的编译器难道没有给您任何提示吗?看看这些内容是不是有帮助 test.c:17:17: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
scanf("%f", &ratio);
~~ ^~~~~~
%d
test.c:19:17: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
scanf("%f", &time);
~~ ^~~~~
%d
2 warnings generated.
可以把警告选项开全一些,编译器不行可以考虑换一个
|