当前位置:   article > 正文

C语言-学生管理系统源代码_学生管理系统c语言代码

学生管理系统c语言代码

一、学生管理系统算法分析(增删改查)

1.插入学生信息(需要具备数据结构基础)

建立头结点、建立新结点函数、建立新结点并插入数据

  1. void InsertStudent() {//插入函数
  2. int x;
  3. struct Student S;
  4. printf("请输入学生的数目:");
  5. scanf("%d", &x);
  6. for (int i = 1; i <= x; i++) {
  7. printf("请分别输入第%d个同学姓名:\n",i);
  8. scanf("%s", &S.name);
  9. printf("请分别输入第%d个同学年龄:\n",i);
  10. scanf("%d", &S.age);
  11. printf("请分别输入第%d个同学电话:\n",i);
  12. scanf("%s", &S.tel);
  13. printf("请分别输入第%d个同学住址:\n",i);
  14. scanf("%s", &S.addr);
  15. CreatNodeList(StudentNode, S);
  16. }
  17. }
  18. struct Node* CreatNode() //建立头节点
  19. {
  20. struct Node* HeadNode = (struct Node*)malloc(sizeof(struct Node));
  21. HeadNode->Next = NULL;
  22. return HeadNode;
  23. }
  24. struct Node* CreatNewNode(struct Student data)
  25. {//建立新结点函数
  26. struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
  27. NewNode->data = data;
  28. NewNode->Next = NULL;
  29. return NewNode;
  30. }
  31. void CreatNodeList(struct Node* HeadNode, struct Student data)
  32. {//建立新节点并插入数据
  33. struct Node* NewNode = CreatNewNode(data);
  34. NewNode->Next = HeadNode->Next;
  35. HeadNode->Next = NewNode;
  36. }

2.删除学生信息

删除结点

  1. void deletestudent() {//删除节点|
  2. char name[10];
  3. printf("请输入需要删除的学生名字:\n");
  4. scanf("%s", &name);
  5. deletenode(StudentNode, name);
  6. }
  7. void deletenode(struct Node* headnode, char* name)
  8. {//删除节点操作
  9. struct Node* posnode = headnode->Next;
  10. struct Node* posnodefront = headnode;
  11. if (posnode == NULL) {
  12. printf("数据为空,无法删除");
  13. }
  14. else
  15. {
  16. while (strcmp(posnode->data.name, name))
  17. {
  18. posnodefront = posnode;
  19. posnode = posnodefront->Next;
  20. if (posnode == NULL) { printf("未找到指定位置,无法删除"); return; }
  21. }
  22. posnodefront->Next = posnode->Next;
  23. free(posnode);
  24. }
  25. }

3.查找学生信息

查找结点、打印单个结点、浏览信息

  1. struct Node* SearchNode(struct Node* StudentNode, char* name) {
  2. //查找节点
  3. struct Node* pMove = StudentNode->Next;
  4. if (pMove == NULL) {
  5. printf("未找到该学生");
  6. return(pMove);
  7. }
  8. else
  9. {
  10. while (strcmp(pMove->data.name, name))
  11. {
  12. pMove = pMove->Next;
  13. if (pMove == NULL)break;
  14. }
  15. return pMove;
  16. }
  17. }
  18. void PrintList(struct Node* StudentNode)
  19. {//打印单个节点
  20. printf("姓名\t年龄\t电话\t\t住址\n");
  21. printf("%s\t%d\t%s\t%s\n", StudentNode->data.name, StudentNode->data.age, StudentNode->data.tel, StudentNode->data.addr);
  22. }
  23. void PrintNode(struct Node* StudentNode)
  24. {//浏览信息
  25. struct Node* pow = StudentNode->Next;
  26. printf("姓名\t年龄\t电话\t\t住址\n");
  27. while (pow) {
  28. printf("%s\t%d\t%s\t%s\n", pow->data.name, pow->data.age, pow->data.tel, pow->data.addr);
  29. pow = pow->Next;
  30. }
  31. }

4.创建菜单

  1. void SystemMenu() {//学生管理系统菜单
  2. printf("---------------------学生信息管理系统---------------------------\n");
  3. printf("\t\t\t1.插入信息\n");
  4. printf("\t\t\t2.浏览信息\n");
  5. printf("\t\t\t3.删除信息\n");
  6. printf("\t\t\t4.修改信息\n");
  7. printf("\t\t\t5.查找信息\n");
  8. printf("\t\t\t6.退出系统\n");
  9. printf("-----------------------------------------------------------------\n");
  10. }

