函数内部实现的规则

函数内部实现的规则

不同功能的函数其内部实现各不相同,看起来似乎无法就 “内部实现”达成一致的 观点。但根据经验,我们可以在函数体的“入口处”和“出口处”从严把关,从而提高 函数的质量。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6     
 7     //显示1,2,3...10
 8     for(int i=1;i<=10;i++)
 9         cout<<i<<" ";
10     cout<<endl;
11 
12     //显示10,9,8...1
13     for(int j=10;j>=1;j--)
14        cout<<j<<" ";
15     cout<<endl;
16 
17     //显示1,3,5...9
18     for(int k=1;k<=10;k=k+2)
19        cout<<k<<" ";
20     cout<<endl;
21 
22     //显示ABC...Z   
23     for(char c='A';c<='Z';c++)
24        cout<<c;
25     cout<<endl;
26 
27     //显示0,0.1,0.2...1.0
28     for(float x=0;x<=1.0;x=x+0.1)
29        cout<<x<<" ";
30     cout<<endl;
31 
32     //显示0,0.1,0.2...1.0
33     for(float x1=0;x1<=1.0+0.1/2;x1=x1+0.1)
34        cout<<x1<<" ";
35     cout<<endl;
36 
37     //计算s=1+2+3...+100
38     int s=0;
39     for(int n=1;n<=100;n++)
40         s=s+n;
41     cout<<"s="<<s<<endl;
42     return 0;
43 }

 

posted @ 2018-08-02 11:55  borter  阅读(141)  评论(0编辑  收藏  举报