当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

重庆邮电大学:《C语言程序设计》第6讲 循环和综合程序设计(谢竞博)

资源类别:文库,文档格式:PPT,文档页数:17,文件大小:111.5KB,团购合买
一、循环结构程序设计 二、综合结构程序设计
点击下载完整版文档(PPT)

第6讲程序的控制结构 循环结构程序设计 综合结构程序设计 ·请大家及时到我的个人主页htp://cs. cqupt. edu.cn/xejb 或公共邮箱htp:/ stu cqupt. edu.cn中查看作业回复及下 载课件自学 因课堂时间有限,不能把所有例题都讲到,请大家课下 定多看相关例题

第6讲 程序的控制结构 循环结构程序设计 综合结构程序设计 •请大家及时到我的个人主页http://cs.cqupt.edu.cn/xiejb 或公共邮箱http://stu.cqupt.edu.cn中查看作业回复及下 载课件自学 •因课堂时间有限,不能把所有例题都讲到,请大家课下一 定多看相关例题

作业答案 执行顺序?? #include maino int ma for(=1:i=1)循环条件

2 作业答案 ▪ 习题4.3:输出小九九乘法表 (关键是循环嵌套的语句执行顺序) 执行顺序?? #include main() { int i, j,m; for (i=1; i=1) /*循环条件*/ •先--和后--不同, •例: x=1; Y=x++; Y=++x; •详细用法请查阅 P1034.4.3一节

用函数计算利息: #include<stdio.h 方米函数功能:根据本金,年份和利率计算利息*/ hi fFin float Interest(float capital, int year, float rate) #inc voic ireturn(capital year*12*rate) void [int void main( ino [int pri tint year float capital, interest prin Sc prin Itf("please input capital and year: " ) sw scanf(e“%f%d",& capital, &year):/输入数据"/ ify icc switch(year)⌒下面根据不同年限和不同利率调用函数吻 ct case 1: interest=Interest(capital, year, 0.0063): break if(y ct case 2: interest=Interest(capital, year, 0.0066), break ct case 3 if(y ce case 4: interest=Interest(capital, year, O0069); break; cl case 5: ifo ci case 6: li d case 7: interest=Interest(capital, year, 0.0075); break 3 default: interest=Interest(capital, year, 0.0085): break [i prl prin: /1 printf("the sum of capital and interest is %f", capital+interest) 最店覆广最后输出计算结果

3 作业答案 ▪ 习题4.9 ▪ 思路:根据输入的year得到不同的利率,由此求 出不同的利息,再输出。 ▪ 方法一:用if-else ▪ 方法二:用switch() ▪ 进一步,可以使用函数。 ▪ } else /*是 0*/ { printf("%d is 0 \n", m);} } if (m % 2 == 0) /*是否为偶数*/ { printf("%d is even\n", m); } /*是偶数*/ else { printf("%d is odd\n", m); } /*是奇数*/ } 方法一: #include void main() {int year;float capital,interest; printf(“please input capital and year:”); scanf(“%f,%d”,&capital,&year); /*输入数据*/ if(year==1) /*下面的几个分支分别根据不同年限计算利息*/ {interest=year*12*0.0063*capital; } if(year==2) {interest=year*12* 0.0066* capital; } if(year==3|| year==4) {interest=year*12* 0.0069* capital; } if(year>=5&& year=8) {interest=year*12* 0.0085* capital; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ } 方法二: #include void main() {int year;float capital,interest; printf(“please input capital and year:”); scanf(“%f,%d”,&capital,&year); /*输入数据*/ switch(year) /*下面的几个分支分别根据不同年限计算利息*/ {case 1:interest=year*12*0.0063*capital; break; case 2:interest=year*12*0.0066*capital; break; case 3: case 4:interest=year*12*0.0069*capital; break; case 5: case 6: case 7:interest=year*12*0.0075*capital; break; default:interest=year*12*0.0085*capital; break; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ } 用函数计算利息: #include /*函数功能:根据本金,年份和利率计算利息*/ float Interest(float capital, int year, float rate) {return (capital*year*12*rate);} void main() {int year;float capital,interest; printf(“please input capital and year:”); scanf(“%f,%d”,&capital,&year); /*输入数据*/ switch(year) /*下面根据不同年限和不同利率调用函数*/ {case 1: interest=Interest(capital,year,0.0063); break; case 2: interest=Interest(capital,year,0.0066); break; case 3: case 4: interest=Interest(capital,year,0.0069); break; case 5: case 6: case 7: interest=Interest(capital,year,0.0075); break; default: interest=Interest(capital,year,0.0085); break; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ }

