400 likes | 559 Vues
Chapter 5. 重覆. 連提款卡都有三次輸入密碼的機會,為何我沒有?. 溫故而知新. #include <iostream.h> #include <conio.h> main() { int in_value; cout<< “ Enter the secret number: ” ; cin>>in_value; if(in_value==29) cout<< “ You guessed the secret number!<br> ” ;
E N D
Chapter 5 重覆
連提款卡都有三次輸入密碼的機會,為何我沒有?連提款卡都有三次輸入密碼的機會,為何我沒有? 溫故而知新 #include <iostream.h> #include <conio.h> main() { int in_value; cout<<“Enter the secret number: ”; cin>>in_value; if(in_value==29) cout<<“You guessed the secret number!\n”; cout<<“Program completed, thank you.\n”; getch(); }
重覆(iteration) • 令某段程式碼重覆執行任意次數的功能 • C++提供三種重覆執行的控制結構 • for迴圈 • 已知或欲執行的迴圈次數固定時較宜使用 • while迴圈 • 若無法預知程式執行次數或無固定的迴圈變量時較宜使用 • do…while迴圈 • 無論如何都必須先執行迴圈主體一次,再行判斷是否要持續執行迴圈時適用
i=i+1 for迴圈 int i; for(i=1;i<10;i++) cout<<i<<endl; • 單一敘述 for (初始狀態設定;執行條件;狀態修訂) 敘述; • 複合敘述 for (初始狀態設定;執行條件;狀態修訂) { 敘述一; 敘述二; : 敘述 n; } int i; for(i=1;i<10;i++) { cout<<“(”<<i<<“)”; cout<<i*i<<endl; }
for迴圈(續) • 當一個for迴圈執行時,以下幾個步驟將依序發生 1.設定計數器變數之起始值 2.檢查執行條件 • 如果結果為真,執行步驟3 • 如果結果為假,執行步驟6 3.執行此迴圈之主體部分 4.迴圈計數器變數內容修訂 5.跳至步驟2執行 6.結束此迴圈 for(i=1;i<10;i++) for(i=1;i<10;i++) for(i=1;i<10;i++)
for迴圈(續) 設定計數器變 數之初始值 No 條件 Yes 敘述式 計數變數修訂
開始 for迴圈(續) sum=0 i=1 No • 設計一程式,計算1+2+3+ … +10= ? i<=10 Yes #include <iostream.h> #include <conio.h> main() { int i,sum; sum=0; for(i=1;i<=10;i++) sum=sum+i; cout<<“1+2+…+10=”<<sum; getch(); } sum=sum+i i=i+1 Print sum 結束
for迴圈(續) #include <iostream.h> #include <conio.h> main() { int a,b,c,i,sum; sum=0; cout<<“起始值=”; cin>>a; cout<<“最終值=”; cin>>b; cout<<“公 差=”; cin>>c; for(i=a;i<=b;i=i+c) sum=sum+i; cout<<“sum=”<<sum; getch(); } • 設計一程式,可給定起始值a、最終值b及公差c,計算出a+(a+c)+(a+2c)+…+b=? • a=2, b=10, c=2 sum=2+4+6+8+10=30
#include <iostream.h> #include <conio.h> main() { int a,i,sum; cout<<“請輸入任意數值”; cin>>a; sum=a; for(i=1;i<=20;i++) { a=a-i; sum=sum+a; } cout<<“sum=”<<sum; getch(); } for迴圈(續) • 設計一程式,給定任意值a,讓a每次分別遞減1,2,…,共計遞減20次求最後總合為何? • a=100 遞減次數=5 sum=100+99+97+94+90+ 85=565
for迴圈(續) • 設計一程式,計算100+98+96+…+2=? #include <iostream.h> #include <conio.h> main() { int i,sum; sum=0; for(i=100;i>=2;i-=2) sum=sum+i; cout<<“100+98+…+2=”<<sum; getch(); } i=i-2
for迴圈(續) • 溫故知新 #include <iostream.h> #include <conio.h> main() { int in_value; cout<<“Enter the secret number: ”; cin>>in_value; for(;in_value!=29;) { cout<<“You have guessed incorrectly, please try again : ”; cin>>in_value; } cout<<“You guessed the secret number!\n”; cout<<“Program completed, thank you.\n”; getch(); }
while迴圈 • 單一敘述 • 複合敘述 while(ch!=‘q’ || ch!=‘Q’) cin>>ch; while(條件運算式) 敘述; while(條件運算式) { 敘述1; 敘述2; : 敘述n; } while(ch!=‘q’ || ch!=‘Q’) { cin>>ch; cout<<ch; }
while迴圈(續) • 緊隨在保留字while後大括號內的敘述將一再重覆執行,直到小括號的條件被判斷為假為止 條件 No Yes 敘述式
while迴圈(續) • 溫故知新 #include <iostream.h> #include <conio.h> main() { int in_value; cout<<“Enter the secret number: ”; cin>>in_value; while(in_value!=29) { cout<<“You have guessed incorrectly, please try again : ”; cin>>in_value; } cout<<“You guessed the secret number!\n”; cout<<“Program completed, thank you.\n”; getch(); }
while迴圈(續) #include <iostream.h> #include <conio.h> main() { char ch; cin>>ch; while(ch!=‘Q’ || ch!=‘q’) { cout<<“你鍵入了”<<ch; cin>>ch; } cout<<“程式結束”; getch(); } • 設計一程式,當使用者鍵入任意字元時,皆同時在螢幕上呈現,惟獨鍵入Q或q時結束程式執行
while迴圈(續) • 設計一程式,顯示出範圍介於1至30000間的Fibonacci數列 • 費氏數列 • fib(0)=1 • fib(1)=1 • fib(n)=fib(n-2)+fib(n-1) if n>2
while迴圈(續) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 #include <iostream.h> #include <conio.h> main() { long temp,lowf,highf; lowf=0; highf=1; while(highf<30000) { cout<<highf<<endl; temp=lowf; lowf=highf; highf=temp+lowf; } getch(); }
do—while迴圈 • 與while相似,不同處在於其條件運算式係在迴圈敘述主體執行後再做檢查 • 單一敘述 複合敘述 do 敘述; while(條件運算式); do { 敘述1; 敘述2; : 敘述n; } while(條件運算式);
do—while迴圈(續) 敘述 Yes 條件 No
do—while迴圈(續) • 設計一溫度轉換程式,使用者可任意選擇攝氏轉華氏或是華氏轉攝氏,轉換完成後詢問使用者是否繼續,倘若選擇繼續,則重新顯示溫度轉換的選項,否則結束程式 1.攝氏轉華氏 2.華氏轉攝氏 請選擇…1 請輸入攝氏溫度=0 攝氏0度=華氏32度 是否繼續(Y/N)…y 1.攝氏轉華氏 2.華氏轉攝氏 請選擇…
do—while迴圈(續) #include <iostrem.h> main() { float c,f,sel; char ch; do{ cout<<“1.攝氏轉華氏 2.華氏轉攝氏 請選擇…”; cin>>sel; if(sel==1) { cout<<“請輸入攝氏溫度=” ; cin>>c; f=(9*c)/5+32; cout<<“攝氏”<<c<<“度=華氏”<<f<<“度”; }
do—while迴圈(續) else { cout<<“請輸入華氏溫度=” ; cin>>f; c=(f-32)*5/9; cout<<“華氏”<<f<<“度=攝氏”<<c<<“度”; } cout<<endl<<“是否繼續(Y/N)…”; cin>>ch; }while(ch==‘Y’ || ch==‘y’); }
巢狀迴圈 for (初始狀態設定;執行條件;狀態修訂) for (初始狀態設定;執行條件;狀態修訂) 敘述; for (初始狀態設定;執行條件;狀態修訂) { 敘述一; 敘述二; : for (初始狀態設定;執行條件;狀態修訂) { 敘述一; 敘述二; : 敘述 n; } }
巢狀迴圈(續) do { 敘述1; 敘述2; : do { 敘述1; 敘述2; : 敘述n; } while(條件運算式); } while(條件運算式); while(條件運算式) { 敘述1; 敘述2; : while(條件運算式) { 敘述1; 敘述2; : 敘述n; } }
美化輸出的setw()欄寬操作子 • setw() • iomanip.h • cout<<setw(n)<<變數或運算式等; • n為整數值,用以指定輸出資料串流佔用的欄寬 • 設定的欄寬大於顯示數值或字串所需的欄寬時,資料向右切齊顯示 chi=98; eng=100 cout<<“國文=”<<chi<<endl; cout<<“英文=”<<eng<<endl; chi=98; eng=100 cout<<“國文=”<<setw(3)<<chi<<endl; cout<<“英文=”<<setw(3)<<eng<<endl; 國文=98 英文=100 國文= 98 英文=100
九九乘法表 2*1= 2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*1=2 cout<<“2*”<<i<<“=”<<setw(2)<<2*i; #include <iostream.h> #include <conio.h> #include <iomanip.h> main() { int i; for(i=1;i<=9;i++) cout<<“2*”<<i<<“=”<<setw(2)<<2*i<<endl; getch(); }
九九乘法表(續) 2*1= 2 : 2*9=18 3*1= 3 : 3*9=27 : : 9*1= 9 : 9*9=81 cout<<i<<“*”<<j<<“=”<<setw(2)<<i*j; 外層迴圈 i=2 j=1~9 i=3 j=1~9 : : i=9 j=1~9 for(i=2;i<=9;i++) for(j=1;j<=9;j++) 內層迴圈
九九乘法表(續) #include <iostream.h> #include <conio.h> #include <iomanip.h> main() { int i,j; for(i=2;i<=9;i++) for(j=1;j<=9;j++) cout<<i<<“*”<<j<<“=”<<setw(2)<<i*j<<endl; getch(); }
九九乘法表(續) 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9 2*2= 4 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 : : : : : : : : : : : : : : : : 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 外層迴圈 i*j=i*j 內層迴圈 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9 2*2= 4 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 : : : : : : : : : : : : : : : : 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
九九乘法表(續) #include <iostream.h> #include <conio.h> #include <iomanip.h> main() { int i,j; for(j=1;j<=9;j++) { for(i=2;i<=9;i++) cout<<i<<“*”<<j<<“=”<<setw(2)<<i*j<<““; cout<<endl; } getch(); }
break敘述 • 當程式執行遇到break時,程式會離開break所在的迴圈或switch敘述 迴圈中的條 件運算式 No Yes break;
break敘述(續) #include <iostream.h> #include <conio.h> main() { int i,a,b,c,stop; cout<<“請輸入終止條件..”; cin>>stop; for(i=1;i<=10000;i++) { a=i; b=i*2; c=a+b; if(c>stop) break; } cout<<“C=”<<c; getch(); }
continue敘述 • 當迴圈內部的條件運算式為真時,回到迴圈的第一道敘述重新執行 Yes 迴圈中的條 件運算式 continue; No
continue敘述(續) #include <iostream.h> #include <conio.h> main() { int sel; char ch; do{ cout<<“1.春 2.夏 3.秋 4.冬…”; cin>>sel; switch(sel) { case 1:cout<<“Spring !!”; break; case 2:cout<<“Summer !!”; break; case 3:cout<<“Autumn !!”; break; case 4:cout<<“Winter !!”; break; default:continue; } cout<<“是否繼續(Y/N)…”; cin>>ch; }while(ch==‘y’ || ch==‘Y’); }
亂數(Random number) • 坊間有許多題庫系統可由電腦任意選題製作成試卷,其方法即採用亂數選擇的方式產生,再如統計與實驗時所需要的大量模擬數據,亦是由亂數協助產生,甚至電動玩具中的武將對決,張飛可以砍司馬懿1~3刀(在相同經驗值與體力時,有時三刀全中,有時卻可能有一刀失誤)等,皆與亂數有密切的關係
亂數(續)-Borland C++ • 亂數值函數 random(n) • stdlib.h • n : 欲產生最大亂數值的上界 • 能產生0~n-1的整數亂數 • 產生亂數種子函數 randomize() • stdlib.h 以及 time.h • 依據時間函數time()啟動亂數產生器產生亂數 • 如果不以randomize()來播放亂數的種子,則每次使用random()函數時,將會得到相同順序的亂數
亂數(續)-Visual C++ • 亂數值函數 rand() • stdlib.h • 能產生0~32767的整數亂數 • 產生亂數種子函數 srand(time(0)) • stdlib.h 以及 time.h • 依據時間函數time()啟動亂數產生器產生亂數 • 如果不以srand()來播放亂數的種子,則每次使用rand()函數時,將會得到相同順序的亂數
亂數(續) • 請設計一程式,由電腦產生一個數字(介於1-100)讓使用者來猜猜看,當使用者猜錯時,提示使用者所猜的數值太大或太小,猜對時顯示恭喜猜對的字句,並詢問是否繼續與電腦玩猜數字遊戲 請輸入你所猜的數字:38 Sorry!!太小 請輸入你所猜的數字:61 Sorry!!太大 請輸入你所猜的數字:52 恭喜答對了!! 是否繼續(Y/N)…y 請輸入你所猜的數字:
亂數(續) #include <iostream.h> #include <stdlib.h> #include <time.h> main() { int comuter,user; char ch; randomize(); do{ computer=random(100)+1; do{ cout<<“請輸入你所猜的數字:”; cin>>user; if(user>computer) cout<<“Sorry!!太大”; else if(user<computer) cout<<“Sorry!!太小”; }while(user!=computer); cout<<“恭喜答對了!!\n\n”; cout<<“是否繼續(Y/N)…”; cin>>ch; cout<<endl; }while(ch==‘Y’ || ch==‘y’); }
Homework #3 • 請寫一個能與電腦玩剪刀、石頭、布的遊戲