C语言学生管理系统(链表以及文件操作)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


struct Student 
{
char m_strName[100];
char m_strSex[10];
char m_strNumber[100];
double m_fEnglish;
double m_fMath;
double m_fComputer;
struct Student *next;
};


void GetInformation(struct Student *human);
void AddInformation(struct Student **head);
void PrintfInformation(struct Student *human);
void Choose(struct Student *head);
void AddResuls(struct Student **head);
void GetResuls(struct Student *human);
void PrintfResuls(struct Student *human);
void SerchInformation(struct Student *head);
void SerchResuls(struct Student **head);
void Many(struct Student *head);
void Change(struct Student **head);
void ChangeHuman(struct Student *human);
void ChangeMenu();
void Delete(struct Student **head);
void Sort(struct Student **head,struct Student *hum);
int Compelete(struct Student *head,struct Student *human);
void Statistic(struct Student *head );
void ReadData (struct Student **head);
void WriteData (struct Student *head);
void Release(struct Student **head);
void AddData(struct Student *head);


int main()
{
struct Student *head=NULL;

Choose(head);

return 0;
}


void MainMenu()
{
printf("\t**********欢迎来到学生管理系统************\n");
printf("\t\t录入学生信息****************1\n");
printf("\t\t查询学生信息****************2\n");
printf("\t\t浏览学生成绩****************3\n");
printf("\t\t浏览全部学生情况************4\n");
printf("\t\t修改学生信息****************5\n");
printf("\t\t删除学生信息****************6\n");
printf("\t\t统计两科以上不及格人数******7\n");
printf("\t\t退出系统********************0\n");
}


void Choose(struct Student *head)
{
int input;

ReadData (&head);

while (1)
{
MainMenu();

printf("请输入数字:");
scanf("%d",&input);

switch(input)
{
case 0:
WriteData (head);
Release(&head);
exit(0);
case 1:
AddInformation(&head);
break;
case 2:
SerchInformation(head);
break;
case 3:
SerchResuls(&head);
break;
case 4:
Many(head);
break;
case 5:
Change(&head);
break;
case 6:
Delete(&head);
break;
case 7:
Statistic(head );
break;
default:
printf ("\n无效选项!\n");
break;
}
}
}


void AddInformation(struct Student **head)
{
struct Student *human;
struct Student *temp=NULL;//中间变量

human = (struct Student *)malloc(sizeof(struct Student));

if (human == NULL)
{
printf("内存分配错误");
exit(1);
}

GetInformation(human);

if (Compelete(*head,human) == 0)
{
printf("该同学已录入信息,请重新输入!\n");
}
else
{
system("cls");
PrintfInformation(human);

if (*head ==NULL)
{
*head = human;
human->next = NULL;
}
else 
{
temp = *head;


while(temp->next!=NULL)
{
temp = temp->next;
}


temp->next = human;
human->next = NULL;
}
}
}


int Compelete(struct Student *head,struct Student *human)
{
struct Student *temp;
int z;

temp=head;

while (temp!=NULL&&(z=strcmp(temp->m_strNumber,human->m_strNumber)!=0))
{
temp = temp->next;
}

return z;
}


void Release(struct Student **head)
{
struct Student *p;
struct Student *q;
while ((*head)!=NULL)
{
p = *head;
q=NULL;
for (; p!=NULL; q=p,p=p->next)
{
free(q);
}
*head = NULL;
}
}


void GetInformation(struct Student *human)
{
printf("请输入学生姓名:");
scanf("%s",human->m_strName);
printf("请输入学生性别:");
scanf("%s",human->m_strSex);
printf("请输入学生学号:");
scanf("%s",human->m_strNumber);
printf("请输入学生英语成绩:");
scanf("%lf",&human->m_fEnglish);
printf("请输入学生数学成绩:");
scanf("%lf",&human->m_fMath);
printf("请输入学生计算机成绩:");
scanf("%lf",&human->m_fComputer);
}


void PrintfInformation(struct Student *human)
{
printf("姓名:%s\n",human->m_strName);
printf("性别:%s\n",human->m_strSex);
printf("学号:%s\n",human->m_strNumber);
printf("英语成绩:%lf\n",human->m_fEnglish );
printf("数学成绩:%lf\n",human->m_fMath );
printf("计算机成绩:%lf\n",human->m_fComputer );
}


