C語言程序設計何欽銘顏暉第3章分支結構_第1頁
C語言程序設計何欽銘顏暉第3章分支結構_第2頁
C語言程序設計何欽銘顏暉第3章分支結構_第3頁
已閱讀5頁,還剩6頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、第3章分支結構【練習3-1】例3-4中使用else-if 語句求解多分段函數,為了檢查else-if 語句的三個分支是否正確,已經設計了三組測試用例,請問還需要增加測試用例 嗎?為什么?如果要增加,請給出具體的測試用例并運行程序。解答:最好再增加兩組測試用例,因為尚未對分段函數參數的邊界值進行測試。可再給 出x=0和x=15時的兩種情況。【練習3-2】計算符號函數的值:輸入一個整數x ,計算并輸出下列分段函數sign(x)的值。-1 x<0ry=sig n( x)= 0x=01 x>0I解答:#i nclude <stdio.h>int main(v oid)int x

2、, y;prin tf("E nter x:");sca nf("%d", &x); if(x>0)y=1;else if (x=0) y=0;else y=-1;printf("sign( %d ) = %dn",x,y);return 0;【練習3-3】統計學生平均成績與及格人數:輸入一個正整數n,再輸入n個學生的成績,計算平均成績,并統計所有及格學生的人數。試編寫相應程序。解答:# in elude <stdio.h>int main(v oid)int coun t, i, n;double grad

3、e, total;printf("Enter n:"); sca nf("%d", &n); total = 0;count = 0;for(i = 1; i <= n; i+) printf("Enter grade #%d: ", i);scanf ("%lf", &grade);total = total + grade; if(grade >= 60) count+; printf("Grade average = %.2fn", total / n); pr

4、intf("Number of pass = %dn", count);return 0;【練習 3-4 】統計字符:輸入 10 個字符,統計其中英文字母、空格或回車、數 字字符和其他字符的個數。試編寫相應程序。解答:#include <stdio.h>int main(void)int i;int blank, digit, letter, other;char c;blank = digit = letter = other = 0;for(i=1;i<=15;i+) c = getchar(); if(c>='a' &&

5、amp; c <= 'z' | c>='A' && c <= 'Z') letter+;else if(c>='0' && c <= '9') digit+;else if(c=' '|c='n') blank+;else other+; printf("letter=%d,blank=%d,digit=%d,other=%dn", letter, blank, digit, other);return

6、 0;【練習 3-5 】輸出閏年:輸出 21 世紀中截至某個年份之前的所有閏年年份。判 斷閏年的條件是: 能被 4整除但不能被 100整除,或者能被 400 整除。試編寫相 應程序。解答:# include <stdio.h>int main(void)int year; printf("Enter year: "); scanf("%d", &year);for(;year>=2000;year-)if( (year % 4 = 0 && year % 100 != 0 ) | year % 400 = 0)

7、printf("It is a leap yearn");else printf("It is not a leap yearn ");return 0;【練習 3-6 】在例 3-8 程序中, 如果把 switch 語句中所有的 break 都去掉,運 行結果會改變嗎?如果有變化,輸出什么?為什么?解答:如果去掉所有的 break 語句,運行結果會改變,輸出 price = 0.0 ,因為不管 choice 值與其中某個常量表達式相等, 當去掉 break 語句時,其后的所有語句 段都將運行,故每次都將執行到 price=0.0 語句為止。【練習 3-

8、7 】成績轉換:輸入一個百分制成績,將其轉換為五分制成績。百分制 成績到五分制成績的轉換規則:大于或等于90分為A,小于90分且大于或等于80分為B,小于80分且大于或等于70分為C,小于70分且大于或等于60分為 D,小于60分為E。試編寫相應程序。解答:#include <stdio.h>int main(void)double grade;printf("Input grade: "); scanf(“ %lf ” ,&grade);If(grade>=90) printf("A"); else if(grade>=

9、80) printf("B");else if(grade>=70) printf("C");else if(grade>=60) printf("D");else printf("E");return 0;或#include <stdio.h>int main(void)double score;char grade;printf("Input your score: ");scanf("%lf",&score); switch(int(sc

