《C程序设计课件第4节.ppt》由会员分享,可在线阅读,更多相关《C程序设计课件第4节.ppt(54页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、(0.3,)/2( )2f uxuup ux 25( )(2)(5) -pxxy xp xp xx( ,)7wvfv wvvoid main() float y,f,p0,p2,p5,x; printf ( please input x: ); scanf(%f,&x); f=(x+x)+(x*0.3)/(7*(x*0.3); p0=(f+x/2)/(2*x); f=(x+(x+2)+(x+2)*0.3)/(7*(x+2)*0.3); p2=(f+(x+2)/2)/(2*x); f=(x+(x+5)+(x+5)*0.3)/(7*(x+5)*0.3); p5=(f+(x+5)/2)/(2*x);
2、 y=(p0*p0+5*x)/(p5-sqrt(x)*p2; printf (y(x)=%8.4fn,y); 0pp x0.3(0.3,)70.3xxxf xxxx 20.32220.3,22720.3xxxfxxxx22pp x 50.35550.3,55750.3xxxfxxxx55pp xP(x)称为函数调用函数定义说明符,小括号()为形式参数表内为函数p的函数体,具体规定p的操作和计算Return:表达式的值作为函数值返回定义函数f执行顺序:1112 13 (分别以x,x+5,x+2为实参调用函数p )14定义函数p主程序主程序调用函数调用函数返回返回结束结束函数函数yxyxf),(函
3、数类函数类型型函数名字函数名字形参列表形参列表复合语句复合语句函数体函数体函数定义说明符函数定义说明符int f (int x)return x*x;int y=9;void f (int x)if (x0)return;y=y+x;分别定义329abx返回值返回值xycmainf内存内存51260600 xyvzumainf内存内存g返回值返回值返回值返回值021440 xyvzumainf内存内存g返回值返回值返回值返回值12144float x,y ; float f(float ,float );float p(float ); void main() printf (please i
4、nput x: );scanf(%f,&x); y=(p(x)*p(x)+(5*x)/(p(x+5)-sqrt(x)*p(x+2); printf (y(x)=%8.4fn,y) float f(float v,float w )return (w+v)/(7*v);float p(float u)return ( f(u*0.3,x+u)+(u/2)/(2*x) );#define multiplier 25173#define increment 13849 #define modulus 65536 int seed=0 ;int random ( void ) seed = ( mul
5、tiplier * seed + increment ) % modulus ; return seed ;ABCDEFB1B2B3读入六个极角的值读入六个极角的值求六个顶点求六个顶点A, B, C, D, E, F 在直角坐标系中的坐标在直角坐标系中的坐标求三双对边交点求三双对边交点B1, B2, B3 坐标坐标验证验证B1, B2, B3 是否在一条直线上是否在一条直线上开始开始结束结束coordinatepx=r*cos(theta);py=r*sin(theta);returntrans_abcdef 转换转换a xa , ya转换转换b xb , yb转换转换d xd , yd 转换
6、转换f xf , yf转换转换e xe , ye转换转换c xc , ycreturnthree_inter求求AB 、DE交点交点B1求求BC 、EF交点交点B2求求CD 、FA交点交点B3returnintersection 求求r, s ; t, u直线方程直线方程l1, l2求求l1、l2交点交点Breturnequtionreturn求求r,s直线方程直线方程l1求求t,u直线方程直线方程l2straightline b=(py*qx-qy*px)/(qx-px)return a=(py-qy)/(px-qx)interwx=(nb-mb)/(ma-na); wy=ma*px+mb;
7、 returntestreturn 求过求过B1, B2直线方程直线方程L判断判断B3是否在直线是否在直线L上上/*PROGRAM Pascal theorem*/#include math.h#include stdio.h#define PI 3.1415927#define eps 1e-5float radius; /* 圆的半径圆的半径 */float theta1, theta2, theta3, theta4, theta5, theta6; / * 六个极角的度数六个极角的度数 */float xa, ya, xb, yb, xc, yc, xd, yd, xe, ye, xf
8、, yf; /* 六个顶点的直角坐标六个顶点的直角坐标 */float b1_x, b1_y, b2_x, b2_y, b3_x, b3_y; /* 三个交点的直角坐标三个交点的直角坐标 */ float b12_a, b12_b; /*B1和和B2构成直线的斜率和截距构成直线的斜率和截距*/*主程序之前这段为主程序之前这段为“函数原型函数原型”以及各个函数返回结果所用变量以及各个函数返回结果所用变量*/void trans_abcdef();float px, py; /* 用来保存用来保存coordinate()转换的直角坐标转换的直角坐标 */void coordinate (float
9、, float);void three_inter();void intersection (float, float, float, float, float, float, float, float);float l1_a, l1_b, l2_a, l2_b; /* 两条直线的斜率和截距两条直线的斜率和截距 */void equation (float, float, float, float, float, float, float, float);float a, b; /* 直线方程的斜率和截距直线方程的斜率和截距 */void straightline(float, float,
10、float, float);float wx, wy; /* 直线交点的直角坐标直线交点的直角坐标 */void inter (float, float, float, float);bool test (float, float, float, float, float, float);/*主函数主函数*/void main() /*读入圆形的半径读入圆形的半径*/ printf(please input the radius of the circle:); scanf(%f, &radius); /*读入六个角读入六个角*/ printf(please input six angle:)
11、; scanf(%f %f %f %f %f %f, &theta1, &theta3, &theta3, &theta4, &theta5, &theta6); trans_abcdef(); /*计算六个定点坐标计算六个定点坐标*/ three_inter(); /*求三个交点求三个交点*/ if( test(b1_x, b1_y, b2_x, b2_y, b3_x, b3_y) ) /*验证验证*/ printf(ok); else printf(There is an error when:n); printf(theta1=%d theta2=%dn, theta1, theta2)
12、; printf(theta3=%d theta4=%dn, theta3, theta4); printf(theta5=%d theta6=%dn, theta5, theta6); /*计算六个顶点坐标计算六个顶点坐标*/void trans_abcdef() coordinate(radius, theta1); xa=px; ya=py; coordinate(radius, theta2); xb=px; yb=py; coordinate(radius, theta3); xc=px; yc=py; coordinate(radius, theta4); xd=px; yd=py
13、; coordinate(radius, theta5); xe=px; ye=py; coordinate(radius, theta6); xf=px; yf=py;/*计算一个顶点坐标计算一个顶点坐标*/void coordinate(float r, float theta) /* 先把先把“角度角度”转换成转换成“弧度弧度”,再转换成直角坐标,再转换成直角坐标 */ px=r*cos(PI*theta/180); py=r*sin(PI*theta/180);/*求三个交点求三个交点*/void three_inter() intersection(xa, ya, xb, yb, x
14、d, yd, xe, ye); b1_x=wx; b1_y=wy; intersection(xb, yb, xc, yc, xe, ye, xf, yf); b2_x=wx; b2_y=wy; intersection(xc, yc, xd, yd, xf, yf, xa, ya); b3_x=wx; b3_y=wy;/*已知四点,求两条直线交点已知四点,求两条直线交点*/void intersection(float rx, float ry, float sx, float sy, float tx, float ty, float ux, float uy) equation (rx,
15、 ry, sx, sy, tx, ty, ux, uy); inter (l1_a, l1_b, l2_a, l2_b);/*已知四点,求两条直线方程已知四点,求两条直线方程*/void equation ( float rx, float ry, float sx, float sy, float tx, float ty, float ux, float uy) straightline(rx, ry, sx, sy); l1_a=a; l1_b=b; straightline(tx, ty, ux, uy); l2_a=a; l2_b=b;/*计算由两点确定直线方程的斜率(计算由两点确定
16、直线方程的斜率(a)和截距()和截距(b)*/void straightline(float ex, float ey, float fx, float fy) a=(fy-ey)/(fx-ex); /* 斜率斜率 */ b=ey-a*ex; /* 截距截距 */*已知两个直线方程的斜率和截距,求它们交点已知两个直线方程的斜率和截距,求它们交点*/void inter ( float ma, float mb, float na, float nb) wx=(nb-mb)/(ma-na); wy=ma*wx+mb;/* 检验检验 */bool test ( float b1_x, float b1_y, float b2_x, float b2_y, float b3_x, float b3_y) straightline(b1_x, b1_y, b2_x, b2_y); if(fabs(b3_y-(a*b3_x+b)eps) return true; else return false;函数的程序设计思想函数的程序设计思想