实验8--自定义数据类型1实验8 自定义数据类型三、实验思考1.有10名学生的数据,每个学生的数据包括学号、姓名、性别、三门课的考试成绩及平均成绩。
要求:(1)编写一个input函数,用来输入10个学生的信息。
(2)编写一个output函数,用来输出10个学生的信息。
(3)计算每个学生的平均成绩,并按平均成绩由小到大进行排序后输出。
2.设有5个学生和教师的数据。
学生的数据包括:姓名、年龄、性别、职业和年级。
教师的数据包括:姓名、年龄、性别、职业和职务。
现要求输入学生和教师的数据,并输出这些数据,要求当职业项为学生时,输出的最后一项为年级;当职业项为教师时,输出的最后一项为职务。
3.利用结构体数组和结构体指针,根据从键盘输入的学生姓名,查找已给出数据的结构体数组,若找到该名学生,则输出其所有信息;若找不到该名学生,则输出相应提示信息。
4.编程实现,从红、黄、蓝、绿四种颜色中任取三种不同的颜色,共有多少种取法,并请输出所有的排列?5.从键盘输入一批正整数(以-1作为输入结束标志),按插表尾形式把它们组成一个线性链表。
然后,从表头开始,遍历所有结点并输出各结点中的数据。
要求线性链表的生成与遍历均写成函数定义。
6.从键盘输入一系列非负整数,遇0时停止。
对于输入的所有偶数和奇数,分别建立一个偶数链表和一个奇数链表,然后输出两个链表中的数据。
答案1.#include<iostream>#include<iomanip>using namespace std;const int n=2; //设置人数常量n,本例为2人struct student{ char num[6]; // 学号char name[8]; // 姓名char sex[2]; // 性别int score[4]; // score[1]~score[3]存放3门课成绩,score[0]存放平均成绩}stu[n];void input() //输入函数input{ int i,j,sum;for(i=0;i<n;i++){ sum=0;cout<<"input the "<<i+1<<"th student's scores:"<<endl;cout<<"No.:";cin>>stu[i].num;cout<<"name:";cin>>stu[i].name;cout<<"sex:";cin>>stu[i].sex;for(j=0;j<3;j++){ cout<<"score"<<j+1<<":";cin>>stu[i].score[j+1];sum=sum+stu[i].score[j+1];}cout<<endl;stu[i].score[0]=sum/3;}}void output(student stu[]) //输出函数output{ int i,j;cout<<"NO. name sex average score1 score2 score3"<<endl;for(i=0;i<n;i++){ cout<<stu[i].num<<setw(7)<<stu[i].name<<setw(7)<<stu[i].sex<<setw(7);for(j=0;j<4;j++)cout<<setw(7)<<stu[i].score[j]<<" ";cout<<endl;}}void sort() //排序函数sort{ int i,j,max;student temp;for(i=0;i<n-1;i++){ max=i;for(j=i+1;j<n;j++)if (stu[max].score[0]<stu[j].score[0]) max=j;temp=stu[i];stu[i]=stu[max];stu[max]=temp;}}void main(){ input();output(stu);sort();cout<<"sort result:"<<endl;output(stu);}2.从题意中可知,学生和教师所包含的数据是不同的,现把数据放在以下表格中。
如果job项为st(学生),则第5项为grade(年级),如果job项为te(教师),则第5项为prof(教授)。
#include<iostream>#include<iomanip>using namespace std;const int n=2; //定义常量n,本例输入2人信息struct person{ char num[4]; // 号码char name[8]; // 姓名char sex[3]; // 性别char job; // 职业union p //共用体类型{ int grade; //年级char duty[8]; //职务}category; //共用体变量category}stte[n]; //结构体数组,n个元素void main(){ int i;cout<<"enter person's information:no,name,sex,job,grade/duty"<<endl;for(i=0;i<n;i++){ cin>>stte[i].num>>stte[i].name>>stte[i].sex>>stte[i].job;if(stte[i].job=='s') cin>>stte[i].category.grade; //若为学生,则输入所在年级elseif (stte[i].job=='t') cin>>stte[i].category.duty; //若为教师,则输入其职位}cout<<endl<<"NO. name sex job grade/duty"<<endl;for(i=0;i<n;i++){ if(stte[i].job=='s')cout<<stte[i].num<<setw(8)<<stte[i].name<<setw(6)<<stte[i].sex<<setw(6)<<stte[i].job<<setw(10)<<stte[i].category.grade<<endl;elsecout<<stte[i].num<<setw(8)<<stte[i].name<<setw(6)<<stte[i].sex<<setw(6)<<stte[i].job<<setw(10)<<stte[i].category.duty<<endl;}}3.学生信息包括学号、姓名、性别。
#include<iostream>#include<iomanip>#include<string>using namespace std;const int n=2; //设置人数常量n,本例为2人struct student{ char num[6]; // 学号char name[8]; // 姓名char sex[3]; // 性别}stu[n];void input() //输入函数input{ int i,sum;for(i=0;i<n;i++){ sum=0;cout<<"input the "<<i+1<<"th student's information:"<<endl;cout<<"No.:";cin>>stu[i].num;cout<<"name:";cin>>stu[i].name;cout<<"sex:";cin>>stu[i].sex;cout<<endl;}}void main(){ int i;char stname[8];input();cout<<"enter the searched name:";cin>>stname;for(i=0;i<n;i++)if (strcmp(stname,stu[i].name)==0 ){ cout<<"the searched information is:"<<endl;cout<<stu[i].num<<setw(7)<<stu[i].name<<setw(7)<<stu[i].sex<<setw(7);cout<<endl;break;}if (i==n) cout<<"sorry,no information found!"<<endl;}4.#include<iostream>#include<iomanip>using namespace std;void main(){ enum color{red,yellow,blue,green}; //声明枚举类型colorcolor test; //定义枚举类型变量testint i,j,k,n=0,loop;for(i=red;i<=green;i++)for(j=red;j<=green;j++)if(i!=j) //前两球颜色不同{for(k=red;k<=green;k++)if((k!=i)&&(k!=j)) //前三球颜色不同{ n=n+1;cout<<setw(3)<<n;for(loop=1;loop<=3;loop++) //对三个球进行处理{ switch(loop){case 1: test=color(i);break; //color(i)强制类型转换,将整型i所对应的枚举元素赋给testcase 2: test=color(j);break;case 3: test=color(k);break;default:break;}switch(test) //判断test的值,输出相应的颜色{case red: cout<<setw(8)<<"red";break;case yellow: cout<<setw(8)<<"yellow";break;case blue: cout<<setw(8)<<"blue";break;case green: cout<<setw(8)<<"green";break;default: break;}}cout<<endl;}}cout<<"total:"<<n<<endl;}5.#include<iostream>#include<iomanip>using namespace std;struct node{ int data;struct node *next;};void creat(node * &head)//建立以head为头指针的链表,head为引用参数,//以使对应的实参为该链表的表头指针{int i;node *last,*p;cout<<"enter positive integer,-1 to end:";cin>>i;if(i==-1) {head=NULL; return;} //置表头指针为空后返回last=head=NULL; //置表头、表尾指针为空,此时为空链表p=new node;p->data=i;p->next=last;head=p;last=p;cin>>i;while (i!=-1){p=new node; //产生一个p所指的动态结点p->data=i;last->next=p;p->next=NULL;last=p; //last 指针指向表尾结点cin>>i;}}void traverse (node *head){ node *p;p=head;cout<<"the result is:"<<endl;;while(p){cout<<p->data<<" ";p=p->next; //使p指针移动到下一个结点}cout<<endl;}void main(){node *head1=NULL;creat(head1);traverse(head1);}6.#include<iostream>#include<iomanip>using namespace std;struct node{ int data;struct node *next;};void creat(node * &head)//建立以head为头指针的链表,head为引用参数,//以使对应的实参为该链表的表头指针{int i;node *last,*p;cout<<"enter non negative integer,0 to end:"<<endl;cin>>i;if(i==0) {cout<<"The linked list is empty!"<<endl; return;} //链表为空,返回last=head;p=new node;p->data=i;p->next=last;head=p;last=p;cin>>i;while (i!=0){p=new node; //产生一个p所指的动态结点p->data=i;last->next=p;p->next=NULL;last=p; //last 指针指向表尾结点cin>>i;}return;}void cre_oddeven(node * &headodd,node * &headeven) //拆分偶数、奇数链表{node *p1,*p2,*even;if(headodd==NULL){cout<<"The linked list is empty!"<<endl;return ;}p1=headodd;even=new node;even->next=NULL;headeven=even;while( p1->next!=NULL) //删除链表中的偶数{ p2=p1;p1=p1->next;if ((p2->data%2==0)&& (p2==headodd) ) //将在第一节点中的偶数,插入偶数链中{ headodd=p1;even->next=p2;even=p2;}else if ((p1->data%2==0))//将不在第一节点中的偶数,插入偶数链中{ p2->next=p1->next;even->next=p1;even=p1;p1=p2;even->next=NULL;}}if (headodd->data%2==0) headodd=0;headeven=headeven->next;}void traverse (node *head){ node *p;p=head;if(p==NULL) cout<<"The linked list is empty!"<<endl;while(p){cout<<p->data<<" ";p=p->next; //使p指针移动到下一个结点}cout<<endl;}void main(){node *head=NULL,*headodd,*headeven=NULL; //headodd,headeven分别为奇、偶链表的头指针creat(head);headodd=head;cout<<"the result is:"<<endl;traverse(headodd);cre_oddeven(headodd,headeven);cout<<"the odd table is:"<<endl;traverse(headodd);cout<<"the even table is:"<<endl;traverse(headeven);}。