我正在使用链表在 c 上创建一个学生管理系统。我创建了一个名为搜索的功能,它从四个选项之一获取用户的输入,然后对学生列表执行搜索操作。
struct Program { //structure to hold information of program
char p_name[50]; //name of program
char p_code[10]; //code of program
char responsible[50]; //person responsible for program
char r_email[50]; //email of responsible
struct Program* next;
};
struct Student { //structure to hold information of student
int personal_no;
char name[50];
char gender[6];
struct Program *study_program;
int age;
char email[50];
struct Student* next;
};
代码没有优化,我会努力做得更好,但这里是功能:
void studyProgram(struct Student *head) {
struct Student * t = NULL;
struct Program* temp = programs;
while (temp != NULL) {
int count = 0;
int male = 0; int female = 0;
float age = 0;
printf("Program: %s\n", temp->p_name);
t = head;
while (t != NULL) {
if (!strcmp(temp->p_code, t->study_program->p_code)) {
count++;
age += t->age;
if (!strcmp(_strupr(t->gender), "MALE")) {
male++;
}
else {
female++;
}
}
t = t->next;
}
age = age / count;
printf("Number of Students: %d\n", count);
printf("Number of Males: %d\n", male);
printf("Number of Females: %d\n", female);
printf("Average age: %f\n", age);
temp = temp->next;
}
}
void search(struct Student *head)
{
int p;
struct Student * temp = head;
int ch;
printf("What do you want to search for:\n");
printf("1.Personal Number\n2.Name\n3.Study Program\n4.Statistics\nOption: ");
scanf("%d", &ch);
if (ch == 1) {
printf("Enter personal number:\n");
scanf("%n", &p);
while(temp!=NULL) {
if(temp->personal_no == p){
display(temp);
return;
}
temp = temp->next;
}
}
else if (ch == 2) {
char n[50];
printf("Enter name:\n");
scanf("%s", n);
while(temp!=NULL) {
if(!strcmp(temp->name, n)){
display(temp);
return;
}
temp = temp->next;
}
}
else if (ch == 3) {
char n[10];
printf("Enter program code:\n");
scanf("%s", n);
while(temp!=NULL) {
if(!strcmp(temp->study_program->p_code, n)) {
display(temp);
}
temp = temp->next;
}
}
else if (ch == 4) {
int count = 0;
int male = 0; int female = 0;
float age = 0;
while(temp!=NULL) {
count++;
age += temp->age;
if(!strcmp(strupr(temp->gender), "MALE")){
male++;
}
else {
female++;
}
temp = temp->next;
}
age = age / count;
printf("Number of Students: %d\n", count);
printf("Number of Males: %d\n", male);
printf("Number of Females: %d\n", female);
printf("Average age: %f\n", age);
studyProgram(head);
}
}
void display(struct Student *head)
{
struct Student * temp = head;
while(temp!=NULL){
printf("Peronsal No: %d\n", temp->personal_no);
printf("Name: %s\n", temp->name);
printf("Gender %s\n", temp->gender);
printf("Study Program: %n\n", temp->study_program->p_name);
printf("Age: %d\n", temp->age);
printf("Email: %s\n", temp->email);
temp = temp->next;
}
}
选项 1-3 执行不言自明的基本搜索操作。选项 4 提供的信息首先是球员总数、男性和女性人数,然后是他们的平均年龄。然后调用学习计划函数,为每个学习计划计算相同的属性。
programs是一个全局变量结构,包含程序(CS,AI)的所有信息。
卡在这个问题上好久了,想不出解决办法。 studyProgram 函数根本不起作用。显示功能不显示任何信息。在情况 1 中,对于个人号码,选择选项时将跳过输入。
谁能指出我哪里出错了?
programs是一个全局变量,包含程序(CS,AI)的所有信息。