10、ore/10) case 10:case 9: grade='A' ; break;case 8:grade='B' break;case 7:grade='C' break;case 6:grade='D' break;default:grade='E' break;printf("Your grade is: %c",grade);return 0;【練習3-8】查詢水果的單價:有4種水果,蘋果(apples )、梨(pears)、桔 子(oranges)和葡萄(grapes),單價分別是3

11、.00元/公斤,2.50元/公斤, 4.10 元/ 公斤和 10.20 元/ 公斤。在屏幕上顯示以下菜單(編號和選項) ,用戶 可以連續查詢水果的單價, 當查詢次數超過 5 次時, 自動退出查詢; 不到 5 次 時,用戶可以選擇退出。當用戶輸入編號14,顯示相應水果的單價(保留兩位小數);輸入 0,退出查詢;輸入其他編號,顯示價格為 0 。試編寫相應程序。1 apples2 pears3 oranges4 grapes0 Exit解答:#include <stdio.h>int main(void)int ri;int choice;float price;for(ri=1; ri

12、<=5; ri+)printf("1 applesn");printf("2 pearsn");printf("3 orangesn");printf("4 grapesn");printf("0 Exitn");scanf("%d", &choice); if(choice=0) break;else switch(choice)case 1: price=3.00; break; case 2: price=2.50; break; case 3: pric

13、e=4.10; break; case 4: price=10.20; break; default: price=0;break;printf("price=%0.2fn", price);return 0;【練習 3-9】 請讀者重新編寫例 3-4 的程序,要求使用嵌套的 if - else 句,并上機運行。解答:#include <stdio.h>int main(void)double x, y;printf("Enter x:") ;scanf("%lf", &x);if (x >15)y = 2.

14、5 * x - 10.5;else if(x<0)y=0;elsey=4*x/3;printf("f(%.2f)=%.2fn", x, y);return 0;兩條【練習 3-10 】在例 3-12 中,改寫 if 語句前, y= x + 1; 和 y= x + 2; 語句的執行條件是什么?改寫后呢?解答:改寫前: y=x+1 的執行條件是 x<1; y=x+2 的執行條件是 1<=x<2 。 改寫后: y=x+1 的執行條件是 x<1; y=x+2 的執行條件是 2<=x 。習題3選擇題1 能正確表示邏輯關系:“a10或aw0”的C語言

15、表達式是DA. a >= 10 or a <= 0 Ba >= 0 | a <= 10C. a >= 10 && a <= 0D. a >= 10 | a <= 02. 在嵌套使用if語句時,C語言規定else總是 CA. 和之前與其具有相同縮進位置的if配對B. 和之前與其最近的if 配對C和之前與其最近的且不帶 else的if配對 D.和之前的第一個if 配對3. 下列敘述中正確的是D 。A. break語句只能用于switch語句B. 在switch語句中必須使用defaultC. break語句必須與switch語句中的c

16、ase配對使用D. 在switch語句中,不一定使用 break語句1x>04. 有一函數y=0 x=0 ,以下程序段中錯誤的是C-1 x<0A. if(x > 0) y = 1;else if(x = 0) y = 0;B.y=0;if(x>0) y=1;else y = -1;C. y = 0;if(x >= 0);if(x > 0) y = 1 ;else if(x<0) y=-1;D. if(x>=0)if(x>0) y=1;else y=0;else y=-1;else y = -1;5. 下列程序段的輸出結果是C int mai

17、n(v oid)int a=2,b=-1,c=2;if(a<b)if(b<0)c=0;else c+;prin tf("%dn",c);return 0;.2 D . 3 A。A. 0 B . 1 C6. 下列程序段的輸出結果是 int main(v oid)int x = 1,a = 0,b = 0;switch(x)case 0case 1case 2 b+; a+ ; a+;b+;printf("a=%d,b=%dn",a,b); return 0;a=1,b=1a=2,b=2Aa=2,b=1BC a=1,b=0D7在執行以下程序時,為

18、使輸出結果為: t=4 ,則給 a 和 b 輸入的值應滿足的 條件是 Cint main(void)int a,b,s,t; scanf("%d,%d",&a,&b);s = 1; t = 1; if(a > 0) s = s + 1; if(a > b) t = s + t; else if(a = b) t = 5; else t = 2 * s; printf("t=%dn",t); return 0;a<b<00>a>bAa>b10<x<100 或者 x<0 的 C 語言

