C++实验代码汇总.docx
《C++实验代码汇总.docx》由会员分享,可在线阅读,更多相关《C++实验代码汇总.docx(40页珍藏版)》请在冰豆网上搜索。
![C++实验代码汇总.docx](https://file1.bdocx.com/fileroot1/2022-11/29/9da151be-9865-43fc-af3e-c94ab81866cd/9da151be-9865-43fc-af3e-c94ab81866cd1.gif)
C++实验代码汇总
实验1简单C++程序设计
2.实验内容
(1)熟悉VisualC++6.0的各个窗口和布局:
包括标题栏、菜单栏、工具栏、工作区窗口、输出窗口等)。
(2)输入下列简单C++程序,完成编译、连接、运行,熟悉C++程序的开发过程。
#include//包含头文件
usingnamespacestd;//打开命名空间std
voidmain()//主函数,程序入口
{intage;//声明一个变量
age=20;//赋值语句
cout<<"Theageis:
\n";/输出一个字符串
cout<return0;//主函数返回0
}//块作用域结束
以上语句有错误吗?
如果有,错误在哪里?
(3)修改第2章课后习题2-15中C++程序的错误,记录改正后程序的运行结果?
(4)编程实现课后习题2-28-
(1),体会break、continue语句的用法。
(5)用For循环结合穷举法找出50~100之间的质数。
(质数的概念自己上网搜索答案)。
(6)用while和do…while循环编程实现用户猜数字游戏:
给定一个1~50之间的整数,让用户猜这个数字,比较两个数的大小,把结果提示给用户,直到猜对为止。
//第3题
#include
usingnamespacestd;
//此段程序系课本61页的2-15题
intmain(){
inti;
intj;
i=10;
j=20;
cout<<"i+j="<
return0;
}
//第4题
#include
usingnamespacestd;
intmain(){
charc;
while
(1){
cout<<"Menu:
A(dd)D(elete)S(ort)Q(uit)Selectone:
"<cin>>c;
if(c=='A'){cout<<"数据已经增加"<elseif(c=='D'){cout<<"数据已经删除"<elseif(c=='S'){cout<<"数据已经排序"<elseif(c=='Q')break;
}
return0;
}
//第5题
#include
#include
usingnamespacestd;
intmain(){
intn=0;
for(inti=50;i<=100;i++){
boolflag=true;
for(intj=2;jif(i%j==0){
flag=false;
break;
}
if(flag==true)cout<
}
return0;
}
//第6题
#include
usingnamespacestd;
main(){
intgnum=0;
intresult=35;
while(gnum!
=result){
cout<<"pleaseguessannumberbetween1and50untilyouaresucceed:
"<cin>>gnum;
if(gnum==result){
cout<<"congrudulation:
youaresucceed."<}
elseif(gnum>result){
cout<<"sorry,yourguessistoolarge!
"<}
else{
cout<<"sorry,yourguessistoosmall!
"<}
}
}
}
实验2函数的定义与使用
2.实验内容
(1)lab2_1.cpp:
写一个判断闰年的函数,在主函数中输入一个年份,输出是否是闰年的信息。
(2)lab2_2.cpp:
实现第三章课后习题3-13的功能,并结合单步跟踪法进行程序的调试。
(3)lab2_3.cpp:
编写3个名为max的重载函数,分别实现求两个整数、三个整数、两个双精度型数最大值的功能。
(4)lab6_4.cpp:
计算如下公式,并输出结果:
其中r、s的值由键盘输入。
sin(x)的值直接调用系统函数
/*第1题:
#include
usingnamespacestd;
intmain(){
intyear;
boolisleapyear;
boolleapjudge(intyear);
cout<<"pleseinputayear:
";
cin>>year;
isleapyear=leapjudge(year);
if(isleapyear)cout<"<elsecout<"<return0;
}
boolleapjudge(intyear){
return((year%400==0)||(year%4==0&&year%100!
=0));
}
*/
/*第2题求斐波那契级数
#include
usingnamespacestd;
intmain(){
intnum;
intfib(intn);
cout<<"pleseinputaninteger:
";
cin>>num;
cout<"<return0;
}
intfib(intn){
intrnum;
if(n==1||n==2)rnum=1;
elsernum=fib(n-1)+fib(n-2);
returnrnum;
}
/*第3题:
重载函数
#include
usingnamespacestd;
intmain(){
inta=1,b=2,c=3;
doubled=4.53231,e=5.2863728;
intmax(intx,inty);
intmax(intx,inty,intz);
doublemax(doublex,doubley);
cout<<"themaximumofaandbis:
"<cout<<"themaximumofa,bandcis:
"<cout<<"themaximumofdandeis:
"<return0;
}
intmax(intx,inty){
if(x>y)returnx;
elsereturny;
}
intmax(intx,inty,intz){
intMnum=x;
if(Mnumif(MnumreturnMnum;
}
doublemax(doublex,doubley){
if(x>y)returnx;
elsereturny;
}
/*第4题:
#include
#include
usingnamespacestd;
intmain(){
doubler,s,k;
cout<<"pleaseinputtworealnumbers:
"<cin>>r>>s;
if(r*r>s*s)k=sin(r*s)/2;
elsek=sqrt(sin(r)*sin(r)+sin(s)*sin(s));
cout<return0;
}
实验3类与对象的设计
三、实验内容
1.Lab3_1.cpp:
定义并实现一个矩形类(rectangle),有长(length)和宽(width)两个属性,带有3个成员函数(showlength:
用于显示矩形的长度,showwidth:
用于显示矩形的宽度,area:
用于计算矩形的面积)。
2.Lab3_2.cpp:
声明一个CPU类,包含主频(frequency),字长(wordlength),CPU倍频系数(coefficient)属性,其中字长为枚举型enumcpu_wordlen={W16,W32,W64,W128,W256},frequency是单位为GHz的实数,coefficient为浮点型数据。
两个公有成员函数run和stop分别表示CPU的运行与停止。
请在构造函数(带参数和不带参数)、拷贝构造函数、析构函数、run和stop函数体给出相应的提示。
说明并实现这个类,观察构造函数、拷贝构造函数和析构函数的调用顺序。
3.lab3_3.cpp:
请使用类的组合来描述计算机类Computer,该类的数据成员有芯片cpu,内存ram,显示器dis_driver,两个成员函数run和stop分别表示计算机的运行与停止,两个自定义的构造函数分别对应有参数和无参数的情况。
芯片cpu是lab3_2中CPU类的对象,内存ram是单位为M的整型数据,显示器dis_driver为屏幕对角线英寸的整型数据。
请写出类Computer的声明,并写出各个成员函数的实现,在每个成员函数的函数体中给出相应的提示。
观察构造函数和复制构造函数的调用顺序。
六、实验小结
#include
usingnamespacestd;
classrectangle{
public:
rectangle():
length(0),width(0){
}
rectangle(floatx,floaty){
length=x;
width=y;
}
voidshowlength(){
cout<<"thelengthofthisrectangleis:
"<}
voidshowwidth(){
cout<<"thewidthofthisrectangleis:
"<}
floatarea(){
returnlength*width;
}
private:
floatlength,width;
};
intmain(){
rectanglerec1;
rec1.showlength();
rec1.showwidth();
cout<<"itsareais:
"<rectanglerec2(3,4);
rec2.showlength();
rec2.showwidth();
cout<<"itsareais:
"<return0;
}
注意:
(1)在调用不带参数的构造函数构造对象时不能写成rectanglerec1();否则会提示如下错误:
leftof'.showlength'musthaveclass/struct/uniontype,说明类的对象没有创建成功
(2)除了书上P108的Clock类的默认构造函数(系统自动定义的构造函数)可用外,其余自己编写的类都必须由用户给出不带参数默认构造函数的函数体,否则会提示如下错误:
“noappropriatedefaultconstructoravailable”
#include
usingnamespacestd;
enumcpu_wordlen{W16,W32,W64,W128,W256};
classCPU{
public:
CPU(){
frequency=0;
wordlength=W16;
coefficient=0;
cout<<"frequency:
"<"<"<cout<<"callingtheconstructorwithoutparameters!
"<}
CPU(floatf,cpu_wordlenl,floatc){
frequency=f;
wordlength=l;
coefficient=c;
cout<<"frequency:
"<"<"<cout<<"callingtheconstructorwithparameters!
"<}
CPU(CPU&C){
frequency=C.frequency;
wordlength=C.wordlength;
coefficient=C.coefficient;
cout<<"callingthecopyconstructor!
"<}
~CPU(){
cout<<"callingthedeconstructor!
"<}
voidrun(){
cout<<"CPUisrunning!
"<}
voidstop(){
cout<<"CPUstops!
"<}
private:
doublefrequency;
enumcpu_wordlenwordlength;
doublecoefficient;
};
intmain(){
doublex=3.2,y=4.5;
CPUmycpu;
mycpu.run();
mycpu.stop();
CPUhiscpu(x,W64,y);
hiscpu.run();
hiscpu.stop();
CPUhercpu(hiscpu);
hercpu.run();
hercpu.stop();
return0;
}
注意:
由于wordlength为enum型变量,要显示对应的值比如“W16”,必须用switch语句或者if语句,如P54的例2-11
#include
usingnamespacestd;
enumcpu_wordlen{W16,W32,W64,W128,W256};
classCPU{
public:
CPU(){
frequency=0;
wordlength=W16;
coefficient=0;
cout<<"frequency:
"<"<"<cout<<"callingCPU'sconstructorwithoutparameters!
"<}
CPU(floatf,cpu_wordlenl,floatc){
frequency=f;
wordlength=l;
coefficient=c;
cout<<"frequency:
"<"<"<cout<<"callingCPU'sconstructorwithparameters!
"<}
CPU(CPU&C){
frequency=C.frequency;
wordlength=C.wordlength;
coefficient=C.coefficient;
cout<<"callingCPU'scopyconstructor!
"<}
~CPU(){
cout<<"callingCPU'sdeconstructor!
"<}
voidrun(){
cout<<"CPUisrunning!
"<}
voidstop(){
cout<<"CPUstops!
"<}
private:
doublefrequency;
enumcpu_wordlenwordlength;
doublecoefficient;
};
classComputer{
public:
Computer(){
cout<<"callingcomputer'sconstructorwithoutparameters!
"<}
Computer(CPUc,intr,intd):
cpu(c),ram(r),dis_driver(d){
cout<<"callingcomputer'sconstructorwithparameters!
"<}
Computer(Computer&comp1):
cpu(comp1.cpu),ram(comp1.ram),dis_driver(comp1.dis_driver){
cout<<"callingcomputer'scopyconstructor!
"<}
~Computer(){
cout<<"callingcomputer'sdeconsturctor!
"<}
voidrun(){
cout<<"computerisrunning!
"<}
voidstop(){
cout<<"computerstops!
"<}
private:
CPUcpu;
intram;
intdis_driver;
};
intmain(){
doublex=2.3,y=4.5;
CPUcpu1;
CPUcpu2(x,W128,y);
Computermycomputer;
Computerhiscomputer(cpu1,256,17);
Computerhercomputer(cpu2,512,19);
return0;
}
大致过程如下:
CPUcpu1;对应前两句;
CPUcpu2(x,W128,y);对应第三四句;
Computermycomputer;会先调用默认构造函数初始化内嵌对象cpu,即对应第5、6句;然后再调用自身的不带参数的构造函数,即第7句;
Computerhiscomputer(cpu1,256,17);因为cpu1已经是定义好的一个对象,而它作为参数,自动会调用CPU类的拷贝构造函数生成一个临时的CPU类的对象,然后用这个临时对象去初始化Computer类的当前对象的内嵌成员cpu,这里会再调用一次CPU类的拷贝构造函数创建临时对象,然后调用Computer类自身的带参数的构造函数,接着CPU类的析构函数释放第二次调用拷贝构造函数时产生的临时对象;总体对应语句8-11;12-15同理;
16-17对应hercomputer对象的释放调用Computer的析构函数,然后调用其内嵌对象的析构函数释放内嵌CPU类的对象;18-19,20-21同理类推;
最后两条语句分别对应cpu2,cpu1对象释放时自动调用的析构函数。
(析构函数的调用顺序和构造函数相反)
实验4含有类的静态成员与类的友元的C++程序的结构设计
三、实验内容
1.设计一个解决王婆卖瓜问题的程序。
王婆卖瓜,每卖一个瓜,需记录该瓜的重量,还要记录所卖出的总重量和总个数。
同时还允许退瓜。
设计一个具有静态数据、函数成员的watermelon类。
实现提示:
西瓜类中,设计3个数据成员(重量weight、总重量total_weiht、总个数total_number)。
因为不论西瓜是否存在,总重量total_weiht和总个数total_number这两个数据总是要保留的,因此这两个数据要申明为静态数据成员。
成员函数:
卖瓜用构造函数模拟,退瓜用析构函数模拟,瓜重用显示disp()成员函数模拟。
为了用不与特定对象相联系的静态成员函数来访问静态数据,还需要定义一个显示总重量和总数的静态成员函数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中;
②将Point与Line类的实现部分放在PL.cpp中;
③主函数(类的使用)文件定义为:
Lab04_3.cpp。
四、实验记录
#include
usingnamespacestd;
classwatermelon{
public:
watermelon(doublew){
weight=w;
total_weight+=weight;
total_number++;
}
~watermelon(){
total_weight-=weight;
total_number--;
cout<<"totalweight:
"<"<}
voiddisp(){
cout<<"watermelonweight:
"<}