实验十结构体和共用体.docx

上传人:b****1 文档编号:775959 上传时间:2023-04-30 格式:DOCX 页数:20 大小:18.87KB
下载 相关 举报
实验十结构体和共用体.docx_第1页
第1页 / 共20页
实验十结构体和共用体.docx_第2页
第2页 / 共20页
实验十结构体和共用体.docx_第3页
第3页 / 共20页
实验十结构体和共用体.docx_第4页
第4页 / 共20页
实验十结构体和共用体.docx_第5页
第5页 / 共20页
实验十结构体和共用体.docx_第6页
第6页 / 共20页
实验十结构体和共用体.docx_第7页
第7页 / 共20页
实验十结构体和共用体.docx_第8页
第8页 / 共20页
实验十结构体和共用体.docx_第9页
第9页 / 共20页
实验十结构体和共用体.docx_第10页
第10页 / 共20页
实验十结构体和共用体.docx_第11页
第11页 / 共20页
实验十结构体和共用体.docx_第12页
第12页 / 共20页
实验十结构体和共用体.docx_第13页
第13页 / 共20页
实验十结构体和共用体.docx_第14页
第14页 / 共20页
实验十结构体和共用体.docx_第15页
第15页 / 共20页
实验十结构体和共用体.docx_第16页
第16页 / 共20页
实验十结构体和共用体.docx_第17页
第17页 / 共20页
实验十结构体和共用体.docx_第18页
第18页 / 共20页
实验十结构体和共用体.docx_第19页
第19页 / 共20页
实验十结构体和共用体.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验十结构体和共用体.docx

《实验十结构体和共用体.docx》由会员分享,可在线阅读,更多相关《实验十结构体和共用体.docx(20页珍藏版)》请在冰点文库上搜索。

实验十结构体和共用体.docx

实验十结构体和共用体

实验十结构体和共用体

预备知识:

(略)

实验程序:

1.

#include

structbb

{

intx;

char*y;

}a[]={{1,"Pascal"},{2,"Fortran"}};

voidmain()

{

structbb*p=a;

charc,*s;

s=++p->y;//即s=++(p->y);

printf("%s\n",s);

c=*p++->y;//即c=*p->y,p++;

printf("%c\n",c);

s=p->y++;//即s=p->y,y++

printf("%s\n",s);

printf("%s\n",p->y);

}

2.

#include

unionee

{

inta;

intb;

}*p,s[4];

voidmain()

{

intn=1,i;

for(i=0;i<4;i++)

{

s[i].a=n;

s[i].b=s[i].a+1;

n+=2;

}

p=&s[0];

printf("%d\n",p->a);

printf("%d\n",++p->a);

}

3.

#include

structstudent

{

intnum;

charname[12];

floatscore[3];

floatave;

};

voidmain()

