C++实验报告Word文档格式.docx

上传人:b****6 文档编号:21696034 上传时间:2023-01-31 格式:DOCX 页数:37 大小:24.28KB
下载 相关 举报
C++实验报告Word文档格式.docx_第1页
第1页 / 共37页
C++实验报告Word文档格式.docx_第2页
第2页 / 共37页
C++实验报告Word文档格式.docx_第3页
第3页 / 共37页
C++实验报告Word文档格式.docx_第4页
第4页 / 共37页
C++实验报告Word文档格式.docx_第5页
第5页 / 共37页
点击查看更多>>
下载资源
资源描述

C++实验报告Word文档格式.docx

《C++实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++实验报告Word文档格式.docx(37页珍藏版)》请在冰豆网上搜索。

C++实验报告Word文档格式.docx

2.24.一元硬币换成1,2,5分硬币

#include<

iostream>

iomanip>

usingnamespacestd;

intmain()

{

inta,b,c,S,i=0,sum=100,o=1,t=2,f=5;

for(a=0;

a<

=(sum/o);

a++)

{

for(b=0;

b<

=(sum/t);

b++)

{

for(c=0;

c<

=(sum/f);

c++)

{

S=a*o+b*t+c*f;

if(S==sum)

{

cout<

<

sum<

"

="

<

setw(3)<

*"

o<

+"

setw

(2)<

t<

<

f<

endl;

i++;

}

}

}

cout<

1元兑换成1、2、5分总共有"

i<

种方法"

system("

pause"

);

return0;

}

实验结果

100=26*1+37*2+0*5

100=27*1+4*2+13*5

100=27*1+9*2+11*5

......

100=96*1+2*2+0*5

100=98*1+1*2+0*5

100=100*1+0*2+0*5

1元兑换成1、2、5分总共有541种方法

2.25。

输入两个整数,由小到大输出

inta,b;

int&

m=a,&

n=b;

输入两个整数:

"

;

cin>

>

a>

b;

由小到大输出:

(a>

b)?

(cout<

n<

m):

m<

n);

9865

6598

7.13.小写字母转换成大写字母

fstream>

fstreamin("

file1.txt"

ios:

:

in);

