其实题目很简单,就是要求写一个能计算你的存款能用多少年多少月的程序。
要程序问你一共有多少存款,你每个月需提款多少,你每年的存款利息是多少,最后算出你所有的存款能用多少年多少月。
我可以写到让程序算出你第一月提款后还剩下多少钱,,,但不知道怎么让他一直计算到我所需的提款少于我的存款,然后计算出我的存款能用多少年多少月。。。我不懂我的while哪里用的不对了。。。。55555555555555555555
请帮忙指点一下吧。。。谢谢
leftover=剩余存款
withdraw=每月提款
nominalrate=年利息
principle=全部存款
interest=每月所得利息
#include <stdio.h>
#include <math.h>
int main(void)
{
float nominalrate, principle, withdraw, interest, leftover;
int months=0, years=0;
printf("This program is a retirement calculator, it calculates how long your savings will last. Please press enter to continue.\n");
printf("Please enter your total savings in dollar: \n");
scanf("%f",&principle);
printf("Please enter the amount of money withdrawn from savings each month: \n");
scanf("%f",&withdraw);
printf("Please enter your yearly interest rate: ");
scanf("%f",&nominalrate);
nominalrate *= 0.01; /*把百分比换算成小数点*/
interest = (principle-withdraw)*nominalrate/12; /*每月所得利息计算*/
leftover = principle-withdraw+interest; /*每月月底的剩余存款计算*/
while(leftover >= withdraw){
if(++months>12) {
months=1;
years ++;
}
}
if(leftover < withdraw){
printf("Your current savings will last %d years and %d months.\n", years,months);
}
return 0;
}
加粗部分是我不知道怎么写的部分。。。。。
[此贴子已经被作者于2007-9-9 21:05:00编辑过]
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <math.h>
int main(void)
{
float nominalrate, principle, withdraw, interest, leftover;
int months=0, years=0;
printf("This program is a retirement calculator, it calculates how long your savings will last. Please press enter to continue.\n");
printf("Please enter your total savings in dollar: \n");
scanf("%f",&principle);
printf("Please enter the amount of money withdrawn from savings each month: \n");
scanf("%f",&withdraw);
printf("Please enter your yearly interest rate: ");
scanf("%f",&nominalrate);
nominalrate *= 0.01; /*把百分比换算成小数点*/
for(leftover=principle;leftover>=withdraw;months++)
{
interest = (leftover-withdraw)*nominalrate/12; /*每月所得利息计算*/
leftover = leftover-withdraw+interest; /*每月月底的剩余存款计算*/
}
years=months/12;
months=months%12;
printf("Your current savings will last %d years and %d months.\n", years,months);
return 0;
}
就这样。你的黑体部分真的是一团糟。。。。呵呵,这么说不要见怪
我不是学经济的,我是这样认为你的每月利息:当月利息等于当月剩余存款乘以月利率。如果不是这样算,请说明
另外,只要认为剩余存款不足以支付下个月的提款,即中止循环,输出能足支付的时间。
[此贴子已经被作者于2007-9-9 15:13:43编辑过]
----------------解决方案--------------------------------------------------------
谢谢jinxin3256,,好人啊。。
我刚才试了一下,试的时候发现有的数组输入后工作,有的数组却不工作。。
还有
years=months/12;
months=years%12;
这两行是什么意思呢?
[此贴子已经被作者于2007-9-9 15:20:21编辑过]
----------------解决方案--------------------------------------------------------
啊。。好像正常工作了~~~。。。。
----------------解决方案--------------------------------------------------------
太感谢了~~~~。。我现在试试把计算结果已表格式样显示出来。
----------------解决方案--------------------------------------------------------
呵呵,我也是菜鸟。刚学这个,不到10天,以后可以多交流
QQ 85505162
----------------解决方案--------------------------------------------------------
years=months/12;
months=months%12;
第一行为求商,取整;
第二行为求模(就是求余数)
[此贴子已经被作者于2007-9-9 15:43:11编辑过]
----------------解决方案--------------------------------------------------------
years=months/12;
months=months%12;
第一行为求商,取整;
第二行为求模(就是求余数)
嗯嗯。。难怪了。。你第一次帖的是years=months%12..所以没工作。。。。现在好了~~~~~谢谢~~~o(∩_∩)o
----------------解决方案--------------------------------------------------------
嗯,开始写错了,改回来了
----------------解决方案--------------------------------------------------------
惭愧啊。。同样都是刚学的。。。。。。。。。。
----------------解决方案--------------------------------------------------------