函数被调用但没有执行任何操作。

huangapple go评论118阅读模式
英文:

Function being called but is not doing anything

问题

我正在使用链表在C中创建一个学生管理系统。我创建了一个名为search的函数,该函数从用户输入中选择四个选项之一,然后执行对学生列表的搜索操作。

struct Program { //结构体用于保存程序信息
	char p_name[50];    //程序名称
	char p_code[10];    //程序代码
	char responsible[50];   //负责程序的人
	char r_email[50];   //负责人的电子邮件
	struct Program *next;
};

struct 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("%d", &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("Personal No: %d\n", temp->personal_no);
        printf("Name: %s\n", temp->name);
        printf("Gender %s\n", temp->gender);
        printf("Study Program: %s\n", temp->study_program->p_name);
        printf("Age: %d\n", temp->age);
        printf("Email: %s\n", temp->email);
        temp = temp->next;
    }
}

选项1到3执行基本的搜索操作,这些操作是很容易理解的。选项4提供有关总学生人数、男性和女性人数以及他们的平均年龄的信息。然后调用studyProgram函数,该函数计算每个学习计划的相同属性。

programs是一个全局变量结构,其中包含所有程序(CS,AI)的信息。

我已经卡在这个问题上很长时间,无法找到解决方案。studyProgram函数根本不起作用。display函数不显示任何信息。在情况1中,当选择该选项时,输入将被跳过。

有人可以指出我在哪里犯了错误吗?

programs是一个全局变量,包含所有程序(CS,AI)的信息。

编辑:

用于创建链表的代码:

struct Student *add(struct Student *head) {
    struct Student *student = (struct Student *)malloc(sizeof(struct Student));
    student = inputStudent(); //从用户那里获取有关学生字段的输入的函数
    student->next = NULL;
    
    if (head == NULL) {
        /* 如果head为NULL,将学生设置为新的head */
        head = student;
    } else {
        /* 如果列表不为空,将学生插入到head的开头 */
        student->next = head;
        head = student;
    }
    return head;
}
英文:

I am creating a student management system in C using linked list. I have created a function called search which has takes input from user from one of four options and then performs search operation on the student list.

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;
};

The code is not optimized and I will work on making in better but here is the function:

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;
}
}

Option 1-3 perform basic search operation which are self explanatory. Option 4 provides information about, firstly, the total no of players, no of males and females and then their average age. The studyProgram function is then called which calculates the same attributes for each study program.

programs is a global variable structure that contains all the information of the programs (CS, AI).

I have been stuck on this for a long time and cannot figure out a solution.
The studyProgram function is not working at all. The display function is not displaying any information. In case 1, for personal number the input is skipped when the option is selected.

Can anybody point out where I am making a mistake(s)?

programs is a global variable that contains all the information of the programs (CS, AI).

Edit:

Code for creating the linked list:

struct Student *add(struct Student *head) {
struct Student *student = (struct Student *)malloc(sizeof(struct Student));
student = inputStudent(); //function to take input from user about student fields
student->next = NULL;
if (head == NULL) {
/* if head is NULL, set student as the new head */
head = student;
} else {
/* if list is not empty, insert student in beginning of head */
student->next = head;
head = student;
}
return head;
}

答案1

得分: 2

以下是翻译好的内容:

有多个问题:

  • char gender[6]; 太短,无法容纳字符串 female
  • printf("Study Program: %n\n", temp->study_program->p_name); 你应该使用 %s 而不是 %n
  • scanf("%n", &p); 你应该使用 %d 来转换为 int 值。
  • scanf("%s", n); 是有风险的:为了避免缓冲区溢出,当 ch == 2 时,你应该使用 %49s,而当 ch == 3 时,你应该使用 %9s
  • 函数 addStudent 结构分配了内存,但指针立即被 inputStudent 的结果覆盖了,而你没有提供 inputStudent 的代码。内存被浪费了。还请注意,在列表的开头插入不需要特殊处理空字符串。下面是一个简化的版本:
struct Student *add(struct Student *head) {
    struct Student *student = inputStudent();
    student->next = head;
    return student;
}
  • 函数 display 输出了完整的学生列表和他们的详细信息。如果没有输出,那么列表可能是空的,或者列表的头部以意外的方式被设置为 NULL

  • 对于情况 ch == 1,输入没有将数字存储在 p 中(因为使用了 %n 而不是 %d),这解释了为什么找不到任何学生。display() 函数似乎预期输出单个学生,但你的实现输出了整个列表。

英文:

There are multiple issues:

  • char gender[6]; is too short to contain the string female.

  • printf("Study Program: %n\n", temp->study_program->p_name); instead of %n, you should use %s

  • scanf("%n", &p); you should use %d to convert an int value.

  • scanf("%s", n); is risky: to avoid buffer overflows, you should use %49s for ch == 2 and %9s for ch == 3

  • function add allocates memory for a Student structure but the pointer gets immediately overwritten by the result of inputStudent, which you did not provide. The memory is lost. Note also that inserting at the beginning of the list does not require special casing the empty string. Here is a simplified version:

    struct Student *add(struct Student *head) {
    struct Student *student = inputStudent();
    student->next = head;
    return student;
    }
    
  • the function display outputs the full list of students and their details. If you do not get any output, the list must be empty or the head of the list must have been set to NULL in an unexpected way.

  • for case ch == 1, the input does not store the number into p (because of the %n instead of %d), which would explain why no student is never found. Function display() seems to be expected to output a single student, but your implementation outputs the full list.

huangapple
  • 本文由 发表于 2023年3月7日 22:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定