问题1_1
程序通过定义结构体变量,存储学生的学号、姓名和三门课的成绩。函数
f
u
n
fun
fun 的功能是: 将形参
a
a
a 所指结构体变量中的数据赋给函数中的结构体变量
b
b
b , 并修改
b
b
b 中的学号和姓名,最后输出修改后的数据。
例如,
a
a
a 所指变量中的学号、姓名和三门课的成绩依次是:
10001
、
"
Q
i
n
x
i
a
o
g
o
n
g
"
、
95
、
80
、
88
10001、"Qinxiaogong"、95、80、88
10001、"Qinxiaogong"、95、80、88 ,则修改后输出
b
b
b 中的数据应为:
10002
、
"
W
e
i
y
a
n
g
"
、
95
、
80
、
88
10002、"Weiyang"、95、80、88
10002、"Weiyang"、95、80、88 。
代码1_1
#include<stdio.h>
#include<string.h>
struct student{
long sno;
char name[10];
float score[3];
};
void fun(struct student a){
struct student b;
int i;
b = a;
b.sno = 10002;
strcpy(b.name, "Weiyang");
printf("\nThe data after modified:\n");
printf("\nNo:%ld Name:%s\nScores:", b.sno, b.name);
for(i=0; i<3; i++)
printf("%6.2f", b.score[i]);
printf("\n");
}
void main(void){
struct student s = {10001, "Qinxiaogong", 95, 80, 88};
int i;
printf("\n\nThe original data:\n");
printf("\nNo:%ld Name:%s\nScores:", s.sno, s.name);
for(i=0; i<3; i++)
printf("%6.2f", s.score[i]);
printf("\n");
fun(s);
}
结果1_1

问题1_2
学生记录由学号和成绩组成, N N N 名学生的数据已放入主函数中的结构体数组中,请编写函数 f u n fun fun的功能是:把分数最低的学生数据放入 b b b 所指的数组中。注意:分数最低的学生可能不止一个,函数返回分数最低的学生人数。
代码1_2
#include<stdio.h>
#define N 16
typedef struct{
char num[10];
int s;
}STREC;
int fun(STREC *a, STREC *b){
int i, j=0, n=0, min;
min = a[0].s;
for(i=0; i<N; i++){
if(a[i].s<min)
min = a[i].s;
}
for(i=0; i<N; i++){
if(a[i].s==min){
*(b+j) = a[i];
j++;
n++;
}
}
return n;
}
void main(void){
STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69},
{"GA04", 85}, {"GA01", 91}, {"GA07", 72},
{"GA08", 64}, {"GA06", 87}, {"GA015", 85},
{"GA013", 91}, {"GA0", 64}, {"GA014", 91},
{"GA011", 91}, {"GA017", 64}, {"GA0", 64},
{"GA016", 72} };
STREC h[N];
int i, n;
n = fun(s, h);
printf("The %d lowest score:\n", n);
for(i=0; i<n; i++)
printf("%s %4d\n", h[i].num, h[i].s);
printf("\n");
}
结果1_2

问题1_3
学生记录由学号和成绩组成, N N N 名学生的数据已放入主函数中的结构体数组 s s s 中,请编写函数 f u n fun fun的功能是:按分数降序排列学生的记录,高分在前,低分在后。
代码1_3
#include<stdio.h>
#define N 16
typedef struct{
char num[10];
int s;
}STREC;
int fun(STREC a[]){
int i, j;
STREC t;
// 冒泡法进行排序
for(i=1; i<N; i++){
for(j=0; j<N-1; j++){
if(a[j].s<a[j+1].s){
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
/*
// 选择法
for(i=0; i<N-1; i++){
p = i;
for(j=i+1; j<N; j++){
if(a[p]>a[j])
p = j;
}
if(p!=i){
t = a[i];
a[i] = a[p];
a[p] = t;
}
}
// 插入法
for(i=0; i<N-1; i++){
t = a[i];
for(j=i-1; a[j]>t&&j>=0; j--)
a[j+1] = a[j];
a[j+1] = t;
}
*/
}
void main(void){
STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69},
{"GA04", 85}, {"GA01", 91}, {"GA07", 72},
{"GA08", 64}, {"GA06", 87}, {"GA015", 85},
{"GA013", 91}, {"GA0", 64}, {"GA014", 91},
{"GA011", 91}, {"GA017", 64}, {"GA0", 64},
{"GA016", 72} };
int i;
fun(s);
printf("The data after sorted:\n");
for(i=0; i<N; i++){
if(i%4==0)
printf("\n");
printf("%s %4d", s[i].num, s[i].s);
}
printf("\n");
}
结果1_3

问题1_4
学生记录由学号和成绩组成, N N N 名学生的数据已放入主函数中的结构体数组 s s s 中,请编写函数 f u n fun fun的功能是:把高于等于平均分的学生数据放在 b b b 所指的数组中,高于等于平均分的学生人数通过形参 n n n 返回,平均值通过函数值返回。
代码1_4
#include<stdio.h>
#define N 16
typedef struct{
char num[10];
float s;
}STREC;
double fun(STREC *a, STREC *b, int *n){
int i;
double av = 0.0;
*n = 0;
for(i=0; i<N; i++)
av += a[i].s;
av = av/N;
for(i=0; i<N; i++){
if(a[i].s>=av){
b[*n] = a[i];
*n = *n+1;
}
}
return av;
}
void main(void){
STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69},
{"GA04", 85}, {"GA01", 91}, {"GA07", 72},
{"GA08", 64}, {"GA06", 87}, {"GA015", 85},
{"GA013", 91}, {"GA0", 64}, {"GA014", 91},
{"GA011", 91}, {"GA017", 64}, {"GA0", 64},
{"GA016", 72} };
STREC h[N];
int i, n;
double ave;
ave = fun(s, h, &n);
printf("The %d studdent data which is higher than %7.3f:\n", n, ave);
for(i=0; i<n; i++){
if(i%4==0)
printf("\n");
printf("%s %4.1f \n", h[i].num, h[i].s);
}
printf("\n");
}
结果1_4


被折叠的 条评论
为什么被折叠?



