实验十结构体和共用体Word下载.docx
《实验十结构体和共用体Word下载.docx》由会员分享,可在线阅读,更多相关《实验十结构体和共用体Word下载.docx(20页珍藏版)》请在冰点文库上搜索。
![实验十结构体和共用体Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-4/29/b6a51ff6-b9f2-4715-b90e-19ab1c0740c5/b6a51ff6-b9f2-4715-b90e-19ab1c0740c51.gif)
p=&
s[0];
%d\n"
a);
++p->
}
3.
structstudent
intnum;
charname[12];
floatscore[3];
floatave;
};
structstudents[2];
inti,k;
2;
scanf("
%d"
&
s[i].num);
//输入结束后回车
getchar();
//消耗缓冲区字符
gets(s[i].name);
//输入结束后回车
%f%f%f"
s[i].score[0],&
s[i].score[1],&
s[i].score[2]);
s[i].ave=(s[i].score[0]+s[i].score[1]+s[i].score[2])/3.0;
k=0;
for(i=1;
if(s[k].ave<
s[i].ave)
k=i;
%d%s%f"
s[k].num,s[k].name,s[k].score[0]);
%f%f%f\n"
s[k].score[1],s[k].score[2],s[k].ave);
4.
structdata
intbianhao;
charname[10];
structdatab[4]={{1001,"
北京"
},{1002,"
天津"
},{1003,"
上海"
},{1004,"
重庆"
structdata*p;
p=b;
for(p=b;
p<
b+4;
p++)
printf("
%d%s\n"
bianhao,p->
name);
5.按字母顺序排序。
stdlib.h>
string.h>
structproduct
floatprice;
voidinput(structproduct*p,intn)
charstr[10];
inti;
n;
i++,p++)
EntertheNameofNo.%d:
"
i+1);
gets(p->
EnterthePriceofNo.%d:
gets(str);
p->
price=atof(str);
voidoutput(structproducts)
%10s\t%10.2f\n"
s.name,s.price);
voidsort(structproduct*p,intn)
structproducts;
inti,j,k;
n-1;
k=i;
for(j=i+1;
j<
j++)
if(strcmp((p+j)->
name,(p+k)->
name)>
0)k=j;
if(k!
=i)
{
s=*(p+i);
*(p+i)=*(p+k);
*(p+k)=s;
}
structproducts[20];
input(s,10);
sort(s,10);
10;
output(s[i]);
6.输出最小值。
int*a,*b,*c,*min;
a=(int*)malloc(sizeof(int));
b=(int*)malloc(sizeof(int));
c=(int*)malloc(sizeof(int));
min=(int*)malloc(sizeof(int));
scanf("
%d%d%d"
a,b,c);
输入的3个数是:
%d%d%d\n"
*a,*b,*c);
*min=*a;
if(*a>
*b)
*min=*b;
if(*min>
*c)
*min=*c;
它们的最小值是:
*min);
free(a);
free(b);
free(c);
free(min);
7.对原题略着更改:
有30个学生,每个学生的数据包括学号、姓名和3门课的成绩,从键盘输入每个学生的数据计算:
⑴每个学生的平均成绩。
⑵计算30个学生每门课程的平均分。
⑶按学生平均分从代到高次序打印出每个学生的各课成绩、3门课的平均成绩。
⑷输出每门课程的平均分。
要求用input函数输入,average1函数求每个学生3门课的平均分,average2函数求30个学生每门课程的平均分,sort函数按学生平均分排序,output函数输出总成绩。
conio.h>
#defineFsizeof(student)
typedefstructscores
{intenglish;
intmath;
intc_language;
intall;
}TP;
typedefstructstudents
{charsid[15];
charname[15];
TPscore;
structstudents*next;
}student;
student*input()
{student*head,*p1,*p2;
intn=0;
charch;
//clrscr();
head=(student*)malloc(F);
head->
next=NULL;
do
{n++;
\n\nPleaseinput%dstudentmessage:
\n\n"
n);
\t%dstudentsid:
"
p1=(student*)malloc(F);
p1->
%s"
p1->
sid);
\n\t%dstudentname:
\n\t%dstudentscores(englesh,math,c_language):
%d,%d,%d"
score.english,&
score.math,&
score.c_language);
p1->
score.all=p1->
score.english+p1->
score.math+p1->
score.c_language;
if(n==1)
{head->
next=p1;
p2=p1;
else
{p2->
p2=p1;
\n\n\t\t\tContinueorback(pressy/n):
);
ch=getchar();
}while(ch=='
y'
||ch=='
Y'
returnhead;
voidaverage1(student*head)
{student*p;
intj;
//clrscr();
p=head->
next;
while(p)
{j=p->
score.all/3;
\n\nname:
%s\taverage:
%d"
name,j);
p=p->
\n\n\nPressenykeyreturn."
getchar();
voidaverage2(student*head)
student*p;
intn=0,temp1=0,temp2=0,temp3=0;
{temp1+=p->
score.english;
temp2+=p->
score.math;
temp3+=p->
n++;
\n\naverageenglishis:
%d\naveragemathis:
%d\naveragec_languageis:
%d\t"
temp1/n,temp2/n,temp3/n);
student*sort(student*head)
{student*head1,*p,*q,*r;
inttemp1=0,temp2=0,temp3=0,temp4;
chars[15],n[15];
head1=head;
for(p=head1->
next!
=NULL;
p=p->
next)
{r=p;
for(q=p->
q;
q=q->
if(q->
score.all>
r->
score.all)
r=q;
if(r!
=p)
{strcpy(s,p->
strcpy(n,p->
temp1=p->
temp2=p->
temp3=p->
temp4=p->
score.all;
strcpy(p->
sid,r->
strcpy(p->
name,r->
p->
score.english=r->
score.math=r->
score.c_language=r->
score.all=r->
strcpy(r->
sid,s);
strcpy(r->
name,n);
r->
score.english=temp1;
score.math=temp2;
score.c_language=temp3;
score.all=temp4;
returnhead1;
voidoutput(student*head)
{student*head2,*p;
inti=1;
head2=sort(head);
for(p=head2->
p!
%s\tsid:
%s\tenglish:
%d\tmath:
%d\tc_language:
%d\taverage:
%d\tmingci:
name,p->
sid,p->
score.english,p->
score.math,p->
score.c_language,p->
score.all/3,i++);
average2(head);
\n\n\n\t\tPressenykeyback."
inti=0,j=1;
head=input();
//clrscr();
\n\n
(1):
average1.\n\n
(2):
average2.\n\n(3):
sort.\n\n(4):
output.\n\n\nPleasechoose:
i);
switch(i)
{case1:
average1(head);
break;
case2:
average2(head);
\n\n\nPressenykeyretuen."
getchar();
case3:
p1=sort(head);
for(p2=p1->
p2!
p2=p2->
next)printf("
\n\t\tname:
%s\tmingci:
p2->
name,j++);
printf("
\n\n\nPressenykeyback."
case4:
output(head);
default:
\nYourchooseisnotright."
break;
}while(i!
=-1);
8.对原题略着更改:
建立一个链表,每一个结点包括的成员为学生学号、平均成绩。
用malloc函数开辟新结点。
要求链表包括8个结点,从键盘输入结点的有效数据。
要求用函数create来建立链表。
实现下列操作:
⑴新增加一个学生的数据。
这个新结点要求按学号顺序插入。
编写一个函数insert来插入结点。
⑵删除第五个结点,并从内存中释放。
程序中要求验证删除的结点确已释放。
⑶查找特定学生的信息。
⑷将链表结点数据输出到屏幕上。
#defineFsizeof(stu)
typedefstructstudent
intsid;
intaverage;
structstudent*next;
}stu;
stu*head;
stu*create()
{stu*p1,*p2;
head=(stu*)malloc(F);
p1=(stu*)malloc(F);
\n\t%dstudentaverage:
average);
ch=getch();
stu*select(stu*head,intx)
stu*s;
s=head->
while(s)
if(s->
sid==x)
s=s->
returns;
stu*insert(stu*head,intx,inty)
stu*p,*r;
//,*q;
r=(stu*)malloc(sizeof(stu));
r->
sid=x;
average=y;
if(p==NULL)/*如果插入空表*/
p=r;
\ninsertsuccess!
!
{while(x>
sid)/*找到插入的位置,按学号大小。
(找到位置或者到了表尾都会跳出循环)*/
{if(p->
next==NULL)break;
p=p->
if(x<
sid)/*插到中间位置*/
{r->
sid=p->
sid;
average=p->
average;
p->
next=p->
next=r;
elseif(x==p->
sid)/*学号不能相同*/
\nError--->
yourinputthissamesid."
else/*插到末尾*/
{p->
stu*get(stu*head,intn)/*得到位置为n的结点的指针*/
{stu*p;
inti;
if(n==0)returnhead;
else
{for(i=1;
returnp;
stu*delete(stu*head,intsid)
stu*p,*q;
inttemp=0,i=0;
if(!
p)
{printf("
\nlistisempty.pressenykeyback."
getch();
returnhead;
}/*表空*/
{while(p)/*查找学号为sid的结点的指针*/
{i++;
/*标记学号为sid的结点的位置*/
if(p->
sid==sid)
{temp=1;
}/*temp=1标记找到了*/
p=p->
if(temp==1)/*如果有学号为sid的结点*/
{q=get(head,i-1);
/*得到sid的前一个结点的指针*/
q->
free(p);
printf("
\n\ndeletesucess!
returnhead;
else/*没有找到*/
{printf("
\n\nNOthisdata.\n"
voidprint(stu*head)
stu*p;
p){printf("
\n%d:
\t%d"
stu*p1;
//*p2;
charch1;
intn,i=0,j=0;
head=create();
//clrscr();
\n1.insert."
\n2.select."
\n3.delect."
\n4.printlist."
\n5.EXIT"
\n............choice(1-5)............."
ch1=getch();
switch(ch1)
case'
1'
:
//clrscr();
\npleaseinputinsertsid.andaverage(like1,1):
%d,%d"
i,&
j);
head=insert(head,i,j);
break;
2'
{
\ninputyouwanttoseletesid:
n);
p1=select(head,n);
{if(p1)printf("
\nsid:
%d\taverage:
sid,p1->
elseprintf("
\nNothisdata."
3'
{//clrscr();
\nPleaseinputyouwantdeletesid:
head=delete(head,n);
\n\n\nPresseny