用c语言写一段学生信息录入可查询系统,需要已录入的任意二十名学生信息,在这段代码中需要用到循环结构,数组,指针,以及函数,并附上这段代码的使用示例。这段代码要求可在Embarcadero dev-c++ 6.3上运行
时间: 2024-02-28 07:57:26 浏览: 78
好的,以下是符合要求的学生信息录入可查询系统代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 20
#define NAME_LENGTH 20
typedef struct {
char name[NAME_LENGTH];
int age;
float grade;
} Student;
void add_student(Student *students, int *num_students);
void search_student(Student *students, int num_students);
void display_students(Student *students, int num_students);
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice;
do {
printf("\n1. Add student\n2. Search student\n3. Display all students\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
search_student(students, num_students);
break;
case 3:
display_students(students, num_students);
break;
case 4:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 4);
return 0;
}
void add_student(Student *students, int *num_students) {
if (*num_students >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return;
}
Student *new_student = &students[*num_students];
printf("Enter name: ");
scanf("%s", new_student->name);
printf("Enter age: ");
scanf("%d", &new_student->age);
printf("Enter grade: ");
scanf("%f", &new_student->grade);
*num_students += 1;
printf("Student added.\n");
}
void search_student(Student *students, int num_students) {
char name[NAME_LENGTH];
printf("Enter name to search: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Name: %s\nAge: %d\nGrade: %0.2f\n", students[i].name, students[i].age, students[i].grade);
return;
}
}
printf("Student not found.\n");
}
void display_students(Student *students, int num_students) {
for (int i = 0; i < num_students; i++) {
printf("Name: %s\nAge: %d\nGrade: %0.2f\n", students[i].name, students[i].age, students[i].grade);
printf("---------------------\n");
}
}
```
使用示例:
1. 打开Embarcadero dev-c++ 6.3,创建一个空的C语言项目。
2. 将上述代码复制到新建的源文件中。
3. 点击“编译并运行”,即可在控制台中运行该程序。
4. 按照程序提示,选择相应的功能进行操作,如添加学生信息、查询学生信息或显示所有学生信息等。
注意:本程序中使用了结构体(struct)来存储学生信息,以及指针(*)来表示学生数组和学生数量。如果您对这些概念不熟悉,建议先学习相关的C语言知识。
阅读全文