实验七 自定义数据类型.docx

上传人:b****6 文档编号:6420166 上传时间:2023-01-06 格式:DOCX 页数:18 大小:101.34KB
下载 相关 举报
实验七 自定义数据类型.docx_第1页
第1页 / 共18页
实验七 自定义数据类型.docx_第2页
第2页 / 共18页
实验七 自定义数据类型.docx_第3页
第3页 / 共18页
实验七 自定义数据类型.docx_第4页
第4页 / 共18页
实验七 自定义数据类型.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

实验七 自定义数据类型.docx

《实验七 自定义数据类型.docx》由会员分享,可在线阅读,更多相关《实验七 自定义数据类型.docx(18页珍藏版)》请在冰豆网上搜索。

实验七 自定义数据类型.docx

实验七自定义数据类型

实验七自定义数据类型

一.实验目的

1.掌握结构体类型变量的定义和使用;

2.掌握结构体数组的概念和应用;

3.了解链表的概念,初步学会对简单链表进行操作。

二.实验内容

1.第七章第一题;

2.第七章第三题;

3.第七章第五题;

4.第七章第六题至第十题;

5.实验心得。

请分别:

(1)用一个文件包含这些函数;

(2)把每个文件作为一个文件,然后将它们放在一个项目文件中处理。

#include

usingnamespacestd;

struct

{intyear;

intmonth;

intday;

}date;

intmain()

{intdays;

cout<<"inputyear,month,day:

";

cin>>date.year>>date.month>>date.day;

switch(date.month)

{case1:

days=date.day;break;

case2:

days=date.day+31;break;

case3:

days=date.day+59;break;

case4:

days=date.day+90;break;

case5:

days=date.day+120;break;

case6:

days=date.day+151;break;

case7:

days=date.day+181;break;

case8:

days=date.day+212;break;

case9:

days=date.day+243;break;

case10:

days=date.day+273;break;

case11:

days=date.day+304;break;

case12:

days=date.day+334;brea

}

if((date.year%4==0&&date.year%100!

=0

||date.year%400==0)&&date.month>=3)

days+=1;

cout<

<<"thdayin"<

return0;

}

好吧,不能再像以前的实验报告那样再发图了,那样好麻烦好费力。

这一题就是知识在以前写过的一个程序上变成了结构体,注意一下格式就行了。

总是很容易忘记在定义结构体后加上“;”号。

#include

#include

usingnamespacestd;

constintn=5;

structstudent

{charnum[6];

charname[8];

intscore[4];

}stu[n];

intmain()