if(!

in){

cout<

Erroropenfile1"

return1;

fstreamout("

file2.txt"

out);

out){

Erroropenfile2"

return2;

charch;

while(in.get(ch)){

if(ch>

='

a'

&

ch<

z'

ch=ch-32;

out.put(ch);

elseout.put(ch);

//cout<

ch;

in.close();

out.close();

提高题

string>

structstudent{

stringX,M;

intscore[10];

};

intmain(){

in.txt"

Erroropenfile1;

out.txt"

Erroropenfile2;

studentstu[4];

for(inti=0;

i<

4;

i++){

intsum=0;

doubleavg=0.0;

in>

stu[i].X>

stu[i].M;

out<

stu[i].X<

for(intj=0;

j<

10;

j++){

in>

stu[i].score[j];

out<

sum+=stu[i].score[j];

//while(in.eof())break;

avg=sum/10.0;

avg<

实验结果:

欧阳浩辰45646465595.4

上官北45656265965.4

季微然42566556424.5

李维58545864655.6

教师签名:

年月日

二、类与对象设计、友元函数与运算符重载

1.理解和对象的概念,掌握声明类和定义对象的方法;

2.掌握构造函数和析构函数的实现方法;

3.掌握对象数组、对象指针和string类的使用方法;

4.掌握静态数据成员和静态成员函数的使用方法;

5.理解友元的概念和掌握友元的使用方法。

教材P134第3.32、3.33、3.34题

教材P134第3.35、3.36题

3、选做题:

参考第3.32题,建立类球体类Sphere和正方体类Cube,要求能够计算球体、正方体的体积。

3.32圆柱体积

classcylinder{

private:

doubler;

doubleh;

public:

cylinder(doublei,doublej)

{r=i;

h=j;

doublevol()

{doubleV;

constdoublePI=3.14;

V=PI*r*r*h;

returnV;

{

cylinderA(1.1,2.2);

cout<

圆柱体积:

A.vol();

8.35868

3.33显示qu*price

classbook{

intqu,price;

book(inti)

qu=i;

price=10*qu;

intshow()

returnprice*qu;

bookA[5]={1,2,3,4,5};

for(inti=0;

i<

5;

i++)

cout<

A[i].show()<

104090160250

3.34显示qu*price(对象数组)

book*pa;

pa=A+4;

{cout<

pa->

show()<

pa--;

250160904010

3.35.显示Stock的值

classStock{

charstockcode[10];

intquan;

doubleprice;

Stock(charna[],intq=1000,doublep=8.98);

voidprint();

Stock:

Stock(charna[],intq,doublep)//类外定义,不带默认参数值

{

for(inti=0;

stockcode[i]=na[i];

//while(stockcode[i]='

\0'

)break;

quan=q;

price=p;

voidStock:

print()

if(stockcode[i]=='

cout<

stockcode[i];

quan<

price<

endl;

Stocksto1("

600001"

3000,5.67);

sto1.print();

Stocksto2("

sto2.print();

60000130005.67

60000110008.98

3.36学生成绩

classStudent{

stringname;

stringstu_no;

floatscore;

staticintcount;

staticfloatsum;

staticfloatavg;

Student(stringstr1,stringstu_no1,floatscore1);

voidshow();

voidshow_count_avg();

Student:

Student(stringstr1,stringstu_no1,floatscore1)

name=str1;

stu_no=stu_no1;

score=score1;

count++;

sum+=score1;

avg=sum/count;

voidStudent:

show()

\n姓名:

name;

\n学号:

stu_no;

\n成绩:

score;

show_count_avg()

\n总人数:

count;

\n平均分:

avg;

\n*******************************"

intStudent:

count=0;

floatStudent:

sum=0.0;

avg=0.0;

Studentstu1("

里美"

"

20140001"

90);

stu1.show();

stu1.show_count_avg();

Studentstu2("

欧浩"

20140002"

80);

stu2.show();

stu2.show_count_avg();

Studentstu3("

网飞"

20140003"

75);

stu3.show();

stu3.show_count_avg();

姓名:

里美

学号:

20140001

成绩:

90

总人数:

1

平均分:

*******************************

欧浩

20140002

80

2

85

网飞

20140003

75

总人数:

3

成绩:

81.6667

提高题:

求球和正方体体积

constdoublePI=3.14;

classSphere{

Sphere(doubler)

radius=r;

V_Sphere=4.0/3*PI*radius*radius*radius;

voidvol()

Sphere'

svolumeis:

V_Sphere<

doubleradius,V_Sphere;

classCube{

Cube(doubler)

V_Cube=radius*radius*radius;

Cube'

V_Cube<

doubleradius,V_Cube;

SphereS(3.0);

CubeC(4.0);

S.vol();

C.vol();

113.04

64

三、派生类与继承

1.掌握派生类的声明方法和派生类构造函数的定义;

2.掌握不同继承方式下,基类成员在派生类中的访问属性;

3.掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则;

4.学习虚基类在解决二义性问题中的作用。

教材P196第4.20、4.21题

教材P134第4.22题

实验二选做题的加强版,设计一个图形类Shape作为父类,圆柱体类Cylinder、球体类Sphere和正方体类Cube为其子类,要求能够图形的体积。

4.20矩形,三角形面积(虚函数)

classarea_cl{

protected:

doubleheight;

doublewidth;

area_cl(doubler,doubles)

height=r;

width=s;

virtualdoublearea()=0;

classrectangle:

publicarea_cl{

rectangle(doubler,doubles):

area_cl(r,s)

{};

doublearea()

{returnheight*width;

classisosceles:

isosceles(doubler,doubles):

return0.5*height*width;

area_cl*op;

rectangleor(10.0,5.0);

isoscelesoi(4.0,6.0);

op=&

or;

rectangle'

sareais:

op->

area()<

oi;

isosceles'

50

12

4.22学生老师信息

classperson{

person();

stringnum;

classstudent:

publicperson{

intclass_num;

student();

voidprint_stu();

};

classteacher:

stringjob;

stringsection;

teacher();

voidprint_teh();

person:

person()

stringnum1;

stringname1;

输入编号:

num1;

输入姓名:

name1;

num=num1;

name=name1;

voidperson:

编号:

num<

姓名:

name<

student:

student()

intclass_num1;

floatscore1;

输入班号:

class_num1;

输入成绩:

score1;

class_num=class_num1;

score=score1;

voidstudent:

print_stu()

*****学生信息:

*****"

print();

班号:

class_num<

score<

teacher:

teacher()

stringjob1;

stringsection1;

输入职称:

job1;

输入部门:

section1;

job=job1;

section=section1;

voidteacher:

print_teh()

*****老师信息:

职称:

job<

部门:

section<

studentstu;

stu.print_stu();

teacherteh;

teh.print_teh();

偶偶

01

96

*****

0001

大大

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 日语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1