📄 c简单管理系统.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct link //定义一个学生结构体
{
int id;
char name[10];
char man[5];
int age;
float weight;
struct link *next;
} student;
student *head ,*tail;
void startmaking(void);
void print(student *stu);
void allprint(void);
void insert (void);
void showmenu(void);
int getchoice(void);
void modify(void);
student *searchByName( char *name);
void searchByName0(char *name,student *stu0[]);
void countWeight(void);
void countAge(void);
void score(void);
void read (void);
void readout(void);
void del(void);
void printone(void);
void destroy(void);
void fprint(FILE *fp);
void des(student *one);
void searchByName_buffer( char *name,student *stu[]);
void main()
{
int choice;
startmaking(); //程序的初始化
while(1)
{
showmenu(); //输出菜单
choice=getchoice(); //获得用户选择信息
switch(choice)
{
case 1:
system("cls");insert();printf("操作成功\n"); break;
case 2:
system("cls");allprint();printf("操作成功\n");break;
case 3:
system("cls");modify();break;
case 4:
system("cls");countAge();printf("\n操作成功\n");break;
case 5:
system("cls"); countWeight();printf("\n操作成功\n");break;
case 6:
system("cls"); score();printf("数据储存操作成功\n");break;
case 7:
system("cls");
destroy();
//tail=head;
read();
allprint();break;
case 8:
system("cls");del();break;
case 9:
system("cls");printone();break;
case 0:
destroy();exit(0);break;
default:
system("cls"); getchar();break;
}
}
}
void startmaking(void) //初始化链表
{
head=(student *)malloc(sizeof(student));
head ->next =NULL;
tail =head;
}
void print(student *stu) //输出学生信息
{
printf("\n学号:\t%d\n姓名:\t%s\n性别:\t%s\n年龄:\t%d\n体重:\t%.2f\n",stu->id,stu->name,stu->man ,stu->age ,stu->weight );
}
void allprint(void) //将所有学生信息输出
{
student *one =head->next ;
while(one!=NULL)
{
print(one);
one=one->next;
}
}
/*void insert (void) //插入一条学生信息
{
student *stu = (student *)malloc(sizeof(student));
printf("插入一个学生数据的详细信息:\n");
printf("学号:");
scanf("%d",&(stu->id));
printf("姓名:");
scanf("%s",&(stu->name));
printf("性别:");
scanf("%s",&(stu->man));
printf("年龄:");
scanf("%d",&(stu->age));
printf("体重:");
scanf("%f",&(stu->weight));
tail->next=stu;
stu->next=NULL;
tail=stu;
}*/
void insert (void) //插入一条学生信息
{
char mm[50];
student *stu = (student *)malloc(sizeof(student));
printf("插入一个学生数据的详细信息:\n");
l0: printf("学号:");
if(!scanf("%d",&(stu->id)))
{
gets(mm);
printf("信息有误\n");
goto l0;
}
printf("姓名:");
scanf("%s",&(stu->name));
printf("性别:");
scanf("%s",&(stu->man));
l1: printf("年龄:");
if(!scanf("%d",&(stu->age)))
{
gets(mm);
printf("信息有误\n");
goto l1;
}
l2: printf("体重:");
if(!scanf("%f",&(stu->weight)))
{
gets(mm);
printf("信息有误\n");
goto l2;
}
tail->next=stu;
stu->next=NULL;
tail=stu;
}
void showmenu(void) //菜单的显示
{
printf("\n*****************菜单*****************\n");
printf("1.插入一条学生数据\n");
printf("2.列出所有学生数据\n");
printf("3.更改学生数据详细信息\n");
printf("4.给定年龄学生数量\n");
printf("5.给定体重学生数量\n");
printf("6.存储数据\n");
printf("7.读取并输出数据\n");
printf("8.删除学生数据\n");
printf("9.查找并输出某个学生数据\n");
printf("0.退出程序\n");
printf("***************************************\n");
}
int getchoice(void) //获得用户输入信息
{
int c;
printf("你的选择:");
scanf("%d",&c);
return c;
}
void modify(void) //更改具体学生信息
{
char name[10];
int i=0;
student *stu[10];
printf("请输入你要更改信息的学生的名字:");
scanf("%s",name);
searchByName_buffer(name,stu); //调用该函数记录对应信息的所有地址
if(stu[0]==NULL)
printf("%s 没有找到\n",name);
else
{
while(stu[i]!=NULL)
{
printf("插入这个学生的详细信息:\n");
printf("学号:");
scanf("%d",&(stu[i]->id));
printf("姓名:");
scanf("%s",&(stu[i]->name));
printf("性别:");
scanf("%s",&(stu[i]->man));
printf("年龄:");
scanf("%d",&(stu[i]->age));
printf("体重:");
scanf("%f",&(stu[i]->weight));
i++;
}
printf("操作成功\n");
}
}
student *searchByName( char *name) //原始查找函数
{
student *cur=head->next;
while(cur!=NULL)
{
if(strcmp(cur->name ,name)==0)
break;
cur=cur->next ;
}
return cur;
}
void searchByName0( char *name,student *stu0[]) //记录满足条件的节点的前一个节点地址用以删除节点
{
int i=0;
student *cur=head->next,*cur0=head;
while(cur!=NULL)
{
if(strcmp(cur->name ,name)==0)
{
stu0[i++]=cur0;
cur=cur->next ;
cur0->next =cur;
}
else
{
cur0=cur0->next;
cur=cur->next;
}
}
stu0[i]=NULL;
}
void countWeight(void) //以体重为条件统计人数
{
student *cur = head->next ;
int count =0;
float w;
printf("输入体重:");
scanf("%f",&w);
while(cur!=NULL)
{
if(abs(cur->weight - w)<0.00001)
count++;
cur=cur->next;
}
printf("体重为%.2f的学生数为:%d",w,count);
}
void countAge(void) //以年龄为条件统计人数
{
student *cur = head->next ;
int count =0;
int a;
printf("输入年龄:");
scanf("%d",&a);
while(cur!=NULL)
{
if(cur->age==a)
count++;
cur=cur->next;
}
printf("年龄为%d的学生数为:%d",a,count);
}
void score (void) //将信息存储到文件中去
{
student *stu = head->next ;
FILE *fp;
fp=fopen("date.txt","a+");
fseek(fp,0,2);
while (stu!=NULL)
{
fprintf(fp,"%d\n%s\n%s\n%d\n%.2f\n",stu->id,stu->name,stu->man,stu->age,stu->weight);
stu=stu->next;
}
fclose(fp);
}
void read (void) //从文件读取信息
{
// student *stu=(student *)malloc(sizeof(student));
// head->next =stu;
FILE *fp;
fp=fopen("date.txt","r");
if(fp==NULL)
printf("文件没有找到\n");
else
{
fseek(fp,0,0);
while (!feof(fp))
{
/*fscanf(fp,"%d\n%s\n%s\n%d\n%f\n",&(stu->id),stu->name,stu->man,&(stu->age),&(stu->weight));
tail->next=stu;
stu->next=NULL;
tail=stu;
student *stu=(student *)malloc(sizeof(student));*/
fprint(fp); //调用该函数完成功能
}
fclose(fp);
}
}
void readout(void) //读信息到屏幕
{
FILE *fp;
char c;
fp=fopen("date.txt","r");
fseek(fp,0,0);
while((c=fgetc(fp))!=EOF)
putchar(c);
fclose(fp);
}
void del(void) //删除所有满足条件的信息
{
char name[10];
int i=0;
student *stu[10],*stu0[10];
printf("请输入你要删除同学的姓名:");
scanf("%s",name);
searchByName_buffer(name,stu); //调用改函数记录满足条件的节点
searchByName0(name,stu0); //调用改函数记录满足条件的前一节点
if(stu[0]==NULL)
printf("%s 没有找到\n",name);
else
{
while(stu0[i]!=NULL)
{
stu0[i]->next=stu[i]->next;
free(stu[i]);
i++;
}
if(stu0[0]->next==NULL)
tail=stu0[0];
else if(stu0[i-1]->next==NULL)
tail=stu0[i-1];
printf("操作成功\n");
}
}
void printone(void) //输出满足条件的所有学生信息
{
char name[10];
int i=0;
student *stu[10];
printf("请输入该学生的姓名:");
scanf("%s",name);
searchByName_buffer(name,stu);
if(stu[0]==NULL)
printf("%s 没有找到\n",name);
else
while(stu[i]!=NULL)
print(stu[i++]);
}
void destroy(void) //将链表所占内存释放
{
//student *one =head->next ;
while(head->next !=NULL)
{
// head->next=one->next;
// free(one);
des(head); //调用改函数
}
//head->next=NULL;
tail=head;
// free(head);
}
void fprint(FILE *fp) //申请空间从文件读信息
{
student *stu=(student *)malloc(sizeof(student));
fscanf(fp,"%d\n%s\n%s\n%d\n%f\n",&(stu->id),stu->name,stu->man,&(stu->age),&(stu->weight));
tail->next=stu;
stu->next=NULL;
tail=stu;
}
void des(student *one) //释放一个节点内存
{
student *mid=one->next;
one->next=mid->next;
free(mid);
}
void searchByName_buffer( char *name,student *stu[]) //记录所有满足条件的节点
{
int i=0;
student *cur=head->next;
while(cur!=NULL)
{
if(strcmp(cur->name ,name)==0)
stu[i++]=cur;
cur=cur->next ;
}
stu[i]=NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -