作业与实验参考答案培训资料

上传人:yulij****0329 文档编号:136646012 上传时间:2020-06-30 格式:PPT 页数:100 大小:1.03MB
返回 下载 相关 举报
作业与实验参考答案培训资料_第1页
第1页 / 共100页
作业与实验参考答案培训资料_第2页
第2页 / 共100页
作业与实验参考答案培训资料_第3页
第3页 / 共100页
作业与实验参考答案培训资料_第4页
第4页 / 共100页
作业与实验参考答案培训资料_第5页
第5页 / 共100页
点击查看更多>>
资源描述

《作业与实验参考答案培训资料》由会员分享,可在线阅读,更多相关《作业与实验参考答案培训资料(100页珍藏版)》请在金锄头文库上搜索。

1、作业与实验参考答案,夏英杰,程序是如何炼成的?,程序设计四步曲: 变量定义 输入 处理/计算 输出,需要注意的问题: 需要存放哪些值?并判断需要定义几个变量? 每个变量需要定义为何种类型? 需要输入几个值(判断哪些值是根据输入数据计算出来的,这些值不需要输入) 经过哪些处理?如何处理? 输出对象是谁?在何处输出? 输入输出语句的格式!,-定义在本函数中用到的变量,-输入待处理的数据,-对输入的数据进行各种运算,-输出计算结果,作业二,1、输入一个华氏温度,要求输出摄氏温度。公式为:,#include void main( ) ,变量定义 输入 处理 输出,float C,F; scanf(%f

2、, ,5/9.0*(F-32) 5.0/9.0*(F-32) 5*(F-32)/9,#include void main( ) float C,F; printf(Input F:); scanf(%f, ,注意: 变量的类型。 除法运算符的使用;数学公式的表示。 输入输出有文字说明。,3、编写程序,输入梯形的上底、下底和高,计算并输出梯形的面积。,#include void main( ) ,变量定义 输入 处理 输出,#include void main( ) float a,b,h,area; printf(Input a,b,h: ); scanf(%f%f%f, ,实验三,1、编写程

3、序,输入圆半径r,求圆周长、圆面积、圆球表面积、圆球体积。,#include void main( ) ,变量定义 输入 处理 输出,#define PI 3.1415926,float r,L,s1,s2,V; /不能写成以下形式s1 s2 s表 s,scanf(%f, /注意scanf函数的格式,L=2*PI*r; s1=PI*r*r; s2=4*PI*r*r; V=4.0/3*PI*r*r*r;,printf(%f, %f, %f, %fn, L,s1,s2,V);,还可以写成: 4/3.0*PI*r*r*r 4.0/3.0*PI*r*r*r 4*PI*r*r*r/3,#include

4、#define PI 3.1415926 void main( ) float r,L,s1,s2,V; printf(Input r:); scanf(%f, ,printf(L=%.2fn, 2*PI*r); printf(s1=%.2fn, PI*r*r); printf(s2=%.2fn, 4*PI*r*r); printf(V=%.2fn, 4.0/3*PI*r*r*r);,注意: #define命令的使用。 变量的类型;变量名不能带下标或汉字,如s1、s表、s。 scanf、printf函数的格式。 除法运算符的使用;数学公式的表示。 输入输出有文字说明,尽量不要使用汉字。,2、有

5、三个电阻r1、r2、r3并联,编写程序计算并输出并联后的电阻r。已知电阻并联公式为:,#include void main( ) ,float r,r1,r2,r3; /不能写成下标的形式r1,r2,r3,scanf(%f%f%f, /scanf函数的格式,r=1/(1/r1+1/r2+1/r3);,printf(r=%.2fn, r);,1/r=1/r1+1/r2+1/r3; r=1/(1/r1+1/r2+1/r3); t=1/r1+1/r2+1/r3; r=1/t; r=r1*r2*r3/(r2*r3+r1*r3+r1*r2); ,#include void main( ) float r