{voidprint(studentstu[6]);

inti,j;

for(i=0;i

{cout<<"inputscoresofstudent"<

"<

cout<<"NO.:

";

cin>>stu[i].num;

cout<<"name:

";

cin>>stu[i].name;

for(j=0;j<3;j++)

{cout<<"score"<

";

cin>>stu[i].score[j];

}

cout<

}

print(stu);

return0;

}

voidprint(studentstu[6])

{inti,j;

cout<<"NO.namescore1score2score3"<

for(i=0;i

{cout<

for(j=0;j<3;j++)

cout<

cout<

}

}

这两道题可以说是上学期我们的c实验的一个节选吧,因为上学期我是编写一个学生成绩系统(也提过了……),只是下面这道题多出了一个最高分。

#include

#include

usingnamespacestd;

constintn=10;

structstudent

{charnum[6];

charname[8];

intscore[4];

floatavr;

}stu[n];

intmain()

{inti,j,max,maxi,sum;

floataverage;

for(i=0;i

{cout<<"inputscoresofstudent"<

cout<<"NO.:

";

cin>>stu[i].num;

cout<<"name:

";

cin>>stu[i].name;

for(j=0;j<3;j++)

{cout<<"score"<

";

cin>>stu[i].score[j];

}

cout<

}

average=0;

max=0;

maxi=0;

for(i=0;i

{sum=0;

for(j=0;j<3;j++)

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

stu[i].avr=sum/3.0;

average+=stu[i].avr;

if(sum>max)

{max=sum;

maxi=i;

}

}

average/=n;

cout<<"NO.namescore1score2score3average"<

for(i=0;i

{cout<

for(j=0;j<3;j++)

cout<

cout<

}

cout<<"average="<

cout<<"Thehighestscoreis:

"<

"<

return0;

实验八类和对象

(一)

1.实验目的

(1)掌握声明类的方法,类和类的成员的概念以及定义对象的方法。

(2)初步掌握用类和对象编制基于对象的程序。

(3)学习检查和调试基于对象的程序。

2.实验内容

(1)第8章第2题;

(2)第8章第4题;

(3)第8章第6题;

3.预习内容

教材第8章。

#include

usingnamespacestd;

classTime

{public:

voidset_time(void)

{cin>>hour;

cin>>minute;

cin>>sec;

}

voidshow_time(void)

{cout<

"<

"<

private:

inthour;

intminute;

intsec;

};

Timet;

intmain()

{

t.set_time();

t.show_time();

return0;

}

终于开始写类了,只是似乎有点水,刚开始怎么也没看懂书上的那个……

//xt8-4-1.cpp(main.cpp)

#include

usingnamespacestd;

#include"xt8-4.h"

intmain()

{Studentstud;

stud.set_value();

stud.display();

return0;

}

#include"xt8-4.h"

#include

usingnamespacestd;

voidStudent:

:

display()

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

}

voidStudent:

:

set_value()

{cin>>num;

cin>>name;

cin>>sex;

}

#include

usingnamespacestd;

classBox

{public:

voidget_value();

floatvolume();

voiddisplay();

public:

floatlengh;

floatwidth;

floatheight;

};

voidBox:

:

get_value()

{cout<<"pleaseinputlengh,width,height:

";

cin>>lengh;

cin>>width;

cin>>height;

}

floatBox:

:

volume()

{return(lengh*width*height);}

voidBox:

:

display()

{cout<

intmain()

{Boxbox1,box2,box3;

box1.get_value();

cout<<"volmueofbax1is";

box1.display();

box2.get_value();

cout<<"volmueofbax2is";

box2.display();

box3.get_value();

cout<<"volmueofbax3is";

box3.display();

return0;

}

硬要说什么总结的话,就是类不会,还得多学习,练习!

#include

usingnamespacestd;

classBox

{public:

voidget_value();

voidvolume();

voiddisplay();

public:

floatlengh;

floatwidth;

floatheight;

floatvol;

};

voidBox:

:

get_value()

{cout<<"pleaseinputlengh,width,height:

";

cin>>lengh;

cin>>width;

cin>>height;

}

voidBox:

:

volume()

{vol=lengh*width*height;}

voidBox:

:

display()

{cout<

intmain()

{Boxbox1,box2,box3;

box1.get_value();

box1.volume();

cout<<"volmueofbax1is";

box1.display();

box2.get_value();

box2.volume();

cout<<"volmueofbax2is";

box2.display();

box3.get_value();

box3.volume();

cout<<"volmueofbax3is";

box3.display();

return0;

}

实验九类和对象

(二)

1.实验目的

(1)进一步加深对类和对象的理解。

(2)掌握类的构造函数和析构函数的概念和使用方法。

(3)掌握对象数组、对象的指针以及使用方法。

(4)掌握友元的概念和使用。

(5)了解类模板的使用方法。

2.实验内容

(1)

1 第9章第6题;

2 第9章第8题;

(2)第9章第9题;

(3)第9章第10题;

(4)教材第9章例9.14的程序;

3.预习内容

教材第9章。

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats){num=n;score=s;}

voiddisplay(){cout<

private:

intnum;

floatscore;

};

intmain()

{Studentstud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return0;

}

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats){num=n;score=s;}

voiddisplay(){cout<

private:

intnum;

floatscore;

};

intmain()

{Studentstud(101,78.5);

voidfun(Student&);

fun(stud);

return0;

}

voidfun(Student&stu)

{stu.display();

stu.change(101,80.5);

stu.display();

}

#include

usingnamespacestd;

classProduct

{public:

Product(intn,intq,floatp):

num(n),quantity(q),price(p){};

voidtotal();

staticfloataverage();

staticvoiddisplay();

private:

intnum;

intquantity;

floatprice;

staticfloatdiscount;

staticfloatsum;

staticintn;

};

voidProduct:

:

total()

{floatrate=1.0;

if(quantity>10)rate=0.98*rate;

sum=sum+quantity*price*rate*(1-discount);

n=n+quantity;

}

voidProduct:

:

display()

{cout<

cout<

}

floatProduct:

:

average()

{return(sum/n);}

floatProduct:

:

discount=0.05;

floatProduct:

:

sum=0;

intProduct:

:

n=0;

intmain()

{

ProductProd[3]={

Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)

};

for(inti=0;i<3;i++)

Prod[i].total();

Product:

:

display();

return0;

}

#include

usingnamespacestd;

classProduct

{public:

Product(intn,intq,floatp):

num(n),quantity(q),price(p){};

voidtotal();

staticfloataverage();

staticvoiddisplay();

private:

intnum;

intquantity;

floatprice;

staticfloatdiscount;

staticfloatsum;

staticintn;

};

voidProduct:

:

total()

{floatrate=1.0;

if(quantity>10)rate=0.98*rate;

sum=sum+quantity*price*rate*(1-discount);

n=n+quantity;

}

voidProduct:

:

display()

{cout<

cout<

}

floatProduct:

:

average()

{return(sum/n);}

floatProduct:

:

discount=0.05;

floatProduct:

:

sum=0;

intProduct:

:

n=0;

intmain()

{

ProductProd[3]={

Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)

};

for(inti=0;i<3;i++)

Prod[i].total();

Product:

:

display();

return0;

}

真的对于这个实验报告我也是要疯了,要找编译器,因为电脑用不了visualc++,然后用新的编译器visualstudio,又不知道该怎么用,总是打不开以前写的程序,一定要新建一个项目才能编译,

而且,编写一个程序不知道怎么的就是要好久,看着别人飞快地写完了,自己却没什么进展很是懊恼,但是不管怎么说自己总是在这方面比别人少花了时间去消化去理解,而且能怎么样呢,不还是得努力学……!

对于类的话,自己是真的不会,只有在c上可以写的程序,自己还能用吧,不管怎样时间还有,努力学吧

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

当前位置:首页 > 考试认证 > 交规考试

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

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