问题又来了,还是同样一道题,我希望把结果用列表一年一年显示出来。
比如:
年 存款 利息
1 1000 (当年利息总结)
2 500 (当年利息总结)
3 0 (当年利息总结)
我写的这个程序只能把最后一年的存款余额和最后一共得到的利息显示出来了。
如果让它一年年都显示出来呢?
(程序在附件里)
[此贴子已经被作者于2007-9-9 21:03:59编辑过]
问题又来了,还是同样一道题,我希望把结果用列表一年一年显示出来。
比如:
年 存款 利息
1 1000 (当年利息总结)
2 500 (当年利息总结)
3 0 (当年利息总结)
我写的这个程序只能把最后一年的存款余额和最后一共得到的利息显示出来了。
如果让它一年年都显示出来呢?
(程序在附件里)
[此贴子已经被作者于2007-9-9 21:03:59编辑过]
#include <stdio.h>
#include <math.h>
#include <dos.h>
int main(void)
{
float nominalrate, principle, withdraw, interest, leftover, balance, interests;
int months=0, years=0, table;
printf("This program is a retirement calculator, it calculates how long your savings will last.\n");
printf("\nPlease enter your total savings in dollar: \n");
scanf("%f",&principle);
while (principle <= 0)
{
printf("\nSorry, you have entered an invalid value. ");
printf("\nPlease enter another number that's greater then 0: \n");
scanf("%f", &principle);
}
printf("\nPlease enter the amount of money withdrawn from savings each month: \n");
scanf("%f",&withdraw);
while (withdraw <= 0)
{
printf("\nSorry, you have entered an invalid value. ");
printf("\nPlease enter another number that's greater then 0: \n");
scanf("%f", &withdraw);
}
printf("\nPlease enter your yearly interest rate: ");
scanf("%f",&nominalrate);
while (nominalrate > 100 || nominalrate <= 0)
{
printf("\nThe number you have entered is invalid.\n ");
printf("\nPlease enter a percentage between 0 and 100, without the percentage symbol: \n");
scanf("%f", &nominalrate);
}
if(principle<withdraw)
printf("\nyou principle have run out!!");
nominalrate *= 0.01; /*This converts percentage to decimal*/
if(principle*nominalrate/12>withdraw)
{printf("\nyou principle never run out!!");
system("pause");}
for(leftover=principle;leftover>=withdraw;months++){
interest = (leftover-withdraw)*nominalrate/12; /*Calculate monthly interest to be credited*/
leftover = leftover-withdraw+interest; /*Calculate the total amount left at the end of the month*/
}
years=months/12;
months=months%12;
printf("\nYour current savings will last %d years and %d months.\n", years,months);
printf("\n\nWould you like to view the details in a table? Yes=1/ No=0: ");
scanf("%d", &table);
if (table == 1)
{float t=0,interest=0;
int months=0,years=0;
printf("\n\n%6s%18s%18s\n", "Year","Balance($)","Interests($)");
printf("-------------------------------------------------\n");
for(leftover=principle;leftover>=withdraw;)
{
months++;
interest = (leftover-withdraw)*nominalrate/12; /*Calculate monthly interest to be credited*/
leftover = leftover-withdraw+interest;
if(months%12!=0)
{
t=t+interest;
}
else if(months%12==0)
{
t=t+interest;
years++;
printf("\n\n%6d%18f%18f\n", years,leftover,t);
t=0;
}
}
if(leftover<withdraw)
{years++;
printf("\n\n%6d%18f%18f\n", years,leftover,t);
}
}
return 0;
}
弄了好久,总算是弄出来了。。。调试通过了。
刚才又修改了下:这个是完全不计入余款利息的,只要发现余额不足下个月取就结束,你可以和下下楼的那个程序对照下,看你要的是哪个
[此贴子已经被作者于2007-9-10 19:39:49编辑过]
#include <stdio.h>
#include <math.h>
#include <dos.h>
int main(void)
{
float nominalrate, principle, withdraw, interest, leftover, balance, interests;
int months=0, years=0, table;
printf("This program is a retirement calculator, it calculates how long your savings will last.\n");
printf("\nPlease enter your total savings in dollar: \n");
scanf("%f",&principle);
while (principle <= 0)
{
printf("\nSorry, you have entered an invalid value. ");
printf("\nPlease enter another number that's greater then 0: \n");
scanf("%f", &principle);
}
printf("\nPlease enter the amount of money withdrawn from savings each month: \n");
scanf("%f",&withdraw);
while (withdraw <= 0)
{
printf("\nSorry, you have entered an invalid value. ");
printf("\nPlease enter another number that's greater then 0: \n");
scanf("%f", &withdraw);
}
printf("\nPlease enter your yearly interest rate: ");
scanf("%f",&nominalrate);
while (nominalrate > 100 || nominalrate <= 0)
{
printf("\nThe number you have entered is invalid.\n ");
printf("\nPlease enter a percentage between 0 and 100, without the percentage symbol: \n");
scanf("%f", &nominalrate);
}
if(principle<withdraw)
printf("\nyou principle have run out!!");
nominalrate *= 0.01; /*This converts percentage to decimal*/
if(principle*nominalrate/12>withdraw)
{printf("\nyou principle never run out!!");
system("pause");}
for(leftover=principle;leftover>=withdraw;months++){
interest = (leftover-withdraw)*nominalrate/12; /*Calculate monthly interest to be credited*/
leftover = leftover-withdraw+interest; /*Calculate the total amount left at the end of the month*/
}
years=months/12;
months=months%12;
printf("\nYour current savings will last %d years and %d months.\n", years,months);
printf("\n\nWould you like to view the details in a table? Yes=1/ No=0: ");
scanf("%d", &table);
if (table == 1)
{float t=0,interest=0;
int months=0,years=0;
printf("\n\n%6s%18s%18s\n", "Year","Balance($)","Interests($)");
printf("-------------------------------------------------\n");
for(leftover=principle;leftover>=withdraw;)
{
months++;
interest = (leftover-withdraw)*nominalrate/12; /*Calculate monthly interest to be credited*/
leftover = leftover-withdraw+interest;
if(months%12!=0)
{
t=t+interest;
}
else if(months%12==0)
{
t=t+interest;
years++;
printf("\n\n%6d%18f%18f\n", years,leftover,t);
t=0;
}
}
if(leftover<withdraw)
{years++;
t=t+interest+leftover*nominalrate/12;
leftover=leftover+leftover*nominalrate/12;
printf("\n\n%6d%18f%18f\n", years,leftover,t);
}
}
return 0;
}
这个是计入了最后余款利息的。。。因为我不知道具体是要算哪一个,最后一个月指的是取完钱后发现下个月不够取,还是发现已经取不出的那个月?