6、,r1,r2,r3; printf(Input r1,r2,r3: ); scanf(%f%f%f, ,注意: 变量的类型;变量名不能带下标或汉字。 scanf、printf函数的格式。 数学公式的表示(不能赋值给表达式)。 输入输出有文字说明;且尽量不要使用汉字。,3、由键盘输入一个10-99之间的整数,将该数分解,分别输出其个位数字和十位数字。例如,输入85,输出:5,8。 提示:用算术运算中的整除和取余运算实现。,#include void main( ) ,int x, a, b;,scanf(%d, ,a=x%10;,printf(“a=%d, b=%dn, a, b);,b=x/1

7、0;,作业四,1、编写程序,输入三角形的三条边,计算并输出三角形的面积。(注意输入的三条边必须要能构成一个三角形)求三角形的面积公式为: 其中s=(a+b+c)/2。,#include #include void main( ) ,float a,b,c,s,area;,scanf(%f%f%f, ,s= (a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c);,printf(area=%.2fn, area);,#include void main( ) ,变量定义 输入 处理 输出,注意: 变量的类型,本题应该是float或double。 scanf、printf

8、函数的格式: scanf(%f%f%f, /printf函数中常加其它说明字符,3. 数学函数的使用方法;同时需加上#include double pow(double x,double y);,程序最前面写上: #include #include ,求得的函数值的类型,a=pow(3.2,5);,double sqrt(double x);,area=sqrt(s*(s-a)*(s-b)*(s-c);,4. 计算的顺序:先计算s,再计算area。-后面用到的 值必须先计算,s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c); printf(area=%.2f

9、n, area);,5. 输入提示语句printf必须在scanf函数之前:,printf(Input a,b,c:); scanf(%f%f%f,6. 是否三角形的判断:任意两边之和大于第三条边,a+bc,b+ca,a+cb, area=sqrt(s*(s-a)*(s-b)*(s-c); printf(area=%.2fn, area);, ,复合语句,#include #include void main( ) float a,b,c,s,area; printf(Input a,b,c: ); scanf(%f%f%f, ,输入提示语句应放在scanf函数之前,if (a+bc ,els

10、e printf(It is not a triangle!n);,2、周期为T秒的人造卫星离地面 的平均高度H的计算公式为:,其中:M=61024kg是地球质量,R=6.371106m是地球半径。 编写程序,输入人造卫星的周期T,计算并输出人造卫星离地面的高度H。 算法提示:求xy结果的数学函数是pow(x, y),1. 变量的类型及个数:double T,H;,2. 输入语句:scanf(“%lf”, /注意需要输入几个值,3. 数学表达式的写法: x=6.67E-11*M*T*T/4*PI*PI; x=6.67E-11*M*T*T/(4*PI*PI); x=6.67E-11*M*T*T/

11、4/PI/PI; H=pow(x, 1.0/3)-R; x=pow(6.67E-11*M*T*T/(4*PI*PI),1.0/3); ,4. 输出语句:printf(%.2En, H); /较大的值用%E输出,#include #include #define PI 3.1415926 void main( ) double M=6E24,R=6.371E6,T,H,x; printf(Input T: ); scanf(%lf, ,#include #include #define PI 3.1415926 #define M 6E24 #define R 6.371E6 void main

12、( ) double T,H,x; printf(nInput T: ); scanf(%lf, ,Input T: 86400 H=3.59E+007,注意: 变量的数据类型 double型数据的输入 实型常量的表示方法 数学函数的使用,3、求任意三个整数的平均值。要求:输入数据与输出结果都应有相应的提示信息。且输出数据取小数点后两位数字显示。,#include void main( ) ,printf(“Input 3 numbers:”);,scanf(%f%f%f, ,ave= (a+b+c)/3;,printf(“average=%.2fn, ave);,#include void

13、main( ) ,变量定义 输入 处理 输出,float a,b,c,ave;,实验五,1、输入一个字符,并输出。其中有一个条件是如果该字符是小写的英文字母,则需把它转换成大写字母再输出。,问题:如何判断小写的英文字母?,小写字母:ch=a printf(Input ch: ); scanf(%c, ,/* ch=getchar( ); */,/* ch=48 ”,实验结果的写法: 3. Input ch: A alpha, Input ch: = other,/ printf(%sn,other); ,4、有一个函数,编写程序,输入x的值,计算并输出y值。,#include void mai

14、n( ) ,变量定义 输入 处理 输出,if (x-1) y=x*x*x-1;,else if (x=1) y=-3*x+1;,/ -1=-1,printf(Input x: ); scanf(%lf, ,if (x-1) y=x*x*x-1;,else if (x=1) y=-3*x+1;,else if (x=-1 ,else printf(It is a common triangle!n);,else printf(It is not a triangle!n);, ,注意: 判断的顺序,即按什么顺序判断才能使程序简洁? 判断原则:先判断特殊的 逻辑表达式的使用 if语句的嵌套,问题:

15、请大家在学完dowhile语句后,限制输入的三条边长必须能构成三角形,否则要求重新输入。,1、,#include void main( ) ,float a,b,c;,printf(Input a,b,c:); scanf(%f%f%f,if (a+b=c | b+c=a | c+a=b) printf(It is not a triangle!n);,else if (a=b,else if (a=b|b=c|c=a) printf(It is a isosceles triangle!n);,else printf(It is a common triangle!n);,注意: 使用多分支语句时的判断顺序 两个实数是否相等的判断:fabs(a-b)c printf(Input x: ); scanf(%d, ,2、用switch实现多分支,#include void main( ) int x; printf(Input x: ); scanf(%d, ,3、从键盘输入若干整数,以0结束,判断并输出其中的最大数。,分析:假定第一个数就是

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号