《C语言》课内实验报告(实验七)

2019-08-30 17:59

《C语言》实验报告

一、实验题目:

结构体的应用

二、实验目的:

1.进一步掌握结构体变量、数组的定义和使用方法,掌握结构体与指针的应用。

2.学习共用体的概念和使用。 3.学习链表的概念和使用。

三、实验内容:

1.有6个学生,每个学生的数据包括学号、姓名、性别、4门课的成绩、总成绩、平均成绩。从键盘输入每个学生信息及4门课成绩,总成绩及平均成绩要通过4门课算出。然后用选择排序法按照总成绩由高到低对6个学生数据进行排序并输出排序结果。要求输入、排序、输出用3个自定义函数实现。编写源程序,给出注释及运行结果。(提示,请参阅教材上292页例11.5及例11.6的程序)。

2.建立一个含有10个结点的单链表,每个节点包括:学号、姓名、性别、年龄和一门课程的成绩。输入一个学号,删去等于此学号的结点;按学号排序向原单链表中插入两个新结点。编写源程序,给出注释及运行结果。(提示,请参阅教材上297页至308页例11.8-例11.11的程序)。

四、实验结果:

1.程序及注释 #include /*标准函数输入输出库*/ #include /*字符库*/ struct student /*定义结构体类型student*/ { int num; /*定义整型变量num*/ char name[10]; /*定义字符型数组name[10]*/ char sex; /*定义字符型变量sex*/ float score[4]; /*定义浮点型数组score[4]*/ float sum; /*定义浮点型变量sum*/ float avg; /*定义浮点型变量avg*/ }stu[6]; /*定义结构体数组变量*/

