| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 709 人关注过本帖
标题:请问用while和for如何编写这个程序
只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:8 回复次数:10 
请问用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
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:2 
用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-12 13:48
幽灵X
Rank: 5Rank: 5
等 级:职业侠客
帖 子:128
专家分:328
注 册:2013-1-28
收藏
得分:2 
楼主,我编好了,可以的话,把分给我吧。
程序代码:
#include<stdio.h>
int main()
{
    int i=1;
    float s=10000;
    char ch;
    ch='y';
    while(ch=='y'||ch=='Y')
    {
        for(;;i++)
        {
            printf("At the end of Year  %d",i);
            printf("you will have  %f\n",s);
            s=s*1.05;break;
        }
        printf("是否继续投资?\n");
        scanf("\n%c",&ch);
    }
    return 0;
} 
2013-02-12 15:16
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
未名湖的云
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:130
专家分:190
注 册:2012-10-11
收藏
得分:2 
回复 4楼 winglesswu
#include <stdio.h>

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);
    }
   
}
2013-02-13 13:10
Susake
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:女儿国的隔壁
等 级:贵宾
威 望:23
帖 子:2288
专家分:6481
注 册:2012-12-14
收藏
得分:2 
。。。。

仰望星空...........不忘初心!
2013-02-14 01:14
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
未名湖的云
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:130
专家分:190
注 册:2012-10-11
收藏
得分:0 
回复 9楼 winglesswu
可以不用设置,但是啊这个是个好习惯~~~
2013-02-15 12:33
快速回复:请问用while和for如何编写这个程序
数据加载中...
 
   



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

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