结构体的有关例题 1.结构体数组元素的输入和输出 #includesiostream.h> #define 4 void main( i struct student char name 16]: float score struct student stN: int 1; cou<"请输入"<<N<"个学生的学号、姓名和成绩"<end; for(i=0; K<N; i++) cin>>sti. no>>sti name>>stiscore; cout<<nIt NO\tName\tScroeln for F0; K<N; i++) cout<<It<<stI. no<<it'<<st[ name<<'It<<st]. score<<endl; 当输入 9911 LiMing 87 9912 liuLiang 78 9913 WangXing 90 9914 ZhaoQin 67 程序运行结果如下: NO LiMin 87 912 liuliang 9913 WangXing 90 14 ZhaoQin 67 2将上例输出语句中结构体数组,改用结构体指针变量。 #includesiostream. h> #define n4 void maino struct student int no char name 16]; float score; struct student stN, "p; int 1;
结构体的有关例题 ⒈结构体数组元素的输入和输出 #include<iostream.h> #define N 4 void main() { struct student {int no; char name[16]; float score;} ; struct student st[N]; int i; cout<<"请输入"<<N<<" 个学生的学号、姓名和成绩"<<endl; for (i=0;i<N;i++) cin>>st[i].no>>st[i].name>>st[i].score; cout<<"\n\t NO.\tName\tScroe\n"; cout<<"\t---------------------\n"; for (i=0;i<N;i++) cout<<'\t'<<st[i].no<<'\t'<<st[i].name<<'\t'<<st[i].score<<endl; } 当输入: 9911 LiMing 87 9912 liuLiang 78 9913 WangXing 90 9914 ZhaoQin 67 程序运行结果如下: NO. Name Score 9911 LiMing 87 9912 liuLiang 78 9913 WangXing 90 9914 ZhaoQin 67 2 将上例输出语句中结构体数组,改用结构体指针变量。 #include<iostream.h> #define N 4 void main() { struct student {int no; char name[16]; float score; } ; struct student st[N], *p; int i;
cou←<"请输入”<<N<<个学生的学号、姓名和成绩"<end; for(F0; <N; i++) cin>>st[i- no>>st. name>>stiscore cout<<"\nIt NO tName\tScroen: cout<<t ∥指针取结构数组首地址 for (F0; K<N; i++) cout<<it'<<p->no<<It'<<p->name<<It'<<p->score<<endl; ∥指针移到下一个结构体元素 3结构体传递参数示例 #includesiostream. h> #define n 4 char name[ 16 int math: int float ave }stN={"Sun",78,91,0},{" Zhang",60,76,0},{Qian",88,95,0},{"Fang",60,70,0}; void show(record s) ave=(s math+s eng)2.0; cout<<s name<<'It<<s math<<'It<<seng<<'t<<s ave<<endl; void maino cou<"姓名"<'<<"数学”<<"t<"英语"<<"t<<"平均分"<endl; cout<<. for(i=0;<N;i++) show(stl) 4返回结构体值的示例 #includesiostream.h> #define n 4 struct record char name 16 int engi float ave
cout<<"请输入"<<N<<" 个学生的学号、姓名和成绩"<<endl; for (i=0;i<N;i++) cin>>st[i].no>>st[i].name>>st[i].score; cout<<"\n\t NO.\tName\tScroe\n"; cout<<"\t---------------------\n"; p=st; //指针取结构数组首地址 for (i=0;i<N;i++) { cout<<'\t'<<p->no<<'\t'<<p->name<<'\t'<<p->score<<endl; p++; //指针移到下一个结构体元素 } } 3 结构体传递参数示例 #include<iostream.h> #define N 4 struct record { char name[16]; int math; int eng; float ave; } st[N]={{"Sun",78,91,0},{"Zhang",60,76,0},{"Qian",88,95,0},{"Fang",60,70,0}}; void show(record s) { s.ave=(s.math+s.eng)/2.0; cout<<s.name<<'\t'<<s.math<<'\t'<<s.eng<<'\t'<<s.ave<<endl; } void main() { int i; cout<<"姓名"<<'\t'<<"数学"<<'\t'<<"英语"<<'\t'<<"平均分"<<endl; cout<<"------------------------------\n"; for (i=0;i<N;i++) show(st[i]); } 4 返回结构体值的示例 #include<iostream.h> #define N 4 struct record { char name[16]; int math; int eng; float ave;
record student; record getrecordo record temp; in>>temp. name>>temp. math>>temp. eng; temp. ave=(temp. math+temp. eng)/2.0 return temp;/返回结构体值 void show(record s) cout<<s name<<'it<<s math<< \t<<seng<<'t<<s ave<<endl; void mainO int 1; cou←<"请输入姓名,数学成绩,英语成绩<<end; for F0; K<N; i++) studenti=getrecordo cout<<"姓名"<<"t<<"数学"<<"'<<"英语"<<"'<<"平均分"<<endl for(i=0;<N;i++) show(studentiD 运行时当你输入 Sun ZhaI 6076 O 9 程序运行结果如下 姓名数学英语平均分 91 84.5 68 91.5 65 5生成有N学生记录的单链表 #includesiostream. h> #define n 4 struct student∥定义链表结点类型 char name 16 float score
} ; record student[N]; record getrecord() { record temp; cin>>temp.name>>temp.math>>temp.eng; temp.ave=(temp.math+temp.eng)/2.0; return temp; //返回结构体值 } void show(record s) { cout<<s.name<<'\t'<<s.math<<'\t'<<s.eng<<'\t'<<s.ave<<endl; } void main() { int i; cout<<"请输入姓名,数学成绩,英语成绩"<<endl; for (i=0;i<N;i++) student[i]=getrecord(); cout<<"姓名"<<'\t'<<"数学"<<'\t'<<"英语"<<'\t'<<"平均分"<<endl; for(i=0;i<N;i++) show(student[i]); } 运行时当你输入: Sun 78 91 Zhang 60 76 Qian 88 95 Fang 60 70 程序运行结果如下: 姓名 数学 英语 平均分 Sun 78 91 84.5 Zhang 60 76 68 Qian 88 95 91.5 Fang 60 70 65 5 生成有 N 学生记录的单链表 #include<iostream.h> #define N 4 struct student //定义链表结点类型 { int no; char name[16]; float score;
student*next; struct student *head. s: void create(/生成单链表 int i; struct student*rear; head=NULLs Ou"请输入"<N<"个学生的学号、姓名和成绩"cend i=0;N;i++) new student;∥申请结构点空间 in>s->no>>s->name>>s> score;∥读入学生数据 if(head==NULL) head /将新插入结点为第一结点 rear>next=s;∥其余结点插入在表尾指针rear后 s→>next=NULL; rear-s ∥移动尾指针 void show0∥显示单链表 struct student*p cout<"lnt学号t姓名分数n"; cout<<t p=head; ∥指针取链表首地址 while(p) cout<<"<p→>no<<"t<p>name<"t'p>score<<endl; p-p 击t 指针移到下一个结构体元素 void maino create;/生成单表 showO;∥显示单链表元素 6在单链表中插入一个结点 void inslink( student*st/插入结点 struct student*p, * q if (head==NULL) head是全局变量
student *next; } ; struct student *head,*s; void create() //生成单链表 { int i; struct student *rear; head=NULL; cout<<"请输入"<<N<<" 个学生的学号、姓名和成绩"<<endl; for (i=0;i<N;i++) { s=new student; //申请结构点空间 cin>>s->no>>s->name>>s->score; //读入学生数据 if(head==NULL) head=s; //将新插入结点为第一结点 else rear->next=s; //其余结点插入在表尾指针 rear 后 s->next=NULL; rear=s; //移动尾指针 } } void show() //显示单链表 { struct student *p; cout<<"\n\t 学号\t 姓名\t 分数\n"; cout<<"\t---------------------\n"; p=head; //指针取链表首地址 while(p) { cout<<'\t'<<p->no<<'\t'<<p->name<<'\t'<<p->score<<endl; p=p->next; //指针移到下一个结构体元素 } } void main() { create(); //生成单表 show(); //显示单链表元素 } 6 在单链表中插入一个结点 void inslink(student *st) //插入结点 { struct student *p,*q; if (head==NULL) //head 是全局变量
head ∥表示表头 st->nextNULL ∥表尾为空指针 return if(p->score>st->score) ∥在第一个结点前插入 & st->nextp p=st return; while(p & p->score<st->score) q=p p=p>next;∥取下一个结点地址 st->nextq->next; 插入结点 q->nextst return void maino i struct student*st create 调生成单表程序 show 调显示单链表程序 udent: cou<"输入插入学号,姓名,分数"; cin>>st->no>>st->name>>st->score inslink(st);∥/调插入程序 showO 注意:本插入函数是对学生成绩按由小到大插入,所以输入学生成绩必须的序。 当输入下数据时: I WU 60 cHEN 70 5 WANG 80 7LI90 插入值为4 JIANG76 程序输出为 学号姓名分数 CHEN 3457 JIANG 76 WANG LI
{ head=st; // 表示表头 st->next=NULL; //表尾为空指针 return; } p=head; if (p->score>st->score) //在第一个结点前插入 { st->next=p; p=st; return; } while(p && p->score<st->score) {q=p; p=p->next; //取下一个结点地址 } st->next=q->next; //插入结点 q->next=st; return; } void main() { struct student *st; create(); //调生成单表程序 show(); //调显示单链表程序 st=new student; cout<<"输入插入学号,姓名,分数"; cin>>st->no>>st->name>>st->score; inslink(st); //调插入程序 show(); } 注意:本插入函数是对学生成绩按由小到大插入,所以输入学生成绩必须的序。 当输入下数据时: 1 WU 60 3 CHEN 70 5 WANG 80 7 LI 90 插入值为 4 JIANG 76 程序输出为: 学号 姓名 分数 --------------------- 1 WU 60 3 CHEN 70 4 JIANG 76 5 WANG 80 7 LI 90