第4章循環(huán)結(jié)構(gòu)程序設(shè)計(jì)ppt課件_第1頁(yè)
第4章循環(huán)結(jié)構(gòu)程序設(shè)計(jì)ppt課件_第2頁(yè)
第4章循環(huán)結(jié)構(gòu)程序設(shè)計(jì)ppt課件_第3頁(yè)
第4章循環(huán)結(jié)構(gòu)程序設(shè)計(jì)ppt課件_第4頁(yè)
第4章循環(huán)結(jié)構(gòu)程序設(shè)計(jì)ppt課件_第5頁(yè)
已閱讀5頁(yè),還剩27頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、2021年11月10日10時(shí)08分1第第4章章 循環(huán)結(jié)構(gòu)程序設(shè)計(jì)循環(huán)結(jié)構(gòu)程序設(shè)計(jì)4.1 循環(huán)語(yǔ)句的四種循環(huán)格式4.1.1 goto 格式 if goto 22021年11月10日10時(shí)08分例: 計(jì)算s=1+2+3+50 #include main() int i=0, s=0; loop: if (i51) s=s+i; i=i+1; goto loop; couts=sendl; 2021年11月10日10時(shí)08分34.1.2 while格式格式 while ; 例例: 計(jì)算計(jì)算 s=1+2+3+502021年11月10日10時(shí)08分4 #include main() int i=0, s

2、=0; while (i50) i=i+1; s=s+i; couts=sendl; 2021年11月10日10時(shí)08分54.1.3 do- while格式格式 do ; while 例例: 計(jì)算計(jì)算 s=1+2+3+502021年11月10日10時(shí)08分6#include main() static int i, s; do s=s+i; i=i+1; while (i51); couts=sendl; 72021年11月10日10時(shí)08分 4.1.4 for( ) 格式格式 for(;) 說(shuō)明:說(shuō)明:1.決定了循環(huán)的初值決定了循環(huán)的初值, 決定循環(huán)的條件決定循環(huán)的條件, 決定循環(huán)的增決定循

3、環(huán)的增量。量。2.在循環(huán)中只執(zhí)行一次。在循環(huán)中只執(zhí)行一次。,在循環(huán)中每次都要執(zhí)行一次。在循環(huán)中每次都要執(zhí)行一次。2021年11月10日10時(shí)08分8例:求例:求s=1+2+3+100 程序程序1: #include main() int i, s=0; for (i=1; i101; i+) s+=i; cout“sum=”sendl; 92021年11月10日10時(shí)08分程序程序2: #include main() int i=1, s; for (s=0; i101; i+) s+=i; coutsum= sendl; 2021年11月10日10時(shí)08分10程序程序3: #include

4、main() int i=1, s=0; for (; i101;) s+=i+; coutsum= sendl; 2021年11月10日10時(shí)08分11程序程序4: #include main() int i=1, s=0; for (; i101; s+=i+); coutsum=sendl; 2021年11月10日10時(shí)08分12程序程序5: #include main() int i=100, s=0; for (; i ; i-) s+=i ; coutsum= sendl; 2021年11月10日10時(shí)08分13例:編程計(jì)算下列各項(xiàng)例:編程計(jì)算下列各項(xiàng)s=1+2+3+100s=1+

5、22+32+1002s=1+3+5+99s=1+1/2+1/3+1/100s=1/1+1/22+1/32+1/1002n!=1*2*3*ns=k!=1+2!+3!+n!s=1-2+3+(-1)nns=1-1/2+1/3+(-1)n/ns=1-22+32-+(-1)n 1002e=1+1/1!+1/2!+1/3!+1/n!直到最后一項(xiàng)小于直到最后一項(xiàng)小于0.000012021年11月10日10時(shí)08分14ex=1+x/1!+x2/2!+x3/3!+xn/n!直到最后直到最后一項(xiàng)小于一項(xiàng)小于0.001s=1-1/2!+1/3!+(-)(n+1)/n! 直到最后一項(xiàng)直到最后一項(xiàng)小于小于0.001s=

6、1+x+x/2!+x/3!+x/n! 直到最后一項(xiàng)直到最后一項(xiàng)小于小于0.00001s=1-x+x/2!-x/3!+(-)(n+1)x/n! 直到最直到最后一項(xiàng)小于后一項(xiàng)小于0.001/4=1-1/3+1/5-1/7+ 求求,直到最后一直到最后一項(xiàng)小于項(xiàng)小于0.0012021年11月10日10時(shí)08分152. 循環(huán)可以從小向大循環(huán)可以從小向大,也可以從大到小。也可以從大到小。例例: 計(jì)算計(jì)算s=1+2+3+100 #include main() int i, s=0; for (i=100;i0;i-) s=s+i; cous=sendl;2021年11月10日10時(shí)08分163循環(huán)變量的值起

7、三個(gè)作用,一是記數(shù),二是可循環(huán)變量的值起三個(gè)作用,一是記數(shù),二是可以參加循環(huán)體計(jì)算,三是用做數(shù)組的下標(biāo)。以參加循環(huán)體計(jì)算,三是用做數(shù)組的下標(biāo)。例例: 計(jì)算計(jì)算10個(gè)數(shù)的和個(gè)數(shù)的和 #include main() int i, s=0; static int a10=1,3,4,5,4,3,3,3,4,2; for (i=1;i10;i+) s=s+ai; couts=sendl; 2021年11月10日10時(shí)08分174. 三個(gè)表達(dá)式可以有,也可以全無(wú),但三個(gè)表達(dá)式可以有,也可以全無(wú),但“;”不不能少。如果表達(dá)式能少。如果表達(dá)式2無(wú),程序?qū)⑦M(jìn)入無(wú)限循環(huán)。無(wú),程序?qū)⑦M(jìn)入無(wú)限循環(huán)。5. 可以用可以

8、用break命令中斷循環(huán)命令中斷循環(huán).例例: 計(jì)算計(jì)算s=1+3+5+n直到最后一項(xiàng)為直到最后一項(xiàng)為99止止. #include main() int i, s=0; for (i=1; ;i=i+2) if (i=101) break; s=s+i; coutsendl;2021年11月10日10時(shí)08分186. 可以用可以用continue命令中斷當(dāng)前這一次循環(huán)命令中斷當(dāng)前這一次循環(huán),繼續(xù)下一次繼續(xù)下一次循環(huán)循環(huán).例例: 計(jì)算計(jì)算s=1+3+5+99 #include main() int i, s=0; for (i=1;i100;i+) if (i%2=0) continue; s=s

9、+i; couts=sendl; 2021年11月10日10時(shí)08分194.2 多層循環(huán)多層循環(huán)例:開設(shè)例:開設(shè)3門課程門課程,計(jì)算每個(gè)學(xué)生平均成績(jī)計(jì)算每個(gè)學(xué)生平均成績(jī) #include main() int i,j,s=0; int a53=87,78,79,87,98,67,56,79,98; for (i=0;i3;i+) s=0; for(j=0;j3;j+) s=s+aij; coutaij ; couts/3endl; 2021年11月10日10時(shí)08分204.3 循環(huán)中斷循環(huán)中斷 break 4.4 exit()函數(shù)函數(shù)例例: 從鍵盤上輸入字母從鍵盤上輸入字母,然后輸出比它的然后

10、輸出比它的ascii碼大碼大1 的字的字母母#include #include stdio.h main() char ch=a; coutinput letter s=; 2021年11月10日10時(shí)08分21while (ch!=#) ch=getchar(); if (ch=z|ch=z ) putchar(ch-25); else putchar(ch+1); 2021年11月10日10時(shí)08分22 題題1:輸出九九乘法表:輸出九九乘法表 1 2 3 4 5 6 7 8 91 1 2 3 4 5 6 7 8 92 2 4 6 8 10 12 14 16 189 9 18 27 36 4

11、5 54 63 72 812021年11月10日10時(shí)08分23#include #include iomanip.hmain()int k,j;cout ; for(k=1;k10;k+) coutksetw(5); /width(5) coutn; 2021年11月10日10時(shí)08分24 for(k=1;k10;k+) coutsetw(5)k; for(j=1;j10;j+) coutsetw(5)k*j; coutendl; / cout.width(5);coutk*j;2021年11月10日10時(shí)08分25#include #include iomanip.hmain()int k

12、,j; cout ; for(k=1;k10;k+) cout.width(5); coutk; coutn;for(k=1;k10;k+) coutsetw(5)k; for(j=1;j10;j+) cout.width(5);coutk*j; coutendl; 2021年11月10日10時(shí)08分26題題2:判斷:判斷m是否是素?cái)?shù)。是否是素?cái)?shù)。 #include main() int k,m,h=1; cinm; for(k=2;km;k+) if (m%k=0) h=0; if( h=1) coutm“ is a prime number”endl; 2021年11月10日10時(shí)08分2

13、7 題題3;求出;求出100以內(nèi)的所有素?cái)?shù)以內(nèi)的所有素?cái)?shù) #include main() int k,m; for(m=3;m100;m=m+1) for(k=2;km;k+) if (m%k=0) break; if(k=m) coutmis a prime numberendl; 2021年11月10日10時(shí)08分28題題4:求:求m,n 的最大公約數(shù)的最大公約數(shù) #include main( ) int m,n,m1,n1,r; cinmn; m1=m;n1=n; r=m%n; while (r!=0) m=n;n=r;r=m%n; coutm=m1endl; coutn1=n1endl

14、; coutr=nendl; 2021年11月10日10時(shí)08分29題5:求fibonaccii(斐波那契)數(shù)列中前20項(xiàng)中每一項(xiàng)(遞推法) 0,1,1,2,4,7,13, #include main() long k,a=0,b=1,c=1,d; for(k=1;k18;k+) d=a+b+c; coutdt; a=b;b=c;c=d; 2021年11月10日10時(shí)08分30題題6:2000年人口年人口13億,年增長(zhǎng)億,年增長(zhǎng)0.5%和和2.1%,多少年后達(dá)到或超過(guò),多少年后達(dá)到或超過(guò)20億億(87,21)? #include main() int n=0; float f=13,e=0.0

15、05; while (f20) f=f*(1+e); n=n+1; coutnendl; 2021年11月10日10時(shí)08分31題題7:產(chǎn)生:產(chǎn)生10個(gè)學(xué)生的成績(jī)個(gè)學(xué)生的成績(jī),呈正態(tài)分布呈正態(tài)分布#include #include #include math.h#include stdlib.h#include iomanip.h main( ) int k;float x;srand(time(null); for(k=1;k11;k+) x=rand()/32767.0*90; x=sin(x*3.14/180)*71+30; cout(int)xsetw(5); if (k%10=0) coutendl; 2021年11月10日10時(shí)08分32題題13:梯形

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論