19、表達式是 (x>10&&x<100)|C0<a<b 二、填空題 1表示條件: (x<0) 2已知 a=7.5 , b=2, c=3.6 ,則表達式 a>b&&c>a|a<b&&!c>b 的值為 0 。3若從鍵盤輸入 58 ,則以下程序段的輸出結果是585858int main(void) int a;scanf("%d",&a);if(a > 50) printf("%d",a);if(a > 40) printf("%d&

20、quot;,a);if(a > 30) printf("%d",a);return 0;4下列程序運行的輸出結果是9 int main(void)char c = b'int k = 4;switch(c)case 'a': k = k + 1;break;case 'b': k = k + 2;case 'c': k = k + 3;printf("%dn",k);return 0; 三程序設計題1. 比較大小:輸入 3 個整數,按從小到大的順序輸出。試編寫相應程序。 解答:#include

21、<stdio.h>int main(void)int a,b,c;char op='<'printf("Enter a:"); scanf("%d",&a);printf("Enter b:"); scanf("%d",&b);printf("Enter c:");scanf("%d",&c);if(a>b) if(b>c)printf("%d%c%d%c%d",c,op,b,op,a)

22、;else if(a>c)printf("%d%c%d%c%d",b,op,c,op,a); else printf("%d%c%d%c%d",b,op,a,op,c);else if(a>c)printf("%d%c%d%c%d",c,op,a,op,b); elseif(b<c)printf("%d%c%d%c%d",a,op,b,op,c); else printf("%d%c%d%c%d",a,op,c,op,b);return 0;2. 高速公路超速處罰:按照規定,在

23、高速公路上行駛的機動車,超過本車道限 速的 10%則處 200 元罰款;若超出 50%,就要吊銷駕駛證。請編寫程序根據車速 和限速判別對該機動車的處理。解答:#include<stdio.h>int main(void)double speed,rate_limiting;printf("Enter speed:");scanf("%lf",&speed);printf("Enter rate_limiting:");scanf("%lf",&rate_limiting); if(rat

24、e_limiting*1.5>speed>rate_limiting*1.1) printf(" 罰款 200 元");else if(speed>rate_limiting*1.5)printf(" 罰款 200 元,并吊銷駕駛證 ");else;return 0;3出租車計價: 某城市普通出租車收費標準如下: 起步里程 3 公里,起步費 10 元;超起步里程后 10 公里內,每公里 2 元,超過 10 公里以上的部分加收 50% 的空駛補貼費,即每公里 3 元;營運過程中,因路阻及乘客要求臨時停車的,按 每 5 分鐘 2 元計收(不

25、足 5 分鐘則不收費)。運價計費尾數四舍五入, 保留到元。 編寫程序,輸入行駛里程(公里)與等待時間 (分鐘) ,計算并輸出乘客應支付的車 費(元)。解答:#include <stdio.h>int main(void)int mile,time,cost;printf("Input mile and time:"); scanf("%d%d",&mile,&time); cost=0;mile=mile+time/5;if(mile<=3)cost=10;else if(mile<=13) cost=10+(mi

26、le-3)*2;elsecost=10+10*2+(mile-13)*3; printf("cost=%dn",cost);return 0;4. 統計學生成績:輸入一個正整數n,再輸入n個學生的成績,統計五分制成績的分布。百分制成績到五分制成績的轉換規則:大于或等于90分為A,小于90分且大于或等于80分為B,小于80分且大于或等于70分為C,小于70分且 大于或等于60分為D,小于60分為E。試編寫相應程序。解答:#i nclude <stdio.h>int main(v oid)int mark, n, i, sum;double average;int

27、coun ta, coun tb, coun tc, coun td, coun te;prin tf("E nter n:");sca nf("%d",&n);coun ta= countb = countc = countd = counte =sum=0;for(i=1; i<=n; i+)sca nf("%d",&mark);sum=sum+mark;if(mark>=90 ) coun ta+;else if (mark>=80) coun tb+;else if(mark>=70) coun tc+;else if(mark>=60) coun td+;else coun te+;average=sum*1.0/n;prin tf("average=%.1fn",average);printf("A:%d, B: %d, C: %d, D: %d, E: %dn",counta, countb, countc, countd,coun te);retur

溫馨提示

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

評論

0/150

提交評論