当前位置:   article > 正文

学生信息管理系统(c++,顺序存储结构)_顺序存储制作信息管理系统

顺序存储制作信息管理系统

任务要求: 

采用顺序存储完成学生信息的管理。学生信息包括:学号、姓名、专业、班级、性别、年龄等。要求实现学生信息数据的录入、插入、删除、查找、修改、排序等。具体步骤包括:定义顺序表的存储结构、实现相应基本操作、实现学生信息数据的管理;对顺序表中的学生信息按学号从小到大排序。

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. //定义学生信息结构体
  6. typedef struct
  7. {
  8. int number;
  9. char name[10];
  10. char major[10];
  11. char grade[10];
  12. char sex[10];
  13. int age;
  14. }Stu;
  15. typedef struct
  16. {
  17. Stu* ST;
  18. int length;
  19. }SqList;
  20. //定义存储学生数
  21. #define SIZE 500
  22. class Student
  23. {
  24. public:
  25. void InputList(Stu* S); //输入函数
  26. int menu(); //菜单
  27. void InitList(SqList* L); //初始化函数
  28. void InsertList(SqList* L, int i, Stu S); //插入函数
  29. void OutputList(SqList* L); //输出函数
  30. void SearchList(SqList* L); //查找函数
  31. void DeleteList(SqList* L); //删除函数
  32. void SortList(SqList* L); //排序函数
  33. void UpdataList(SqList* L); //修改函数
  34. };
  35. int main(int argc, const char** agrv)
  36. {
  37. Student student;
  38. SqList L;
  39. Stu S;
  40. student.InitList(&L);
  41. while (1)
  42. {
  43. int order = student.menu();
  44. switch (order)
  45. {
  46. case 0:
  47. {
  48. return 0;
  49. }
  50. case 1:
  51. {
  52. student.InputList(&S);
  53. student.InsertList(&L, 1, S);
  54. break;
  55. }
  56. case 2:
  57. {
  58. student.InputList(&S);
  59. int w = 1;
  60. cout << "请输入插入位置:";
  61. cin >> w;
  62. student.InsertList(&L, w, S);
  63. break;
  64. }
  65. case 3:
  66. {
  67. student.SearchList(&L);
  68. break;
  69. }
  70. case 4:
  71. {
  72. student.DeleteList(&L);
  73. break;
  74. }
  75. case 5:
  76. {
  77. student.OutputList(&L);
  78. break;
  79. }
  80. case 6:
  81. {
  82. student.SortList(&L);
  83. break;
  84. }
  85. case 7:
  86. {
  87. student.UpdataList(&L);
  88. break;
  89. }
  90. default:
  91. cout << "输入有误,请重新输入!" << endl;
  92. break;
  93. }
  94. }
  95. return 0;
  96. }
  97. //函数名:菜单显示函数
  98. //函数功能:显示操作选项
  99. //参数:无
  100. //返回值:选项值
  101. int Student::menu()
  102. {
  103. int n;
  104. while (1)
  105. {
  106. cout << "*************欢迎使用学生信息管理系统*************" << endl;
  107. cout << "1.录入学生信息 2.插入学生数据" << endl;
  108. cout << "3.查找学生信息 4.删除学生信息" << endl;
  109. cout << "5.显示学生信息 6.按学号排序学生信息" << endl;
  110. cout << "7.修改学生信息 0.退出" << endl;
  111. cout << "**************************************************" << endl;
  112. cout << "请选择操作:";
  113. cin >> n;
  114. return n;
  115. }
  116. }
  117. //函数名:初始化
  118. //函数功能:初始化线性表
  119. //参数:表L
  120. //返回值:无
  121. void Student::InitList(SqList* L)
  122. {
  123. L->ST = (Stu*)malloc(SIZE * sizeof(Stu));
  124. if (L->ST == NULL)
  125. {
  126. cout << "内存申请失败!" << endl;
  127. exit(1);
  128. }
  129. L->length = 0;
  130. }
  131. //函数名:插入函数
  132. //函数功能:在指定位置插入记录
  133. //参数:表L,插入位置i,记录S
  134. //返回值:无
  135. void Student::InsertList(SqList* L, int i, Stu S)
  136. {
  137. if (i<1 || i>L->length + 1)
  138. {
  139. cout << "插入位置异常!" << endl;
  140. }
  141. else if (L->length == SIZE)
  142. {
  143. cout << "超过最大存储数量!" << endl;
  144. }
  145. else
  146. {
  147. for (int j = L->length; j > i - 1; j--)
  148. L->ST[j] = L->ST[j - 1];
  149. L->ST[i - 1] = S;
  150. L->length++;
  151. cout << "输入成功!" << endl;
  152. }
  153. }
  154. //函数名:输入函数
  155. //函数功能:输入信息
  156. //参数:记录S
  157. //返回值:无
  158. void Student::InputList(Stu* S)
  159. {
  160. cout << "请输入学号:";
  161. cin >> S->number;
  162. cout << "请输入姓名:";
  163. cin >> S->name;
  164. cout << "请输入专业:";
  165. cin >> S->major;
  166. cout << "请输入班级:";
  167. cin >> S->grade;
  168. cout << "请输入性别:";
  169. cin >> S->sex;
  170. cout << "请输入年龄:";
  171. cin >> S->age;
  172. }
  173. //函数名:输出函数
  174. //函数功能:输出信息
  175. //参数:表L
  176. //返回值:无
  177. void Student:: OutputList(SqList* L)
  178. {
  179. if (L->length != 0)
  180. {
  181. for (int i = 0; i < L->length; i++)
  182. {
  183. cout << L->ST[i].number << '\t';
  184. cout << L->ST[i].name << '\t';
  185. cout << L->ST[i].major << '\t';
  186. cout << L->ST[i].grade << '\t';
  187. cout << L->ST[i].sex << '\t';
  188. cout << L->ST[i].age << '\t';
  189. cout << endl;
  190. }
  191. }
  192. else
  193. {
  194. cout << "没有学生信息,请先录入!" << endl;
  195. }
  196. }
  197. //函数名:查找函数
  198. //函数功能:按名字查找指定学生信息
  199. //参数:表L
  200. //返回值:无
  201. void Student::SearchList(SqList* L)
  202. {
  203. char Sname[10];
  204. int i;
  205. cout << "请输入要查询的学生姓名:";
  206. cin >> Sname;
  207. for (i = 0; i < L->length; i++)
  208. {
  209. if (!strcmp(L->ST[i].name, Sname))
  210. {
  211. cout << "已找到该学生!" << endl;
  212. cout << L->ST[i].number << '\t';
  213. cout << L->ST[i].name << '\t';
  214. cout << L->ST[i].major << '\t';
  215. cout << L->ST[i].grade << '\t';
  216. cout << L->ST[i].sex << '\t';
  217. cout << L->ST[i].age << endl;
  218. break;
  219. }
  220. }
  221. if (i == L->length)
  222. {
  223. cout << "没有找到该学生!" << endl;
  224. }
  225. }
  226. //函数名:删除函数
  227. //函数功能:删除指定姓名的学生
  228. //参数:表L
  229. //返回值:无
  230. void Student:: DeleteList(SqList* L)
  231. {
  232. char Dname[10];
  233. int i;
  234. cout << "请输入要删除的学生姓名:";
  235. cin >> Dname;
  236. for (i = 0; i < L->length; i++)
  237. {
  238. if (!strcmp(L->ST[i].name, Dname))
  239. {
  240. for (int j = i + 1; j < L->length; j++)
  241. {
  242. L->ST[j - 1] = L->ST[j];
  243. }
  244. L->length--;
  245. break;
  246. }
  247. }
  248. if (i == L->length)
  249. {
  250. cout << "没有找到该学生!";
  251. }
  252. else
  253. {
  254. cout << "删除完成!" << endl;
  255. }
  256. }
  257. //函数名:排序函数
  258. //函数功能:按学号从小到大排序
  259. //参数:表L
  260. //返回值:无
  261. void Student::SortList(SqList* L)
  262. {
  263. for (int i = 0; i < L->length; i++)
  264. {
  265. for (int j = 0; j < L->length - i - 1; j++)
  266. {
  267. if (L->ST[j].number > L->ST[j + 1].number)
  268. {
  269. SqList T;
  270. InitList(&T);
  271. T.ST[0] = L->ST[j];
  272. L->ST[j] = L->ST[j + 1];
  273. L->ST[j + 1] = T.ST[0];
  274. }
  275. }
  276. }
  277. cout << "排序完成!" << endl;
  278. }
  279. //函数名:修改函数
  280. //函数功能:修改指定姓名的学生的信息
  281. //参数:表L
  282. //返回值:无
  283. void Student::UpdataList(SqList* L)
  284. {
  285. char Uname[10];
  286. int i;
  287. int m = 0;
  288. cout << "请输入要修改信息的学生姓名:";
  289. cin >> Uname;
  290. for (i = 0; i < L->length; i++)
  291. {
  292. if (!strcmp(L->ST[i].name, Uname))
  293. {
  294. cout << "已找到该学生!" << endl;;
  295. cout << L->ST[i].number << '\t';
  296. cout << L->ST[i].name << '\t';
  297. cout << L->ST[i].major << '\t';
  298. cout << L->ST[i].grade << '\t';
  299. cout << L->ST[i].sex << '\t';
  300. cout << L->ST[i].age << endl;
  301. break;
  302. }
  303. }
  304. if (i == L->length)
  305. {
  306. cout << "没有找到该学生!";
  307. return;
  308. }
  309. cout << "1.number 2.name 3.major" << endl;
  310. cout << "4.grade 5.sex 6.age" << endl;
  311. cout << "请输入想要修改的信息:";
  312. cin >> m;
  313. switch (m)
  314. {
  315. case 1:
  316. cout << "请输入新的学号:";
  317. cin >> L->ST[i].number;
  318. break;
  319. case 2:
  320. cout << "请输入新的姓名:";
  321. cin >> L->ST[i].name;
  322. break;
  323. case 3:
  324. cout << "请输入新的专业:";
  325. cin >> L->ST[i].major;
  326. break;
  327. case 4:
  328. cout << "请输入新的班级:";
  329. cin >> L->ST[i].grade;
  330. break;
  331. case 5:
  332. cout << "请输入新的性别:";
  333. cin >> L->ST[i].sex;
  334. break;
  335. case 6:
  336. cout << "请输入新的年龄:";
  337. cin >> L->ST[i].age;
  338. break;
  339. default:
  340. cout << "输入有误,请重新操作!";
  341. return;
  342. }
  343. cout << "修改完成!" << endl;
  344. }

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

闽ICP备14008679号