当前位置:网站首页>手写学生管理系统
手写学生管理系统
2022-07-29 05:07:00 【cpp编程】

#include <stdio.h> #include <stdlib.h> int main(void) { printf("hello world\n"); system("pause"); return 0; }4. 编写功能菜单 初级版 int main(void) { printf(" 学生信息管理系统\n"); printf("1. 输入学生信息\n"); printf("3. 删除学生信息\n"); printf("3. 删除学生信息\n"); printf("4. 修改学生信息\n"); printf("5. 插入学生信息\n"); printf("6. 学生成绩排名\n"); printf("7. 统计学生总数\n"); printf("8. 显示所有信息\n"); printf("0. 退出系统\n"); system("pause"); return 0; }

导入第 3 行工具,初始化窗口大小
void init() { char cmd[128]; sprintf(cmd, "mode con lines=%d cols=%d", WIN_HEIGHT, WIN_WIDTH); system(cmd); }int main(void) { init(); ...... return 0; }
创建菜单函数 menu
void menu() { system("cls"); printTableHead(MENU_WIDTH); printTableMidInfo(MENU_WIDTH, "学生信息管理系统"); printTableMidInfo(MENU_WIDTH, ""); const char* subMenus[] = { "1. 输入学生信息", "2. 查找学生信息", "3. 删除学生信息", "4. 修改学生信息", "5. 插入学生信息", "6. 学生成绩排名", "7. 统计学生总数", "8. 显示所有信息", "0. 退出系统 " };int count = sizeof(subMenus) / sizeof(subMenus[0]); for (int i = 0; i < count; i++) { printTableMidInfo(MENU_WIDTH, subMenus[i]); }printTableMidInfo(MENU_WIDTH, ""); printTableTail(MENU_WIDTH); printMidInfo("请选择(0-8): "); }
调用 menu 函数
int main(void) { init(); menu(); return 0; }

void init() { char cmd[128]; sprintf(cmd, "mode con lines=%d cols=%d", WIN_HEIGHT, WIN_WIDTH); system(cmd); system("color 1f"); //system("color f0\n"); }
5. 菜单选择
int main(void) { init(); menu(); int n; scanf("%d", &n); while (1) { switch (n) { case 1: input();break;
case 2: search();break; case 3: del(); break; case 4: modify(); break; case 5: insert(); break; case 6: order(); break; case 7: total(); break; case 8: show(); break; default:break; }waitConfirm(); menu(); rewind(stdin); // fflush(stdin),在 VS2015 以上无效 scanf("%d", &n); }return 0; }
void input() { system("cls"); printf("输入...\n"); }void search() { system("cls"); printf("查询...\n"); }void del() { system("cls"); printf("删除...\n"); }void modify() { system("cls"); printf("修改...\n"); }void insert() { system("cls");
printf("插入...\n"); }void order() { system("cls"); printf("排序...\n"); }void total() { system("cls"); printf("统计...\n"); }void show() { system("cls"); printf("显示...\n"); }void waitConfirm() { rewind(stdin); //flush(stdin)在 VS2015 以上无效,使用 rewind,清空缓存 getch(); }
struct student { int num; //学号 char name[16]; int cLang; //C 语言 int algo; //算法 int database; //数据库 int sum; };
#define MAX_COUNT 100
struct student stu[MAX_COUNT]; int currentCount = 0;
void init() { char cmd[128]; sprintf(cmd, "mode con lines=%d cols=%d", WIN_HEIGHT, WIN_WIDTH); system(cmd); system("color 1f"); //system("color f0\n"); memset(stu, 0, sizeof(stu)); FILE* fp = fopen("data.txt", "rb"); if (fp == NULL) { //printf("文件不存在!\n"); currentCount = 0; return; }int i = 0; while (!feof(fp)) { int ret = fread(&stu[i], sizeof(struct student), 1, fp); if (ret == 1) { i++; } }currentCount = i; }
void input() { char str[16]; struct student s; while (1) { system("cls"); printf("输入学生信息(y/n):"); rewind(stdin); //清空输入缓存区 scanf("%s", str); if (strcmp(str, "Y") != 0 && strcmp(str, "y") != 0) { break; }s = inputInfo(); if (searchStu(s.num) >= 0) { printf("学号[%d] 已经存在!\n", s.num); waitConfirm(); continue; }stu[currentCount++] = s; if (!save()) { printf("保存失败!\n"); }else {printf("保存成功!\n"); }waitConfirm(); }printf("\n 结束输入!\n"); }
struct student inputInfo() { //可优化成使用指针参数 struct student s; rewind(stdin); //清空输入缓存区 printf("学号:"); scanf("%d", &s.num);
printf("姓名:"); scanf("%s", s.name); printf("C 语言:"); scanf("%d", &s.cLang); printf("算法:"); scanf("%d", &s.algo); printf("数据库:"); scanf("%d", &s.database); s.sum = s.cLang + s.algo + s.database; return s; }
int searchStu(int snum) { for (int i = 0; i < currentCount; i++) { if (stu[i].num == snum) { return i; } }return -1; }
bool save() { FILE *fp = fopen("data.txt", "wb"); if (fp == NULL) { fclose(fp); return false; }for (int i = 0; i < currentCount; i++) { if (fwrite(&stu[i], sizeof(struct student), 1, fp) != 1) { fclose(fp); return false; } }fclose(fp); return true; }
#define RECORDER_PER_PAGE 10 void show() { system("cls"); if (currentCount == 0) { printf("还没有学生信息!\n"); return; }int pageCount = (currentCount + RECORDER_PER_PAGE - 1) / RECORDER_PER_PAGE; char buff[64]; for (int i = 0; i < pageCount; i++) { showPage(i * RECORDER_PER_PAGE, (i + 1) * RECORDER_PER_PAGE - 1); sprintf(buff, "共%d 页 第[%d]页", pageCount, i + 1); printMidInfo(buff); if (i < pageCount - 1) { waitConfirm(); } } }
// 表头信息 char head[][COL_LEN_MAX] = { "学号", "姓名", "C 语言", "算法", "数据库", "总分" }; void showPage(int startIndex, int endIndex) { if (endIndex >= currentCount) { endIndex = currentCount - 1; }if (endIndex - startIndex + 1 > RECORDER_PER_PAGE) { endIndex = startIndex + RECORDER_PER_PAGE - 1; }char row[6][COL_LEN_MAX]; system("cls"); printTableHead(TABLE_WIDTH, 6);
printTableRow(TABLE_WIDTH, head, sizeof(head) / sizeof(head[0])); printTableMidLine(TABLE_WIDTH, 6); for (int i = startIndex; i <= endIndex; i++) { sprintf(row[0], "%d", stu[i].num); sprintf(row[1], "%s", stu[i].name); sprintf(row[2], "%d", stu[i].cLang); sprintf(row[3], "%d", stu[i].algo); sprintf(row[4], "%d", stu[i].database); sprintf(row[5], "%d", stu[i].sum); printTableRow(TABLE_WIDTH, row, 6); if (i < endIndex) { printTableMidLine(TABLE_WIDTH, 6); }else {printTableTail(TABLE_WIDTH, 6); } } }
void search() { int snum = 0; system("cls"); printf("请输入学号:"); scanf("%d", &snum); int i = searchStu(snum); if (i < 0) { printf("没有找到这名学生!\n"); return; }char row[6][COL_LEN_MAX]; char head[][COL_LEN_MAX] = { "学号", "姓名", "C 语言", "算法", "数据库", "总分" }; printTableHead(TABLE_WIDTH, 6); printTableRow(TABLE_WIDTH, head, sizeof(head) / sizeof(head[0]));
printTableMidLine(TABLE_WIDTH, 6); sprintf(row[0], "%d", stu[i].num); sprintf(row[1], "%s", stu[i].name); sprintf(row[2], "%d", stu[i].cLang); sprintf(row[3], "%d", stu[i].algo); sprintf(row[4], "%d", stu[i].database); sprintf(row[5], "%d", stu[i].sum); printTableRow(TABLE_WIDTH, row, 6); printTableTail(TABLE_WIDTH, 6); }
void del() { FILE* fp; int snum = 0; char str[16] = ""; system("cls"); printf("请输入学号:"); scanf("%d", &snum); int i = searchStu(snum); if (i<0) { printf("没有找到这名学生!\n"); return; }printf("找到这条记录,是否删除?(y/n)"); scanf("%s", str); if (strcmp(str, "Y") == 0 || strcmp(str, "y") == 0) { for (int j = i; j < currentCount; j++) { stu[j] = stu[j + 1]; }currentCount--; if (save()) { printf("删除成功!\n"); }
else {printf("保存文件失败!\n"); } }else {printf("取消删除!\n"); } }
void modify() { int snum; system("cls"); printf("请输入要修改的学生的学号: "); scanf("%d", &snum); int i = searchStu(snum); if (i < 0) { printf("没有找到这名学生!\n"); return; }printf("找到了这名学生, 可以修改他的信息!\n"); printf("姓名:"); scanf("%s", stu[i].name); printf("C 语言:"); scanf("%d", &stu[i].cLang); printf("算法:"); scanf("%d", &stu[i].algo); printf("数据库:"); scanf("%d", &stu[i].database); stu[i].sum = stu[i].cLang + stu[i].algo + stu[i].database; if (save()) { printf("修改成功!\n"); }else {printf("保存文件失败!\n"); } }
int snum; system("cls"); printf("请输入要插入的位置(学号):"); scanf("%d", &snum); int destIndex = searchStu(snum); if (destIndex < 0) { printf("没有这名学生,插入位置错误!\n"); return; }struct student t = inputInfo(); int i = searchStu(t.num); if (i >= 0) { printf("学号[%d]已经存在! \n", t.num); return; }for (int j = currentCount-1; j > destIndex; j--) { stu[j + 1] = stu[j]; }stu[destIndex + 1] = t; currentCount++; if (save()) { printf("插入成功!\n"); } else { printf("保存文件失败!\n"); }
void order() {
if (currentCount == 0) { printf("还没有学生记录!\n"); return; }for (int i = 0; i < currentCount - 1; i++) { for (int j = i + 1; j < currentCount; j++) { if (stu[i].sum < stu[j].sum) { struct student t = stu[i]; stu[i] = stu[j]; stu[j] = t; } } }if (!save()) { printf("排序后,保存文件失败!\n"); }else {show(); } }
今天的分享就到这里了,大家要好好学C语言/C++哟~
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)加君羊获取哦~
C语言C++编程学习交流圈子,企鹅君羊:763855696
边栏推荐
猜你喜欢
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
TCP three handshakes and four waves
Deadlock analysis using jstack, jconsole, and jvisualvm
NumPy基础
Google GTEST event mechanism
How to solve the problem of configuring the progress every time Office2010 is opened?
三层项目的架构分析及构造方法的参数名称注入
玩家访问网站自动弹窗加QQ群方法以及详细代码
Solution | get the relevant information about the current employees' highest salary in each department |
Apache POI实现Excel导入读取数据和写入数据并导出
随机推荐
[config] configure array parameters
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
传奇开区网站如何添加流量统计代码
后置通知的流程分析与功能实现有哪些内容你还记得吗?
[file download] easyexcel quick start
Deep learning brush a bunch of tricks of SOTA
优炫数据库启动失败,报网络错误
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
SparkSql批量插入或更新,保存数据到Mysql中
Mysql语句中的函数
Modification of annotation based three-tier project and the way of adding package scanning
Rolabelimg to data format data
虚拟偶像的歌声原来是这样生成的!
Mysql的自连接和联合查询
Qml类型:State 状态
The representation of time series analysis: is the era of learning coming?
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
Mysql多对多关系,分组拼接把多个数据查询到一条数据上
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Quick start JDBC