程序构造思路:
第一点:在求复利计算中达到终值所需年份是利用穷举法计算1-100年间符合的时间,最终求出存款所需时间。
第二点:在求复利计算中利用复利公式的逆推,求出年利率的公式。
#include<stdio.h> #include<math.h> #include<stdlib.h> void YEAR() { int year,flat=0; double P,F; double i; printf("请输入本利和:"); scanf("%lf",&F); printf("请输入复利次数(年):"); scanf("%d",&year); printf("请输入本金:"); scanf("%lf",&P); i=pow(F/P, 1.0/year)-1; printf("年利率为:%lf",i); } void time() { int n,flat=0; double P,i,F; printf("请输入本利和:"); scanf("%lf",&F); printf("请输入年回报率:"); scanf("%lf",&i); printf("请输入本金:"); scanf("%lf",&P); for(n=1;n<100;n++) //穷举法求100年满足的计息期数 { if((P*(pow((1+i),n)))>=F) { printf("计息期数:%d ",n); flat=1; break; } } if(flat==0) printf("在100年内没有符合计息期数! "); } void menu() { puts(" "); puts(" |******************************************************|"); puts(" | 利息计算系统 |"); puts(" |******************************************************|"); puts(" | 1: 复利计算 |"); puts(" | 2: 单利计算 |"); puts(" | 3: 逆推计算 |"); puts(" | 4: 期数计算 |"); puts(" | 5: 利率计算 |"); puts(" | 0: 退出程序 |"); puts(" |******************************************************|"); printf("请选择<1~5>:"); } void Fuli() { int year;//year表示复利年限 double p;//p表示本金 double i;//i表示年利率 double F;//表示复利后的终值 int k; printf("请输入复利次数(年):"); scanf("%d",&year); printf(" 请输入本金:"); scanf("%lf",&p); printf(" 请输入年利率:"); scanf("%lf",&i); for(k=1;k<=year;k++){ F=p*(1+i); p=F; } printf(" 复利后的终值为:"); printf("%.2lf",F); } void Danli() { int year;//year表示复利年限 double p;//p表示本金 double i;//i表示年利率 double Fv;//表示复利后的终值 printf("请输入复利次数(年):"); scanf("%d",&year); printf(" 请输入本金:"); scanf("%lf",&p); printf(" 请输入年利率:"); scanf("%lf",&i); printf(" 单利后的终值为:"); Fv=p*(1+i*year); printf("%.2lf",Fv); } void Nitui() { int Year; double P,i; double S; double E,D=1; int l; printf("输入期待金额为:"); scanf("%lf",&S); printf(" 请输入存储年限:"); scanf("%d",&Year); printf(" 请输入年利率:"); scanf("%lf",&i); for(l=1;l<=Year;l++) { E=D*(1+i); D=E; } P=S/D; printf(" 应输入的本金为:"); printf("%.2lf",P); } main(){ int n; while(1) { menu(); scanf("%d",&n); if(n==0) break; switch(n) { case 1: Fuli(); break; case 2: Danli();break; case 3: Nitui();break; case 4: time();break; case 5: YEAR(); break; case 0: n=0;exit(0); } } }
![](https://images2015.cnblogs.com/blog/809053/201603/809053-20160316125114959-1105429432.png)
![](https://images2015.cnblogs.com/blog/809053/201603/809053-20160316125126474-2110932258.png)