学生管理系统c语言源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "stdio.h"
#include "string.h"
#define M 3
#define error(FOUND) printf("内存不足,在函数 "FOUND"\n")
typedef struct Student
{
char *name;
char *number;
float score[M];
float avg;
float sum;
}* studentPint,studentNod;
typedef struct STULink
{
studentPint student;
struct STULink *next;
}* studentLinkPoint,stuLink;
studentLinkPoint head;//表头指针
int flag;//判断是否求和
studentLinkPoint end;//表尾指针
void initLinkTable(studentLinkPoint *p);//初始化表头
studentLinkPoint insert(studentLinkPoint,studentLinkPoint *);//插入一个节点
studentLinkPoint input();//输入一个节点
void menu();//菜单
void del(studentLinkPoint,studentLinkPoint);//回收所有用malloc分配的内存
void display(studentLinkPoint );//显示所有学生信息
int sum(studentLinkPoint);//求和
void avg(studentLinkPoint);//求平均分
void disAvgSum(studentLinkPoint);//显示总分和平均分
char *inputNumber();//专门用来输入一个学生的学号,返回该字符串的指针
void delstudent(studentLinkPoint,char *);//删除一个学生
void change(studentLinkPoint,char *);//修改一个学生
void search(studentLinkPoint,char *);//查找一个学生
void paixu(studentLinkPoint head);//排序姓名
void writeStu(studentLinkPoint);//写入文件
int readStu();//读入文件
void create();//检查文件
/************************************************************************/
int main()
{
initLinkTable(&head);//初始化表头
readStu();//读入源文件
while (1)
{
menu();
system("cls");
}
}
void initLinkTable(studentLinkPoint *p)
{
(*p)=(studentLinkPoint)malloc(sizeof(stuLink));
(*p)->next=0;
(*p)->student=0;
end=(*p);
flag=0;
}
studentLinkPoint insert(studentLinkPoint temp,studentLinkPoint *end)
{
if((*end)->next==0)
{
(*end)->next=temp;
(*end)=temp;
flag=0;
return head;
}
else
{
return 0;
}
}
studentLinkPoint input()
{
studentLinkPoint temp=(studentLinkPoint)malloc(sizeof(stuLink));
char name[20];
char number[18];
int i;
if(temp==0)
{
error("input");
return 0;
}
else
{
temp->student=(studentPint)malloc(sizeof(studentNod));
if(temp->student==0)
{
error("input");
return 0;
}
}
printf("\n输入学生的姓名:");
scanf("%s",name);
temp->student->name=(char *)malloc(sizeof(char)*(strlen(name)+1));
strcpy(temp->student->name,name);
printf("\n输入学生的学号:");
scanf("%s",number);
temp->student->number=(char *)malloc(sizeof(char)*(strlen(number)+1));
strcpy(temp->student->number,number);
printf("\n输入%d门课程的成绩:\n",M);
for(i=0;i
scanf("%f",&(temp->student->score[i]));
}
system("cls");
printf("你输入的信息如下:\n");
printf("姓名:%-15s学号:%-15s%d门课程分数: ",temp->student->name,temp->student->number,M);
for(i=0;i
printf("\n");
t
emp->next=0;
return temp;
}
void menu()
{
static int c;
system("color 2f");
printf("\n%-15c1、输入学生信息. ",' ');
printf("%-5c5、修改学生信息.\n\n",' ');
printf("%-15c2、显示每个学生信息. ",' ');
printf("%-5c6、查找学生信息.\n\n",' ');
printf("%-15c3、显示每个学生总分和平均分. ",' ');
printf("%-5c7、按姓名排序.\n\n",' ');
printf("%-15c4、删除学生信息. ",' ');
printf("%-5c8、写入文件.\n\n",' ');
printf("%-15c0、退出.\n",' ');
printf("\n\n选择你的操作:");
scanf("%d",&c);
switch (c)
{
case 1:
insert(input(),&end);
system("pause");
break;
case 2:
display(head);
system("pause");
break;
case 3:
disAvgSum(head);
system("pause");
break;
case 4:
delstudent(head,inputNumber());
system("pause");
break;
case 5:
change(head,inputNumber());
system("pause");
break;
case 6:
search(head,inputNumber());
system("pause");
break;
case 7:
paixu(head);
system("pause");
break;
case 8:
writeStu(head);
system("pause");
break;
case 0:
del(head,end);
break;
}
if(c==0)
exit(1);
}
void del(studentLinkPoint p,studentLinkPoint End)
{
studentLinkPoint q;
if(end==head)
{
free(head);
head=end=0;
}
else
{
q=p->next;
while (q->next)
{
p=q;
q=q->next;
free(p->student->name);
free(p->student->number);
free(p->student);
free(p);
}
free(q->student->name);
free(q->student->number);
free(q->student);
free(q);
end=0;
free(head);
}
}
void display(studentLinkPoint head)
{
studentLinkPoint p=head->next;
int i=0;
printf("\n姓名 学号 %d门课程成绩 总分 平均分\n",M);
for(;p!=0;p=p->next)
{
printf("%-10s%-10s ",p->student->name,p->student->number);
while(i
printf("%4.1f ",p->student->score[i]);
i++;
}
i=0;
if(flag)
printf(" %3.1f %3.1f\n",p->student->sum,p->student->avg);
printf("\n");
}
}
int sum(studentLinkPoint p)
{
studentLinkPoint temp;
int i;
if(p->next!=0)
{
temp=p->next;
}
else
{
printf("没有数据\n");
return 0;
}
for (temp->student->sum=0;temp!=0;temp=temp->next)
{
temp->student->sum=0;
for (i=0;i
temp->student->sum+=temp->student->score[i];
}
}
flag=1;
return 1;
}
void avg(studentLinkPoint head)
{
studentLinkPoint temp;
if(!flag)
{
sum(head);
}
if(flag)
{
temp=head->next;
for (;temp!=0;temp=temp->next)
{
temp->student->avg=temp->student->sum/M;
}
}
}
void disAvgSum(studentLinkPoint head)
{
studentLinkPoint temp=head->next;
avg(head);
if(flag)
{
printf(" 姓名 学号 总分 平均分\n");
for (;temp!=0;temp=temp->next)
{
printf("%-10s%-10s %5.1f %.1f\n",temp->student->name,temp->student->number,temp->student-
>sum,temp->student->avg);
}
}
}
char *inputNumber()
{
static char number[18];
printf("输入该学生的学号:");
scanf("%s",number);
return number;
}
void delstudent(studentLinkPoint head,char *number)
{
studentLinkPoint temp=head->next;
studentLinkPoint swap=0;
printf("--------- ");
if(temp!=0&&strcmp(number,temp->student->number)==0)
{printf("--------- ");//如果是第一条记录
swap=temp;
head->next=temp->next;
if(head->next==0)
end=head;
}
else if(temp!=0)
{
for(;temp->next!=0;temp=temp->next)
{
if(strcmp(number,temp->next->student->number)==0)
{
swap=temp->next;
temp->next=temp->next->next;
if (temp->next==0)
{/*如果是最后一条记录*/
end=temp;
printf("--------");
}
}
break;
}
}
if(swap==0)
printf("\n没有找到学号\"%s\"\n",number);
else
{
printf("------- ");
free(swap);
printf("------- ");
flag=0;
printf("\n删除成功!\n");
}
}
void change(studentLinkPoint head,char *number)
{
studentLinkPoint temp=head->next;
studentLinkPoint temp0=0;
char name[20];
char number0[18];
float score;
int i;
if(temp!=0&&strcmp(number,temp->student->number)==0)
{
temp0=temp;
}
else if(temp!=0)
{
for(;temp->next!=0;temp=temp->next)
{
if(strcmp(number,temp->next->student->number)==0)
{
temp0=temp->next;
break;
}
}
}
if(temp0==0)
printf("\n没有找到%s\n",number);
else
{
printf("姓名:%-15s学号:%-15s%d门课程分数: ",temp0->student->name,temp0->student->number,M);
for(i=0;i
printf("\n");
printf("\n输入学生的姓名 (保持请输入 n):");
scanf("%s",name);
if(strcmp(name,"n")!=0)
strcpy(temp0->student->name,name);
printf("\n输入学生的学号 (保持请输入 n):");
scanf("%s",number0);
if(strcmp(number0,"n")!=0)
strcpy(temp0->student->number,number0);
printf("\n输入%d门课程的成绩( 保持请输入 -1):",M);
for(i=0;i
scanf("%f",&score);
if(score!=-1)
temp0->student->score[i]=score;
}
system("cls");
printf("你输入的信息如下:\n");
printf("姓名:%-15s学号:%-15s%d门课程分数: ",temp0->student->name,temp0->student->number,M);
for(i=0;i
printf("\n");
flag=0;
printf("\n修改成功!\n");
}
}
void search(studentLinkPoint head,char *number)
{
studentLinkPoint temp=head->next;
studentLinkPoint temp0=0;
char name[20];
char number0[18];
float score;
int i;
if(temp!=0&&strcmp(number,temp->student->number)==0)
{
temp0=temp;
}
else if(temp!=0)
{
for(;temp->next!=0;temp=temp->next)
{
if(strcmp(number,temp->next->student->number)==0)
{
temp0=temp->next;
break;
}
}
}
if(temp0==0)
printf("\n没有找到%s\n",number);
else
{
printf("\n姓名:%-15s
学号:%-15s%d门课程分数: ",temp0->student->name,temp0->student->number,M);
for(i=0;i
printf("\n\n");
}
}
void paixu(studentLinkPoint head)
{
int count=0;
studentLinkPoint *a;
studentLinkPoint temp=head->next;
int i,j;
int loop=1;
for (;temp!=0;temp=temp->next)
{
count++;
}
a=(studentLinkPoint *)malloc(sizeof(studentLinkPoint)*count);
for(i=0,temp=head->next;i
a[i]=temp;
temp=temp->next;
}
for(i=1;i
break;
else
loop=0;
for (j=0;j
if (strcmp(a[j+1]->student->name,a[j]->student->name)==-1)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
loop=1;
}
}
}
printf("\n姓名 学号 %d门课程成绩 总分 平均分\n",M);
for(j=0,i=0;i
printf("%-10s%-10s ",a[i]->student->name,a[i]->student->number);
while(j
printf("%.1f ",a[i]->student->score[j]);
j++;
}
j=0;
if(flag)
{
printf(" %3.1f %3.1f\n",a[i]->student->sum,a[i]->student->avg);
}
printf("\n");
}
}
void writeStu(studentLinkPoint p)
{
FILE *fp1=fopen("Student.txt","w+");
FILE *fp2=fopen("temp.dat","w+");
int i,count=0;
studentLinkPoint temp=p->next;
for(;temp!=0;temp=temp->next)
{
fprintf(fp1,"%-20s%-20s",temp->student->name,temp->student->number);
for(i=0;i
fprintf(fp1,"%-8.1f",temp->student->score[i]);
}
//fprintf(fp1,"\n");
count++;
}
fprintf(fp2,"%3d",count);
fclose(fp1);
fclose(fp2);
}
int readStu()
{
FILE *fp1;
FILE *fp2;
int i;
int count;
studentLinkPoint temp;
char name[20];
char number[18];
create();
fp1=fopen("temp.dat","r");
fp2=fopen("Student.txt","r");
fscanf(fp1,"%3d",&count);
while(count)
{
temp=(studentLinkPoint)malloc(sizeof(stuLink));
if(temp==0)
{
error("readStu");
return 0;
}
else
{
temp->student=(studentPint)malloc(sizeof(studentNod));
if(temp->student==0)
{
error("readStu");
return 0;
}
}
fscanf(fp2,"%s%s",name,number);
temp->student->name=(char *)malloc(sizeof(char)*(strlen(name)+1));
strcpy(temp->student->name,name);
temp->student->number=(char *)malloc(sizeof(char)*(strlen(number)+1));
strcpy(temp->student->number,number);
temp->next=0;
for (i=0;i
fscanf(fp2,"%f",&temp->student->score[i]);
}
insert(temp,&end);
count--;
}
fclose(fp1);
fclose(fp2);
return 1;
}
void create()
{
FILE *fp=fopen("Student.txt","r");
int count=0;
if(fp==0)
{
fp=fopen("Student.txt","w+");
fclose(fp);
fp=fopen("temp.dat","w+");
fprintf(fp,"%3d",count);
fclose(fp);
}
else
{
fclose(fp);
}
}