我爱橙 发表于 2022-5-2 15:58:00

EX4.23 存款利率计算器V1.0

本帖最后由 我爱橙 于 2022-5-2 16:34 编辑

输入银行定期存款的年利率rate,存款期n年,存款本金capital,试编程计算并输出n年后的本利之和deposit。程序中所有浮点数的数据类型均为double类型。
提示:

从键盘输入数据可以使用函数scanf()。本例中为scanf("%lf,%d,%lf", &rate, &n, &capital);
本程序最终计算的是复利。
计算幂的数学函数为pow(a,n), 代表a的n次幂。
使用数学函数,需要在程序开头加上编译预处理指令 #include <math.h>
以下为程序的一个运行示例:
Please enter rate, year, capital:

0.0225,10,1000↙

deposit=1249.203


#include <stdio.h>
#include <math.h>
int main()

{
int n;
double a,rate,capital,deposit;

printf( "Please enter rate, year, capital:\n");
scanf("%lf,%d,%lf", &rate, &n, &capital);
a=pow(rate,n);
deposit=capital*(1+a);
printf("deposit=%.3f\n",deposit);
return 0;
}

为什么实现不了程序功能,结果一直是0?

风车呼呼呼 发表于 2022-5-2 16:06:18

scanf("%lf,%d,%lf", &rate, &n, &capital);
输入的时候写了逗号没

isdkz 发表于 2022-5-2 16:08:03

本帖最后由 isdkz 于 2022-5-2 16:11 编辑

一个小于 0 的小数的幂只会越来越小,

a 本就是一个很小的小数,你再给它来个几次方就趋近于 0 了,

所以你对年利率的计算有点误解#include <stdio.h>
#include <math.h>
int main()

{
int n;
double a,rate,capital,deposit;

printf( "Please enter rate, year, capital:\n");
scanf("%lf,%d,%lf", &rate, &n, &capital);
a=pow(1 + rate,n);                // 改了这里
deposit=capital* a;               // 改了这里
printf("deposit=%.3f\n",deposit);
return 0;
}

我爱橙 发表于 2022-5-2 16:09:17

风车呼呼呼 发表于 2022-5-2 16:06
输入的时候写了逗号没

啊,输了逗号之后结果是1000 还是没实现功能QAQ

hornwong 发表于 2022-5-2 16:46:55

{:5_108:}
页: [1]
查看完整版本: EX4.23 存款利率计算器V1.0