1. 程式人生 > >實驗1:c++簡單程序設計(1)

實驗1:c++簡單程序設計(1)

sed 窮舉 data- 技術分享 簡單 復習 efault black default

//文中有格式錯誤請無視

//這個編輯器一言難盡

實驗目的


1. 掌握c++中類c部分的編程知識: 數據類型,常量,變量,運算符,表達式,分支結構,循環結構

2. 掌握C++中數據輸入和輸出的基本方法

3. 熟練使用c++程序開發環境,掌握c++程序編寫、編譯、運行、調試的方法

實驗準備


1. 簡單的c++程序結構 學習/復習教材「2.1.3 C++程序實例」

2. c++中數據輸入輸出的基本方法 學習/復習教材2.3節,學習C++中I/O流、預定義的插入符<<和提取符>>的基本用法。

3. if語句、switch語句、while語句、do…while語句的用法 學習/復習教材2.4節,通過示例理解背後簡單算法及c++分支語句、循環語句的用法

4. 自定義數據類型: typedef,枚舉類型用法 學習/復習教材2.5節,結合示例理解枚舉類型和int型在類型轉換時的註意事項

實驗內容


Part2: 編程練習

教材第2章習題2-28 簡單的菜單程序

教材第2章習題2-29 窮舉法求質數

教材第2章習題2-32 猜數

教材第2章習題2-34排列組合

實驗結論


2-28簡單的菜單程序(if-else

Code:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     cout<<"
Menu:A(dd) D(elete) S(ort) Q(uit)"; 6 cout<<"Select one:"; 7 char k; 8 while(cin>>k) 9 { 10 if(k==A) 11 cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 12 else if(k==D) 13 cout<<"
Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 14 else if(k==S) 15 cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 16 else if(k==Q) 17 break; 18 else 19 cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 20 } 21 return 0; 22 }

Screenshot:

技術分享圖片

(switch

Code:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)";
 6     cout<<"Select one:";
 7     char k;
 8     while(cin>>k&&k!=Q)
 9     {
10         switch(k)
11         {
12             case A:
13                 cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
14                 continue;
15             case D:
16                 cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
17                 continue;
18             case S:
19                 cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
20                 continue;
21             default:cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
22         }
23     }
24     return 0;
25 }

Screenshop:

技術分享圖片

2-29 窮舉法求質數(while

Code:

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7     cout<<"1~100間的質數為:"<<endl; 
 8     int x=1;
 9     int a,b,c=0;
10     while(x<=100)
11     {
12         a=sqrt(x);
13         if(x!=1)
14         {
15             b=2;
16             while(x%b!=0&&b<=a)
17                  b++; 
18             if(b>a)
19             {
20                 c++;
21                 cout<<setw(5)<<x;
22                 if(c%5==0)
23                     cout<<"\n";
24             }
25         }
26         x++;
27     }
28     return 0;
29  } 

Screenshop:

技術分享圖片

(for

Code:

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7     cout<<"1~100間的質數為:"<<endl; 
 8     int x;
 9     int a,b,c=0;
10     for(x=1;x<=100;x++)
11     {
12         a=sqrt(x);
13         if(x==1)
14             continue;
15         else
16         {
17             for(b=2;b<=a;b++)
18                 if(x%b==0)
19                     break;
20             if(b>a)
21             {
22                 c++;
23                 cout<<setw(5)<<x;
24                 if(c%5==0)
25                     cout<<"\n";
26             }
27         }
28     }
29     return 0;
30  } 

Screenshop:

技術分享圖片

(do-while

Code:

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7     cout<<"1~100間的質數為:"<<endl; 
 8     int x=0;
 9     int a,b,c=0;
10     do
11     {
12         x++;
13         a=sqrt(x);
14         if(x!=1)
15         {
16             b=1;
17             do
18             {
19                 b++;
20             }while(x%b!=0&&b<=a);
21             if(b>a)
22             {
23                 c++;
24                 cout<<setw(5)<<x;
25                 if(c%5==0)
26                     cout<<"\n";
27             }
28         }
29     }while(x<100);
30     return 0;
31  } 

Screenshop:

技術分享圖片

