void calculate_department( struct student* head) { int count; //count用来统计各学院人数 int m,n; //m用来避免重复打印 struct student* pa ,*pb,*pc; pa = pb= pc = head; n = 1; while (pa != NULL) { count = 0; pb = head; while (pb != NULL) { if (strcmp(pa->department, pb->department) == 0) { count++; } pb = pb->pNext; } printf("\n%s有%d名学生。\n", pa->department, count); do { pa = pa->pNext; n++; pc = head; m = 1; while (pc != pa && strcmp(pa->department, pc->department) != 0) { pc = pc->pNext; m++; } } while (pa != NULL && m != n); } return; }
时间: 2024-04-01 20:34:10 浏览: 9
这是一个统计学院人数的函数,它接收一个指向学生结构体链表头的指针作为参数。函数使用两个指针pb和pa遍历链表,分别统计不同学院的人数,最后输出结果。其中,pa指向当前正在统计人数的学院,pb遍历整个链表,如果pb所指向的学院与pa相同,则count加1。另外,函数使用一个辅助指针pc和计数器m来避免重复打印,具体实现是在每次pa指向新的学院时,将pc重新指向链表头,然后遍历链表,直到找到与pa相同的学院或者到达链表尾部为止,这样,如果在遍历到pa之前找到了与pa相同的学院,就说明之前已经统计过该学院的人数,不需要重复打印。
相关问题
(4)基于结构体,编写一个班级成绩排序系统。 定义一个结构体 Student ,包含姓名( name ,字符串)、学号( id , int 型)、3门课程成绩( score , double 数组)、平均成绩( avg _ score , double 型)。输入每个学生的姓名、学号、3 门课程成绩。计算平均成绩,按平均成绩由高到低排序后输出。 填写下列程序。 # include < stdio . h > # define N 3/*学生人数*/ struct Student void input _ student info ( struct Student * p , int n ) int i ; for ( i -0:i< n ; i ++_ printf " printf (" student % dn ", i +1); printf (" name :"); gets ( printf (" id :"); scanf ("% d " printf (" scores :"); scanf ("%1f%1f%1f", printf ("- fflush ( stdin ); - An "); An "); void calculate _ avg _ score ( struct Student * p . int n ) inti ; for ( i -0; i < n ; itt , p -> avg _ score = void print student info ( struct Student * p , int n ) int i ; printf (" -\ n "); printf ("%15s%8s%8s%8s%8s%12sn"," name "," id "," scorel ","score2","score3"," avg score "); for ( i =0; i < n ; itt , ptt )
#include <stdio.h>
#include <string.h>
#define N 3
// 定义结构体
struct Student {
char name[20]; // 姓名
int id; // 学号
double score[3]; // 3门课程成绩
double avg_score; // 平均成绩
};
// 输入每个学生的信息
void input_student_info(struct Student *p, int n) {
int i;
for (i = 0; i < n; i++, p++) {
printf("student %d\n", i+1);
printf("name: ");
getchar(); // 清空输入缓存
gets(p->name);
printf("id: ");
scanf("%d", &p->id);
printf("scores: ");
scanf("%lf%lf%lf", &p->score[0], &p->score[1], &p->score[2]);
}
}
// 计算每个学生的平均成绩
void calculate_avg_score(struct Student *p, int n) {
int i;
for (i = 0; i < n; i++, p++) {
p->avg_score = (p->score[0] + p->score[1] + p->score[2]) / 3.0;
}
}
// 按平均成绩由高到低排序后输出
void print_student_info(struct Student *p, int n) {
int i, j;
struct Student temp;
// 冒泡排序
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-1-i; j++) {
if (p[j].avg_score < p[j+1].avg_score) {
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
// 输出排序结果
printf("\n%15s%8s%8s%8s%8s%12s\n", "name", "id", "score1", "score2", "score3", "avg score");
for (i = 0; i < n; i++, p++) {
printf("%15s%8d%8.1lf%8.1lf%8.1lf%12.1lf\n", p->name, p->id, p->score[0], p->score[1], p->score[2], p->avg_score);
}
}
int main() {
struct Student stu[N];
input_student_info(stu, N); // 输入每个学生的信息
calculate_avg_score(stu, N); // 计算每个学生的平均成绩
print_student_info(stu, N); // 按平均成绩由高到低排序后输出
return 0;
}
void fun2_calculate(struct student s[],int n);为什么会报错,说应输入声明
这个错误可能是因为你在调用 `fun2_calculate` 函数之前没有在代码中声明该函数。
在 C 语言中,你需要在使用函数之前声明它,以便编译器知道它的返回类型和参数类型。在你的代码中,可能需要在调用 `fun2_calculate` 函数之前添加一个函数声明,类似于这样:
```
void fun2_calculate(struct student s[], int n);
```
这个声明应该在调用 `fun2_calculate` 函数之前,可以在代码的开头或者在 `main` 函数之前添加。这个声明告诉编译器 `fun2_calculate` 函数的返回类型和参数类型,使编译器能够正确地编译你的代码。
如果你已经添加了函数声明,那么可能是因为函数定义和声明不一致导致的错误。你需要确保函数声明和函数定义中的参数类型和数量都是相同的。