void scan() /*定义函数scan*/ { int i,j; /*定义整型变量i,j*/ printf(\Score1\\tScore2\\tScore3\\tScore4\\n\

- 1 -

《C语言》实验报告

/*换行,输出Please input the following information:,换行两次,再输出Num、 Name、Sex、Score1、Score2、Score3、Score4,中间以Tab隔开,再换行*/ for(i=0;i<6;i++) /*for循环*/ { stu[i].sum=0; /*将0赋值给stu[i].sum*/ scanf(\ [i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);

/*输入结构体各成员的值*/ for(j=0;j<4;j++) /*for循环*/ stu[i].sum=stu[i].sum+stu[i].score[j]; /*求4科成绩的和*/ stu[i].avg=stu[i].sum/4; } /*求4科成绩的平均值*/ }

void compare(struct student *p) /*定义函数compare*/ { int i,j; /*定义整型变量i,j*/ struct student t; /*调用结构体*/ for(i=0;i<5;i++,p++) /*for循环*/ for(j=1;j<6;j++) /*for循环*/ { if(p->sum<(p+j)->sum) /*if语句*/ { t=*(p+j);*(p+j)=*p;*p=t;} } /*交换指针(p+j)与指针p的值*/ }

void print() /*定义函数print*/

{ int i; /*定义整型变量i*/ printf(\输出Num、Name 、Sex、Scores、Sum、Average,中间以Tab隔开,再换行*/

for(i=0;i<6;i++) /*for循环*/ { printf(\ /*输出stu[i].num的值,Tab*/ printf(\ /*输出stu[i].name的值,Tab*/ printf(\ /*输出stu[i].sex的值,Tab*/ printf(\ /*以%5.1f 格式输出stu[i].score[0],Tab*/ printf(\ /*以%5.1f 格式输出stu[i].score[1],Tab*/ printf(\ /*以%5.1f 格式输出stu[i].score[2],Tab*/ printf(\以%5.1f格式输出stu[i].score[3],2*Tab*/ printf(\ /*以%6.1f格式输出stu[i].sum,Tab*/ printf(\ /*以%6.1f格式输出stu[i].avg,换行*/ }

void main() /*空类型主函数*/ { scan(); /*调用函数scan*/ compare(stu); /*调用函数compare*/ print(); /*调用函数print*/ }

运行结果

- 2 -

《C语言》实验报告

2.程序及注释

#include /*标准输入输出库*/ #include /*内存分配函数库*/ #define NULL 0 /*宏定义下,NULL代表0*/ #define LEN sizeof(struct student) /*宏定义下,LEN代表结构体字节数*/ struct student /*定义结构体类型student*/ {

int no; /*结构体包含整型变量no*/ char name[10]; /*结构体包含字符型数组name[10]*/ char sex; /*结构体包含字符型变量sex*/ int age; /*结构体包含整型变量age*/ float score; /*结构体包含浮点型变量score*/ struct student *next; /*结构体包含结构体指针变量next*/ };

int n; /*定义整型变量n*/

struct student *creat(void) /*定义struct student类型的指针变量creat*/ { struct student *head; /*声明struct student类型的指针变量head*/ struct student *p1,*p2; /*声明struct student类型的指针变量p1,p2*/ n=0; /*将0赋给n*/ p1=p2=(struct student *)malloc(LEN); /*开辟一个新单元*/

printf(\ /*输出Num等*/ scanf(\&p1->age,&p1->score); /*输入值,分别给no,name,sex,age,score*/ head=NULL; /*将NULL赋给head*/ while(p1->no!=0) /*while循环*/ { n=n+1; /*将n+1赋给n*/ if(n==1)head=p1; /*如果n==1,将p1赋给head*/ else p2->next=p1; /*否则,将p1赋给next*/

- 3 -

《C语言》实验报告

p2=p1; /*将p1赋给p2*/ p1=(struct student *)malloc(LEN);

/*强制类型转换,使指针的基本类型改变为struct student的类型,并 将开辟的长度为LEN、struct student的类型的内存区的指针赋予p1*/ scanf(\ &p1->score); /*输入值,分别给no,name,sex,age,score*/ } /*连续输入学生信息,找到输入学号为0为止*/ p2->next=NULL; /*将NULL赋给next*/ return(head); /*返回值head*/ } /*建立动态链表*/

void print(struct student *head) /*定义函数print*/ { struct student *p; /*声明struct student类型的指针变量p*/ printf(\ /*输出Num等*/ p=head; /*将head赋给p*/ if(head!=NULL) /*if语句*/ Do /*do while循环*/ {

printf(\

p->score); /*输出no,name,sex,age,score的值*/ p=p->next; /*将next赋值给p*/ } while(p!=NULL) ; /*输出所有学生的信息*/ }

struct student *del(struct student *head,long no)

/*定义struct student类型的指针变量del*/ { struct student *p1,*p2; /*声明struct student类型的指针变量p1,p2*/ if(head==NULL){printf(\ No information \\n\ /*if语句*/ p1=head; /*将head赋给p1*/ while(no!=p1->no&&p1->next!=NULL){p2=p1;p1=p1->next;}

/*p1指向的不是所要找的节点,且后面还有节点,*p1后移一个节点*/ if(no==p1->no) /*if语句*/ { if(p1==head)head=p1->next;

/*if语句,p1若指向首节点,把第二各节点地址赋给head*/ else p2->next=p1->next; /*否则将下一结点地址赋给前一节点*/ n=n-1; } /*将n-1赋给n*/ else printf(\ The number you input cannot be found \\n\否则输出*/ return(head); /*返回值head*/ }

struct student *insert(struct student *head,struct student *stud)

/*定义struct student类型的指针变量insert*/ { struct student *p0,*p1,*p2; /*声明struct student类型的指针变量p0,p1,p2*/ p1=head; /*将head赋给p1*/

- 4 -

《C语言》实验报告

}

void main() /*空类型主函数*/ { struct student *head,*stu; /*声明struct student类型的指针变量head,stu*/ long del_num; /*定义长整型变量del_num;*/ printf(\ /*换行,输出Please input:,换行*/ head=creat(); /*将creat()赋给head*/ print(head); /*调用函数print*/ printf(\

/*输出The number you want to delete:*/ scanf(\ /*输入del_num的值*/ while(del_num!=0) /*while循环*/ { head=del(head,del_num); /*将del赋给head*/ print(head); /*调用函数print*/ printf(\

/*输出The number you want to delete:*/ scanf(\ } /*输入del_num的值*/ printf(\:\

/*输出Please input the information you want to insert:*/ stu=(struct student *)malloc(LEN);

/*强制类型转换,使指针的基本类型改变为struct student的类型,并 将开辟的长度为LEN、struct student的类型的内存区的指针赋予stu*/ scanf(\,&stu->score); /*输入值,分别给no,name,sex,age,score*/ while(stu->no!=0) /*while循环*/ { head=insert(head,stu); /*将insert赋给head*/ print(head); /*调用函数print*/ printf(\:\

/*输出Please input the information you want to insert:*/

- 5 -

p0=stud; /*将head赋给p1*/ if(head==NULL) /*if语句*/ { head=p0;p0->next=NULL; } /*使p0指向的节点作为头结点*/ Else /*否则*/ { while((p0->no>p1->no)&&(p1->next!=NULL)) /*while循环*/ { p2=p1;p1=p1->next; } /*p1后移一个节点*/ if(p0->no<=p1->no) /*if语句*/ { if(head==p1)head=p0; /*查到原来的一个节点之前*/ else p2->next=p0; /*插到p2指向的节点之后*/ p0->next=p1; } /*插到p0指向的节点之后*/ Else /*否则*/

{ p1->next=p0; /*插到p1指向的节点之后*/ p0->next=NULL; } } /*查到最后的节点之后*/ n=n+1; /*将n+1赋给n*/ return(head); /*返回值head*/

《C语言》实验报告

stu=(struct student *)malloc(LEN);

/*强制类型转换,使指针的基本类型改变为struct student的类型,并 将开辟的长度为LEN、struct student的类型的内存区的指针赋予stu*/ scanf(\,&stu->score); } /*输入值,分别给no,name,sex,age,score*/ } /*若添加的学号不等0,则继续执行添加功能*/

运行结果

五、实验体会或遇到问题:

1.不知道为什么用Turbo C++运行没用,每次都显示出:

所以说最后结果是用Visual C++ 6.0做的,结果证明程序还是没有问题的

2.动态链表不熟悉 3.结构体相关不熟悉

4.注释都有些困难了,程序太长,太难

- 6 -


《C语言》课内实验报告(实验七).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新课标人教版高中英语必修1--选修8单词默写

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: