赞
踩
嗨嗨嗨!!这里是是等等(。・∀・)ノ゙经过大一上学期的学习,老师希望我们能运用所学知识来编写一个学生管理系统,我也是磕磕碰碰完成啦。代码呢,总共500来行,用链表做的,总体是跟着视频来的,链接我放在后面啦,需要的小伙伴可以去看看(视频里用到的编译器是VS,我用到的编译器是CLion,所以可能有的地方不太一样)。那么来看看代码吧(ง •_•)ง
我也是挑了一个比较简单的,但是功能还是很全的o( ̄▽ ̄)o
一个学生通讯管理系统,学生信息包括职学号、姓名、班级、住址、电话、QQ,其中学号不重复。该系统具有以下功能:
(1)学生通讯信息录入(储存在文件中);
(2)学生通讯信息浏览;
(3)查询功能:包括按学号查询、按姓名查询,按班级查询;
(4)学生通讯信息删除:按学号删除、按姓名删除;
(5)学生通讯信息修改:按学号修改、按姓名修改;
(6)学生通讯信息排序:按学号升序、按学号降序;
(7)学生通讯信息统计:统计各班人数、统计各省份人数;
定义一个结构体存放学生通讯信息;定义节点,节点的数据域为存放学生信息的结构体;创建节点和创建表单独写一个函数,方便调用;
#include<stdio.h> #include <stdlib.h> #include <string.h> #define N 6 //班级数量 //数据设计 struct student{ int num; char name[20]; int class; char addr[50]; char tel[20]; char QQ[50]; }; struct Node{ struct student data; struct Node *next; }; //创建表 struct Node *createList(){ struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); headNode->next = NULL; return headNode; } //创建节点 struct Node *createNode(struct student data){ struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } //函数声明 void menu();//菜单 void keyDown();//用户交互 void keydown1();//用户交互 void keydown2();//用户交互 void keydown3();//用户交互 void keydown4();//用户交互 void keydown5();//用户交互 void insertNodeByHead(struct Node* headNode,struct student data);//1.录入 void printList(struct Node* headNode);//2.浏览 void searchInfoByNum(struct Node* headNode,int num);//3.按学号查询 void searchInfoByName(struct Node* headNode,char *name);//3.按名字查询 void searchInfoByClass(struct Node* headNode,int class);//3.按班级查询 void deleteNumNode(struct Node* headNode,int num);//4.按学号删除 void deleteNameNode(struct Node* headNode,char *name);//4.按姓名删除 void alterNumNode(struct Node* headNode,int num);//5.按学号修改 void alterNameNode(struct Node* headNode,char* name);//5.按姓名修改 void sortUp(struct Node* headNode);//6.按学号升序排序 void sortDrop(struct Node* headNode);//6.按学号降序排序 void countClass(struct Node* headNode);//7.统计各班人数 void countProvince(struct Node* headNode);//7.统计各省份人数 void readInFromFile(struct Node* headNode,char* filename);//文件读操作 void writeInFromFile(struct Node* headNode,char* filename);//文件写操作 struct Node* list = NULL;//创建链表,在主函数内赋值 int main() { list = createList(); readInFromFile(list,"student.txt"); while (1) { menu(); keyDown(); system("pause"); system("cls"); } }
这里就没什么可说的啦,为了方便大家理解,我做了个树状图( ̄▽ ̄)*
//菜单 void menu(){ printf("****************【学生通讯录管理系统】**************\n"); printf("*\t\t1.学生通讯信息录入 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t2.学生通讯信息浏览 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t3.查询功能 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t4.学生通讯信息删除 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t5.学生通讯信息修改 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t6.学生通讯信息排序 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t7.学生通讯信息统计 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t0.退出系统 *\n"); printf("****************************************************\n"); } //用户交互 void keyDown(){ struct student data; int choice = 0; scanf("%d",&choice); switch(choice){ case 0: printf("正常退出。"); system("pause"); exit(0); break; case 1: printf("----------------【学生通讯信息录入】----------------\n"); printf("请输入学生的学号,姓名,班级,地址,电话,QQ:\n"); fflush(stdin); scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ); insertNodeByHead(list,data); printf("录入成功!\n"); break; case 2: printf("----------------【学生通讯信息浏览】----------------\n"); printList(list); break; case 3: printf("----------------【查询功能】----------------\n"); printf("\t\t1.按学号查询\n"); printf("\t\t2.按姓名查询\n"); printf("\t\t3.按班级查询\n"); keydown1(); break; case 4: printf("----------------【学生通讯信息删除】----------------\n"); printf("\t\t1.按学号删除\n"); printf("\t\t2.按姓名删除\n"); keydown2(); break; case 5: printf("----------------【学生通讯信息修改】----------------\n"); printf("\t\t1.按学号查找并修改\n"); printf("\t\t2.按姓名查找并修改\n"); keydown3(); break; case 6: printf("----------------【学生通讯信息排序】----------------\n"); printf("\t\t1.按学号升序排序\n"); printf("\t\t2.按学号降序排序\n"); keydown4(); break; case 7: printf("----------------【学生通讯信息统计】----------------\n"); printf("\t\t1.统计各班人数\n"); printf("\t\t2.统计各省份人数\n"); keydown5(); break; default: printf("选择错误,请重新输入......\n"); system("pause"); } writeInFromFile(list,"student.txt"); } void keydown1(){ int choice = 0; scanf("%d",&choice); struct student data; switch(choice){ case 1: printf("----------------【按学号查询】----------------\n"); printf("请输入学号:"); scanf("%d",&data.num); searchInfoByNum(list,data.num); break; case 2: printf("----------------【按姓名查询】----------------\n"); printf("请输入姓名:"); scanf("%s",data.name); searchInfoByName(list,data.name); break; case 3: printf("----------------【按班级查询】----------------\n"); printf("请输入班级:"); scanf("%d",&data.class); searchInfoByClass(list,data.class); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown2(){ struct student data; int choice = 0; scanf("%d",&choice); switch(choice){ case 1: printf("----------------【按学号删除】----------------\n"); printf("请输入删除学生的学号:\n"); scanf("%d",&data.num); deleteNumNode(list,data.num); break; case 2: printf("----------------【按姓名删除】----------------\n"); printf("请输入删除学生的姓名:\n"); scanf("%s",data.name); deleteNameNode(list,data.name); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown3() { struct student data; int choice = 0; scanf("%d", &choice); switch (choice) { case 1: printf("----------------【按学号查找并修改】----------------\n"); printf("请输入需要修改学生的学号:\n"); scanf("%d", &data.num); alterNumNode(list, data.num); break; case 2: printf("----------------【按姓名查找并修改】----------------\n"); printf("请输入需要修改学生的姓名:\n"); scanf("%s", data.name); alterNameNode(list,data.name); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown4(){ int choice = 0; scanf("%d", &choice); switch (choice) { case 1: printf("----------------【按学号升序排序】----------------\n"); sortUp(list); break; case 2: printf("----------------【按学号降序排序】----------------\n"); sortDrop(list); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown5(){ int choice = 0; scanf("%d", &choice); switch (choice) { case 1: printf("----------------【统计各班人数】----------------\n"); countClass(list); break; case 2: printf("----------------【统计各省份人数】----------------\n"); countProvince(list); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } }
运行界面
录入:将学生通讯信息储存在链表中。
浏览:遍历链表。
//录入 void insertNodeByHead(struct Node* headNode,struct student data) { struct Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode; } //浏览 void printList(struct Node* headNode){ struct Node* pMove = headNode->next; printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); while (pMove){ printf("%d\t%s\t%d\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.class,pMove->data.addr,pMove->data.tel,pMove->data.QQ); pMove = pMove->next; } printf("\n"); }
运行界面
遍历链表,如该学生存在,则打印该学生信息,不存在则退出。
//按学号查询 void searchInfoByNum(struct Node* headNode,int num){ struct Node* pMove = headNode; while (pMove){ if(pMove -> data.num == num){ printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr, pMove->data.tel, pMove->data.QQ); break; } pMove = pMove -> next; } if(pMove == NULL){ printf("学号不存在,无法查找!\n "); return ; } } //按名字查询 void searchInfoByName(struct Node* headNode,char *name){ struct Node* pMove = headNode; while (pMove){ if(strcmp(pMove -> data.name,name) == 0){ printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr, pMove->data.tel, pMove->data.QQ); break; } pMove = pMove -> next; } if(pMove == NULL){ printf("名字不存在,无法查找!\n "); return ; } } //按班级查询 void searchInfoByClass(struct Node* headNode,int class){ struct Node* pMove = headNode; printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); while (pMove){ if(pMove -> data.class == class){ printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr, pMove->data.tel, pMove->data.QQ); } pMove = pMove -> next; } if(pMove == NULL){ printf("班级不存在,无法查找!\n "); return ; } }
运行介面
遍历链表,如存在该学生,释放节点(即删除),不存在则退出。
//按学号删除 void deleteNumNode(struct Node* headNode,int num){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; while(posNode -> data.num != num) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找学号位置,无法删除!"); return; } else{ posFrontNode -> next = posNode -> next; free(posNode); printf("删除成功!"); } } //按名字删除 void deleteNameNode(struct Node* headNode,char *name){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; if(posNode == NULL){ printf("名字不存在,无法删除!\n "); return; } while(strcmp(posNode -> data.name,name)) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找名字位置,无法删除!"); return; } else{ posFrontNode -> next = posNode -> next; free(posNode); printf("删除成功!"); } }
运行介面
先删除再重新录入,结合删除功能和录入功能。
//按学号修改 void alterNumNode(struct Node* headNode,int num){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; while(posNode -> data.num != num) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找该学生,无法修改!"); return; } else{ struct student data; posFrontNode -> next = posNode -> next; free(posNode); printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n"); fflush(stdin); scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ); insertNodeByHead(list,data); printf("修改成功!"); } } //按姓名修改 void alterNameNode(struct Node* headNode,char* name){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; while(strcmp(posNode -> data.name,name)) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找该学生,无法修改!"); return; } else{ struct student data; posFrontNode -> next = posNode -> next; free(posNode); printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n"); fflush(stdin); scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ); insertNodeByHead(list,data); printf("修改成功!"); } }
运行界面
采用了冒泡排序法。
//按学号升序排序 void sortUp(struct Node* headNode){ struct Node* turn; struct Node* move; struct student temp; for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){ for(move = headNode -> next;move -> next != NULL;move = move -> next){ if(move -> data.num > move -> next -> data.num){ temp = move -> data; move -> data = move -> next -> data; move -> next -> data = temp; } } } printList(list); } //按学号降序排序 void sortDrop(struct Node* headNode){ struct Node* turn; struct Node* move; struct student temp; for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){ for(move = headNode -> next;move -> next != NULL;move = move -> next){ if(move -> data.num < move -> next -> data.num){ temp = move -> data; move -> data = move -> next -> data; move -> next -> data = temp; } } } printList(list); }
运行介面
遍历链表,定义计数数组,用来存放各班级人数或各省份人数,最后将统计结果输出。
//统计各班级人数 void countClass(struct Node* headNode){ int i; int count[N + 1] = {0}; struct Node* pMove = headNode; while (pMove){ for( i = 1;i <= N; i++){ if (pMove->data.class == i ) count[i]++; } pMove = pMove -> next; } for (int i = 1;i <= N; i++) { printf("%d班的人数为:%d\n",i,count[i]); } } //统计各省份人数 void countProvince(struct Node* headNode){ char province[34][100] = {"Hebei","Shanxi","Liaoning","Jilin","Heilongjiang","Jiangsu","Zhejiang","Anhui","Fujian","Jiangxi","Shandong","Henan","Hubei","Hunan","Guangdong","Hannan","Sichuan","Guizhou","Yunnan","Shanxi","Gansu","Qinghai","Taiwan","Neimenggu","Guangxi","Xizang","Ningxia","Xinjiang","Beijiang","Tianjing","Shanghai","Chongqing","Xianggang","Aomen"}; int i; int count[34] ={0}; struct Node* pMove = headNode; while (pMove){ for( i = 0;i < 34; i++){ if (strcmp(pMove->data.addr,province[i]) == 0 ) count[i]++; } pMove = pMove -> next; } for (int i = 0;i < 34; i++) { if(count[i] != 0) printf("地址为%s的人数为%d\n",province[i],count[i]); } }
运行介面
#include<stdio.h> #include <stdlib.h> #include <string.h> #define N 6 //数据设计 struct student{ int num; char name[20]; int class; char addr[50]; char tel[20]; char QQ[50]; }; struct Node{ struct student data; struct Node *next; }; //创建表 struct Node *createList(){ struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); headNode->next = NULL; return headNode; } //创建结点 struct Node *createNode(struct student data){ struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } //函数声明 void menu();//菜单 void keyDown();//用户交互 void keydown1();//用户交互 void keydown2();//用户交互 void keydown3();//用户交互 void keydown4();//用户交互 void keydown5();//用户交互 void insertNodeByHead(struct Node* headNode,struct student data);//1.录入 void printList(struct Node* headNode);//2.浏览 void searchInfoByNum(struct Node* headNode,int num);//3.按学号查询 void searchInfoByName(struct Node* headNode,char *name);//3.按名字查询 void searchInfoByClass(struct Node* headNode,int class);//3.按班级查询 void deleteNumNode(struct Node* headNode,int num);//4.按学号删除 void deleteNameNode(struct Node* headNode,char *name);//4.按姓名删除 void alterNumNode(struct Node* headNode,int num);//5.按学号修改 void alterNameNode(struct Node* headNode,char* name);//5.按姓名修改 void sortUp(struct Node* headNode);//6.按学号升序排序 void sortDrop(struct Node* headNode);//6.按学号降序排序 void countClass(struct Node* headNode);//7.统计各班人数 void countProvince(struct Node* headNode);//7.统计各省份人数 void readInFromFile(struct Node* headNode,char* filename);//文件读操作 void writeInFromFile(struct Node* headNode,char* filename);//文件写操作 struct Node* list = NULL; int main() { list = createList(); readInFromFile(list,"student.txt"); while (1) { menu(); keyDown(); system("pause"); system("cls"); } } //菜单 void menu(){ printf("****************【学生通讯录管理系统】**************\n"); printf("*\t\t1.学生通讯信息录入 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t2.学生通讯信息浏览 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t3.查询功能 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t4.学生通讯信息删除 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t5.学生通讯信息修改 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t6.学生通讯信息排序 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t7.学生通讯信息统计 *\n"); printf("*--------------------------------------------------*\n"); printf("*\t\t0.退出系统 *\n"); printf("****************************************************\n"); } //用户交互 void keyDown(){ struct student data; int choice = 0; scanf("%d",&choice); switch(choice){ case 0: printf("正常退出。"); system("pause"); exit(0); break; case 1: printf("----------------【学生通讯信息录入】----------------\n"); printf("请输入学生的学号,姓名,班级,地址,电话,QQ:\n"); fflush(stdin); scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ); insertNodeByHead(list,data); printf("录入成功!"); break; case 2: printf("----------------【学生通讯信息浏览】----------------\n"); printList(list); break; case 3: printf("----------------【查询功能】----------------\n"); printf("\t\t1.按学号查询\n"); printf("\t\t2.按姓名查询\n"); printf("\t\t3.按班级查询\n"); keydown1(); break; case 4: printf("----------------【学生通讯信息删除】----------------\n"); printf("\t\t1.按学号删除\n"); printf("\t\t2.按姓名删除\n"); keydown2(); break; case 5: printf("----------------【学生通讯信息修改】----------------\n"); printf("\t\t1.按学号查找并修改\n"); printf("\t\t2.按姓名查找并修改\n"); keydown3(); break; case 6: printf("----------------【学生通讯信息排序】----------------\n"); printf("\t\t1.按学号升序排序\n"); printf("\t\t2.按学号降序排序\n"); keydown4(); break; case 7: printf("----------------【学生通讯信息统计】----------------\n"); printf("\t\t1.统计各班人数\n"); printf("\t\t2.统计各省份人数\n"); keydown5(); break; default: printf("选择错误,请重新输入......\n"); system("pause"); } writeInFromFile(list,"student.txt"); } void keydown1(){ int choice = 0; scanf("%d",&choice); struct student data; switch(choice){ case 1: printf("----------------【按学号查询】----------------\n"); printf("请输入学号:"); scanf("%d",&data.num); searchInfoByNum(list,data.num); break; case 2: printf("----------------【按姓名查询】----------------\n"); printf("请输入姓名:"); scanf("%s",data.name); searchInfoByName(list,data.name); break; case 3: printf("----------------【按班级查询】----------------\n"); printf("请输入班级:"); scanf("%d",&data.class); searchInfoByClass(list,data.class); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown2(){ struct student data; int choice = 0; scanf("%d",&choice); switch(choice){ case 1: printf("----------------【按学号删除】----------------\n"); printf("请输入删除学生的学号:\n"); scanf("%d",&data.num); deleteNumNode(list,data.num); break; case 2: printf("----------------【按姓名删除】----------------\n"); printf("请输入删除学生的姓名:\n"); scanf("%s",data.name); deleteNameNode(list,data.name); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown3() { struct student data; int choice = 0; scanf("%d", &choice); switch (choice) { case 1: printf("----------------【按学号查找并修改】----------------\n"); printf("请输入需要修改学生的学号:\n"); scanf("%d", &data.num); alterNumNode(list, data.num); break; case 2: printf("----------------【按姓名查找并修改】----------------\n"); printf("请输入需要修改学生的姓名:\n"); scanf("%s", data.name); alterNameNode(list,data.name); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown4(){ int choice = 0; scanf("%d", &choice); switch (choice) { case 1: printf("----------------【按学号升序排序】----------------\n"); sortUp(list); break; case 2: printf("----------------【按学号降序排序】----------------\n"); sortDrop(list); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } void keydown5(){ int choice = 0; scanf("%d", &choice); switch (choice) { case 1: printf("----------------【统计各班人数】----------------\n"); countClass(list); break; case 2: printf("----------------【统计各省份人数】----------------\n"); countProvince(list); break; default: printf("选择错误,请重新输入......\n"); system("pause"); break; } } //录入 void insertNodeByHead(struct Node* headNode,struct student data) { struct Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode; } //浏览 void printList(struct Node* headNode){ struct Node* pMove = headNode->next; printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); while (pMove){ printf("%d\t%s\t%d\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.class,pMove->data.addr,pMove->data.tel,pMove->data.QQ); pMove = pMove->next; } printf("\n"); } //按学号查询 void searchInfoByNum(struct Node* headNode,int num){ struct Node* pMove = headNode; while (pMove){ if(pMove -> data.num == num){ printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr, pMove->data.tel, pMove->data.QQ); break; } pMove = pMove -> next; } if(pMove == NULL){ printf("学号不存在,无法查找!\n "); return ; } } //按名字查询 void searchInfoByName(struct Node* headNode,char *name){ struct Node* pMove = headNode; while (pMove){ if(strcmp(pMove -> data.name,name) == 0){ printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr, pMove->data.tel, pMove->data.QQ); break; } pMove = pMove -> next; } if(pMove == NULL){ printf("名字不存在,无法查找!\n "); return ; } } //按班级查询 void searchInfoByClass(struct Node* headNode,int class){ struct Node* pMove = headNode; printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n"); while (pMove){ if(pMove -> data.class == class){ printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr, pMove->data.tel, pMove->data.QQ); } pMove = pMove -> next; } if(pMove == NULL){ printf("班级不存在,无法查找!\n "); return ; } } //按学号删除 void deleteNumNode(struct Node* headNode,int num){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; while(posNode -> data.num != num) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找学号位置,无法删除!"); return; } else{ posFrontNode -> next = posNode -> next; free(posNode); printf("删除成功!"); } } //按名字删除 void deleteNameNode(struct Node* headNode,char *name){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; if(posNode == NULL){ printf("名字不存在,无法删除!\n "); return; } while(strcmp(posNode -> data.name,name)) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找名字位置,无法删除!"); return; } else{ posFrontNode -> next = posNode -> next; free(posNode); printf("删除成功!"); } } //按学号修改 void alterNumNode(struct Node* headNode,int num){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; while(posNode -> data.num != num) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找该学生,无法修改!"); return; } else{ struct student data; posFrontNode -> next = posNode -> next; free(posNode); printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n"); fflush(stdin); scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ); insertNodeByHead(list,data); printf("修改成功!"); } } //按姓名修改 void alterNameNode(struct Node* headNode,char* name){ struct Node* posNode = headNode -> next; struct Node* posFrontNode = headNode; while(strcmp(posNode -> data.name,name)) { posFrontNode = posNode; posNode = posNode -> next; } if(posNode == NULL){ printf("未找该学生,无法修改!"); return; } else{ struct student data; posFrontNode -> next = posNode -> next; free(posNode); printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n"); fflush(stdin); scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ); insertNodeByHead(list,data); printf("修改成功!"); } } //按学号升序排序 void sortUp(struct Node* headNode){ struct Node* turn; struct Node* move; struct student temp; for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){ for(move = headNode -> next;move -> next != NULL;move = move -> next){ if(move -> data.num > move -> next -> data.num){ temp = move -> data; move -> data = move -> next -> data; move -> next -> data = temp; } } } printList(list); } //按学号降序排序 void sortDrop(struct Node* headNode){ struct Node* turn; struct Node* move; struct student temp; for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){ for(move = headNode -> next;move -> next != NULL;move = move -> next){ if(move -> data.num < move -> next -> data.num){ temp = move -> data; move -> data = move -> next -> data; move -> next -> data = temp; } } } printList(list); } //统计各班级人数 void countClass(struct Node* headNode){ int i; int count[N + 1] = {0}; struct Node* pMove = headNode; while (pMove){ for( i = 1;i <= N; i++){ if (pMove->data.class == i ) count[i]++; } pMove = pMove -> next; } for (int i = 1;i <= N; i++) { printf("%d班的人数为:%d\n",i,count[i]); } } //统计各省份人数 void countProvince(struct Node* headNode){ char province[34][100] = {"Hebei","Shanxi","Liaoning","Jilin","Heilongjiang","Jiangsu","Zhejiang","Anhui","Fujian","Jiangxi","Shandong","Henan","Hubei","Hunan","Guangdong","Hannan","Sichuan","Guizhou","Yunnan","Shanxi","Gansu","Qinghai","Taiwan","Neimenggu","Guangxi","Xizang","Ningxia","Xinjiang","Beijiang","Tianjing","Shanghai","Chongqing","Xianggang","Aomen"}; int i; int count[34] ={0}; struct Node* pMove = headNode; while (pMove){ for( i = 0;i < 34; i++){ if (strcmp(pMove->data.addr,province[i]) == 0 ) count[i]++; } pMove = pMove -> next; } for (int i = 0;i < 34; i++) { if(count[i] != 0) printf("地址为%s的人数为%d\n",province[i],count[i]); } } //文件读操作 void readInFromFile(struct Node* headNode,char* filename){ struct student data; FILE *fp; fp = fopen(filename,"r"); if(fp == NULL){ printf("文件打开失败!"); } while (fscanf(fp,"%d\t%s\t%d\t%s\t%s\t%s\n",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ) != EOF){ insertNodeByHead(headNode,data); } fclose(fp); } //文件写操作 void writeInFromFile(struct Node* headNode,char* filename){ FILE *fp = fopen(filename,"w"); struct Node* pMove = headNode -> next; while(pMove){ fprintf(fp,"%d\t%s\t%d\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.class,pMove->data.addr,pMove->data.tel,pMove->data.QQ); pMove = pMove -> next; } fclose(fp); }
好了,大概就是这样了,后期我还增加了登录和注册功能(比较简单的),需要的小伙伴可以找我要( ̄▽ ̄)",如果程序上有什么问题,希望大家可以帮忙指出,我会及时更正的(☆▽☆)。
视频链接在这,老师讲得很好,大家可以看看。C/C++项目教程:学生信息管理系统,大学里老师布置的作业你会做吗?基础不牢固的赶紧学习一下。
下面这个是我测试程序用到的数据,分享给大家啦,省得大家一个个敲(~o ̄3 ̄)~
10001 Lily 1 Beijing 13836763922 5467334567
10002 Sophia 1 Shanxi 13987654890 8894563208
10003 Olivia 1 Shanxi 19176490875 3456998711
10004 Amelia 1 Tianjin 13887490654 3746529087
10005 Freya 1 Fujian 13399878956 3647589022
10006 Aria 2 Nanjing 13399568956 3847590865
10007 Navbar 2 Guangxi 13890878956 3748598877
10008 Mila 2 Beijing 13399864956 3377287653
10009 Grace 2 Fujian 13397898956 3748595999
10010 Layla 2 Hebei 19909878956 3374727177
10011 Maryam 3 Beijing 13996328956 4758349993
10012 Willow 3 Hubei 13399878943 3894859020
10013 Elsie 3 Qinghai 13988956654 3848569652
10014 Bailey 3 Hebei 13399878546 5748399302
10015 Blew 3 Gansu 13399648956 3847539534
10016 Chukka 3 Nanjing 19176496780 5345329464
10017 Dock 4 Hebei 19176887875 3465364536
10018 String 4 Nanjing 19176490543 3246524645
10019 Fibula 4 Sichuan 19176490098 5432543465
10020 Grain 4 Sichaun 19176490567 3465236534
10021 High 4 Guizhou 19176490453 3456326455
10022 Janara 5 Taiwan 19176490875 6346456346
10023 Jannean 5 Nanjing 19176095875 3462456234
10024 Kayne 5 Beijing 19176497645 8437536754
10025 Lalaine 5 Shanxi 19176490634 6345734766
10026 Mikeia 5 Taiwan 19176478984 3475357349
10027 Mieshia 5 Sichuan 19176490556 4357347568
10028 Narali 6 Nanjing 19109890875 4373765826
10029 Enemy 6 Taiwan 19176498794 6427358754
10030 Conchie 6 Hebei 19176494444 3475684675
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。