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

广东海洋大学:《C++程序设计》实验5 编译预处理和多文件的组织

资源类别:文库,文档格式:DOC,文档页数:4,文件大小:37.5KB,团购合买
实验5编译预处理和多文件的组织 【实验目的】 1掌握多文件程序的编译与连接。 2掌握宏定义命令。 3了解文件包含命令和条件编译命令。
点击下载完整版文档(DOC)

实验5编译预处理和多文件的组织 【实验目的】 1掌握多文件程序的编译与连接 2掌握宏定义命令 3了解文件包含命令和条件编译命令。 【实验内容】 1用3个文件来求两正整数的最大公约数和最小公倍数,一个文件含主函 数,其它两文件分别含求最大公约数和最小公倍数的定义。 ∥主函数cpp #include unsigned int yue(unsigned int x, unsigned int y); unsigned int bei(unsigned int x, unsigned int y); void ma f unsigned int m, n, b, y; scanf ("%u %u", &m, &n) b=bei(n, m); printf("%u, %uIn, y, b); ∥最大公约数cpp unsigned int yue(unsigned int x, unsigned int y) fint t; if (x<y) return x 最小公倍数cpp unsigned int bei(unsigned int x, unsigned int y) if (x<y)

实验 5 编译预处理和多文件的组织 【实验目的】 1 掌握多文件程序的编译与连接。 2 掌握宏定义命令。 3 了解文件包含命令和条件编译命令。 【实验内容】 ⒈用 3 个文件来求两正整数的最大公约数和最小公倍数,一个文件含主函 数,其它两文件分别含求最大公约数和最小公倍数的定义。 //主函数.cpp #include unsigned int yue(unsigned int x,unsigned int y); unsigned int bei(unsigned int x, unsigned int y); void main ( ) { unsigned int m,n,b,y; scanf ("%u%u",&m,&n); y=yue(n,m); b=bei(n,m); printf ("%u,%u\n",y,b); } //最大公约数.cpp unsigned int yue(unsigned int x, unsigned int y) {int t; if (x<y) { t=x; x=y; y=t;} while (y!=0) {t=x%y; x=y; y=t;} return x;} //最小公倍数.cpp unsigned int bei(unsigned int x, unsigned int y) {int t; if (x<y) { t=x; x=y; y=t;} for ( ; ; x+=x) { if(x%y==0) break;} return x;}

2用4个文件来计算二次方程ax2+bx+c=0的根,一个文件含主函数,其它 个文件分别含当b2-4ac>0、b2-4ac=0及b2-4ac #include oid gz float, float, float oid eg(float, float); void xg(float, float, float); void maino i float a, b, c, d: cout>a d=b*b-4÷a if (a=0 cou void gz(float a, float b, float d) {cout void eg(float a, float b) cout #include void xg(float a, float b, float d cout<<"方程有两个虚根根!"<<nd b(2=a);

⒉用 4 个文件来计算二次方程 ax2+bx+c=0 的根,一个文件含主函数,其它 三个文件分别含当 b 2 -4ac>0、b 2 -4ac=0 及 b 2 -4ac #include void gz(float,float,float); void eg(float,float); void xg(float,float,float); void main() { float a,b,c,d; cout>a; cout>b; cout>c; d=b*b-4*a*c; if (a==0) cout0) gz(a,b,d); else if (d==0) eg(a,b); else xg(a,b,d); } //c1.cpp #include #include void gz(float a,float b,float d) { cout void eg(float a,float b) { cout #include void xg(float a,float b,float d) { cout<<"方程有两个虚根根!"<<endl; float p=-b/(2*a);

float sqrt(-d)/(2*a); cout #include void gz float, float, float) id eg(float, float); void xg(float, float, float); lc.cpp #include hea. h void main( ∥ cl.cpp #include h. h' void gz( float a, float b, float d) ∥2.cpp #include head.h" void eg(float a, float b) ∥c3cpp #include head. h void eg(float a, float b) 4用条件编译命令编写一程序,当“# define ff存在时,计算的是圆柱体表面 积;当“# define ff不存在时,计算的是圆锥体表面积 #include #include #define pl3. 1415926 #define ff no cout<<r? cout<< h=?

float q=sqrt(-d)/(2*a); cout #include void gz(float,float,float); void eg(float,float); void xg(float,float,float); //c.cpp #include "hea.h" void main() { … } //c1.cpp #include "h.h" void gz(float a,float b,float d) { … } //c2.cpp #include "head.h" void eg(float a,float b) { … } //c3.cpp #include "head.h" void eg(float a,float b) { … } ⒋用条件编译命令编写一程序,当“#define FF”存在时,计算的是圆柱体表面 积;当“#define FF”不存在时,计算的是圆锥体表面积。 #include #include #define PI 3.1415926 #define FF void main() { long r,h; cout>r; cout<<"h=?";

#ifdef FF cou← tfunl(int) int fun 2(int); void maino) [ int n I if (fun 1(n))cout <<<<",;1 cout<<endl; for(n=1;n<=100;n++) 【if(fun2(n)cout<n≤",";} cout<≤endl; 文件 int fun (int k) f int flag=l: for(int F2; K<=k/2; i++) i if (k%F=0) ag return fla 3cpp文件: int fun(int k) f int flag=0; if(k%13=0)nag=1; return flag:

cin>>h; #ifdef FF cout int fun1(int); int fun2(int); void main() { int n; for (n=1;n<=100;n++) { if (fun1(n)) cout<<n<<","; } cout<<endl; for (n=1;n<=100;n++) { if (fun2(n)) cout<<n<<","; } cout<< endl; } //c1.cpp 文件: int fun1(int k) { int flag=1; for(int i=2;i<=k/2;i++) { if (k%i==0) { flag=0; break; } } return flag; } //c3.cpp 文件: int fun2(int k) { int flag=0; if (k%13==0) flag=1; return flag; }

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

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

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