0320-复利计算器代码
#include<stdio.h>
#include<math.h>
void Danli();
void Fuli();
void getPrincipal();
void getYear();
void getYearRate();
void getMutualFund();
double p,i,f;
int n;
main()
{
int num=0;
while(1){
printf("\t\t\t|************复利计算器************|\n");
printf("\t\t\t|------------0.退出计算------------|\n");
printf("\t\t\t|------------1.单利----------------|\n");
printf("\t\t\t|------------2.复利----------------|\n");
printf("\t\t\t|------------3.计算本金------------|\n");
printf("\t\t\t|------------4.年限估算------------|\n");
printf("\t\t\t|------------5.估算年回报率--------|\n");
printf("\t\t\t|------------6.定额定投收益计算----|\n");
printf("\t\t\t|----------------------------------|\n");
printf("请输入选择:");
scanf("%d",&num);
switch(num)
{
case 0:
return 0;
case 1:
Danli();
break;
case 2:
Fuli();
break;
case 3:
getPrincipal();
break;
case 4:
getYear();
break;
case 5:
getYearRate();
break;
case 6:
getMutualFund();
break;
default:
return 0;
}
}
}
void Danli()
{
printf("请输入本金:");
scanf("%lf",&p);
printf("请输入年利率(百分比):");
scanf("%lf",&i);
printf("请输入存储年限:");
scanf("%d",&n);
f = p+p*(i/100)*n;
printf("单利终值为:%0.2lf\n",f);
}
void Fuli()
{
printf("请输入本金:");
scanf("%lf",&p);
printf("请输入年利率(百分比):");
scanf("%lf",&i);
printf("请输入存储年限:");
scanf("%d",&n);
f = p*(pow(1+i/100,n));
printf("复利终值为:%0.2lf\n",f);
}
void getPrincipal()
{
printf("请输入目标金额:");
scanf("%lf",&f);
printf("请输入目标利率(百分比):");
scanf("%lf",&i);
printf("请输入目标年限:");
scanf("%d",&n);
p = f/pow(1+(i/100),n);
printf("需要投入的本金为:%0.2lf\n",p);
}
void getYear()
{
printf("请输入本利和:");
scanf("%lf",&f);
printf("请输入年回报率(百分比):");
scanf("%lf",&i);
printf("请输入本金:");
scanf("%lf",&p);
for(n=1;;n++)
{
if((p*(pow((1+i/100),n)))>=f)
{
printf("最低年限为:%d\n\n",n);
break;
}
}
}
void getYearRate()
{
printf("请输入本利和:");
scanf("%lf",&f);
printf("请输入本金:");
scanf("%lf",&p);
printf("请输入目标年限:");
scanf("%d",&n);
i = pow((f/p),1.0/n)-1;
printf("年回报率为:%0.2lf\n",i);
}
void getMutualFund()
{
printf("请输入每年定投金额:");
scanf("%lf",&p);
printf("请输入年收益(百分比):");
scanf("%lf",&i);
printf("请输入定投年数:");
scanf("%d",&n);
f = p*(1+i/100)*(pow((1+i/100),n)-1)/(i/100);
printf("基金定投收益为:%0.2lf\n",f);
}