S1E9课后作业的动动手第一题的答案
本帖最后由 花椒壹拾伍 于 2019-1-28 18:19 编辑我把参考答案复制进编译器后运算出来的“首期付款”为什么是216000.02(保留两位小数)了?为什么有个小数点的0.2?
我自己后面还加了段代码(红色部分),来分析下首期付款的计算过程,(1 - (float)ratio / 10)的计算结果是0.3,那么0.3乘以总房价的720000,结果应该是不会有小数点后面的值的,为什么运行下来就是有?
#include <stdio.h>
#include <math.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("房款总额:%f元\n", total_price);
printf("首期付款:%f元\n", down_payment);
printf("贷款总额:%f元\n", loan);
printf("还款总额:%f元\n", total_repay);
printf("支付利息:%f元\n", interest);
printf("月均还款:%f元\n", ave_repay);
float a= (1 - (float)ratio / 10);
float b= total_price * a;
printf("%f\n",a);
printf("%f",b);
return 0;
}
以下是我自己的写的代码,并没有这个问题:
#include <stdio.h>
#include <math.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);
total_price = price * area;
down_payment = total_price * (10 - ratio) * 0.1;
loan = total_price - down_payment;
mir = yir / 12;
ave_repay = 3438.55;
total_repay = ave_repay * time * 12;
interest = time * 12 * ave_repay - loan;
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);
return 0;
} down_payment 在哪??????? ba21 发表于 2019-1-28 18:55
down_payment 在哪???????
啥意思?down_payment就是首期付款啊,一开始第三句就定义了:float ave_repay,down_payment
页:
[1]