前言
这是学校布置的期末作业,自己完成后,给后来人铺铺路。
主要功能:
运行程序,可以从txt中读取学号、姓名、性别、年龄、备注。然后可以在程序里面进行录入、显示、删除、保存、修改、查询、排序的操作。最终目的是用程序系统化的进行输入,最后保存到原来的txt文件中。
关于代码:
懒得写注释了。学校要求的使用结构体数组,不过我这个是用链表结构做的,功能更完善,内存使用更自由。想用链表做的同学们可以看看,根据自己的需求也可以改改照搬。
主要涉及到的知识点:文件读取生成链表、链表的插入、链表的遍历显示、链表的删除节点、输出到文件中、链表节点的定位查找、链表交换节点的冒泡排序。
代码展示:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<conio.h>
void title(){
printf("+-----班级档案管理系统-----+\n");
printf("| A:学生信息录入 |\n");
printf("| B:学生信息显示 |\n");
printf("| C:学生信息保存 |\n");
printf("| D:学生信息删除 |\n");
printf("| E:学生信息修改 |\n");
printf("| F:学生信息查询 |\n");
printf("| S:按年龄排序 |\n");
printf("| G:退出系统 |\n");
printf("+--------------------------+\n");
printf("请输入功能代码:");
}
void modifytitle(){
printf("+----------------+\n");
printf("| 1.修改学号 |\n");
printf("| 2.修改姓名 |\n");
printf("| 3.修改性别 |\n");
printf("| 4.修改年龄 |\n");
printf("| 5.修改备注 |\n");
printf("| 6.退出修改 |\n");
printf("+----------------+\n");
}
void searchtitle(){
printf("+------------------+\n");
printf("| 1.按学号查询 |\n");
printf("| 2.按姓名查询 |\n");
printf("| 3.按性别查询 |\n");
printf("| 4.按年龄查询 |\n");
printf("| 5.退出查询 |\n");
printf("+------------------+\n");
printf("请输入功能数字:");
}
typedef struct node{
char num[10];
char name[12];
int sex;
int year;
char note[20];
struct node *next;
}Node,*Link;
Link getnode(){
int sex,year;
char num[10],name[12],note[20];
Link head=(Link)malloc(sizeof(Node));
head->next=NULL;
Link rear=head;
FILE *r=fopen("ManageData.txt","r");
while(fscanf(r,"%s%s%d%d%s",num,name,&sex,&year,note)!=EOF){ //一行一行读取,直到EOF行尾是空
Link node=(Link)malloc(sizeof(Node));
strcpy(node->num,num);
strcpy(node->name,name);
node->sex=sex;
node->year=year;
strcpy(node->note,note);
rear->next=node;
rear=node;
}
rear->next=NULL;
fclose(r);
return head;
}
int insertnode(Link head,int n,char num[],char name[],int sex,int year,char note[]){
Link p=head;
int count=0;
while(p!=NULL && count<n-1){
p=p->next;
count++;
}
if(p==NULL)return 0;
Link node=(Link)malloc(sizeof(Node));
strcpy(node->num,num);
strcpy(node->name,name);
node->sex=sex;
node->year=year;
strcpy(node->note,note);
node->next=p->next;
p->next=node;
return 1;
}
void display(Link head){
Link p=head->next;
printf("+------------+------------------+---------+----------+------------\n");
while(p){
printf("| 学号:%4s | 姓名:%-10s | 性别:%1d | 年龄:%2d | 备注:%s\n",p->num,p->name,p->sex,p->year,p->note);
p=p->next;
}
printf("+------------+------------------+---------+----------+------------\n");
}
void outnode(Link head){
FILE *w=fopen("ManageData.txt","w");
Link p=head->next;
while(p){
fprintf(w,"%s %s %d %d %s\n",p->num,p->name,p->sex,p->year,p->note);
p=p->next;
}
fclose(w);
}
int deletenode(Link head,int n){
if(head==NULL || head->next==NULL)return 0;
Link q=head,p=head->next;
int count=1;
while(p && count<n){
count++;
q=q->next;
p=p->next;
}
if(p==NULL)return 0;
q->next=p->next;
free(p);
return 1;
}
void modifynum(Link head,int n,char num[]){
Link p=head->next;
int count=1;
while(p && count<n){
p=p->next;
count++;
}
if(p==NULL){
printf("修改失败,位置溢出\n");
return;
}
strcpy(p->num,num);
printf("修改成功\n");
}
void modifyname(Link head,int n,char name[]){
Link p=head->next;
int count=1;
while(p && count<n){
p=p->next;
count++;
}
if(p==NULL){
printf("修改失败,位置溢出\n");
return;
}
strcpy(p->name,name);
printf("修改成功\n");
}
void modifysex(Link head,int n,int sex){
Link p=head->next;
int count=1;
while(p && count<n){
p=p->next;
count++;
}
if(p==NULL){
printf("修改失败,位置溢出\n");
return;
}
p->sex=sex;
printf("修改成功\n");
}
void modifyyear(Link head,int n,int year){
Link p=head->next;
int count=1;
while(p && count<n){
p=p->next;
count++;
}
if(p==NULL){
printf("修改失败,位置溢出\n");
return;
}
p->year=year;
printf("修改成功\n");
}
void modifynote(Link head,int n,char note[]){
Link p=head->next;
int count=1;
while(p && count<n){
p=p->next;
count++;
}
if(p==NULL){
printf("修改失败,位置溢出\n");
return;
}
strcpy(p->note,note);
printf("修改成功\n");
}
void searchnum(Link head,char num[]){
Link p=head->next;
int flag=0,count=1,start=1;
while(p){
if(strcmp(p->num,num)==0){
flag=1;
if(start==1){
printf("---------------------------------查询成功------------------------------------\n");
}
printf("+----------+------------+------------------+---------+----------+------------\n");
printf("| 位置:%2d | 学号:%4s | 姓名:%-10s | 性别:%1d | 年龄:%2d | 备注:%s\n",count,p->num,p->name,p->sex,p->year,p->note);
start++;
}
count++;
p=p->next;
}
if(flag==0){
printf("+------------+\n");
printf("| 查询失败 |\n");
printf("| 查无此人 |\n");
printf("+------------+\n");
}else{
printf("+----------+------------+------------------+---------+----------+------------\n");
}
}
void searchname(Link head,char name[]){
Link p=head->next;
int flag=0,count=1,start=1;
while(p){
if(strcmp(p->name,name)==0){
flag=1;
if(start==1){
printf("---------------------------------查询成功------------------------------------\n");
}
printf("+----------+------------+------------------+---------+----------+------------\n");
printf("| 位置:%2d | 学号:%4s | 姓名:%-10s | 性别:%1d | 年龄:%2d | 备注:%s\n",count,p->num,p->name,p->sex,p->year,p->note);
start++;
}
count++;
p=p->next;
}
if(flag==0){
printf("+------------+\n");
printf("| 查询失败 |\n");
printf("| 查无此人 |\n");
printf("+------------+\n");
}else{
printf("+----------+------------+------------------+---------+----------+------------\n");
}
}
void searchsex(Link head,int sex){
Link p=head->next;
int flag=0,count=1,start=1;
while(p){
if(p->sex==sex){
flag=1;
if(start==1){
printf("---------------------------------查询成功------------------------------------\n");
}
printf("+----------+------------+------------------+---------+----------+------------\n");
printf("| 位置:%2d | 学号:%4s | 姓名:%-10s | 性别:%1d | 年龄:%2d | 备注:%s\n",count,p->num,p->name,p->sex,p->year,p->note);
start++;
}
count++;
p=p->next;
}
if(flag==0){
printf("+------------+\n");
printf("| 查询失败 |\n");
printf("| 查无此人 |\n");
printf("+------------+\n");
}else{
printf("+----------+------------+------------------+---------+----------+------------\n");
}
}
void searchyear(Link head,int year){
Link p=head->next;
int flag=0,count=1,start=1;
while(p){
if(p->year==year){
flag=1;
if(start==1){
printf("---------------------------------查询成功------------------------------------\n");
}
printf("+----------+------------+------------------+---------+----------+------------\n");
printf("| 位置:%2d | 学号:%4s | 姓名:%-10s | 性别:%1d | 年龄:%2d | 备注:%s\n",count,p->num,p->name,p->sex,p->year,p->note);
start++;
}
count++;
p=p->next;
}
if(flag==0){
printf("+------------+\n");
printf("| 查询失败 |\n");
printf("| 查无此人 |\n");
printf("+------------+\n");
}else{
printf("+----------+------------+------------------+---------+----------+------------\n");
}
}
void sort(Link head,int len){
for (int i=0;i<len-1;i++){
Link left=head,mid=head->next,right=mid->next;
for(int j=0;j<len-i-1;j++){
if(mid->year>right->year){
Link leftnext=left->next;
left->next=mid->next;
mid->next=right->next;
right->next=leftnext;
Link temp=mid;
mid=right;
right=temp;
}
left=left->next;
mid=mid->next;
right=right->next;
}
}
}
int main(){
int n,sex,year,cookie=0;
char num[10],name[12],note[20];
char username[]="root",password[]="123456cwy",inputusername[20],inputpassword[20];
char input=' ';
Link head=getnode();
printf("输入用户名:");scanf("%s",inputusername);
printf("输入密码:");scanf("%s",inputpassword);
while(strcmp(username,inputusername)!=0 || strcmp(password,inputpassword)!=0){
printf("+------------------+\n");
printf("| 用户名或密码错误 |\n");
printf("+------------------+\n");
printf("输入用户名:");scanf("%s",inputusername);
printf("输入密码:");scanf("%s",inputpassword);
}
printf("+----------+\n");
printf("| 登陆成功 |\n");
printf("+----------+\n");
Sleep(1000);
title();
start:
while(input!='G'){
fflush(stdin);
input=getchar();
switch(input){
case 'A':
printf("输入要录入的位置:");scanf("%d",&n);
printf("输入四位学号:");scanf("%s",num);
printf("输入姓名:");scanf("%s",name);
printf("输入性别代码:");scanf("%d",&sex);
printf("输入年龄:");scanf("%d",&year);
printf("输入备注:");scanf("%s",note);
insertnode(head,n,num,name,sex,year,note)?printf("录入成功\n"):printf("录入失败,位置溢出\n");
Sleep(1000);
title();
break;
case 'B':
printf("----------------------------班级档案------------------------------\n");
display(head);
printf("+--------------+\n");
printf("| 按任意键继续 |\n");
printf("+--------------+\n");
getch();
title();
break;
case 'C':
outnode(head);
printf("保存成功\n");
Sleep(1000);
title();
break;
case 'D':
printf("输入删除数据的位置:");scanf("%d",&n);
deletenode(head,n) ? printf("删除成功\n") : printf("删除失败,位置溢出\n");
Sleep(1000);
title();
break;
case 'E':
printf("+------------------------+\n");
printf("| 修改数据需要先确认密码 |\n");
printf("+------------------------+\n");
while(cookie==0){
printf("确认密码:");scanf("%s",inputpassword);
if(strcmp(password,inputpassword)==0){
break;
}else if(strcmp(inputpassword,"Q")==0){
printf("+--------------+\n");
printf("| 退出确认成功 |\n");
printf("+--------------+\n");
Sleep(1500);
title();
goto start;
}else{
printf("+---------------------------------+\n");
printf("| 密码错误,若想退出登录可输入Q |\n");
printf("+---------------------------------+\n");
}
}
cookie=1;
printf("+--------------------------------------+\n");
printf("| 确认成功,再次进入修改界面不需要密码 |\n");
printf("+--------------------------------------+\n");
Sleep(1000);
modifytitle();
while(1){
fflush(stdin);
printf("输入功能数字:");scanf("%d",&n);
if(n==1){
printf("输入要修改的位置:");scanf("%d",&n);
printf("输入更正后的学号:");scanf("%s",num);
modifynum(head,n,num);
Sleep(1000);
modifytitle();
}else if(n==2){
printf("输入要修改的位置:");scanf("%d",&n);
printf("输入更正后的姓名:");scanf("%s",name);
modifyname(head,n,name);
Sleep(1000);
modifytitle();
}else if(n==3){
printf("输入要修改的位置:");scanf("%d",&n);
printf("输入更正后的性别:");scanf("%d",&sex);
modifysex(head,n,sex);
Sleep(1000);
modifytitle();
}else if(n==4){
printf("输入要修改的位置:");scanf("%d",&n);
printf("输入更正后的年龄:");scanf("%d",&year);
modifyyear(head,n,year);
Sleep(1000);
modifytitle();
}else if(n==5){
printf("输入要修改的位置:");scanf("%d",&n);
printf("输入更正后的备注:");scanf("%s",note);
modifynote(head,n,note);
Sleep(1000);
modifytitle();
}else if(n==6){
printf("+----------+\n");
printf("| 退出成功 |\n");
printf("+----------+\n");
Sleep(1000);
title();
break;
}else{
printf("+----------------------+\n");
printf("| 未知命令,请重新输入 |\n");
printf("+----------------------+\n");
Sleep(1000);
modifytitle();
}
}
break;
case 'F':
searchtitle();
scanf("%d",&n);fflush(stdin);
if(n==1){
printf("请输入查询的学号:");gets(num);
searchnum(head,num);
printf("按任意键继续...\n");
getch();
}else if(n==2){
printf("请输入查询的姓名:");gets(name);
searchname(head,name);
printf("按任意键继续...\n");
getch();
}else if(n==3){
printf("请输入查询的性别:");scanf("%d",&sex);
searchsex(head,sex);
printf("按任意键继续...\n");
getch();
}else if(n==4){
printf("请输入查询的年龄:");scanf("%d",&year);
searchyear(head,year);
printf("按任意键继续...\n");
getch();
}else if(n==5){
printf("+----------+\n");
printf("| 退出成功 |\n");
printf("+----------+\n");
Sleep(1000);
title();
break;
}
title();
break;
case 'G':
printf("+----------+\n");
printf("| 退出成功 |\n");
printf("+----------+\n");
Sleep(1000);
break;
case 'S':
printf("输入长度:");scanf("%d",&n);
sort(head,n);
printf("排序成功\n");
title();
break;
default:
printf("未知命令,请重新输入\n");
Sleep(1000);
title();
break;
}
}
}