课堂练习P107例4-24 输入整数K 0= n+十 输出k%10 k=k/10 未除尽> 输出位数n 结束

4 课堂练习 P107例4-24 输入整数K 输出位数n 计算 结束 求倒数第n位并输出 T 还未除尽 n=0 n++; 输出k%10; k=k/10;

#include void maino) 例4-24程序 int kn=o: printf=? ) P107书中程 scanf(gd", &k); 序有何不妥? printf( \n): n+十 /计数器n加1,下边要计算倒数第n位*/ printf(“%d,k%10):/求倒数第n位并输出*/ k/=10 /*为下一次循环做准备*/ 3 while(k>0: printf( \nn=%", n)

5 #include void main( ) { int k,n=0; printf("k=?"); scanf("%d",&k); printf("\n"); do { n++; /* 计数器n加1,下边要计算倒数第n位*/ printf(“%d”,k%10); /* 求倒数第n位并输出*/ k/=10; /* 为下一次循环做准备*/ } while(k>0); printf("\n n=%d",n); } 例4-24程序 •P107书中程 序有何不妥?

P111例4-28 求1!2!++n! n算法: 输入 计算 循环开始:i=1:sum=0 循环条件:i<=n 循环体:计算i,然后,sum+=i! 输出

6 P111例4-28 ▪ 求1!+2!+…+n! ▪ 算法: ▪ 输入 ▪ 计算 ▪ 输出 ▪循环开始:i=1;sum=0; ▪循环条件:i<=n ▪循环体:计算i!,然后,sum+=i!

P111例4-28程序 #include maino [int i, n; double term, sum; prnf(“ please input n:")冫 scant%d”,&n) 递推法! for(i=1, term=1, sum=0; i<=n; i++l 求还可以怎 i term"=i- 样求? sum=sum+term 找规律(通项)* prnf(“sum=%fn,sum)

7 P111例4-28程序一 #include main() {int i,n; double term,sum; printf(“please input n:”); scanf(“%d”,&n); for(i=1,term=1,sum=0;i<=n;i++) { term*=i; sum=sum+term; } /* 找规律(通项) */ printf(“sum=%lf\n”,sum); } •递推法! •求i!还可以怎 样求?

P111例4-28程序二 #include maino [int i, n; double term, sum; printf("please input n: ) scant%d”,&n) 循环嵌套 for(i=1, sum=0; i<=n; i++) 执行顺序? for(j=1:j<≡i+ 前边的一个 [term"=j:] 程序效率要 高些 sum=sum+term /找规律(通项) C printf("sum=7f in, sum)

8 P111例4-28程序二 #include main() {int i,n; double term,sum; printf(“please input n:”); scanf(“%d”,&n); for(i=1,sum=0;i<=n;i++) { for(j=1;j<=i;j++) {term*=j;} sum=sum+term; } /* 找规律(通项) */ printf(“sum=%lf\n”,sum); } •循环嵌套 •执行顺序? •前边的一个 程序效率要 高些

例4-35百钱买鸡问题 ∫x+y+z=100 5x+3+z/3=100(2) 用列举(又称穷举、枚举)法 include maino int x,y,z: printf( Man (t Women (t Childern\n) for(x=0:x<=20:x++) for(y=0:y<=33:y*+) z=100-X-y (5x+3y+2/3.0==100) printf \n%d\t%d\t%d\n",x, y, z)

9 例4-35百钱买鸡问题 ▪ 用列举(又称穷举、枚举)法 #include main() { int x,y,z; printf("Man \t Women \t Childern\n"); for (x=0; x<=20; x++) for (y=0; y<=33; y++) {z = 100 – x - y; if (5*x+3*y+z/3.0 == 100) printf(“\n%d\t%d\t%d\n",x,y,z); } }    + + = + + = 5 3 / 3 100 (2) 100 (1) x y z x y z

写循环结构程序设计 般情况下 如果循环次数已知,用£or 如果循环次数未知,用whi1e 如果循环体至少要执行一次,用do-whie !!!关键 单重循环:归纳出每次循环要做的共同工作(规 律),即是循环体 循环嵌套:(例:九九乘法表)根据关系分析 清楚哪是外层,哪是内层,一层一层写算法, 层一层写程序 10

10 写循环结构程序设计 ▪ 一般情况下: ▪ 如果循环次数已知,用for ▪ 如果循环次数未知,用while ▪ 如果循环体至少要执行一次,用do-while ▪ !!!关键 ▪ 单重循环:归纳出每次循环要做的共同工作(规 律),即是循环体。 ▪ 循环嵌套: (例:九九乘法表)根据关系分析 清楚哪是外层,哪是内层,一层一层写算法, 一层一层写程序

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共17页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有