5.在菜单的基础上插入相应函数执行对应功能

目的在于信息的读取

  1. void KeyRecive() {
  2. int x;
  3. struct Student s;//学生人数
  4. scanf("%d", &x);
  5. switch (x)
  6. {
  7. case 1:
  8. printf("\t【插入信息】\n");
  9. InsertStudent();//插入学生信息
  10. SaveFile1(S1, StudentNode);
  11. printf("插入操作执行完成!-------------");
  12. break;
  13. case 2:
  14. printf("\t【浏览信息】\n");
  15. PrintNode(StudentNode);
  16. printf("浏览操作执行完成--------------");
  17. break;
  18. case 3:
  19. printf("\t【删除信息】\n");
  20. deletestudent();
  21. SaveFile1(S1, StudentNode);
  22. printf("删除操作执行完成---------------");
  23. break;
  24. case 4:
  25. printf("\t【更改信息】\n");
  26. printf("请输入更改的姓名:");
  27. scanf("%s", &s.name);
  28. if (SearchNode(StudentNode, s.name) == NULL) //异常处理报错
  29. {
  30. printf("未找到相关信息!\n");
  31. }
  32. else
  33. {
  34. struct Node* s1 = SearchNode(StudentNode, s.name);
  35. printf("请输入新的学生信息:\n");
  36. printf("请输入同学姓名:\n");
  37. scanf("%s", &s1->data.name);
  38. printf("请输入同学年龄:\n");
  39. scanf("%d", &s1->data.age);
  40. printf("请输入同学电话:\n");
  41. scanf("%s", &s1->data.tel);
  42. printf("请输入同学住址:\n");
  43. scanf("%s", &s1->data.addr);
  44. printf("修改操作执行完成---------------");
  45. SaveFile1(S1, StudentNode);
  46. }
  47. break;
  48. case 5:
  49. printf("\t【查找信息】\n");
  50. printf("请输入查找的姓名:");
  51. scanf("%s", &s.name);
  52. if (SearchNode(StudentNode, s.name) == NULL) {
  53. printf("未找到相关信息!\n");
  54. }
  55. else
  56. {
  57. PrintList(SearchNode(StudentNode, s.name));
  58. printf("查找操作执行完成---------------");
  59. }
  60. break;
  61. default:printf("输入数据有误,重新输入"); KeyRecive();
  62. break;
  63. case 6:
  64. printf("\t【退出系统】\n");
  65. exit(0);
  66. break;
  67. }
  68. }

6.文件操作

读取和保存

  1. void ReadFromFile(char* filename, struct Node* StudentNode)
  2. {
  3. FILE* fp = fopen(filename, "r");
  4. if (fp == NULL)
  5. {
  6. fp = fopen(filename, "w");
  7. }
  8. struct Student data;
  9. while (fscanf(fp, "%s\t%d\t%s\t%s\n", &data.name, &data.age,
  10. &data.tel, &data.addr) != EOF)
  11. {
  12. CreatNodeList(StudentNode, data);
  13. memset(&data, 0, sizeof(data));
  14. }
  15. fclose(fp);
  16. }
  17. void SaveFile1(char* filename, struct Node* StudentNode)
  18. {
  19. FILE* fp = fopen(filename, "w");
  20. struct Node* pow = StudentNode->Next;
  21. while (pow) {
  22. fprintf(fp, "%s\t%d\t%s\t%s\n", pow->data.name, pow->data.age, pow->data.tel, pow->data.addr);
  23. pow = pow->Next;
  24. }
  25. fclose(fp);
  26. }

7.实现主函数功能

  1. int main(void) {
  2. StudentNode = CreatNode();
  3. ReadFromFile(S1, StudentNode);
  4. while (1)
  5. {
  6. SystemMenu();
  7. KeyRecive();
  8. system("pause");
  9. system("cls");
  10. }
  11. }

