用C语言写一个简易的课程管理系统,需要对该程序增加非常详细的注释信息,功能有添加课程,删除课程,输出所有课程,课程信息只有课程名,使用一个char类型的二维数组存储课程信息,在添加课程之后可以选择继续添加和返回上一级,添加课程需要判断该课程是否已存在,存在则添加失败,删除课程需要判断该课程是否存在,不存在则删除失败,
时间: 2024-03-24 15:41:47 浏览: 13
好的,我可以帮你解答这个问题。以下是一个简单的课程管理系统的C语言实现,包含添加课程、删除课程和输出所有课程的功能。注释详细解释了每个步骤的作用。
```c
#include <stdio.h>
#include <string.h>
// 定义一个二维字符数组,用于存储课程信息
char courses[100][50];
// 记录当前已经添加了多少门课程
int courseCount = 0;
// 添加课程函数
void addCourse() {
char name[50];
// 获取要添加的课程名
printf("请输入要添加的课程名:");
scanf("%s", name);
// 判断该课程是否已存在
for (int i = 0; i < courseCount; i++) {
if (strcmp(courses[i], name) == 0) {
printf("该课程已存在!\n");
return;
}
}
// 课程不存在,则添加课程
strcpy(courses[courseCount], name);
courseCount++;
printf("添加课程成功!\n");
}
// 删除课程函数
void removeCourse() {
char name[50];
// 获取要删除的课程名
printf("请输入要删除的课程名:");
scanf("%s", name);
// 判断该课程是否存在
for (int i = 0; i < courseCount; i++) {
if (strcmp(courses[i], name) == 0) {
// 课程存在,则删除课程
for (int j = i; j < courseCount - 1; j++) {
strcpy(courses[j], courses[j+1]);
}
courseCount--;
printf("删除课程成功!\n");
return;
}
}
// 课程不存在,则删除失败
printf("该课程不存在!\n");
}
// 输出所有课程函数
void printCourses() {
if (courseCount == 0) {
printf("还没有课程!\n");
return;
}
printf("所有课程:\n");
for (int i = 0; i < courseCount; i++) {
printf("%s\n", courses[i]);
}
}
int main() {
int choice;
do {
printf("请选择功能:\n");
printf("1. 添加课程\n");
printf("2. 删除课程\n");
printf("3. 输出所有课程\n");
printf("4. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addCourse();
break;
case 2:
removeCourse();
break;
case 3:
printCourses();
break;
case 4:
printf("再见!\n");
break;
default:
printf("输入有误,请重新输入!\n");
break;
}
} while (choice != 4);
return 0;
}
```
希望这个例子可以帮到你,如果还有其他问题,请随时问我!
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)