1、C+实验代码汇总实验1 简单C+程序设计2. 实验内容(1) 熟悉Visual C+ 6.0的各个窗口和布局:包括标题栏、菜单栏、工具栏、工作区窗口、输出窗口等)。(2) 输入下列简单C+程序,完成编译、连接、运行,熟悉C+程序的开发过程。#include /包含头文件using namespace std; /打开命名空间stdvoid main( ) /主函数,程序入口 int age; /声明一个变量 age= 20; /赋值语句 coutThe age is:n; /输出一个字符串 coutageendl; /输出变量中的值 return 0; /主函数返回0 /块作用域结束以上语句有
2、错误吗?如果有,错误在哪里?(3) 修改第2章课后习题2-15中C+程序的错误,记录改正后程序的运行结果?(4) 编程实现课后习题2-28-(1),体会break、continue语句的用法。(5) 用For循环结合穷举法找出50100之间的质数。(质数的概念自己上网搜索答案)。(6) 用while和dowhile循环编程实现用户猜数字游戏:给定一个150之间的整数,让用户猜这个数字,比较两个数的大小,把结果提示给用户,直到猜对为止。/第3题#include using namespace std;/此段程序系课本61页的2-15题int main() int i; int j; i=10;
3、j=20; couti+j=i+j; return 0;/第4题#include using namespace std;int main() char c; while (1) coutMenu: A(dd) D(elete) S(ort) Q(uit) Select one:c; if (c=A) cout数据已经增加endl; else if (c=D) cout数据已经删除endl; else if (c=S) cout数据已经排序endl; else if (c=Q) break; return 0;/第5题#include #include using namespace std;
4、int main() int n=0; for (int i=50;i=100;i+) bool flag=true; for (int j=2;jsqrt(i);j+) if (i%j=0) flag=false; break; if (flag=true) coutiis a zhishuendl; return 0;/第6题#include using namespace std;main() int gnum=0; int result=35; while(gnum!=result) coutplease guess an number between 1 and 50 until y
5、ou are succeed:gnum; if (gnum=result) coutcongrudulation:you are succeed.result) coutsorry, your guess is too large!endl; else coutsorry, your guess is too small!endl; 实验2 函数的定义与使用2. 实验内容(1) lab2_1.cpp:写一个判断闰年的函数,在主函数中输入一个年份,输出是否是闰年的信息。(2) lab2_2.cpp:实现第三章课后习题3-13的功能,并结合单步跟踪法进行程序的调试。(3) lab2_3.cpp:编
6、写3个名为max的重载函数,分别实现求两个整数、三个整数、两个双精度型数最大值的功能。(4) lab6_4.cpp: 计算如下公式,并输出结果:其中r、s的值由键盘输入。sin(x)的值直接调用系统函数/*第1题:#include using namespace std;int main() int year; bool isleapyear; bool leapjudge(int year); coutyear; isleapyear=leapjudge(year); if (isleapyear) coutyear is a leap year!endl; else coutyear is
7、 not a leap year!endl;return 0;bool leapjudge(int year) return (year%400=0)|(year%4=0 & year%100!=0);*/*第2题求斐波那契级数#include using namespace std;int main() int num; int fib(int n); coutnum; coutnums Fibonacci number is:fib(num)endl; return 0;int fib(int n) int rnum; if (n=1 | n=2) rnum=1; else rnum=fi
8、b(n-1)+fib(n-2); return rnum;/*第3题:重载函数#include using namespace std;int main() int a=1,b=2,c=3; double d=4.53231,e=5.2863728; int max(int x,int y); int max(int x,int y,int z); double max(double x,double y); coutthe maximum of a and b is:max(a,b)endl; coutthe maximum of a, b and c is:max(a,b,c)endl;
9、coutthe maximum of d and e is:max(d,e)y) return x; else return y; int max(int x,int y,int z) int Mnum=x; if (Mnumy) Mnum=y; if (Mnumy) return x; else return y; /*第4题:#include #include using namespace std;int main() double r,s,k; coutplease input two real numbers:rs; if (r*rs*s) k=sin(r*s)/2; else k=
10、sqrt(sin(r)*sin(r)+sin(s)*sin(s); coutkendl; return 0;实验3 类与对象的设计三、实验内容1Lab3_1.cpp:定义并实现一个矩形类(rectangle),有长(length)和宽(width)两个属性,带有3个成员函数(showlength:用于显示矩形的长度,showwidth:用于显示矩形的宽度,area:用于计算矩形的面积)。2Lab3_2.cpp:声明一个CPU类,包含主频(frequency),字长(wordlength),CPU倍频系数(coefficient)属性,其中字长为枚举型enum cpu_wordlen=W16,W
11、32,W64,W128,W256, frequency是单位为GHz的实数,coefficient为浮点型数据。两个公有成员函数run和stop分别表示CPU的运行与停止。请在构造函数(带参数和不带参数)、拷贝构造函数、析构函数、run和stop函数体给出相应的提示。说明并实现这个类,观察构造函数、拷贝构造函数和析构函数的调用顺序。3. lab3_3.cpp:请使用类的组合来描述计算机类Computer,该类的数据成员有芯片cpu,内存ram,显示器 dis_driver,两个成员函数run和stop分别表示计算机的运行与停止,两个自定义的构造函数分别对应有参数和无参数的情况。芯片cpu是la
12、b3_2中CPU类的对象,内存ram是单位为M的整型数据,显示器 dis_driver为屏幕对角线英寸的整型数据。请写出类Computer的声明,并写出各个成员函数的实现,在每个成员函数的函数体中给出相应的提示。观察构造函数和复制构造函数的调用顺序。六、实验小结#include using namespace std;class rectanglepublic: rectangle():length(0),width(0) rectangle(float x,float y) length=x; width=y; void showlength() coutthe length of this
13、 rectangle is:lengthendl; void showwidth() coutthe width of this rectangle is:widthendl; float area() return length*width; private: float length,width;int main() rectangle rec1; rec1.showlength(); rec1.showwidth(); coutits area is:rec1.area()endl; rectangle rec2(3,4); rec2.showlength(); rec2.showwid
14、th(); coutits area is:rec2.area()endl; return 0;注意:(1) 在调用不带参数的构造函数构造对象时不能写成rectangle rec1();否则会提示如下错误:left of .showlength must have class/struct/union type,说明类的对象没有创建成功(2) 除了书上P108的Clock类的默认构造函数(系统自动定义的构造函数)可用外,其余自己编写的类都必须由用户给出不带参数默认构造函数的函数体,否则会提示如下错误:“no appropriate default constructor available”#
15、include using namespace std;enum cpu_wordlenW16,W32,W64,W128,W256;class CPUpublic: CPU() frequency=0; wordlength=W16; coefficient=0; coutfrequency: frequency wordlength:wordlength coefficient:coefficientendl; coutcalling the constructor without parameters!endl; CPU(float f,cpu_wordlen l,float c) fre
16、quency=f; wordlength=l; coefficient=c; coutfrequency: frequency wordlength:wordlength coefficient:coefficientendl; coutcalling the constructor with parameters!endl; CPU(CPU &C) frequency=C.frequency; wordlength=C.wordlength; coefficient=C.coefficient; coutcalling the copy constructor!endl; CPU() cou
17、tcalling the deconstructor!endl; void run() coutCPU is running!endl; void stop() coutCPU stops!endl; private: double frequency; enum cpu_wordlen wordlength; double coefficient;int main() double x=3.2,y=4.5; CPU mycpu; mycpu.run(); mycpu.stop(); CPU hiscpu(x,W64,y); hiscpu.run(); hiscpu.stop(); CPU h
18、ercpu(hiscpu); hercpu.run(); hercpu.stop(); return 0;注意:由于wordlength为enum型变量,要显示对应的值比如“W16”,必须用switch语句或者if语句,如P54的例2-11#include using namespace std;enum cpu_wordlenW16,W32,W64,W128,W256;class CPUpublic: CPU() frequency=0; wordlength=W16; coefficient=0; coutfrequency: frequency wordlength:wordlength
19、 coefficient:coefficientendl; coutcalling CPUs constructor without parameters!endl; CPU(float f,cpu_wordlen l,float c) frequency=f; wordlength=l; coefficient=c; coutfrequency: frequency wordlength:wordlength coefficient:coefficientendl; coutcalling CPUs constructor with parameters!endl; CPU(CPU &C)
20、frequency=C.frequency; wordlength=C.wordlength; coefficient=C.coefficient; coutcalling CPUs copy constructor!endl; CPU() coutcalling CPUs deconstructor!endl; void run() coutCPU is running!endl; void stop() coutCPU stops!endl; private: double frequency; enum cpu_wordlen wordlength; double coefficient
21、;class Computerpublic: Computer() coutcalling computers constructor without parameters!endl; Computer(CPU c,int r,int d):cpu(c),ram(r),dis_driver(d) coutcalling computers constructor with parameters!endl; Computer(Computer &comp1):cpu(comp1.cpu),ram(comp1.ram),dis_driver(comp1.dis_driver) coutcallin
22、g computers copy constructor!endl; Computer() coutcalling computers deconsturctor!endl; void run() coutcomputer is running!endl; void stop() coutcomputer stops!endl; private: CPU cpu; int ram; int dis_driver; ;int main() double x=2.3,y=4.5; CPU cpu1; CPU cpu2(x,W128,y); Computer mycomputer; Computer
23、 hiscomputer(cpu1,256,17); Computer hercomputer(cpu2,512,19); return 0;大致过程如下:CPU cpu1;对应前两句;CPU cpu2(x,W128,y);对应第三四句;Computer mycomputer;会先调用默认构造函数初始化内嵌对象cpu,即对应第5、6句;然后再调用自身的不带参数的构造函数,即第7句;Computer hiscomputer(cpu1,256,17); 因为cpu1已经是定义好的一个对象,而它作为参数,自动会调用CPU类的拷贝构造函数生成一个临时的CPU类的对象,然后用这个临时对象去初始化Comp
24、uter类的当前对象的内嵌成员cpu,这里会再调用一次CPU类的拷贝构造函数创建临时对象,然后调用Computer类自身的带参数的构造函数,接着CPU类的析构函数释放第二次调用拷贝构造函数时产生的临时对象;总体对应语句8-11;12-15同理;16-17对应hercomputer对象的释放调用Computer的析构函数,然后调用其内嵌对象的析构函数释放内嵌CPU类的对象;18-19,20-21同理类推;最后两条语句分别对应cpu2,cpu1对象释放时自动调用的析构函数。(析构函数的调用顺序和构造函数相反)实验4 含有类的静态成员与类的友元的C+程序的结构设计三、实验内容1设计一个解决王婆卖瓜问
25、题的程序。王婆卖瓜,每卖一个瓜,需记录该瓜的重量,还要记录所卖出的总重量和总个数。同时还允许退瓜。设计一个具有静态数据、函数成员的watermelon类。实现提示:西瓜类中,设计3个数据成员(重量weight、总重量total_weiht、总个数total_number)。因为不论西瓜是否存在,总重量total_weiht和总个数total_number这两个数据总是要保留的,因此这两个数据要申明为静态数据成员。成员函数:卖瓜用构造函数模拟,退瓜用析构函数模拟,瓜重用显示disp()成员函数模拟。为了用不与特定对象相联系的静态成员函数来访问静态数据,还需要定义一个显示总重量和总数的静态成员函数
26、total_disp()。2设计一个程序,其中有3个类,即CBank,BBank和GBank,分别为中国银行类、工商银行类和农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另有一个友元函数total用于计算储户在这3家银行中的总存款数。3. 设计一个程序,其中有2个类,Point类为点类,包含2个私有数据x和y,表示点的坐标,line类为直线类,包含3个私有数据a,b和c,表示直线方程ax+by+c=0。另有一个友元函数dist,用于计算一个点到直线的距离。点与直线之间的距离计算公式如下:要求: 将Point与Line类的定义放在头文件head.h中; 将Poi
27、nt与Line类的实现部分放在PL.cpp中; 主函数(类的使用)文件定义为:Lab04_3.cpp。四、实验记录#include using namespace std;class watermelonpublic: watermelon(double w) weight=w; total_weight+=weight; total_number+; watermelon() total_weight-=weight; total_number-; couttotal weight:total_weight total number:total_numberendl; void disp() coutwatermelon weight:weightendl;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1