C语言程序设计实验指导书
实验10 结构体和共用体
一、实验目的:
1.掌握结构体类型变量的定义及使用
2.掌握结构体变量的引用形式,结构体数组的应用
3.理解结构体作为不同数据类型的一个整体在实际编程中的应用 二、实验内容:
1.实验要求: 输入6名学生的基本信息,每名学生的基本信息包括:学号、姓名、性别、年龄、语文成绩、数学成绩、物理成绩、总分、平均分等数据项。根据各科成绩计算总分和平均分,并输出这6名学生的信息。
2.分析与设计: 利用结构体类型数组元素存放6名学生的信息,并根据要求求出总分和平均分。
3.源代码: #define N 6
#include “stdio.h” struct student {
char num[8]; char name[10]; char sex; int age;
int chi_score,math_score,phy_score; int sum; int average; }; main() { int i;
struct student stud[N];/*输入N名学生的基本信息*/ for(i=0;i printf(“\\nThe student_number of number %d is: “,i+1); gets(stud[i].num); printf(“\\nThe student_name of number %d is: “,i+1); gets(stud[i].name); printf(“\\nThe student_sex of number %d is: “,i+1); 20 C语言程序设计实验指导书 stud[i].sex=getchar(); printf(“\\nThe student_age of number %d is: “,i+1); scanf(“%d”,&stud[i].age); printf(“\\nThe Chinese_score of number %d is: “,i+1); scanf(“%d”,&stud[i].chi_score); printf(“\\nThe math_score of number %d is: “,i+1); scanf(“%d”,&stud[i].math_score); printf(“\\nThe physics_score of number %d is: “,i+1); scanf(“%d”,&stud[i].phy_score); } /*计算学生的总分和平均分*/ for(i=0;i { stud[i].sum=stud[i.chi_score+stud[i].math_score+stud[i].phy_score; stud[i].average=stud[i].sum/3; } /*输出学生的基本信息情况*/ printf(“\\nNumber Name Sex Age Chinese Math Physics Sum Average”); printf(“\\n----------------------------------------------------”); for(i=0;i printf(\\n%-8s%-10s<=”,stud[i].num,stud[i].name;stud[i].sex, stud[i].age); printf(mmm”,stud[i].chi_score,stud[i].math_score, stud[i].phy_score); printf(“mm”,stud[i].sum,stud[i].average); } printf(“\\n----------------------------------------------------”); } 选做题: 1.有一个long型整数,要求将该数的前两个字节和后两个字节的数据分别输出,用十六进制形式输出。 2. 口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中取出3个球,问得到3种不同色的球的可能取法,打印出每种组合的3种颜色。球只能是5种颜色之一,而且要判断各球是否同色,应使用枚举类型变量处理。 21 C语言程序设计实验指导书 实验11 文件 一、实验目的: 1.掌握文件以及缓冲文件系统、文件指针的概念 2.掌握文件的打开、关闭、读、写等操作 3.学会用缓冲文件系统对文件进行简单的操作 二、实验内容: 1.实验要求:有5个学生,每个学生有3门课的成绩,从键盘输入以下数据(包括学号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分存放在磁盘文件”student”中。 学生原有数据: A2005001 Wang 89,98,67 A2005002 Li 70,80,90 A2005006 Sun 78,88,80 A2005012 Zhang 100,87,91 A2005020 Zhao 81,64,92 2.分析与设计: 用结构体数组保存学生的基本信息,利用文件的写操作将学生的基本信息写入文件”student.dat”中。 3.源代码: #include struct student_type { char name[10]; int num; int score[3]; int ave; } struct student_type stud[SIZE]; main() { void save(); int i,sum[SIZE]; FILE *fpt; clrscr(); for(i=0;i for(i=0;i 22 C语言程序设计实验指导书 { scanf(“%s %d %d %d %d”,stud[i].name,&stud[i].num, &stud[i].score[0],&stud[i].score[1],&stud[i].score[2]); sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2]; stud[i].ave=sum[i]/3; } save(); fpt=fopen(“student.dat”,”rb”); printf(“\\n 姓名 学号 成绩1 成绩2 成绩3 平均分\\n”); printf(“--------------------------------------------------\\n”); for(i=0;i fread(&stud[i],sizeof(struct student_type),1,fpt); printf(“%-10s = ] ] ] ]\\n”,stud[i].name,stud[i].num, stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave); } fclose(fpt); } void save() { FILE *fp; int i; if((fp=fopen(“student.dat”,”wb”))==NULL) { printf(“本文件不能打开,出错\\n”); exit(0); } for(i=0;i if(fwrite(&stud[i],sizeof(struct student_type),-1,fp)!=1) { printf(“文件写入数据时出错!\\n”); exit(0); } fclose(fp); } 选做题: 编写一个通讯录,要求字段包括:姓名、E-mail、QQ、联系电话。通过键盘输入数据,并把数据存在一个文件中,通过查找显示一个人员的具体信息。 23 C语言程序设计实验指导书 课程设计:单链表的操作 设计要求 本次课程设计的目的是在学习动态分配函数的基础上,利用带指针选项的结构体构造单链表,并实现单链表的插入、删除和查找等操作。 所需知识 处理动态链表所需的函数: ?malloc函数 –函数原型:void *malloc(unsigned int size); –作用:在动态区分配一个长度为size的连续空间,函数返回值是一个指向分配域起始地址的指针,如内存空间不足,返回空指针NULL。 (此处:void为无确定类型) ?calloc函数 –函数原型:void *calloc(unsigned n,unsigned size); –作用:在内存动态区分配n个长度为size的连续空间,函数返回指向分配域起始地址的指针,若分 配不成功,返回NULL值。 ?free函数 –函数原型:void free(void *p); –作用:释放由p指向的内存区,使这部分内存区能被其它变量使用。P所指向的是最近一次calloc或malloc分配的存储区域。 free函数无返回值。 ?注:旧版本提供的malloc和calloc函数得到的是指向字符型数据的指针。 ?ANSI C提供的malloc和calloc函数规定为void *类型,这并不是说该函数调用后无返回值,而是返回一个结点的地址,该地址的类型为void(无类型或类型不确定),即一段存储区的首址,其具体类型无法确定,只有使用时根据各个域值数据再确定。 3.系统分析 共有以下几大功能模块: 主函数:完成对各函数的声明和调用 建立函数:完成单链表的建立 插入函数:在已有单链表上插入结点 删除函数:在已有单链表上删除某个结点 输出函数:输出已建立的单链表 4.设计程序代码 #include \#include \#define NULL 0 #define LEN sizeof (struct student) 24 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库0614111、2班《C语言实验指导书》(6)在线全文阅读。
相关推荐: