功能
1.创建链表
2.查找
3.遍历
4.删除
5.修改
6.保存和文件读取
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NMBSCORE 3
struct StNode
{
int num; //学号
char name[20]; //姓名
char sex; //性别
int age; //年龄
int grade; //班级
int score[NMBSCORE]; //成绩
StNode* next;
};
//读取文件
struct StNode* ReadFile(const char* filename,struct StNode* head)
{
struct StNode* node,*tmp;
FILE* fp;
int i;
if ((fp = fopen(filename,"r")) == 0)
{
printf("文件打开失败\n");
return 0;
}
while(!feof(fp))
{
node = (struct StNode*)malloc(sizeof(struct StNode));
fscanf(fp,"%d%20s %c%4d%4d",&node->num,node->name,&node->sex,&node->age,&node->grade);
for(i=0;i<NMBSCORE;i++)
fscanf(fp,"%4d",&node->score[i]);
fscanf(fp,"\n");
node->next = 0;
if (head == 0)
{
head = node;
tmp = head;
}else
{
tmp->next = node;
tmp = node;
}
}
fclose(fp);
return head;
}
//录入信息
struct StNode* Input(struct StNode* head)
{
struct StNode* p,*node;
int i;
system("cls");
node = (struct StNode*)malloc(sizeof(struct StNode));
//录入信息
printf("请输入学号:");
scanf("%d",&node->num);
printf("请输入姓名:");
scanf("%s",node->name);
printf("请输入性别:");
scanf(" %c",&node->sex);
printf("请输入年龄:");
scanf(" %d",&node->age);
printf("请输入班级:");
scanf(" %d",&node->grade);
printf("请输入%d门课程的成绩:",NMBSCORE);
for(i=0;i<NMBSCORE;i++)
scanf("%d",&node->score[i]);
node->next = 0;
if (head == 0)
{
head = node;
//
}else
{
p = head;
while(p->next)
p = p->next;
//新建节点
p->next = node;
}
system("pause");
return head;
}
//显示所有学生信息
void showAll(struct StNode* head)
{
struct StNode* p;
int i=1,j;
system("cls");
p = head;
while(p)
{
printf("学生%d学号:%d\n",i,p->num);
printf("学生%d姓名:%s\n",i,p->name);
printf("学生%d性别:%c\n",i,p->sex);
printf("学生%d年龄:%d\n",i,p->age);
printf("学生%d班级:%d\n",i,p->grade);
printf("学生%d的成绩:",i);
for(j=0;j<NMBSCORE;j++)
printf("%d ",p->score[j]);
printf("\n");
i++;
p = p->next;
}
system("pause");
}
//查询信息
void Find(struct StNode* head)
{
int opt;
struct StNode* p = head;
int id,j;
char name[20]={0};
system("cls");
printf("1.按学号查询\n");
printf("2.按姓名查询\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("请输入学号:");
scanf("%d",&id);
while(p)
{
if(p->num == id)
{
printf("学生学号:%d\n",p->num);
printf("学生姓名:%s\n",p->name);
printf("学生性别:%c\n",p->sex);
printf("学生年龄:%d\n",p->age);
printf("学生班级:%d\n",p->grade);
printf("学生成绩:");
for(j=0;j<NMBSCORE;j++)
printf("%d ",p->score[j]);
printf("\n");
break;
}
p = p->next;
}
if(p==0)
printf("查无此人\n");
break;
case 2:
printf("请输入姓名:");
scanf("%s",name);
while(p)
{
if(strcmp(p->name,name) ==0)
{
printf("学生学号:%d\n",p->num);
printf("学生姓名:%s\n",p->name);
printf("学生性别:%c\n",p->sex);
printf("学生年龄:%d\n",p->age);
printf("学生班级:%d\n",p->grade);
printf("学生成绩:");
for(j=0;j<NMBSCORE;j++)
printf("%d ",p->score[j]);
printf("\n");
break;
}
p = p->next;
}
if(p==0)
printf("查无此人\n");
break;
}
system("pause");
}
//修改学生信息
void ModInfo(struct StNode* head)
{
struct StNode* node = 0;
int id,i;
system("cls");
printf("请输入需要修改信息的学生学号:");
scanf("%d",&id);
node = head;
while(node)
{
if(node->num == id)
break;
else
node = node->next;
}
if(node == 0)
printf("查无此人\n");
else
{
memset(node->name,0,20);
printf("请输入姓名:");
scanf("%s",node->name);
printf("请输入性别:");
scanf(" %c",&node->sex);
printf("请输入年龄:");
scanf(" %d",&node->age);
printf("请输入班级:");
scanf(" %d",&node->grade);
printf("请输入%d门课程的成绩:",NMBSCORE);
for(i=0;i<NMBSCORE;i++)
scanf("%d",&node->score[i]);
}
system("pause");
}
//删除学生信息
void DeleteInfo(struct StNode* head)
{
struct StNode* node,*pre;
int id,i;
system("cls");
printf("请输入需要删除信息的学生学号:");
scanf("%d",&id);
if (head->num == id)
{
node = head->next;
free(head);
head = node;
printf("该学生信息已经删除\n");
system("cls");
return;
}else
{
pre = head;
node = head->next;
while(node)
{
if (node->num == id)
{
pre->next = node->next;
free(node);
node = 0;
printf("该学生信息已经删除\n");
system("pause");
return;
}else
{
pre = node;
node = node->next;
}
}
printf("查无此人\n");
system("pause");
}
}
//学生信息统计
void Tongji(struct StNode* head)
{
struct StNode* node;
int opt = 0;
int count = 0;
char ch;
int grade;
system("cls");
printf("1.按照性别统计\n");
printf("2.按照年级统计\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("请输入性别:");
scanf(" %c",&ch);
node = head;
count = 0;
while(node)
{
if(node->sex == ch)
count++;
node = node->next;
}
printf("数量:%d\n",count);
break;
case 2:
printf("请输入年级:");
scanf("%d",&grade);
node = head;
count = 0;
while(node)
{
if(node->grade == grade)
count++;
node = node->next;
}
printf("数量:%d\n",count);
break;
}
system("pause");
}
//学生信息保存
void Save(const char* name,struct StNode* head)
{
struct StNode* node;
FILE* fp;
int i;
system("cls");
if ((fp = fopen(name,"w")) == 0)
{
printf("文件打开失败\n");
system("pause");
return;
}
node = head;
while(node)
{
fprintf(fp,"%d%20s %c%4d%4d",node->num,node->name,node->sex,node->age,node->grade);
for(i=0;i<NMBSCORE;i++)
fprintf(fp,"%4d",node->score[i]);
fprintf(fp,"\n");
node = node->next;
}
fclose(fp);
printf("文件保存成功!\n");
system("pause");
}
int main()
{
struct StNode* head = 0;
int opt = 0;
int bgo = 1;
char* filename = "students.txt";
head = ReadFile(filename,head); //读取文件
while(bgo)
{
system("cls");
printf("--------------学生信息管理系统---------------|\n");
printf("| 1.录入学生信息 |\n");
printf("| 2.显示所有学生信息 |\n");
printf("| 3.学生信息查询 |\n");
printf("| 4.学生信息修改 |\n");
printf("| 5.学生信息删除 |\n");
printf("| 6.学生信息统计 |\n");
printf("| 7.学生信息保存 |\n");
printf("| 0.退出系统 |\n");
printf("----------------------------------------------\n");
scanf("%d",&opt);
switch(opt)
{
case 0:
bgo = 0;
break;
case 1:
head = Input(head);
break;
case 2:
showAll(head);
break;
case 3:
Find(head);
break;
case 4:
ModInfo(head);
break;
case 5:
DeleteInfo(head);
break;
case 6:
Tongji(head);
break;
case 7:
Save(filename,head);
break;
}
}
return 0;
}