当前位置: 代码迷 >> C语言 >> [求助]还是同一道存款计算程序,有一些关于列表的问题在11楼。
  详细解决方案

[求助]还是同一道存款计算程序,有一些关于列表的问题在11楼。

热度:187   发布时间:2007-09-09 21:01:06.0

问题又来了,还是同样一道题,我希望把结果用列表一年一年显示出来。

比如:

年 存款 利息
1 1000 (当年利息总结)
2 500 (当年利息总结)
3 0 (当年利息总结)


我写的这个程序只能把最后一年的存款余额和最后一共得到的利息显示出来了。
如果让它一年年都显示出来呢?
(程序在附件里)





[此贴子已经被作者于2007-9-9 21:03:59编辑过]


----------------解决方案--------------------------------------------------------
年 存款 利息

这个嘛```你可以在计算MONEY之前printf出来``
然后在计算的时候```

你是 年 存款 利息``计算一个输出一个吧```

在输出年下的数字时后面多点空个``存款一样``利息输完一个\N````

这是输出格式问题```LZ可以在printf语句里修改```



----------------解决方案--------------------------------------------------------

#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编辑过]


----------------解决方案--------------------------------------------------------
非常感谢~。。我电脑中毒了。。。重装系统。。还没办法开Q。。。。现谢谢你的帮忙了~
----------------解决方案--------------------------------------------------------

#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;
}

这个是计入了最后余款利息的。。。因为我不知道具体是要算哪一个,最后一个月指的是取完钱后发现下个月不够取,还是发现已经取不出的那个月?


----------------解决方案--------------------------------------------------------
这些问题都是我们新手常犯的问题呀!我们以后得注意了呀!

一起加油呀!
----------------解决方案--------------------------------------------------------
楼上所说的问题是?
----------------解决方案--------------------------------------------------------
  相关解决方案