2-32 猜數(while

Code:

 1 #include<iostream>
 2 #include<ctime>
 3 #include<cstdlib>
 4 using namespace std;
 5 int main()
 6 {
 7     int x;
 8     int a;
 9     srand(time(NULL));
10     x=1+rand()%(100-1+1);
11     cout<<"Your guess number is(1~100): ";
12     cin>>a;
13     while(a!=x)
14     {
15         if(a<x)
16         {
17             cout<<"Bigger than the number."<<endl;
18             cout<<"Your guess number is(1~100): ";
19             cin>>a;
20         }
21         else
22         {
23             cout<<"Lower than the number. "<<endl;
24             cout<<"Your guess number is(1~100): ";
25             cin>>a;
26         }
27     }
28     cout<<"Congretulations.You guessed it.~";
29     return 0;
30  }

Screenshop:

技術分享圖片

(do-while

Code:

 1 #include<iostream>
 2 #include<ctime>
 3 #include<cstdlib>
 4 using namespace std;
 5 int main()
 6 {
 7     int x;
 8     int a;
 9     srand(time(NULL));
10     x=1+rand()%(100-1+1);
11     cout<<"Your guess number is(1~100): ";
12     do
13     {
14         cin>>a;
15         if(a<x)
16         {
17             cout<<"Bigger than the number."<<endl;
18             cout<<"Your guess number is(1~100): ";
19         }
20         else if(a>x)
21         {
22             cout<<"Lower than the number. "<<endl;
23             cout<<"Your guess number is(1~100): ";
24         }
25     }while(a!=x);
26     cout<<"Congretulations.You guessed it.~";
27     return 0;
28  }

Screenshop:

技術分享圖片

//暴力循環

2-34排列組合(排列

Code:

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std; 
 4 int main()
 5 {
 6     int x[3],k=0,i;
 7     for(x[0]=1;x[0]<=5;x[0]++)
 8     {
 9         for(x[1]=1;x[1]<=5;x[1]++)
10         {
11             if(x[1]==x[0])
12                 continue;
13             else
14             {
15                 for(x[2]=1;x[2]<=5;x[2]++)
16                 {
17                     if(x[2]==x[0]||x[2]==x[1])
18                         continue;
19                     else
20                     {
21                         k++;
22                         for(i=0;i<=2;i++)
23                         {
24                             if(i!=2)
25                            {
26                                 switch(x[i])
27                                 {
28                                     case 1:
29                                         cout<<"red ";
30                                         continue;
31                                     case 2:
32                                         cout<<"yellow ";
33                                         continue;
34                                     case 3:
35                                         cout<<"blue ";
36                                         continue;
37                                     case 4:
38                                         cout<<"white ";
39                                         continue;
40                                     case 5: 
41                                         cout<<"black ";
42                                 }
43                             }
44                             else
45                             {
46                                 switch(x[i])
47                                 {
48                                     case 1:
49                                         cout<<"red\n";
50                                         continue;
51                                     case 2:
52                                         cout<<"yellow\n";
53                                         continue;
54                                     case 3:
55                                         cout<<"blue\n";
56                                         continue;
57                                     case 4:
58                                         cout<<"white\n";
59                                         continue;
60                                     case 5:
61                                     cout<<"black\n";
62                                 }
63                             }
64                         }    
65                     }
66                 }
67             }
68         }
69     }
70     cout<<"Total: "<<k;
71     return 0;
72  }

Screenshop:

技術分享圖片

(組合

Code:

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std; 
 4 int main()
 5 {
 6     int x[3],k=0,i;
 7     for(x[0]=1;x[0]<=5;x[0]++)
 8         for(x[1]=x[0]+1;x[1]<=5;x[1]++)
 9             for(x[2]=x[1]+1;x[2]<=5;x[2]++)
10             { 
11                 k++;
12                 for(i=0;i<=2;i++)
13                 {
14                     if(i!=2)
15                     {
16                         switch(x[i])
17                         {
18                             case 1:
19                                 cout<<"red ";
20                                 continue;
21                             case 2:
22                                 cout<<"yellow ";
23                                 continue;
24                             case 3:
25                                 cout<<"blue ";
26                                 continue;
27                             case 4:
28                                 cout<<"white ";
29                                 continue;
30                             case 5: 
31                                 cout<<"black ";
32                         }
33                     }
34                     else
35                     {
36                         switch(x[i])
37                         {
38                             case 1:
39                                 cout<<"red\n";
40                                 continue;
41                             case 2:
42                                 cout<<"yellow\n";
43                                 continue;
44                             case 3:
45                                 cout<<"blue\n";
46                                 continue;
47                             case 4:
48                                 cout<<"white\n";
49                                 continue;
50                             case 5:
51                             cout<<"black\n";
52                         }
53                     }
54                 }
55             }    
56     cout<<"Total: "<<k;
57     return 0;
58  }

Screenshop:

技術分享圖片

實驗總結


這次試驗主要是類C的部分,CPP的特性我還沒有完全體會到(從I/O流已經可以看出些特點的程度)。

在進行多次判斷時還是用switch更方便。

多數時候for循環較為好用。

寫程序時盡量避免嵌套過多層循環,嚴重拖慢編譯和運行速度。

srand(time(NULL));

x=1+rand()%(100-1+1);

以此來取一定範圍內的隨機數。

枚舉數據類型不同於整型或字符型。

實驗1:c++簡單程序設計(1)