运行代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS
  2. //指定的源文件的开头定义
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <String.h>
  6. //初始化所有函数
  7. void SystemMenu();//菜单
  8. void KeyRecive();//接受信息
  9. void InsertStudent();//插入学生信息
  10. struct Node* CreatNode();//创建结构体
  11. struct Node* CreatNewNode(struct Student data);//结构体的数据域
  12. void CreatNodeList(struct Node* HeadNode, struct Student data);
  13. void deletestudent();//删除信息
  14. void deletenode(struct Node* headnode, char* name);
  15. //struct Node* changenode(struct Node* S);
  16. struct Node* SearchNode(struct Node* StudentNode, char* name);
  17. void PrintNode(struct Node* StudentNode);
  18. void PrintList(struct Node* StudentNode);
  19. void ReadFromFile(char* filename, struct Node* StudentNode);//读取文件
  20. void SaveFile1(char* filename, struct Node* StudentNode);//保存文件
  21. struct Node* StudentNode;
  22. char S1[5] = { 's','.','t','x','t' };
  23. struct Student {
  24. char name[20];//名字
  25. int age;//年龄
  26. char tel[20];//电话
  27. char addr[20];//住址
  28. };
  29. struct Node {//定义结构体 的数据域和指针
  30. struct Student data;
  31. struct Node* Next;
  32. };
  33. void SystemMenu() {//学生管理系统菜单
  34. printf("---------------------学生信息管理系统---------------------------\n");
  35. printf("\t\t\t1.插入信息\n");
  36. printf("\t\t\t2.浏览信息\n");
  37. printf("\t\t\t3.删除信息\n");
  38. printf("\t\t\t4.修改信息\n");
  39. printf("\t\t\t5.查找信息\n");
  40. printf("\t\t\t6.退出系统\n");
  41. printf("-----------------------------------------------------------------\n");
  42. }
  43. void KeyRecive() {
  44. int x;
  45. struct Student s;//学生人数
  46. scanf("%d", &x);
  47. switch (x)
  48. {
  49. case 1:
  50. printf("\t【插入信息】\n");
  51. InsertStudent();//插入学生信息
  52. SaveFile1(S1, StudentNode);
  53. printf("插入操作执行完成!-------------");
  54. break;
  55. case 2:
  56. printf("\t【浏览信息】\n");
  57. PrintNode(StudentNode);
  58. printf("浏览操作执行完成--------------");
  59. break;
  60. case 3:
  61. printf("\t【删除信息】\n");
  62. deletestudent();
  63. SaveFile1(S1, StudentNode);
  64. printf("删除操作执行完成---------------");
  65. break;
  66. case 4:
  67. printf("\t【更改信息】\n");
  68. printf("请输入更改的姓名:");
  69. scanf("%s", &s.name);
  70. if (SearchNode(StudentNode, s.name) == NULL) //异常处理报错
  71. {
  72. printf("未找到相关信息!\n");
  73. }
  74. else
  75. {
  76. struct Node* s1 = SearchNode(StudentNode, s.name);
  77. printf("请输入新的学生信息:\n");
  78. printf("请输入同学姓名:\n");
  79. scanf("%s", &s1->data.name);
  80. printf("请输入同学年龄:\n");
  81. scanf("%d", &s1->data.age);
  82. printf("请输入同学电话:\n");
  83. scanf("%s", &s1->data.tel);
  84. printf("请输入同学住址:\n");
  85. scanf("%s", &s1->data.addr);
  86. printf("修改操作执行完成---------------");
  87. SaveFile1(S1, StudentNode);
  88. }
  89. break;
  90. case 5:
  91. printf("\t【查找信息】\n");
  92. printf("请输入查找的姓名:");
  93. scanf("%s", &s.name);
  94. if (SearchNode(StudentNode, s.name) == NULL) {
  95. printf("未找到相关信息!\n");
  96. }
  97. else
  98. {
  99. PrintList(SearchNode(StudentNode, s.name));
  100. printf("查找操作执行完成---------------");
  101. }
  102. break;
  103. default:printf("输入数据有误,重新输入"); KeyRecive();
  104. break;
  105. case 6:
  106. printf("\t【退出系统】\n");
  107. exit(0);
  108. break;
  109. }
  110. }
  111. void InsertStudent() {//插入函数
  112. int x;
  113. struct Student S;
  114. printf("请输入学生的数目:");
  115. scanf("%d", &x);
  116. for (int i = 1; i <= x; i++) {
  117. printf("请分别输入第%d个同学姓名:\n",i);
  118. scanf("%s", &S.name);
  119. printf("请分别输入第%d个同学年龄:\n",i);
  120. scanf("%d", &S.age);
  121. printf("请分别输入第%d个同学电话:\n",i);
  122. scanf("%s", &S.tel);
  123. printf("请分别输入第%d个同学住址:\n",i);
  124. scanf("%s", &S.addr);
  125. CreatNodeList(StudentNode, S);
  126. }
  127. }
  128. struct Node* CreatNode() //建立头节点
  129. {
  130. struct Node* HeadNode = (struct Node*)malloc(sizeof(struct Node));
  131. HeadNode->Next = NULL;
  132. return HeadNode;
  133. }
  134. struct Node* CreatNewNode(struct Student data)
  135. {//建立新结点函数
  136. struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
  137. NewNode->data = data;
  138. NewNode->Next = NULL;
  139. return NewNode;
  140. }
  141. void CreatNodeList(struct Node* HeadNode, struct Student data)
  142. {//建立新节点并插入数据
  143. struct Node* NewNode = CreatNewNode(data);
  144. NewNode->Next = HeadNode->Next;
  145. HeadNode->Next = NewNode;
  146. }
  147. void deletestudent() {//删除节点|
  148. char name[10];
  149. printf("请输入需要删除的学生名字:\n");
  150. scanf("%s", &name);
  151. deletenode(StudentNode, name);
  152. }
  153. void deletenode(struct Node* headnode, char* name)
  154. {//删除节点操作
  155. struct Node* posnode = headnode->Next;
  156. struct Node* posnodefront = headnode;
  157. if (posnode == NULL) {
  158. printf("数据为空,无法删除");
  159. }
  160. else
  161. {
  162. while (strcmp(posnode->data.name, name))
  163. {
  164. posnodefront = posnode;
  165. posnode = posnodefront->Next;
  166. if (posnode == NULL) { printf("未找到指定位置,无法删除"); return; }
  167. }
  168. posnodefront->Next = posnode->Next;
  169. free(posnode);
  170. }
  171. }
  172. struct Node* SearchNode(struct Node* StudentNode, char* name) {
  173. //查找节点
  174. struct Node* pMove = StudentNode->Next;
  175. if (pMove == NULL) {
  176. printf("未找到该学生");
  177. return(pMove);
  178. }
  179. else
  180. {
  181. while (strcmp(pMove->data.name, name))
  182. {
  183. pMove = pMove->Next;
  184. if (pMove == NULL)break;
  185. }
  186. return pMove;
  187. }
  188. }
  189. void PrintList(struct Node* StudentNode)
  190. {//打印单个节点
  191. printf("姓名\t年龄\t电话\t\t住址\n");
  192. printf("%s\t%d\t%s\t%s\n", StudentNode->data.name, StudentNode->data.age, StudentNode->data.tel, StudentNode->data.addr);
  193. }
  194. void PrintNode(struct Node* StudentNode)
  195. {//浏览信息
  196. struct Node* pow = StudentNode->Next;
  197. printf("姓名\t年龄\t电话\t\t住址\n");
  198. while (pow) {
  199. printf("%s\t%d\t%s\t%s\n", pow->data.name, pow->data.age, pow->data.tel, pow->data.addr);
  200. pow = pow->Next;
  201. }
  202. }
  203. void ReadFromFile(char* filename, struct Node* StudentNode)
  204. {
  205. FILE* fp = fopen(filename, "r");
  206. if (fp == NULL)
  207. {
  208. fp = fopen(filename, "w");
  209. }
  210. struct Student data;
  211. while (fscanf(fp, "%s\t%d\t%s\t%s\n", &data.name, &data.age,
  212. &data.tel, &data.addr) != EOF)
  213. {
  214. CreatNodeList(StudentNode, data);
  215. memset(&data, 0, sizeof(data));
  216. }
  217. fclose(fp);
  218. }
  219. void SaveFile1(char* filename, struct Node* StudentNode)
  220. {
  221. FILE* fp = fopen(filename, "w");
  222. struct Node* pow = StudentNode->Next;
  223. while (pow) {
  224. fprintf(fp, "%s\t%d\t%s\t%s\n", pow->data.name, pow->data.age, pow->data.tel, pow->data.addr);
  225. pow = pow->Next;
  226. }
  227. fclose(fp);
  228. }
  229. int main(void) {
  230. StudentNode = CreatNode();
  231. ReadFromFile(S1, StudentNode);
  232. while (1)
  233. {
  234. SystemMenu();
  235. KeyRecive();
  236. system("pause");
  237. system("cls");
  238. }
  239. }

插入操作运行结果

 浏览操作运行结果

删除操作运行结果

 修改操作运行结果

 查找操作运行结果

查找失败

 查找成功

 退出系统

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/382647
推荐阅读
相关标签
  

闽ICP备14008679号