void PrintfResuls(struct Student *human)
{
printf("姓名:%s\n",human->m_strName );
printf("英语成绩:%lf\n",human->m_fEnglish );
printf("数学成绩:%lf\n",human->m_fMath );
printf("计算机成绩:%lf\n",human->m_fComputer );
}


void SerchInformation(struct Student *head)
{
struct Student *human;
struct Student *hum;

hum = (struct Student *)malloc(sizeof(struct Student));
human = head;

printf("请输入学生学号:");
scanf("%s",hum->m_strNumber);

while (human!=NULL)
{
if (strcmp(human->m_strNumber,hum->m_strNumber)==0)
{
PrintfInformation(human);
return;
}

human = human -> next;
}

printf("该学生信息不存在,请重新查找\n");
}


void SerchResuls(struct Student **head)
{
struct Student *human;
struct Student *hum;

hum = (struct Student *)malloc(sizeof(struct Student));
human = *head;

printf("请输入要查找的学生学号:");
scanf("%s",hum->m_strNumber);

while (human!=NULL)
{
if (strcmp(human->m_strNumber, hum->m_strNumber)==0)
{
PrintfResuls(human);
Sort(head,human);

return;
}

human = human->next;
}

printf("该学生信息不存在,请先进行录入!\n");
}


void Many(struct Student *head)
{
struct Student *human;
int total = 0;

human = head;

while (human!=NULL)
{
PrintfInformation(human);

human = human->next;

total++;
}

printf("学生人数%d人",total);
}


void Change(struct Student **head)
{
struct Student *human;
struct Student *hum;

hum = (struct Student *)malloc(sizeof(struct Student));
human = *head;

printf("请输入要修改学生的学号:");
scanf("%s",hum->m_strNumber);

while (human!=NULL)
{
if (strcmp(human->m_strNumber,hum->m_strNumber)==0)
{
ChangeHuman(human);

return;
}


human = human->next;
}
printf("该学生信息不存在,请重新查找\n");
}


void ChangeHuman(struct Student *human)
{
ChangeMenu();

int input;

printf("请输入选项:");
scanf("%d",&input);

switch(input)
{
case 1:
printf("请输入学生姓名:");
scanf("%s",human->m_strName);
break;
case 2:
printf("请输入学生性别:");
scanf("%s",human->m_strSex);
break;
case 3:
printf("请输入学生学号:");
scanf("%s",human->m_strNumber);
break;
case 4:
printf("请输入学生英语成绩:");
scanf("%lf",&human->m_fEnglish);
break;
case 5:
printf("请输入学生数学成绩:");
scanf("%lf",&human->m_fMath);
break;
case 6:
printf("请输入学生计算机成绩:");
scanf("%lf",&human->m_fComputer);
break;
}
}


void ChangeMenu()
{
printf("修改学生姓名----------1\n");
printf("修改学生性别----------2\n");
printf("修改学生学号----------3\n");
printf("修改学生英语成绩------4\n");
printf("修改学生数学成绩------5\n");
printf("修改学生计算机成绩----6\n");
}


void Delete(struct Student **head)
{
struct Student *human;
struct Student *hum;
struct Student *temp;

temp = NULL;
human = *head;
hum = (struct Student *)malloc(sizeof(struct Student));

printf("请输入要删除的学生学号:");
scanf("%s",hum->m_strNumber);

if(Compelete(*head,hum)==0)
{
for (; strcmp(human->m_strNumber,hum->m_strNumber)!=0; human=human->next)
{
temp=human;
}
if (temp==NULL)
{
*head=human->next;
}
else 
{
temp->next=human->next;
}

free(human);
temp=NULL;
}
else 
{
printf("不存在该学生,请先进行录入\n");
}
}


void Sort(struct Student **head,struct Student *hum)
{

struct Student *endpt;    
struct Student *p;        
struct Student *p1,*p2;
int count1 = 1;//这三个整形变量为计数器,计算学生的成绩排名
int count2 = 1;
int count3 = 1;

p1 = (struct Student *) malloc (sizeof(struct Student));
p1->next = *head;          
*head = p1;                  

    for (endpt = NULL; endpt != *head; endpt = p)   
    {
for (p = p1 = *head; p1->next->next != NULL; p1 = p1->next)
{
if (p1->next->m_fEnglish < p1->next->next->m_fEnglish)   
{
p2 = p1->next->next;     
p1->next->next = p2->next;     
p2->next = p1->next;   
p1->next = p2;     
p = p1->next->next;   
}

    }

for (p = *head; strcmp(p->m_strNumber,hum->m_strNumber) !=0; p = p->next)
{
count1++;
}


printf("英语成绩排名:%d\n",count1-1);

for (endpt = NULL; endpt != *head; endpt = p)   
    {
for (p = p1 = *head; p1->next->next != NULL; p1 = p1->next)
{
if (p1->next->m_fMath < p1->next->next->m_fMath)   
{
p2 = p1->next->next;     
p1->next->next = p2->next;     
p2->next = p1->next;   
p1->next = p2;     
p = p1->next->next;   
}
}
    }

for (p = *head; strcmp(p->m_strNumber,hum->m_strNumber) !=0; p = p->next)
{
count2++;
}


printf("数学成绩排名:%d\n",count2-1);

for (endpt = NULL; endpt != *head; endpt = p)   
    {
for (p = p1 = *head; p1->next->next != NULL; p1 = p1->next)
{
if (p1->next->m_fEnglish < p1->next->next->m_fEnglish)   
{
p2 = p1->next->next;     
p1->next->next = p2->next;     
p2->next = p1->next;   
p1->next = p2;     
p = p1->next->next;   
}
}
    }


p1 = *head;              
*head = p1->next;    
free (p1);          
p1 = NULL;  

for (p = *head; strcmp(p->m_strNumber,hum->m_strNumber) !=0; p = p->next)
{
count3++;
}


printf("计算机成绩排名:%d\n",count3);
}


void Statistic(struct Student *head )
{
struct Student *human;
human = head;
int a = 0;

for (; human!=NULL; human=human->next)
{
if ((human->m_fEnglish<60 &&human->m_fMath<60)||(human->m_fEnglish<60&&human->m_fComputer<60)||(human->m_fComputer<60&&human->m_fMath))
{
PrintfInformation(human);
a++;


}
}
printf("共有%d人不合格",a);
}


void ReadData(struct Student **head)
{
FILE *fp;
char c;
struct Student *human;
struct Student *temp;

char ads[45];


if ((fp=fopen("D:\\1.txt","r"))==NULL)
{
printf("打开文件失败,请建立文件");
}

fseek(fp,45L,SEEK_SET); //将文字读取掉
c = fgetc(fp);


if (c==EOF) //判断文件是否为空
{
return ;
}
else
{
fseek(fp,45L,SEEK_SET);      //重新将光标定位到文件头部

while (!feof(fp))
{


human = (struct Student *)malloc(sizeof(struct Student));
if (human == NULL)
{
printf("内存分配错误");
exit(1);
}

if (*head == NULL)
{
*head = human;
human->next = NULL;
}
else 
{
temp = *head;

while(temp->next!=NULL)
{
temp = temp->next;
}

temp->next = human;
human->next = NULL;
}


fscanf(fp,"%s", human->m_strName);
fscanf(fp,"%s",human->m_strSex);
fscanf(fp,"%s",human->m_strNumber);
fscanf(fp,"%lf",&human->m_fEnglish);
fscanf(fp,"%lf",&human->m_fMath);
fscanf(fp,"%lf",&human->m_fComputer);
}
}
fclose(fp);
}


void WriteData (struct Student *head)
{
FILE *fp;
struct Student *human;
struct Student *temp;

human = head;

if ((fp=fopen("D:\\1.txt","w"))==NULL)
{
printf("打开文件失败,请建立文件");
}


fprintf(fp,"姓名\t性别\t学号\t英语成绩\t数学成绩\t计算机成绩");

while(human!=NULL)
{

fprintf(fp,"\n%s\t%s\t%s\t%lf\t%lf\t%lf",human->m_strName,human->m_strSex, human->m_strNumber,human->m_fEnglish, human->m_fMath, human->m_fComputer);
human = human->next;
}


fclose(fp);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值