浙师大C语言上机实验12结构体答案
上机实验十二 指针
班级 学号 姓名 一. 实验目的和要求
1 掌握结构、结构数组、结构指针的定义及使用;
2 掌握简单的链表的定义及使用;
3 掌握共用体和枚举类型的定义及使用;
4 掌握文件的简单的定义、打开、写、关闭等操作。
二. 实验内容
【实验题程序填空,结构类型 struct score 包含一名学生的 3 门学科成绩和个 人平均成绩,要求输入某位学生的三门课程成绩,并计算其平均成绩。
1 用普通的结构变量实现
#include
structscore //定义结构类型
int math;//数学成绩
int phys;//物理成绩
int chem;//化学成绩
float aver;//平均成绩
;
void main struct scores;//定义结构变量 s
scanf “%d%d%d”, &s.math,&s.phys,&s.chem ;//输入 s 的前三个成员 三科成绩
s.aver float s.math+s.phys+s.chem /3;//计算 s 的平均成绩分量
printf “aver:%.2f\n”, s.aver ;//输出 s 的平均成绩分量 2 用结构指针
#include
structscore //定义结构类型
int math;//数学成绩
int phys;//物理成绩
int chem;//化学成绩
float aver;//平均成绩
;
void main struct scores, *p;//定义结构变量 s 和结构指针 p p &s;//令 p 指向 s
scanf “%d%d%d”, &p- math, &p- phys,& p- chem ;//输入 p 的前三个分量 三科成绩
p- aver float p- math+p- phys+p- chem /3 ; //计算 p 的平均成绩分量
printf “aver:%.2f\n”, p- aver ;//输出 p 的平均成绩分量 【实验题程序填空,定义一个关于平面点的结构类型,并定义一个含 5 个元素 的结构数组,用于存放 5 个平面点;然后输入这些点的坐标值,并统计位于半径为 3、 以原点为圆心的圆之内的点的个数。
#include
struct point //定义结构类型 struct point
double a; double b;
;
void main int i, num 0;
struct point p[5];//定义结构数组p printf “Input 5 points: \n” ;
for i 0;i 5; i++ scanf “%lf%lf”, &p[i].a , &p[i].b ;//输入 5 个点的坐标
for i 0;i 5; i++ //统计位于指定圆内的点的个数
if p[i].a*p[i].a+p[i].b*p[i].b 9 num++;
printf “ num %d \n”, num ; ?运行程序,输入 1.0 2.1 -3 4.23 9.0 -8 4.5 -1 -0.25 -1.3输出 num
【实验题程序改错,要求输入 5 名学生的学号、姓名和成绩,将这 5 名学生按成绩的降序排序后输出。
#include
struct student //定义结构类型
int num; //学号
charname[20]; //姓名
float score; //成绩
;
void main int i,j;
struct student s[5],temp;
printf “Input information of 5 students:\n” ;
for i 0; i 5; i++
scanf “%d%s%f”,&s[i].num,&s[i].name,&s[i].score ;/*******/
for i 4; i 0; i--
for j 0; j i; j++
if s[j].score s[j+1].score temp s[j]; s[j] s[j+1]; s[j+1] temp; printf “\n After bubble sorted:\n” ;//输出成绩分量
for i 0; i 5; i++
printf “%d %s %.1f \n”,s[i].num,s[i].name,s[i].score ; 改正后,运行程序,若输入: 则输出:
【实验题建立静态链表
#include
#include
main struct stu int num;