{

structstudents[2];

inti,k;

for(i=0;i<2;i++)

{

scanf("%d",&s[i].num);//输入结束后回车

getchar();//消耗缓冲区字符

gets(s[i].name);//输入结束后回车

scanf("%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;i<2;i++)

{

if(s[k].ave

k=i;

}

printf("%d%s%f",s[k].num,s[k].name,s[k].score[0]);

printf("%f%f%f\n",s[k].score[1],s[k].score[2],s[k].ave);

}

4.

#include

structdata

{

intbianhao;

charname[10];

};

voidmain()

{

structdatab[4]={{1001,"北京"},{1002,"天津"},{1003,"上海"},{1004,"重庆"}};

structdata*p;

p=b;

for(p=b;p

printf("%d%s\n",p->bianhao,p->name);

}

5.按字母顺序排序。

#include

#include

#include

structproduct

{

charname[10];

floatprice;

};

voidinput(structproduct*p,intn)

{

charstr[10];

inti;

for(i=0;i

{

printf("EntertheNameofNo.%d:

",i+1);

gets(p->name);

printf("EnterthePriceofNo.%d:

",i+1);

gets(str);p->price=atof(str);

}

}

voidoutput(structproducts)

{

printf("%10s\t%10.2f\n",s.name,s.price);

}

voidsort(structproduct*p,intn)

{

structproducts;

inti,j,k;

for(i=0;i

{

k=i;

for(j=i+1;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;

}

}

}

voidmain()

{

structproducts[20];

inti;

input(s,10);

sort(s,10);

for(i=0;i<10;i++)

output(s[i]);

}

6.输出最小值。

#include

#include

voidmain()

{

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);

printf("输入的3个数是:

%d%d%d\n",*a,*b,*c);

*min=*a;

if(*a>*b)

*min=*b;

if(*min>*c)

*min=*c;

printf("它们的最小值是:

%d\n",*min);

free(a);free(b);free(c);free(min);

}

7.对原题略着更改:

有30个学生,每个学生的数据包括学号、姓名和3门课的成绩,从键盘输入每个学生的数据计算:

⑴每个学生的平均成绩。

⑵计算30个学生每门课程的平均分。

⑶按学生平均分从代到高次序打印出每个学生的各课成绩、3门课的平均成绩。

⑷输出每门课程的平均分。

要求用input函数输入,average1函数求每个学生3门课的平均分,average2函数求30个学生每门课程的平均分,sort函数按学生平均分排序,output函数输出总成绩。

#include

#include

#include

#include

#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++;

printf("\n\nPleaseinput%dstudentmessage:

\n\n",n);

printf("\t%dstudentsid:

",n);

p1=(student*)malloc(F);p1->next=NULL;

scanf("%s",p1->sid);

printf("\n\t%dstudentname:

",n);

scanf("%s",p1->name);

printf("\n\t%dstudentscores(englesh,math,c_language):

",n);

scanf("%d,%d,%d",&p1->score.english,&p1->score.math,&p1->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->next=p1;

p2=p1;

}

printf("\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;

printf("\n\nname:

%s\taverage:

%d",p->name,j);

p=p->next;

}

printf("\n\n\nPressenykeyreturn.");

getchar();

}

voidaverage2(student*head)

{

student*p;intn=0,temp1=0,temp2=0,temp3=0;

p=head->next;

while(p)

{temp1+=p->score.english;

temp2+=p->score.math;

temp3+=p->score.c_language;

p=p->next;n++;

}

printf("\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;p->next!

=NULL;p=p->next)

{r=p;

for(q=p->next;q;q=q->next)

if(q->score.all>r->score.all)

r=q;

if(r!

=p)

{strcpy(s,p->sid);strcpy(n,p->name);

temp1=p->score.english;

temp2=p->score.math;

temp3=p->score.c_language;

temp4=p->score.all;

strcpy(p->sid,r->sid);strcpy(p->name,r->name);

p->score.english=r->score.english;

p->score.math=r->score.math;

p->score.c_language=r->score.c_language;

p->score.all=r->score.all;

strcpy(r->sid,s);strcpy(r->name,n);

r->score.english=temp1;

r->score.math=temp2;

r->score.c_language=temp3;

r->score.all=temp4;

}

}

returnhead1;

}

voidoutput(student*head)

{student*head2,*p;inti=1;

//clrscr();

head2=sort(head);

for(p=head2->next;p!

=NULL;p=p->next)

printf("\n\nname:

%s\tsid:

%s\tenglish:

%d\tmath:

%d\tc_language:

%d\taverage:

%d\tmingci:

%d",p->name,p->sid,p->score.english,p->score.math,p->score.c_language,p->score.all/3,i++);

average2(head);

printf("\n\n\n\t\tPressenykeyback.");

getchar();

}

voidmain()

{student*head,*p1,*p2;

inti=0,j=1;

head=input();

do

{

//clrscr();

printf("\n\n

(1):

average1.\n\n

(2):

average2.\n\n(3):

sort.\n\n(4):

output.\n\n\nPleasechoose:

");

scanf("%d",&i);

switch(i)

{case1:

average1(head);break;

case2:

//clrscr();

average2(head);printf("\n\n\nPressenykeyretuen.");getchar();break;

case3:

//clrscr();

p1=sort(head);for(p2=p1->next;p2!

=NULL;p2=p2->next)printf("\n\t\tname:

%s\tmingci:

%d",p2->name,j++);printf("\n\n\nPressenykeyback.");getchar();break;

case4:

output(head);break;

default:

printf("\nYourchooseisnotright.");break;

}

}while(i!

=-1);

}

 

8.对原题略着更改:

建立一个链表,每一个结点包括的成员为学生学号、平均成绩。

用malloc函数开辟新结点。

要求链表包括8个结点,从键盘输入结点的有效数据。

要求用函数create来建立链表。

实现下列操作:

⑴新增加一个学生的数据。

这个新结点要求按学号顺序插入。

编写一个函数insert来插入结点。

⑵删除第五个结点,并从内存中释放。

程序中要求验证删除的结点确已释放。

⑶查找特定学生的信息。

⑷将链表结点数据输出到屏幕上。

#include

#include

#include

#include

#defineFsizeof(stu)

typedefstructstudent

{

intsid;

intaverage;

structstudent*next;

}stu;stu*head;

stu*create()

{stu*p1,*p2;

intn=0;charch;

head=(stu*)malloc(F);head->next=NULL;

do

{n++;

printf("\n\nPleaseinput%dstudentmessage:

\n\n",n);

printf("\t%dstudentsid:

",n);

p1=(stu*)malloc(F);p1->next=NULL;

scanf("%d",&p1->sid);

printf("\n\t%dstudentaverage:

",n);

scanf("%d",&p1->average);

if(n==1)

{head->next=p1;p2=p1;}

else

{p2->next=p1;

p2=p1;

}

printf("\n\n\t\t\tContinueorback(pressy/n):

");

ch=getch();

}while(ch=='y'||ch=='Y');

returnhead;

}

stu*select(stu*head,intx)

{

stu*s;

s=head->next;

while(s)

{

if(s->sid==x)

break;

s=s->next;

}

returns;

}

stu*insert(stu*head,intx,inty)

{

stu*p,*r;//,*q;

//clrscr();

p=head->next;

r=(stu*)malloc(sizeof(stu));

r->sid=x;

r->average=y;

if(p==NULL)/*如果插入空表*/

{

p=r;

r->next=NULL;

printf("\ninsertsuccess!

!

");

}

else

{while(x>p->sid)/*找到插入的位置,按学号大小。

(找到位置或者到了表尾都会跳出循环)*/

{if(p->next==NULL)break;p=p->next;}

if(xsid)/*插到中间位置*/

{r->sid=p->sid;

r->average=p->average;

p->sid=x;

p->average=y;

r->next=p->next;

p->next=r;

printf("\ninsertsuccess!

!

");

}

elseif(x==p->sid)/*学号不能相同*/

printf("\nError--->yourinputthissamesid.");

else/*插到末尾*/

{p->next=r;

r->next=NULL;

printf("\ninsertsuccess!

!

");

}

}

returnhead;

}

stu*get(stu*head,intn)/*得到位置为n的结点的指针*/

{stu*p;inti;

p=head->next;

if(n==0)returnhead;

else

{for(i=1;i

p=p->next;

returnp;

}

}

stu*delete(stu*head,intsid)

{

stu*p,*q;inttemp=0,i=0;

p=head->next;

if(!

p)

{printf("\nlistisempty.pressenykeyback.");getch();returnhead;}/*表空*/

else

{while(p)/*查找学号为sid的结点的指针*/

{i++;/*标记学号为sid的结点的位置*/

if(p->sid==sid)

{temp=1;break;}/*temp=1标记找到了*/

p=p->next;

}

if(temp==1)/*如果有学号为sid的结点*/

{q=get(head,i-1);/*得到sid的前一个结点的指针*/

q->next=p->next;

free(p);

printf("\n\ndeletesucess!

!

!

");

returnhead;

}

else/*没有找到*/

{printf("\n\nNOthisdata.\n");

returnhead;

}

}

}

voidprint(stu*head)

{

stu*p;

p=head->next;

if(!

p){printf("\nlistisempty.pressenykeyback.");getch();}

while(p)

{

printf("\n%d:

\t%d",p->sid,p->average);

p=p->next;

}

}

voidmain()

{

stu*p1;//*p2;

charch1;

intn,i=0,j=0;

head=create();

do

{

//clrscr();

printf("\n1.insert.");

printf("\n2.select.");

printf("\n3.delect.");

printf("\n4.printlist.");

printf("\n5.EXIT");

printf("\n............choice(1-5).............");

ch1=getch();

switch(ch1)

{

case'1':

{

//clrscr();

printf("\npleaseinputinsertsid.andaverage(like1,1):

");

scanf("%d,%d",&i,&j);

head=insert(head,i,j);

printf("\n\n\nPressenykeyback.");getch();

break;

}

case'2':

{

//clrscr();

printf("\ninputyouwanttoseletesid:

");

scanf("%d",&n);

p1=select(head,n);

{if(p1)printf("\nsid:

%d\taverage:

%d",p1->sid,p1->average);

elseprintf("\nNothisdata.");

}

printf("\n\n\nPressenykeyback.");getch();

break;

}

case'3':

{//clrscr();

printf("\nPleaseinputyouwantdeletesid:

");

scanf("%d",&n);

head=delete(head,n);

printf("\n\n\nPresseny

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

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2