第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 <stdio. h> maino int ma for(=1:i<=9:i+ printf(%4d", m) for(=1:9:2/互不相关的循环一和后一一不同, printf("\n") 先一 printf( x=1 printf("n"):/以上打印表头* y=x++ 习题4.6: y=++x; 详细用法请查阅 scanf(“g%d,&rum):输 P1034.4.3一节 sum1+=mm:/求和步 whil(-mm>=1)循环条件
2 作业答案 ▪ 习题4.3:输出小九九乘法表 (关键是循环嵌套的语句执行顺序) 执行顺序?? #include <stdio.h> main() { int i, j,m; for (i=1; i<=9; i++) printf(“%4d”, m); printf("\n"); for (i=1; i<=9; i++) printf(" -"); printf(“\n”); /*以上打印表头*/ for (i=1; i<=9; i++) /*i可表示第一个乘数从1到9*/ { for (j=1; j<=9; j++) /*j可表示第二个乘数从1到9*/ { m=i*j; printf("%4d", m); } printf("\n"); } } /*互不相关的循环可以使用相同的循环变量*/ ▪习题4.6/*相关的循环(例如嵌套)一定不能使用相同的循环变量 : */ ▪scanf(“%d”,&num);/*输入*/ ▪sum1+=num;/*求和*/ ▪while(--num>=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<stdio.h> 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<=7) {interest=year*12* 0.0075* capital; } if(year>=8) {interest=year*12* 0.0085* capital; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ } 方法二: #include<stdio.h> 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<stdio.h> /*函数功能:根据本金,年份和利率计算利息*/ 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 <stdio .h> 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 <stdio.h> 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书中程 序有何不妥?