【正文】
area=%.2f\n,r,area)。 } } 例 break舉例:小寫字母轉(zhuǎn)換成大寫字母 ,直至輸入非字母字符 include main() { int i,j。 char c。 while(1) { c=getchar()。 if(c=39。a39。 amp。amp。 c=39。z39。) putchar(c39。a39。+39。A39。)。 else break。 } } include /*pute 1/(1x) */ int main() { float sum,x,term。 int n。 printf(Input the value of x:)。 scanf(%f,amp。x)。 for(term=1,sum=0,n=1。n100。n++) { if(term) goto output。 sum+=term。 term*=x。 } printf(Not Reach the Desire Accuracy!\n\n)。 goto end。 output: printf(Exit from Loop\n)。 printf(term=%.5f, Sum=%.5f\n\n,term,sum)。 end: return 0。 } break用到前面的例子 (為解決列循環(huán) )卻不方便: 求 1/(1x)=1+x+x2+x3+……+x n, x1, 算到通項(xiàng)小于 ? continue語(yǔ)句 p163 ?功能:結(jié)束本次循環(huán),跳過(guò)循環(huán)體中 一部分 尚未執(zhí)行的語(yǔ)句,進(jìn)行下一次是否執(zhí)行循環(huán)體的判斷 ?僅用于循環(huán)語(yǔ)句中 expr …… continue。 …… 假 (0) 真 (非 0) while 真 (非 0) do …… continue。 …... expr 假 (0) while expr2 …… continue。 …... 假 (0) 真 (非 0) for expr1 expr3 例 求輸入的十個(gè)整數(shù)中正數(shù)的個(gè)數(shù)及其平均值 /*using continue statement*/ include main() { int i,num=0,a。 float sum=0。 for(i=0。i10。i++) { scanf(%d,amp。a)。 if(a=0) continue。 num++。 sum+=a。 } printf(%d plus integer39。s sum :%\n,num,sum)。 printf(Mean value:%\n,sum/num)。 } ?程序舉例 t=1,pi=0,n=,s=1 當(dāng) |t| 1e6 pi=pi+t n=n+2 s=s t=s/n pi=pi*4 輸出 pi 分子: 1, 1, 1, 1… 分母: 1, 3, 5, 7, ... 為止 最后一項(xiàng)的絕對(duì)值小于 的近似值,直到 公式求 例用 6 10 7 1 5 1 3 1 1 4 + + 187。 p p L L 例 求 Fibonacci數(shù)列: 1, 1, 2, 3, 5, 8, …… 的前 40個(gè)數(shù) f1=1,f2=1 for i=1 to 20 輸出 f1,f2 f1=f1+f2 f2=f2+f1 1 5 34 233 1597 10946 75025 514229 3524578 24157817 1 8 55 377 2584 17711 121393 832040 5702887 39088169 2 13 89 610 4181 28657 196418 1346269 9227465 63245986 3 21 144 987 6765 46368 317811 2178309 14930352 102334155 ) 3 ( ) 2 ( 1 2 ) 1 ( 1 1 2 1 179。 + = = = = = n F F F n F n F n n n 例 判斷 m是否素?cái)?shù) 讀入 m k= m i=2 當(dāng) i k m被 i整除 真 假 用 break 結(jié)束循環(huán) i=i+1 i k+1 真 假 輸出 :m? 是素?cái)?shù) ? 輸出 :m? 不 是素?cái)?shù) ? 例 簡(jiǎn)單密碼系統(tǒng) 例如 Hello,world! 譯成密碼: Lipps,asvph! 解碼: 例 pp168 Bonomial Coeffcients are used in the study of bonomial distributions. It is given by B(m,x)=m!/(x!(mx)!), m=x A table of bonomial Coeffcients is required to be printed as follows: Mx 0 1 2 3 4 5 6 7 8 9 10 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 7 1 7 21 35 35 21 7 1 8 1 8 28 56 70 56 28 8 1 9 1 9 36 84 126 126 84 36 9 1 10 1 10 45 120 210 252 210 120 45 10 1 Problem Analysis: B(m,0) = 1。 B(m,x) = B(m,x1)[(mx+1)/x] , x = 1,2,3,...,m B(0,0) =1。 例 pp169 In an anization , the employees are grouped according to their basic pay for purpose of certain perks. The payrange and the number of employees in each group are as follows: GROUP PAYRANGE NUMBER OF EMPLOYEES 1 7501500 12 2 15013000 23 3 30014500 35 4 45016000 20 5 above6000 11 Draw a histogram to highlight the group sizes. 例 pp171 Minimum Cost: The cost of operation of a unit consists of two ponents C1 and C2 which can be expressed as functions of a parameter p as follows: C1=308p C2=10+p2 The parameter p ranges from 0 to 10. Determine the value of p with an accuracy of + where the cost of operation would be minimum . Problem Analysis: Total cost =C1+C2=408p+p2 The cost is 40 when p =0,and 33 when p = 1 and 60 when p = 10. The cost , therefore , decreases first and then increases. 例 pp172 Plotting of Two Functions: y1 = exp(ax) y2 = exp(ax2/2) x varying from 0 to Program Analysis: Initially when x = 0, y1 = y2= 1 and the graphs start from the same point. The curves cross when they are again equal at x = . The program should have appropriate branch statements to print the graph points at the following three condition: 1. y1 y2 2. y1 y2 3. y1 = y2 The functions y1 and y2 are normalized and converted to intgers as follows: y1 = 50 exp (ax) + y2 = 50 exp (ax2/2) +