C语言代码使用结构体表示一个学生的信息,包括学号,姓名,性别,盛日,班级,每门课的成绩,并编写程序完成学生信息的录入,查询,信息列表的功能
时间: 2024-05-16 17:12:22 浏览: 79
使用结构体数组,利用函数实现按学生学号和姓名的查询操作.c
以下是使用C语言的结构体表示学生信息的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_CLASS_LEN 10
#define MAX_SUBJECTS 5
#define MAX_STUDENTS 100
struct student {
int id;
char name[MAX_NAME_LEN];
char gender;
char birthday[11];
char class[MAX_CLASS_LEN];
int scores[MAX_SUBJECTS];
};
int num_students = 0;
struct student students[MAX_STUDENTS];
void add_student() {
struct student s;
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student gender (M/F): ");
scanf(" %c", &s.gender);
printf("Enter student birthday (YYYY-MM-DD): ");
scanf("%s", s.birthday);
printf("Enter student class: ");
scanf("%s", s.class);
printf("Enter student scores (separated by spaces): ");
for (int i = 0; i < MAX_SUBJECTS; i++) {
scanf("%d", &s.scores[i]);
}
students[num_students++] = s;
printf("Student added successfully!\n");
}
void list_students() {
printf("ID\tName\tGender\tBirthday\tClass\tMath\tEnglish\tPhysics\tChemistry\tBiology\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t%s\t%c\t%s\t%s\t", students[i].id, students[i].name, students[i].gender, students[i].birthday, students[i].class);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%d\t", students[i].scores[j]);
}
printf("\n");
}
}
void search_student() {
int id;
printf("Enter student ID to search: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("ID\tName\tGender\tBirthday\tClass\tMath\tEnglish\tPhysics\tChemistry\tBiology\n");
printf("%d\t%s\t%c\t%s\t%s\t", students[i].id, students[i].name, students[i].gender, students[i].birthday, students[i].class);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%d\t", students[i].scores[j]);
}
printf("\n");
return;
}
}
printf("Student not found!\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Add student\n2. List students\n3. Search student\n4. Exit\n\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
list_students();
break;
case 3:
search_student();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
这个程序可以实现录入、查询和显示学生信息的功能。你可以根据自己的需求进行修改。
阅读全文