




已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第四章 選擇所有的演算法(Algorithm)都可以用循序(sequential),選擇(selection)以及重複(repetition)三類流程完整描述4.1 Algorithms描述方式l 1.Pseudocode(虛擬程式碼)-較常用 以口語化的方式描述執(zhí)行步驟 Example:(以sequential structure描述) 將兩個數(shù)值存入電腦,存成x和y兩個變數(shù) 將x和y相加,相加後除以2,存入變數(shù)Average 將Average輸出l 2.流程圖(常用流程圖符號請參考表4.1(p.4-4) start輸入x,y計算Average輸出結(jié)束 P.S課本(4-3.3flowchart)有詳細流程圖page4-4(表4-1)有符號名稱介紹l 3.程式碼(以c+描述) int average; int x,y; cinxy; average=(x+y)/2 cout The average value of x and yis0 x=y y0)b.字元比次序(大小):因為char以1byte之整數(shù)儲存 AC E=C 範例程式1 /CharInput.cpp #include using std:cin; using std:cout; int main() char ch; coutPlease input a character(y or n): ch; if(ch=Y|ch=y) coutYou input a yes endl; else coutYou input a no endl;注意:=不可寫成=(assignment operator)會變成將(Y或y)(N或n)指派給chl 2. if statement(省略else) if(條件式)statement 1(加練習題1)l 3. Nested if-else statements 將if-else結(jié)構(gòu)置入if and else之間 for example if(條件1) if(條件2)statement1elsestatement2else statement3範例程式2假設(shè)方程式為,當時,根為,當,根為試寫一程式,判斷方程式之根為實數(shù)或複數(shù),並求其解/Root2.cpp#include using std:cin; using std:cout; using std:endl; int main() double a,b,c,D;coutPlease input a: a; coutPlease input b:b; coutPlease input c:c; if(a=0) if(b=0) coutNo answerendl; else coutOnly one solution- c/bendl;else D=b*b- 4.0*a*c; if(D=0) coutDouble same roots:-b/(2.0*a)0.0) coutTwo real rootsendl; coutFirst root:(-b+sqrt(0)/(2.0*a)endl; coutSecond root:(-b-sqrt(0)/(2.0*a)endl;else coutTwo wnjutic endl; coutFirst root:(-b)/(2.0*a)+isqrt(-D)/(2.0*a)endl; coutSecond root: (-b)/(2.0*a)-isqrt(-D)/(2.0*a)endl;return 0;範例程式3(進階題)試寫一程式,經(jīng)由輸入三角形三邊長,判斷是否可以形成三角形,若可以則計算各內(nèi)角角度。(資料不足時可自行假設(shè))#include using std:cin;using std:cout;using std:endl;#include #include using std:setprecision;int main()double a,b,c;double h;double C1,C2,C3; double max;coutabc; if ( (a+b)c & (b+c)a & (c+a)b )cout These three lengths can form a triangle max)max = a;if ( b max)max = b;h = pow(a+b+c)*(a+b-c)*(a-b+c)*(b+c-a),0.5)/(2*b);cout The height is hendl;C1=asin(h/max)*180/3.14;C2=180-asin(h/a)*180/3.14;C3=(180-C1-C2);cout The angle of tirangle is setprecision(5)C1C, C2C and C3C;else cout These three lengths cannot form a triangleendl;return 0;練習1:寫一個程式,經(jīng)由輸入兩個整數(shù),判斷第一個數(shù)字是否為第二個數(shù)字的倍數(shù)練習2:寫一程式,要求使用者輸入座標(X,Y),判斷其象限l 4. If-else chain(較為簡易可讀) if(條件1) statement1 else if(條件2) statement2 else if(條件3) statement3 else statement4範例程式1Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group./ 本程式寫法有很多種,限於只能用if指令,故程式較長,且故意將寫法與同學不同#include using std:cin;using std:cout;using std:endl;int main()int a,b,c,d,e;int max;int min;couta;max = a;min = a;coutb;if ( b max)max = b;else if (b min)min = b;coutc;if ( c max)max = c;else if (c min)min = c;coutd;if ( d max)max = d;else if (d min)min = d;coute;if ( e max)max = e;else if (e min)min = e;cout nMaximum value is max;cout nMinimum value is minendl;return 0;範例程式2綜合所得稅所得淨額稅率累進差額 /Tax.cpp #include using std:cin; using std:cout; using std:endl; int main() float Income,Tax; double a=0.06,b=0.13,c=0.21,d=0.3,e=0.4; coutPlease input Net Income:Income; if(Income0.0) coutYou input a minus income!endl; else if(Income330000.0) Tax=Income * a; else if(Income890000.0) Tax=Income * b-23100; else if(Income1780000.0) Tax=Income * c-94300; else if(Income3340000.0) Tax=Income * d-254500; else Tax =Income * e-588500; coutYou must payt Taxt to governmentendl; return 0; 範例程式3假設(shè)便利商店工讀生月薪依下列公式計算:060hr,75元/小時;6175hr,以1.2倍計算;76小時以後以1.75倍計算。試撰寫一程式,程式中輸入工讀生該月工作時數(shù),計算實領(lǐng)薪水。#include using std:cout;using std:cin;using std:endl;int main()int WH;double Salary;cout WH;if (WH=60)Salary = 75*WH;else if (WH 75)Salary = 75*(60+15*1.2+double(WH-75)*1.75);cout You can earn $ Salary N.T.n;return 0;範例程式4將學生依下列分類方式分級。059:E; 6069:D; 7079:C; 8089:B; 90100:A;試撰寫一程式,程式中輸入學生成績,印出其分數(shù)所屬等級。#include using std:cout;using std:cin;using std:endl;int main()int exam;cout exam;if (exam60)coutEn;else if (exam 70)coutDn;else if (exam 80)coutCn;else if (exam 90)coutBn;else if (exam = 100)coutAn;return 0;4.3 Switch敘述語法switch(整數(shù)表達式) case 值1: statement 1 break; case 值2: statement 2 break; default: statement 3範例程式1.Seasons.cpp依使用者輸入月份判斷該月份所屬季節(jié)/Seasons.cpp#includeusing std:cin;using std:cout;using std:endl;int main() int month; coutn Please input a month:month;if(month1|month12) coutYou input a wrong month!nendl;else coutmonthmonth is; switch(month%12)/3) case 0: coutWinterendl; break; case 1:coutSpringendl; break; case 2:coutSummerendl; break; case 3:coutAutumnendl; break; default:coutYou never arrival here!endl;return 0;範例程式2寫一個程式,讀入學生成績等級(有A or aB or bC or cD or d),當輸入A時,印出Excellent!,B時,印出Good!,C時,印出Be study hard!,其他則印出Failed!#include using std:cin;using std:cout;using std:endl;int main()char grade;coutgrade;switch(grade)case A: case a:coutExcellentendl;break;case B: case b:coutGoodendl;break;case C: case c:coutbe study hardendl;break;case D: case d:coutFailedendl;break;default:coutYou input a wrong characterendl;return 0;範例程式3.說明char也可以比對大小,且字母的ASCII值介於65和122之間本程式檢查使用者輸入的字母是子音還是母音 /CharTest.cpp #include using std:cin; using std:cout; using std:endl; int main() char c; coutn Please input a character:c; if(c122) coutYour input is not a char!; else coutThe char you input is; switch(c) case a: case e: case i:case o:case u:case A:case E:case I:case O:case U:cout母音字母endl;break;default: cout子音字母endl;return 0;範例程式4以Switch撰寫程式:將學生成績依下列方式分級。(059:E)、(6069:D)、(7079:C)、(8089:B)、(90100:A),輸入分數(shù)時,自動告知其等級。#include using std:cin;using std:cout;using std:endl;int main()int exam;coutexam;if (exam 100 | exam 0)cout You input a wrong value.endl;elseswitch(exam/10)case 0: case 1: case 2: case 3: case 4: case 5:coutEendl;break;case 6:coutDendl;break;case 7: coutCendl;break;case 8:coutBendl;break;case 9:cout Aendl;break;default:coutProgram never arriveendl;return 0;範例程式5請用Switch 指令撰寫停車場收費系統(tǒng):已知收費停車場小客車收費20元/hr,大客車收費50元/hr,機車10元/hr,腳踏車5元/hr,不足一小時以一小時計算,不足10分鐘不算費用,請設(shè)計一個收費系統(tǒng),能輸入車種及停車時數(shù),自動判斷應收費用。#include using std:cin;using std:cout;using std:endl;#include int main()int kind;double time;int itime,pay;cout please input the kind of carn 1 for car, 2 for bus, 3 for motorcycle, 4 for bicyclekind;if ( (kind=1) & (kind =4)cout please input parking hours:time;if (int(time*60)%60 10)itime = floor(time);elseitime = ceil(time);switch(kind)case 1: pay = itime*20;break;case 2:pay = itime*50;break;case 3:pay = itime*10;break;case 4:pay = itime*5;break;default:cout The program never arrive here. endl;cout You must pay pay dollars.endl; else cout Your input cannot regonize.endl;return 0;範例程式6試用switch語法撰寫一程式,計算大明是否被列入明道管理學院期中預警名單之中,已知計算方式為(A)期中前小考三次平均得x,占60%。(B)作業(yè)成績有兩次得h,占40%。(C)加分紀錄a,額外計算。(D)扣分紀錄m,額外計算。故期中成績x計算公式如下:輸入大明各項成績,總成績小於60分時,列入預警名單,低於70分時,列入觀察名單。高於90分時,列入優(yōu)秀名單。#include using std:cin;using std:cout;using std:endl;#include int main()double t1,t2,t3;double h1,h2;double a;double m;double y;int judge; coutPlease input the test record with 3 timest1t2t3;cout Pplease input homework record with 2 timesh1h2;cout Please input the add recorda;cout Please input the minus recordm;y = (t1+t2+t3)/3*0.6+(h1+h2)/2*0.4+a-m;judge = int(y/10);switch(judge)case 0: case 1: case 2: case 3: case 4: case 5: coutYou must study very hard.endl; break;case 6: coutYou must study hard.endl; break;case 9: case 10: coutYou are very good student.endl; break;default: coutYou are good student.endl;return 0;4.4 條件運算子(Conditional Operator)語法:條件式 ? 表達式1 : 表達式2說明:當條件式的值為true,選擇表達式1,否則選擇表達式2Example:1.if(ab) coutab; else cout=b;可寫成(ab)?(a=b);Example:2.if(ab) c=a; else c=b; 可寫成c=(ab) ? a : b);範例程式1.Condition.cpp可判斷兩個數(shù)值何者較大,及奇偶數(shù) /Condition.cpp #include using std:cin; using std:cout; using std:endl; int main() float a,b; int N; cout(1)Please input two numbers:a; cinb; coutThe bigger one between a and b is b? a : b )endl; cout(2)Please input a integer N:N; coutN is(N%2?odd:even)endl; return 0;範例程式2.試撰寫一程式,輸入三個整數(shù),找出最大及最小值,並計算三數(shù)之平均值。注意,平均值可能有小數(shù)點。#include using std:cout;using std:cin;using std:endl;int main()int A,B,C;int max,min;double average;cout A;cinB;cinC; max = ( (C (AB?A:B)?C:(AB?A:B) );min = ( (C (AB?A:B)?C:(AB?A:B) ); average = double(A+B+C)/3;cout nMaximum value is max;cout nMinimum value is min;cout nAverage value is average;return 0;練習題.買東西時,輸入所應付款金額及實際交給店員金額,輸出則為應找回最少的鈔票數(shù)與錢幣數(shù)。例如買了一個東西33元,給店員1000元,應找回967元,其中有500元一張、100元四張、 50元硬幣一個、十元硬幣一個、五元硬幣一個及一元硬幣一個。l *第四章作業(yè)l *1. Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group.2. Write a program that reads in the radius of a circle and prints the circles diameter, circumferences and area.3. Writes a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers.4. Write a program that calculates the squares and cubes of the numbers from 0 to 10 and uses tabs to print the following tables of values:Number square cube0 0 01 1 12 4 83 9 27.10 100 10005. Write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each.6. Write a program that reads in two integers and determines and prints if the first is a multiple of the second.7. Suppose the students can pass the exam if his grade is greater than 60. Write a program that can input the students grade and determine whether he can pass the exams. 8. 已知兩點可以決定一條線性函數(shù),試寫一程式,經(jīng)由輸入兩點座標即可計算及之值。9. 已知二次方程式,試寫一程式,經(jīng)由輸入、及求算其根。10. 試寫一程式,經(jīng)由輸入三角形三邊長,判斷是否可以形成三角形,若可以則計算各內(nèi)角角度。(資料不足時可自行假
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 基于綠色生態(tài)的農(nóng)業(yè)經(jīng)濟一體化管理合同
- 人物動作課件
- 兒童畫斑馬課件圖文
- 綠色食品供應與銷售合作合同
- 培訓企業(yè)的課件
- 崗位評估培訓課件
- 2026版《全品高考》選考復習方案生物02 第二單元 細胞的結(jié)構(gòu)與物質(zhì)的運輸06 課時作業(yè)(六) 細胞器與生物膜系統(tǒng)含答案
- 正定企業(yè)培訓課件
- 林業(yè)有害生物防治基礎(chǔ)設(shè)施建設(shè)項目規(guī)劃設(shè)計方案(范文模板)
- 培訓課件和講師
- 2025年校長職級考試題及答案
- 統(tǒng)借統(tǒng)還資金管理辦法
- 國家能源集團采購管理規(guī)定及實施辦法知識試卷
- 2023-2024學年四川省成都市高新區(qū)八年級(下)期末數(shù)學試卷
- 2025年廣西繼續(xù)教育公需科目考試試題和答案
- 2024年廣州市南沙區(qū)社區(qū)專職招聘考試真題
- 心理健康科普常識課件
- 山東醫(yī)藥技師學院招聘筆試真題2024
- 倉庫超期物料管理制度
- 奶茶公司供應鏈管理制度
- 加氣站風控分級管理制度
評論
0/150
提交評論