| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 709 人关注过本帖
标题:请问用while和for如何编写这个程序
取消只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:8 回复次数:4 
请问用while和for如何编写这个程序
要求编写一个投资的小程序,比如你有10000元本金,每年的回报率在5%,那么几年后的回报情况如下:


At the end of Year 1   you will have   10500.00

At the end of Year 2   you will have   11025.00

At the end of Year 3   you will have   11576.25

…..etc

At the end of Year 10 you will have   16288.95。
要求:1 计算回报时用for循环 2 每过一年后要求提示是否继续投资,用while循环。
小弟我对如何编写循环还不是很清楚,请大侠赐教,并祝大家新年快乐,事业有成。
搜索更多相关主题的帖子: 如何 
2013-02-12 12:25
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
我写了下面这个程序,可以编译但是运行时却没有任何反应。请大侠指点,谢谢。
#include <stdio.h>

main()
{
double principal, rate, gain;
int i;
char yn;

while (yn=='Y')
{
printf("\nPlease input your initial investment:$\n");
scanf("%lf", & principal);
printf("\nPlease input the interest rate you expect:\n");
scanf("%lf", &rate);
rate = rate /100.0;

for (i=0; i<10; i++)
{
gain=principal*(1.0 + rate);
printf ("\nAt the end of Year %d\tyou will have %20.2lf", i, gain);
}
getchar();
printf( "\n\n\nWould you like continue to calculate your gain? Y/N ");
while ( getchar() != '\n' );
scanf ("%c", &yn);
}

}
2013-02-13 12:28
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
以下是引用zklhp在2013-2-12 13:48:21的发言:

用linux下的一个计算软件bc写了一个 看来这玩意挺好用的嘛
$ bc
scale=10
x = 10000
rate = 1.05
i = 0
for (i = 1; i < 20; i++)
{
print "At the end of Year "
print i
print "you will have "
print (x*rate^i)
print "\n"
}

At the end of Year 1 you will have 10500.00
At the end of Year 2 you will have 11025.0000
At the end of Year 3 you will have 11576.250000
At the end of Year 4 you will have 12155.06250000
At the end of Year 5 you will have 12762.8156250000
At the end of Year 6 you will have 13400.9564060000
At the end of Year 7 you will have 14071.0042260000
At the end of Year 8 you will have 14774.5544370000
At the end of Year 9 you will have 15513.2821590000
At the end of Year 10 you will have 16288.9462670000
At the end of Year 11 you will have 17103.3935810000
At the end of Year 12 you will have 17958.5632600000
At the end of Year 13 you will have 18856.4914230000
At the end of Year 14 you will have 19799.3159940000
At the end of Year 15 you will have 20789.2817940000
At the end of Year 16 you will have 21828.7458830000
At the end of Year 17 you will have 22920.1831780000
At the end of Year 18 you will have 24066.1923360000
At the end of Year 19 you will have 25269.5019530000

如果不是写作业我肯定用这玩意算 呵呵

纯属娱乐 祝楼主新年快乐

版主太厉害了,可是不适合提交作业的要求。祝新年继续快乐。
2013-02-13 12:30
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
重新改进了,但是还是不行。请指点.
#include <stdio.h>
main()
{
double principal, rate, gain;
int i, years;
char yn;



printf("\nPlease input your initial investment:$\n");
scanf("%lf", & principal);
printf("\nPlease input the interest rate you expect:\n");
scanf("%lf", &rate);
rate = rate /100.0;
printf("\nPlease input the number of years you would like to do your investment: ");
scanf("%d", &years);
printf("\nWould you like to calculate your gain? Y/N ");
fflush(stdin);
scanf ("%c", &yn);

while (yn=='Y')
{
for (i=0; i<years; i++)
{
gain=principal*(1.0 + rate);
printf ("\nAt the end of Year %d\tyou will have %20.2lf", i, gain);
}
getchar();
printf( "\n\n\nWould you like continue to calculate your gain? Y/N ");
while ( getchar() != '\n' );
scanf ("%c", &yn);
}
}
2013-02-13 13:07
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
以下是引用未名湖的云在2013-2-13 13:10:14的发言:

#include  
 
void main()
{
    double principal, rate, gain;
    int i;
    char yn = 'Y';
    //这个yn没有初值,这个while循环是不会进入的(也就是第一次不可能成功直接就结束了)
    while (yn=='Y')
    {
        //接下来就得把它置为另一个,
        yn = 'N';
        printf("\nPlease input your initial investment:$\n");
        scanf("%lf", & principal);
        printf("\nPlease input the interest rate you expect:\n");
        scanf("%lf", &rate);
        rate = rate /100.0;
         
        for (i=0; i<10; i++)
        {
            gain=principal*(1.0 + rate);
            //这里是为了作为下一次的本金,懂得吧???
            principal = gain;
            printf ("\nAt the end of Year %d\tyou will have %20.2lf", i, gain);
        }
        getchar();
        printf( "\n\n\nWould you like continue to calculate your gain? Y/N ");
        //这里先修改为这个
        scanf ("%c", &yn);
    }
     
}

谢谢,请问为什么在while的Loop里还要设置yn='N'呢,谢谢。
2013-02-14 06:41
快速回复:请问用while和for如何编写这个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015907 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved