用C语言写一个课程信息管理系统
时间: 2023-07-18 10:06:53 浏览: 117
C语言写的课程管理系统
这里提供一个简单的课程信息管理系统的C语言代码示例,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COURSES 100
typedef struct course {
char id[10];
char name[50];
char instructor[20];
int credit;
} Course;
Course courses[MAX_COURSES];
int num_courses = 0;
void add_course() {
if(num_courses >= MAX_COURSES) {
printf("Too many courses. Cannot add more.\n");
return;
}
Course new_course;
printf("Enter course ID: ");
scanf("%s", new_course.id);
for(int i = 0; i < num_courses; i++) {
if(strcmp(courses[i].id, new_course.id) == 0) {
printf("Course with the same ID already exists.\n");
return;
}
}
printf("Enter course name: ");
scanf("%s", new_course.name);
printf("Enter instructor name: ");
scanf("%s", new_course.instructor);
printf("Enter credit: ");
scanf("%d", &new_course.credit);
courses[num_courses++] = new_course;
printf("Course added successfully.\n");
}
void delete_course() {
char id[10];
printf("Enter course ID to delete: ");
scanf("%s", id);
for(int i = 0; i < num_courses; i++) {
if(strcmp(courses[i].id, id) == 0) {
for(int j = i; j < num_courses - 1; j++) {
courses[j] = courses[j+1];
}
num_courses--;
printf("Course deleted successfully.\n");
return;
}
}
printf("Course not found.\n");
}
void list_courses() {
printf("ID\tName\tInstructor\tCredit\n");
for(int i = 0; i < num_courses; i++) {
printf("%s\t%s\t%s\t%d\n", courses[i].id, courses[i].name, courses[i].instructor, courses[i].credit);
}
}
int main() {
int choice;
do {
printf("\n1. Add course\n");
printf("2. Delete course\n");
printf("3. List courses\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_course();
break;
case 2:
delete_course();
break;
case 3:
list_courses();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 4);
return 0;
}
```
该代码实现了一个简单的课程信息管理系统,其中包括添加课程、删除课程和列出所有课程的功能。每门课程都包括ID、名称、教师和学分等信息。可以通过菜单进行选择功能。当添加课程时,会检查是否达到上限,并且不允许添加相同ID的课程。当删除课程时,会检查是否存在该课程。当列出所有课程时,会按照ID、名称、教师和学分的顺序显示所有课程。
阅读全文