题目:学生成绩管理系统(C语言)

2024-11-23 10:24:47
推荐回答(1个)
回答1:

#include
#include
#include
#include

#define max 20

typedef struct student //学生
{
char sno[max]; // 学号
char sname[max]; //姓名
char sex[max]; //性别
char age[max]; //年龄
char depart[max]; //系
char classs[max]; //班
char grade[max]; //年级

struct student* next;
} student;

student* head;

int LogOn() //登录模块,已实现输入密码不回显,如果中途发现输错某几位,可退格键重输
{
char username[max],password[max];
printf("\n请输入用户名:");
scanf("%s",username);
printf("\n请输入密码(最多15位):");

//开始以不回显且支持退格方式获取输入密码
int i=0;
while((i>=0)&&(password[i++]=getch())!=13)//条件i>=0是用于限制退格的范围
{
if(password[i-1]=='\b')//对退格键的处理
{
printf("%c%c%c",'\b','\0','\b');
i=i-2;
}
else
printf("*");
}
password[--i]='\0';

//已获取密码。验证用户身份
if(!strcmp(username,"zhang")&&!strcmp(password,"8147086"))
{
printf("\n登录成功!");
return 1;
}
else
return 0;

}

void regist()
{
char ch;
student *s,*ptr; //s用来建新结点,ptr用来暂存头结点

do
{

s=(student*)malloc(sizeof(student)); // 新建一个学生结点

printf("\n开始注册..."); //开始注册
printf("\n请输入该学生的学号:");
scanf("%s",s->sno);
printf("\n请输入该学生的姓名:");
scanf("%s",s->sname);
printf("\n请输入该学生的性别:");
scanf("%s",s->sex);
printf("\n请输入该学生的年龄:");
scanf("%s",s->age);
printf("\n请输入该学生的系:");
scanf("%s",s->depart);
printf("\n请输入该学生所在的班:");
scanf("%s",s->classs);
printf("\n请输入该学生所在的年级");
scanf("%s",s->grade);

ptr=head;
head=s;//将新结点插入队头
s->next=ptr;

fflush(stdin);
printf("\n请问是否继续注册?(Y/N)");
scanf("%c",&ch);
}while(ch=='Y'||ch=='y');

return;
}

void ElePrint(char str[]) //输出单个元素
{
if(str==NULL) exit(0);
printf("%s",str);
for(unsigned int i=0;i<12-strlen(str);i++) printf(" ");//为了对齐输出,需插入一些空格
return;
}

int LinePrint(student *ptr) //输出一行
{
if(ptr==NULL) //检查传进来的指针
return 0;

printf("\n");
ElePrint(ptr->sno);
ElePrint(ptr->sname);
ElePrint(ptr->age);
ElePrint(ptr->sex);
ElePrint(ptr->depart);
ElePrint(ptr->classs);
ElePrint(ptr->grade);

return 1;
}

void print() //输出全部学生信息
{
student *ptr=head;
printf("\n学号 姓名 年龄 性别 系 班 年级 ");
while(ptr)
{
LinePrint(ptr);
ptr=ptr->next;
}
printf("\n");
return;
}
void search()//查询模块
{
int method;//查询方式
char no[max],name[max],departm[max],clss[max],grades[max]; //用来接收查询关键字
while(1)
{
printf("\n请选择查询方式");
printf("\n1.按学号查询");
printf("\n2.按姓名查询");
printf("\n3.按所在系查询");
printf("\n4.按所在班级查询");
printf("\n5.按所在年级查询");
printf("\n6.打印全部学生信息");
printf("\n7.返回主菜单\n");

scanf("%d",&method);

student *p=head,*temp;

switch(method)
{
case 1:
printf("\n请输入要查询的学号:");
scanf("%s",no);
while(p)
{
if(!strcmp(p->sno,no))
break;
else
{
temp=p;
p=p->next;
}
}
printf("\n学号 姓名 年龄 性别 系 班 年级 ");
LinePrint(p);
break;
case 2:
printf("\n请输入要查询的姓名:");
scanf("%s",name);
printf("\n学号 姓名 年龄 性别 系 班 年级 ");
while(p)
{
if(!strcmp(p->sname,name))
LinePrint(p);
p=p->next;
}
break;
case 3:
printf("\n请输入学生所在的系:");
scanf("%s",departm);
printf("\n学号 姓名 年龄 性别 系 班 年级 ");
while(p)
{
if(!strcmp(p->depart,departm))
LinePrint(p);
p=p->next;
}
break;
case 4:
printf("\n请输入学生所在的班:");
scanf("%s",clss);
printf("\n请输入学生所在的年级:");
scanf("%s",grades);
printf("\n学号 姓名 年龄 性别 系 班 年级 ");
while(p)
{
if(!strcmp(p->classs,clss)&&!strcmp(p->grade,grades))
LinePrint(p);
p=p->next;
}
break;

case 5:
printf("\n请输入学生所在的年级:");
scanf("%s",grades);
printf("\n学号 姓名 年龄 性别 系 班 年级 ");
while(p)
{
if(!strcmp(p->grade,grades))
LinePrint(p);
p=p->next;
}
break;

case 6:
print();
break;

case 7:
return;
default:
printf("很抱歉,暂无此查询方式!");
break;
}
}

}

void modify()//修改学生信息
{
char num[max];
student *p=head;
printf("\n请输入要修改的学生的学号:");
scanf("%s",num);
while(p)
{
if(!strcmp(p->sno,num))
break;
else
p=p->next;
}
if(p==NULL)
{
printf("\n错误:没有此学生的信息!\n");
return;

}
LinePrint(p);

printf("\n请输入要修改的该学生的信息:");
printf("\n1.姓名");
printf("\n2.性别");
printf("\n3.年龄");
printf("\n4.所在的系");
printf("\n5.所在的班");
printf("\n6.所在的年级");

char name1[max],sex1[max],age1[max],depart1[max],class1[max],grade1[max];
int select;
fflush(stdin);
scanf("%d",&select);
printf("\n请输入新的信息:");

switch(select)
{
case 1:
scanf("%s",name1);
strcpy(p->sname,name1);
break;
case 2:
scanf("%s",sex1);
strcpy(p->sex,sex1);
break;
case 3:
scanf("%s",age1);
strcpy(p->age,age1);
break;
case 4:
scanf("%s",depart1);
strcpy(p->depart,depart1);
break;
case 5:
scanf("%s",class1);
strcpy(p->classs,class1);
break;
case 6:
scanf("%s",grade1);
strcpy(p->grade,grade1);
break;
default:
printf("\nError!");
break;
}

LinePrint(p);
return;
}

void del()// 删除某学生的信息
{
student *p=head,*temp=head,*s;
char num1[max];
printf("\n请输入要删除的学生的学号:");
scanf("%s",num1);
while(p)//查找该学生所在的结点
{
if(!strcmp(p->sno,num1))
break;
else
{
temp=p;
p=p->next;
}

}//while
if(!p)
{
printf("\n不存在此学生的信息.");
return;
}
LinePrint(p);//输出该学生的信息
printf("\n请问真的要删除该学生的信息吗?(Y/N)");
char ch;
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
{
s=p->next;
temp->next=s;
free(p);
printf("\n已经删除该学生的信息.");
}
return;
}

void sort() //排序模块。将学生记录按学号从小到大排列。用起泡排序算法实现
{

student *ptr,*s=head,*p;
int count=0,count1;
while(s)//统计链表结点个数
{
count++;
s=s->next;
}

for(int i=1;i{
ptr=head;
p=NULL;
count1=count-i; //用来控制每轮起泡排序的终点,即每次把学号最小的结点移到倒数第i个结点
while(ptr&&ptr->next&&(count1--))
{
if(strcmp(ptr->sno,ptr->next->sno)>0)
{
s=ptr->next;
ptr->next=s->next;
if(p==NULL) //ptr处于队头时
head=s;
else
p->next=s;
s->next=ptr;
p=s;
}
else
{
ptr=ptr->next;
if(p==NULL) //ptr处于队头时
p=head;
else
p=p->next;
}
}
}
return;
}

void quit()
{
char ch;
printf("\n真的要退出?(Y/N)");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
exit(0);
return;
}

int main()
{
int option;

printf("\nCopyright@2005 KongXinCai All rights reserved.");
printf("\n欢迎使用学生信息管理系统!\n");

//登录模块
int icheck=0;
while(icheck<3)
{
if(LogOn()==0)
icheck++;
else
break;

}
if(icheck==3)
{
printf("\n连续登录三次不成功,退出!");
exit(0);
}

//系统界面

while(1)
{
printf("\n\n请选择需要的服务:");
printf("\n1.注册");
printf("\n2.查询");
printf("\n3.修改");
printf("\n4.删除");
printf("\n5.排序");
printf("\n7.求平均");
printf("\n6.退出\n");

scanf("%d",&option);

switch(option)
{
case 1:
regist();
break;
case 2:
search();
break;
case 3:
modify();
break;
case 4:
del();
break;
case 5:
sort();
break;
case 6:
quit();
break;
}

}

return 0;

}

回答者: 493838148 - 七级 2010-6-12 11:27

检举 //我以前写的
#include "stdio.h"
#include "math.h"
#define N 30

struct student
{
long int num;
char name[10];
char sex[3];
int Chinese,Math,English,total,average;
}stu[N]={{4001,"lks","m",100,100,100},{4002,"lyt","w",99,63,89},{4003,"asd","w",60,70,80},{4004,"fgh","m",41,100,98},
{4005,"asd","m",74,74,87},{4006,"yt","m",85,58,78},{4007,"qwe","w",74,100,87},{4008,"dcs","w",74,85,96},
{4009,"qfg","m",74,78,36},{4010,"gft","m",45,85,98},{4011,"mn","m",60,77,17},{4012,"yer","w",78,78,100},{4013,"lsd","m",100,89,85}
},s;
int M=13;
void modify(struct student stu[])
{
int t;
long num;
char z;
t=search(stu)-400;
printf("input num:");
scanf("%ld",&stu[t].num);
printf("input name:");
scanf("%s",stu[t].name);
printf("input sex:");
scanf("%s",stu[t].sex);
printf("input score of Chinese:");
scanf("%d",&stu[t].Chinese);
printf("input score of Math:");
scanf("%d",&stu[t].Math);
printf("input score of English:");
scanf("%d",&stu[t].English);
stu[t].average=(stu[t].English+stu[t].Math+stu[t].Chinese)/3;
stu[t].total=stu[t].English+stu[t].Math+stu[t].Chinese;
printf("\tNUM\tNAME\tSEX\tCHINESE\tMATH\tENGLISH\tTOTAL\tAVERAGE\n");
printf("\t%4ld\t",stu[t].num);
printf(" %s\t",stu[t].name);
printf("%s\t",stu[t].sex);
printf("%4d\t%4d\t%4d\t%4d%8d\n",stu[t].Chinese,stu[t].Math,stu[t].English,stu[t].total,stu[t].average);
printf("Next?y/n???");
scanf("%s",&z);
if (z=='y')
modify(stu);
}
void output(struct student stu[])
{
int i;
printf("\tNUM\tNAME\tSEX\tCHINESE\tMATH\tENGLISH\tTOTAL\tAVERAGE\n");
for (i=0;i {
printf("\t%4ld\t",stu[i].num);
printf(" %s\t",stu[i].name);
printf("%s\t",stu[i].sex);
printf("%4d\t%4d\t%4d\t%4d%8d\n",stu[i].Chinese,stu[i].Math,stu[i].English,stu[i].total,stu[i].average);
}
}
void sort(struct student stu[])
{
int i,j,t;
printf("1.an yingyu chengcheji paixu\n2.an yuwen chengji paixu\n3.an shuxue chengji paixu\n4.an pingjue chengji paixu\n5.an zongfen paixu\n");
scanf("%d",&t);
switch (t)
{
case 1:
for (i=0;i for (j=0;j {
if (stu[j].English {
s=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s;
}
}
output(stu);
getch();
break;
case 2:
for (i=0;i for (j=0;j {
if (stu[j].Chinese {
s=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s;
}
}
output(stu);
getch();
break;
case 3:
for (i=0;i for (j=0;j {
if (stu[j].Math {
s=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s;
}
}
output(stu);
getch();
break;
case 4:
for (i=0;i for (j=0;j {
if (stu[j].average {
s=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s;
}
}
output(stu);
getch();
break;
case 5:
for (i=0;i for (j=0;j {
if (stu[j].total {
s=stu[j];
stu[j]=stu[j+1];
stu[j+1]=s;
}
}
output(stu);
getch();
break;
}
}
int search(struct student stu[])
{
long t,i,z,K='y';
char name[20];
printf("1.an xuehao chazhao\n2.an xingming chazhao\n");
scanf("%d",&z);
switch (z)
{
case 1:
printf("qing shuru xuehao:\n");
scanf("%ld",&t);
for (i=0;i {
if (stu[i].num==t)
{
K='x';
printf("\tNUM\tNAME\tSEX\tCHINESE\tMATH\tENGLISH\tTOTAL\tAVERAGE\n");
printf("\t%4ld\t",stu[i].num);
printf(" %s\t",stu[i].name);
printf(" %s\t",stu[i].sex);
printf("%4d\t%4d\t%4d\t%4d%6d\n",stu[i].Chinese,stu[i].Math,stu[i].English,stu[i].total,stu[i].average);
return(stu[i].num);
getche();
break;
}
}
if (K='y')
{
printf("No find");
getchar();
}
break;
case 2:
printf("qing shuru xingming:");
scanf("%s",name);
for (i=0;i {
if (strcmp(stu[i].name,name)==0)
{
K='x';
printf("\tNUM\tNAME\tSEX\tCHINESE\tMATH\tENGLISH\tTOTAL\tAVERAGE\n");
printf("\t%4ld\t",stu[i].num);
printf(" %s\t",stu[i].name);
printf(" %s\t",stu[i].sex);
printf("%4d\t%4d\t%4d\t%4d%6d\n",stu[i].Chinese,stu[i].Math,stu[i].English,stu[i].total,stu[i].average);
getche();
break;
}
}
if (K='y')
{
printf("No find");
getchar();
}
break;
}
}

void insert(struct student stu[])
{
int i;
char z='y';
for (i=M;i<=N&&z=='y';i++)
{
printf("input num:");
scanf("%ld",&stu[i].num);
printf("input name:");
scanf("%s",stu[i].name);
printf("input name:");
scanf("%s",stu[i].sex);
printf("input score of Chinese:");
scanf("%d",&stu[i].Chinese);
printf("input score of Math:");
scanf("%d",&stu[i].Math);
printf("input score of English:");
scanf("%d",&stu[i].English);
stu[i].average=(stu[i].English+stu[i].Math+stu[i].Chinese)/3;
stu[i].total=stu[i].English+stu[i].Math+stu[i].Chinese;
printf("Next?y/n???");
z=getche();
if (z!='y')
{
M=i+1;
sort(stu);
output(stu);
}
}
}
void delete(struct student stu[])
{
long t,i,z;
char l,name[20];
printf("1.an xuehao shanchu/n2.an xingming chanchu");
scanf("%d",&z);
switch (z)
{
case 1:
printf("qing shuru xuehao:");
scanf("%ld",&t);
for (i=0;i {
if (stu[i].num==t)
{
printf("\tNUM\tNAME\tSEX\tCHINESE\tMATH\tENGLISH\tTOTAL\tAVERAGE\n");
printf("\t%4ld\t",stu[i].num);
printf(" %s\t",stu[i].name);
printf("%s\t",stu[i].sex);
printf("%4d\t%4d\t%4d\t%4d%8d\n",stu[i].Chinese,stu[i].Math,stu[i].English,stu[i].total,stu[i].average);
break;
}
}
printf("Go on?y/n???");
scanf("%s",&l);
if (l=='y')
{
for (;i {
stu[i]=stu[i+1];
}
stu[M-1].num=0;
output(stu);
}
else
break;
case 2:
printf("qing shuru xingming:");
scanf("%s",name);
for (i=0;i {
if (strcmp(stu[i].name,name)==0)
{
printf("\tNUM\tNAME\tSEX\tCHINESE\tMATH\tENGLISH\tTOTAL\tAVERAGE\n");
printf("\t%4ld\t",stu[i].num);
printf(" %s\t",stu[i].name);
printf("%s\t",stu[i].sex);
printf("%4d\t%4d\t%4d\t%4d%8d\n",stu[i].Chinese,stu[i].Math,stu[i].English,stu[i].total,stu[i].average);
break;
}
}
printf("Go on?y/n???");
scanf("%s",&l);
if (l=='y')
{
for (;i {
stu[i]=stu[i+1];
}
stu[M-1].num=0;
output(stu);
}
else
break;
}
}

main()
{
int choice,i;
clrscr();
for (i=0;i {
stu[i].average=(stu[i].English+stu[i].Math+stu[i].Chinese)/3; ;
stu[i].total=stu[i].English+stu[i].Math+stu[i].Chinese;
}
for (;;)
{
printf("\n------ xuesheng chengji guanli xitong------\n");
printf("1.modify xuesheng jilu \n");
printf("2.output xuesheng jilu\n");
printf("3.sort xuesheng jilu\n");
printf("4.search xuesheng jilu\n");
printf("5.insert xuesheng jilu\n");
printf("6.delete xuesheng jilu\n");
printf("7. exit\n");
printf("pllease input(1-7):\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
modify(stu);
break;
case 2:
output(stu);
break;
case 3:
sort(stu);
break;
case 4:
search(stu);
break;
case 5:
insert(stu);
break;
case 6:
delete(stu);
break;
case 7:
exit();
break;
}
}
getch();
}