C上机实验报告(自定义数据类型类与对象Ⅰ).docx_第1页
C上机实验报告(自定义数据类型类与对象Ⅰ).docx_第2页
C上机实验报告(自定义数据类型类与对象Ⅰ).docx_第3页
C上机实验报告(自定义数据类型类与对象Ⅰ).docx_第4页
C上机实验报告(自定义数据类型类与对象Ⅰ).docx_第5页
已阅读5页,还剩32页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+上机实验报告实验名称:自定义数据类型 类和对象专业班级:姓 名:学 号:实验日期:目录1. 实验目的2. 实验内容3. 程序代码4. 调试结果5. 实验心得1.实验目的实验7(1) 掌握结构体类型变量的定义和使用;(2) 掌握结构体类型组的概念和应用;(3) 了解链表的概念,初步学会对简单链表进行操作。实验8(1) 掌握声明类的方法,类和类的成员的概念以及定义对象的方法;(2) 初步掌握用类和对象编制基于对象的程序;(3) 学习检查和调试基于对象的程序。2.实验内容实验7编程序,然后上机调试。(1) 定义一个结构体变量(包括年,月,日),编程序,要求输入年,月,日, 程序能计算并输出该日在本年中是第几天。注意闰年问题。(2) 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号),name(姓名),score3(3门课的成绩)。用主函数输入这些数据,用print函数输出这些数据。(3) 有10个学生,每个学生的数据包括学号,姓名,3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课总平均成绩,以及最高分的学生的数据(包括学号,姓名3门课的成绩,平均分数)。要求用一个cin语句输入10个学生数据;用一个average函数求总平均分;用max函数找出最高分学生数据;总平均分和最高分的学生的数据都在主程序中输出。(4) 建立单向动态链表,并对它 ,删除和输出等操作。包括以下任务:.写一个函数creat,用来建立一个动态链表。各结点的数据有键盘输入。.写一个函数print,将上题建立的链表中各结点的数据依次输出。.写一个函数del,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。.写一个函数insert,用来向动态链表插入一个结点。.将以上4个函数组成一个程序,由主程序先后调用这些函数,实现链表的建立,输出,删除和插入,在主程序中指定需要删除和插入的结点。实验8(1) 有以下程序:#includeusing namespace std;class Time /定义Time类public: /数据成员为公用的int hour;int minute;int sec;int main()Time t1; /定义t1为Time类对象cint1.hour; /输入设定的时间cint1.minute;cint1.sec;coutt1.hour:t1.minute:t1.secendl; /输出时间return 0;改写程序,要求:.将数据成员改为私有的;.将输入和输出的功能改为由成员函数实现;.在类体内定义成员函数。然后编译和运行程序。(2) 分别给出如下的3 个文件:.含类定义的头文件student.h/student.h (这是头文件,在此文件中进行类的声明)class Student /类声明public: void display(); /公用成员函数原型声明 private: int num; char name20; char sex;.包含成员函数定义的源文件student.cpp/student.cpp 在此文件中进行函数的定义#include #includestudent.h /不要漏写此行,否则编译通不过void Student:display() /在类外定义display类函数coutnum:numendl;coutname:nameendl;coutsex:sexendl;.包含主函数的源文件main.cpp/mian.cpp 主函数模块#include#includestudent.h /将类声明头文件包含进来int main()Student stud; /定义对象stud.display(); /执行stud对象的display函数return 0;请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value,上机调试并运行。(3) 需要求3个长方柱的体积,请编一个基于对象的程序。数据成员length(长),width(宽),height(高)。要求用成员函数实现以下功能:.由键盘分别输入3个长方柱的长,宽,高;.计算长方柱的体积;.输出3个长方柱的体积。请编写程序,上机调试并运行。3.程序代码实验7(1)#includeusing namespace std;structint year;int month;int day;date;int main()int days;coutdate.yeardate.monthdate.day;switch(date.month)case 1:days=date.day; break;case 2:days=date.day+31; break;case 3:days=date.day+59; break;case 4:days=date.day+90; break;case 5:days=date.day+120; break;case 6:days=date.day+151; break;case 7:days=date.day+181; break;case 8:days=date.day+212; break;case 9:days=date.day+243; break;case 10:days=date.day+273; break;case 11:days=date.day+304; break;case 12:days=date.day+334; break;if(date.year%4=0&date.year%100!=0)|date.year%400=0)&date.month=3) days+=1; coutdate.month/date.dayis thedaysth day in date.year.endl;return 0;(2)#include#includeusing namespace std;const int n=5;struct studentchar num6;char name8;int score4;stun;int main()void print(student stu); int i,j; for(i=0;in;i+) coutinput score of studenti+1:endl; coutstui.num; ; for(j=0;j3;j+) coutscorej+1stui.scorej; coutendl; print(stu);return 0;void print(student stu)int i,j; coutNO. name score1 score2 score3endl; for(i=0;in;i+) coutstui.num setw(10) ; for(j=0;j3;j+) coutsetw(3)stui.scorej ; coutendl; (3)#include#includeusing namespace std;const int n=3;struct studentchar num6; char name8; int score4; float avr;stun;int main()int i,j,max,maxi,sum; float average; for(i=0;in;i+) coutinput scores of studenti+1endl; coutstui.num; ; for(j=0;j3;j+) coutscorej+1stui.scorej; coutendl; average=0; max=0; maxi=0; for(i=0;i,n;i+) sum=0; for(j=0;jmax) max=sum; maxi=i; average/=n;cout NO. name score1 score2 score3 averageendl;for(i=0;jn;i+)coutsetw(8)stui.num setw(10) ;for(j=0;j3;j+)coutsetw(3)stui.scorej ;coutstui.avrendl;coutaverage=averageendl;coutThe highest score is:,score total:maxendl;return 0;(4). 写一个函数creat,用来建立一个动态链表。各结点的数据有键盘输入。#includeusing namespace std;#define NULL 0struct studentlong num;float score;student *next;int n;studnt *creat(void)student *head;student *p1,*p2;n=0;p1=p2=new student;cinp1-nump1-score;head=NULL;while(p1-num!=0)n=n+1;if(n=1) head=p1;else p2-next=p1;p2=p1;p1=new student;cinp1-nump1-score;p2-next=NULL;return(head);.写一个函数print,将上题建立的链表中各结点的数据依次输出。void print(student *head)student *p;coutNow,Thesenrecords are:endl;p=head;if(head!=NULL)docoutnum scorenext;while(p!=NULL);.写一个函数del,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。#includeusing namespace std;#define NULL 0struct studentlong num;float score;student *next;int n;student *del(student *head,long,num)student *p1,*p2;if(head=NULL)coutlist null!num&p1-next!=NULL)p2=p1;p1=p1-next;if(num=p1-num)if(p1=head) head=p1-next;else p2-next=p1-next;coutdelete:numendl;n=n-1else coutcannot findnext=NULL;elsewhile(p0-nump1-num)&(p1-next!=NULL)p2=p1;p1=p1-next;if(p0-numnum)if(head=p1) head=p0;else p2-next=p0;p0-next=p1;elsep1-next=p0;p0-next=NULL;n=n+1;return(head);.将以上4个函数组成一个程序,由主程序先后调用这些函数,实现链表的建立,输出,删除和插入,在主程序中指定需要删除和插入的结点。#includeusing namespace std;#define NULL 0struct student long num;float score;student *next;int n;int main()student *creat(void);student *del(student *,long);student *insert(student *,student *);void print(student *);student *head,*stu;long del_num;coutinout records:endl;head=creat();print(head);coutendldel_num;while(del_num!=0);head=del(head,del_num);print(head);coutdel_num;coutendlstu-numstu-score;while(stu-num!=0)head=insert(head,stu);print(head);coutstu-numstu-score;return 0;student *creat(viod)student *head;student *p1,*p2;n=0;p1=p2=new student;cinp1-nump1-score;head=NULL;while(p1-num!=0)n=n+1;if(n=1) head=p1;else p2-next=p1;p2=p1;p1=new student;cinp1-nump1-score;p2-next=NULL;return(head);student *del(student *head,long,num)student *p1,*p2;if(head=NULL)coutlist null!num&p1-next!=NULL)p2=p1;p1=p1-next;if(num=p1-num)if(p1=head) head=p1-next;else p2-next=p1-next;coutdelete:numendl;n=n-1else coutcannot findnext=NULL;elsewhile(p0-nump1-num)&(p1-next!=NULL)p2=p1;p1=p1-next;if(p0-numnum)if(head=p1) head=p0;else p2-next=p0;p0-next=p1;elsep1-next=p0;p0-next=NULL;n=n+1;return(head);void print(student *head)student *p;coutNow,Thesenrecords are:endl;p=head;if(head!=NULL)docoutnum scorenext;while(p!=NULL);实验8(1)#includeusing namespace std;class Time public: void set_time(void) cinhour; cinminute; cinsec; void show_time(void) couthour:minute:secendl; private: int hour; int minute; int sec;Time t;int main()t.set_time(); t.show_time(); return 0;(2)为便于查找,将源文件main.cpp改名为xt8-4-1.cpp,student.cpp改名为xt8-4-2.cpp,student.h改名为xt8-4.h。.文件xt8-4-1.cpp/xt8-4-1.cpp(即main.cpp) 这是主函数和有关的include命令#includeusing namespace std;#includext8-4-1.h /即student.hint main()Student stud;stud.set_value();stud.display();return 0;.文件xt8-4-2.cpp/xt8-4-2.cpp(即student.cpp) 在此文件中进行函数的定义#includeusing namespace std;#includext8-4.h 不要漏写此行void Student:display() /在类外定义display类函数coutnum:numendl;coutname:nameendl;coutsex:sexnum;cinname;cinsex;.文件xt8-4.h/xt8-4.h(即student.h) 在此文件中进行类定义class Studentpublic:void display();void set_value();private:int num;char name20;char sex;(3)#includeusing namespace std;class Box /定义Box类public:void get_value();float volume();void display()public:float length;float width;float height;void Box:get_value() /输入数据的函数coutlength;cinwidth;cinheight;float Box:volume()return(length*width*height);void Box:display()coutvolume()endl;int main()Box box1,box2,box3; /定义3个box类的对象box1.get_value(); /输入box1的数据coutvolume of box1 is; /对输出box1的文字说明box1.display(); /输出box1的体积box2.get_value();coutvolume of box2 is;box2.display();box3.get_value();coutvolume of box3 is;box3.display();return 0;4.调试结果实验7(1)input year,month,day:2005 10 110/1 is the 274th day in 2005.Input year,month,day:2008 10 110/1 is the 275th day in 2008.(2)input scores of student 1: NO:101 NAME:Li score 1:90 score 2:79 score 3:89 input scores of student 2: NO:102 NAME:Ma score 1:97 score 2:90 score 3:68 input scores of student 3: NO:103 NAME:Wang score 1:77 score 2:70 score 3:78 input scores of student 4: NO:104 NAME:Fun score 1:67 score 2:89 score 3:56 input scores of student 5: NO:105 NAME:Xue score 1:87 score 2:65 score 3:69NO name score 1 score 2 score 3101 Li 90 79 89102 Ma 97 90 68103 Wang 77 70 78104 Fun 67 89 56105 Xue 87 65 69(3)input scores of student 1:NO:101name:Wangscore1:93score2:89score3:87input scores of student 2:NO:102name:Liscore1:85score2:80score3:78input scores of student 3:NO:103name:Zhaoscore1:65score2:70score3: 59input scores of student 4:NO:104name:Mascore1:77score2:70score3:83input scores of student 5:NO:105name:Hanscore1:70score2:67score3:60input scores of student 6:NO:106name:Zhangscore1:99score2:97score3:95input scores of student 7:NO:107name:Zhouscore1:88score2:89score3:88input scores of student 8:NO:108name:Chenscore1:87score2:88score3:85input scores of student 9:NO:109name:Yangscore1:72score2:70score3:69input scores of student 10:NO:110name:Liuscore1:78score2:80score3:83 NO Name score1 score2 score3 average101 Wang 93 89 87 89.67102 Li 85 80 78 81.00103 Zhao 65 70 59 64.67104 Ma 77 70 83 76.67105 Han 70 67 60 65.67 106 Zhang 99 97 95 97.00107 Zhou 88 89 88 88.33108 Chen 87 8

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论