C语言学生管理系统

论如何写一个相当不错的学生管理系统
附带:防止暴力输入。
附带:登录注册,多个用户。
附带:特色增删改查,(一种排版的思路)。
简单易懂,纯手工制作。
``
#include<stdio.h>
#include<stdlib.h>`
#include<windows.h>
#include<string.h>
#include<conio.h>
typedef struct Student
{
	char Name[300];
	int Student_number;
    int Score;
	int Aeg;
	struct Student *next;
}STU;
//用于开创一个头指针 
STU *CreateList()
{
	STU *head;
	head=(STU *)malloc(sizeof(STU));
	head->next=NULL;
	return head; 
}
//信息加载模块 
STU *LoadList(STU *p)
{

	STU *pnew=NULL;
	FILE *fileopen=NULL;
	fileopen=fopen("D:/C/StudentList.txt","a+");
	if(fileopen==NULL){		
		printf("\n不能打开文件\n");
		system("pause");
		exit(1);
	}
	while(1){
		pnew=(STU *)malloc(sizeof(STU));		
		if(pnew==false){
			printf("\n内存申请失败!!!\n");
		}
//		fscanf(fileopen"%s %d %d %d",&pnew->Name,&pnew->Aeg,&pnew->Student_number,&p->Score);		
//		else{
			if(fscanf(fileopen,"%s %d %d %d \n",pnew->Name,&pnew->Aeg,&pnew->Student_number,&pnew->Score)==EOF){				
				break;
			}
//		if(fread(pnew,sizeof(STU),1,fileopen)==0){			
//			break;
//		}                                                                                          //为啥呢 
		pnew->next=NULL;
		p->next=pnew;
		p=p->next; 			
	}
	fclose(fileopen);
	return p;
}
//用于增加学生信息的模块
int Compare(STU *p,int n)
{ 
	STU *move;
	move=p->next;
	while(move!=NULL){	     
		if(n==move->Student_number){
			return 0;		
		}
		move=move->next;		
	}
	if(move==NULL){
		return 1;
	}
}
int AddList(STU *p,STU *P)
{
	STU *m;
	m=P;
	STU *pnew;
	int i;
	int n,y,z;
	long long int x;
	printf("请输入您要输入的学生个数:\n");
	while(1){
        if((scanf("%d",&y)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			break;
		}
	}	
	for(i=1;i<=y;++i){
		pnew=(STU *)malloc(sizeof(STU));
		printf("请输入第%d个学生的信息:\n",i);
		printf("名字:");
		scanf("%s",&pnew->Name);
		while(1){	
		printf("年龄:");
        if((scanf("%d",&n)==0)){
        	printf("你输入的格式错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			pnew->Aeg=n;
			break;
		}
	}
		while(1){	
		printf("学号:");
        if((scanf("%d",&x)==0)){
        	printf("你输入的格式错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			int q;
		    q=Compare(m,x);
			if(q==0){
				printf("你输入的学生信息重复,请重新输入!!!\n");
				continue;
			}
			else if(q==1){
				pnew->Student_number=x;
				goto end;
			}
		}
	}
	end:	
	while(1){
		printf("成绩:");
        if((scanf("%d",&z)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
            pnew->Score=z;
			break;
		}
	}
		pnew->next=NULL;
		p->next=pnew;
		p=p->next; 
	} 
}
//用于删除新增加学生的信息 
void DeleteList(STU *p)
{
	STU *temp;
	temp=p;
	p=p->next;
	int Student_Number;
	printf("\n请输入您要删除学生的学号:\n");
	scanf("%d",&Student_Number);
	while(p!=NULL){
		if(Student_Number==p->Student_number){
			temp->next=p->next;
			free(p);
			printf("\n已删除该学生信息!!!\n");
			break;
		}
		temp=temp->next;
		p=p->next;
	}
	if(p==NULL)
	printf("\n未找到该学生信息,请检查您输入的学号是否正确!!!\n");
}
//用于打印出当前新增加的学生信息 
void PrintAllNode(STU *p)
{
	STU *k=NULL;
	k=p;
	k=k->next;
	while(k!=NULL){		
		printf("名子:\t%s\t年龄:\t%d\t学号:\t%d\t成绩:\t%d\n",k->Name,k->Aeg,k->Student_number,k->Score);
		k=k->next;
	}
}
//用于对学生信息排序的模块 
void Sorting(STU *p)
{
	STU *pb,*pf,temp;
	pf=p;
	if(p==NULL){
		printf("不需要排序\n");
		goto end;
	}
	if(p->next==NULL){
		printf("不需要排序\n");
		goto end;
	}
	pf=pf->next;
	while(pf->next!=NULL){
		pb=pf->next;
		while(pb!=NULL){
			if(pf->Score>pb->Score){
				temp=*pf;
				*pf=*pb;
				*pb=temp;
				temp.next=pf->next;
				pf->next=pb->next;
				pb->next=temp.next; 
			}
			pb=pb->next;
		}
		pf=pf->next;
	}
	end:
	printf(" ");
}	
//用于退出系统的模块 
int Quit()
{
	return 0;
}
//用于保存的模块 
void Preservation(STU *p)
{
	STU *P=NULL;
	P=p->next;//有效   
	FILE *fp;//文件指针指向文件;
	fp=fopen("D:/C/StudentList.txt","w");		
	if(fp==NULL){
		printf("不能够打开文件\n"); 
	}
    else{
   	   printf("开始将您进行的操作进行保存\n");
	   while(P!=NULL){
//	   fwrite(P,sizeof(STU),1,fp);
//        fscanf(fp,"%s%d%d%d",&P->Name,&P->Aeg,&P->Student_number,&P->Score);
        fprintf(fp,"%s %d %d %d\n",P->Name,P->Aeg,P->Student_number,P->Score);
	   P=P->next;
   }
	fclose(fp);
   }	 			
}
//用于寻找学生信息的模块 
void SearchStudent(STU *p)
{
	endl:
	int n,y;
    int Student_Number;
	STU *move;
	move=p->next;
	printf("1.学号 2.成绩 3.年龄 \n");
	printf("请问你按什么方式查找:");
	while(1){
        if((scanf("%d",&n)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			break;
		}
	   }	
    if(n==1){     	
    while(1){
	    printf("请输入你要查找的学号:\n");
        if((scanf("%d",&y)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			break;
		}
	   }
	printf("结果如下:\n"); 
	while(move!=NULL){
		if(y==move->Student_number){
			printf("名字:%s年龄:%d学号:%d成绩:%d\n",move->Name,move->Aeg,move->Student_number,move->Score);
			break;
		}
		move=move->next;
	}
	   if(move==NULL)
       printf("\n没有找到该学生,请检查您输入的学号是否正确!!!\n\n"); 
	}
	else if(n==2){     	
    while(1){
	    printf("请输入你要查找的成绩:\n");
        if((scanf("%d",&y)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			break;
		}
	   }
	printf("结果如下:\n"); 
	while(move!=NULL){
		if(y==move->Score){
			printf("名字:%s年龄:%d学号:%d成绩:%d\n",move->Name,move->Aeg,move->Student_number,move->Score);
		}
		move=move->next;
	}
	}
	else if(n==3){     	
    while(1){
	    printf("请输入你要查找的年龄:\n");
        if((scanf("%d",&y)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			break;
		}
	   }
	printf("结果如下:\n"); 
	while(move!=NULL){
		if(y==move->Aeg){
			printf("名字:%s年龄:%d学号:%d成绩:%d\n",move->Name,move->Aeg,move->Student_number,move->Score);
		}
		move=move->next;
	}
	}
	else if(n!=1||n!=2||n!=3){
		printf("请输入有效指令!!\n");
		goto endl;
	} 
}
void Modify(STU *p)
{
	STU *P;
	STU *move;
	int n;
	int K=0;
	move=p->next;	
endl:
	while(1){
	    printf("请输入你要修改的学生的学号:");		
        if((scanf("%d",&n)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else{
			break;
		}
	}
while(move!=NULL){
	if(n==move->Student_number){
		printf("原本信息如下:\n");
		printf("名字:%s年龄:%d学号:%d成绩:%d\n",move->Name,move->Aeg,move->Student_number,move->Score);
		printf("==========================================\n");
		printf("1.名字2.年龄3.学号4.成绩\n");		
		while(1){
			int o;
	  	printf("请选择修改内容:");
        if((scanf("%d",&o)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		else if(o==1){
			printf("请输入修改后的名字:");
			scanf("%s",move->Name);
			K++;
            break;			 
		}
		else if(o==2){
		    while(1){
            int d;
	  	    printf("请输入修改后的年龄:");
            if((scanf("%d",&d)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		    }
			else{
				move->Aeg=d;
				K++;
				break;
			}			
	}		
	    }
	    else if(o==3){
	    	while(1){
            int D;
	  	    printf("请输入修改后的学号:");
            if((scanf("%d",&D)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		    }
			else{
				move->Student_number=D;
				K++;
				break;
			}			
		}
		}
		else if(o==4){
			while(1){
            int D2;
	  	    printf("请输入修改后的成绩:");
            if((scanf("%d",&D2)==0)){
        	printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		    }
			else{
				move->Score=D2;
				K++;
				break;
			}			
		}
		}
	   if(K!=0){
	   	printf("\n修改完毕\n"); 
	   	system("pause");
		break;
	   }
    }
}  
else if(move==NULL&&K!=0){
		printf("没有找到你要修改的学生信息,请检查无误后再输入\n");
		system("pause");
		system("cls");
		goto endl; 
	}
	move=move->next;
}
}	
//用于控制功能的模块 
int ContorlFunction(STU *p)
{
	system("cls");
	int q;
	printf("=========================================================\n");
	printf("                   ■□■□■□■□■□■□              \n");
	printf("                   □                    □              \n");
	printf("                   □ 学生信息管理系统   □              \n");
	printf("                   □                    □              \n");
	printf("                   ■□■□■□■□■□■□              \n");
	printf("          温馨提示:有效指令为菜单功能前的数字!!!        \n");
	printf("                    1.新增学生信息                       \n");
	printf("                    2.删除学生信息                       \n");
	printf("                    3.查看所有学生信息                   \n");
	printf("                    4.查看某个学生信息                   \n");
	printf("                    5.修改学生信息                       \n");
	printf("                    6.结束本次访问                       \n");
	printf("=========================================================\n");
	char S[30];
	int s;
	STU *C;
do{
	printf("\n请您选择功能:\n\n");
	scanf("%s",S);
	if(strlen(S)>1){
		printf("您输入的指令有误,请重新输入!!!\n");
		system("pause");
		continue;
	}
	if(!(strcmp(S,"1"))||!(strcmp(S,"2"))||!(strcmp(S,"3"))||!(strcmp(S,"4"))||!(strcmp(S,"5"))||!(strcmp(S,"6"))){
		break;
	}
}while(1);
if(!strcmp(S,"1")){
	s=1;
}
if(!strcmp(S,"2")){
	s=2;
}
if(!strcmp(S,"3")){
	s=3;
}
if(!strcmp(S,"4")){
	s=4;
}
if(!strcmp(S,"5")){
	s=5;
}
if(!strcmp(S,"6")){
	s=6;
}
int m;
		switch(s){
			case 1:C=LoadList(p);m=AddList(C,p);if(m==0){
				break;
			}Sorting(p);Preservation(p);system("pause");break;
			case 2:LoadList(p);DeleteList(p);Sorting(p);Preservation(p);system("pause");break;
			case 3:LoadList(p);PrintAllNode(p);system("pause");break;
		    case 4:LoadList(p);SearchStudent(p);system("pause");break;
			case 5:LoadList(p);Modify(p);Preservation(p);PrintAllNode(p);break;	   
		    case 6:q=Quit();s=q;break; 
			default :break;
		}
		system("cls");
	return s;
}
//创建储存用户信息的结构体              (已经完成   且有双重保险    )
typedef struct users_ID
{
	char user_ID[30];
	char user_Secret[30];
	char user_Problem[30];
	struct users_ID *next;
}ID;
int Compare_ID(ID *p,char Users_ID[100])
{ 
	ID *move;
	move=p->next;
	while(move!=NULL){	     
		if(strcmp(Users_ID,move->user_ID)==0){
			return 0;		
		}
		move=move->next;		
	}
	if(move==NULL){
		return 1;
	}
}
//创建储存用户信息的链表头 
ID *Create_head()
{
	ID *head;
	head=(ID *)malloc(sizeof(ID));
	if(head==false){
		printf("头节点内存申请失败!!!");
		system("pause");
		exit(1);
	}
	head->next=NULL;
	return head;
}
//新增或注册的模块 
void Add_ID(ID *p,ID *C)
{
	int x;
	int n;
	char ID_LinShi[30];
	char Secret_LinShi[30];
	char Problem_LinShi[30];
	FILE *fp,*flieopen;
	ID *pnew;
	fp=fopen("D:/C/users_ID.txt","r");
	if(fp==NULL){
		printf("新增节点时打开文件失败!!!");
		system("pause");
		exit(1);
	}
	fscanf(fp,"%s %s %s",ID_LinShi,Secret_LinShi,Problem_LinShi);
	fclose(fp);
	if(strlen(ID_LinShi)>1){
		printf("你已经注册过,是否选择继续添加用户:\n1.是 2.否");
	while(1){
		if((scanf("%d",&n))==0){
			printf("你输入的指令错误,请重新输入!!!\n");
        	fflush(stdin);
			continue; 
		}
		if(n==2){
			goto end1;
		}
		break;
	}
}
	pnew=(ID *)malloc(sizeof(ID));
	if(pnew==false){
		printf("新增节点内存申请失败!!!");
		system("pause");
		exit(1);
	}
	printf("账号长度不超过十个字符");
	printf("请输入注册信息:\n");
	while(1){
		printf("账号:");
	    scanf("%s",pnew->user_ID);
	    x=Compare_ID(C,pnew->user_ID);
	    if(x==0){
	    	printf("你注册的账号重复!!!\n");
			continue; 
		}
	    if(strlen(pnew->user_ID)>10){
		printf("你输入的账号太长,请重新输入!!\n");
		system("pause");
		continue;
	    }
		break; 
	}
	printf("密码不能超过十个字符\n");
	while(1){
		printf("密码:");
		scanf("%s",pnew->user_Secret);
		if(strlen(pnew->user_Secret)>10){
			printf("你输入的密码过长,请重新输入!!!");
			system("pause");
			continue;
		}
		break;
	}
	printf("密保不超过十字符:\n"); 
	while(1){
		printf("密保:");
		scanf("%s",pnew->user_Problem);
		if(strlen(pnew->user_Problem)>10){
			printf("你输入的密保过长,请重新输入!!!");
			system("pause");
			continue;
		}
		break;
	}
	pnew->next=NULL;
	p->next=pnew;
end1:
printf(" ");
}
//用于找回密码的模块
void Seek_ID(ID *p)
{
	char Seek_ID_LinShi[30];
	ID *k=NULL;
	k=p->next;//k为第一个有效数据; 
	printf("密保不超过十个字符\n");
    while(1){
    	printf("请输入你要寻找的账户密保:");
	    scanf("%s",Seek_ID_LinShi);
	    if(strlen(Seek_ID_LinShi)>10){
		printf("你输入的密保过长,请重新输入!!");
		system("pause");
		continue;
	    }
	    break;
	}
	while(k!=NULL){
		if(strcmp(k->user_Problem,Seek_ID_LinShi)==0){
			printf("已找到该用户,信息如下:\n");
			printf("账号:%s   密码:%s   密保:%s   \n",k->user_ID,k->user_Secret,k->user_Problem);
			system("pause");
		}
		k=k->next;
	}
} 
//用于修改用户密码模块
void Modify(ID *p)//修改密码 
{
	char Problem[30];
	ID *k=NULL;
	k=p->next;
	printf("密保长度不超过十个字符\n");
	printf("请输入你要修改的账户的密保:");
	scanf("%s",Problem);
	while(k!=NULL){
		if(strcmp(k->user_Problem,Problem)==0){
			printf("你原来的账户信息如下:\n");
			printf("账号:%s   密码:%s   密保:%s   ",k->user_ID,k->user_Secret,k->user_Problem);
			printf("\n");
		    while(1){
		    		printf("密码长度不超过十个字符,请输入你要修改的密码:");
			        scanf("%s",k->user_Secret);
			        if(strlen(k->user_Secret)>10){
				    printf("你修改后的密码太长,请重新输入!!");
				    system("pause");
				    continue;
			        }
			        break;
			}
		}
		k=k->next;
	}		
} 
//用于打印所有用户账号信息的模块
void Printf_All_ID(ID *p)
{
	ID *k;
	k=p;
	k=k->next;
	printf("结果如下:\n");
	while(k!=NULL){	
		printf("账号:%s\n",k->user_ID,k->user_Secret,k->user_Problem);
		k=k->next;
	}
	system("pause");
}
//用于加载到栈内存的模块
ID *Load_All_ID(ID *p)
{
	ID *pnew=NULL;
	FILE *fileopen=NULL;
	fileopen=fopen("D:/C/users_ID.txt","a+");
	if(fileopen==NULL){		
		printf("\n不能打开文件\n");
		system("pause");
		exit(1);
	}
	while(1){
		pnew=(ID *)malloc(sizeof(ID));		
		if(pnew==false){
			printf("\n内存申请失败!!!\n");
		}
			if(fscanf(fileopen,"%s %s %s\n",pnew->user_ID,pnew->user_Secret,pnew->user_Problem)==EOF){				
				break;
			}                                                                                         //为啥呢 
		pnew->next=NULL;
		p->next=pnew;
		p=p->next; 			
	}
	fclose(fileopen);
	return p;
}
//用于保存账户信息的模块;
void Preservation_ID(ID *p)
{
	FILE *fp;
	ID *k=NULL;
	k=p->next;
	fp=fopen("D:/C/users_ID.txt","w");
	if(fp==false){
		printf("保存时打开文件失败!!");
		system("pause");
		exit(1);
	}	
	while(k!=NULL){
		fprintf(fp,"%s %s %s\n",k->user_ID,k->user_Secret,k->user_Problem);
		k=k->next;
	}	
	fclose(fp);
}
//用于登录模块;
int Loagin(ID *p)
{
	int i;
	system("cls");
	int count=0;
	ID *k=NULL;
	k=p->next;
	char ID[30];
	char Scret[30];
	printf("                   ■□■□■□■□■□■□■                \n");
	printf("                   □      欢迎您来到      □                \n");
	printf("                   □ 学生信息管理登录系统 □                \n");
	printf("                   □                      □                \n");
	printf("                   ■□■□■□■□■□■□■                \n");
int s=0;
while(1){
	FILE *fp;
	fp=fopen("D:/C/users_ID.txt","r");
	if(fp==NULL){
		printf("登陆时打开文件失败!!!");
		system("pause");
		exit(1); 
	}
	while(1){
		count++;
	printf("账号:");
	scanf("%s",ID);
	if(strlen(ID)>10){
		printf("你输入的账号太长,请重新输入!!");
		system("pause");
		continue;
	}
	break;
}
      printf("密码:");
	do{
		
		char ch;
		ch=getch();
		if(ch==' '){
			continue;
		}
		if(ch=='\r'){
			break;
		}
		if(ch=='\b'&&i>=0){
			if(i==0){
				continue;
			}
			printf("\b \b");
			i=i-1;
		    Scret[i+2]='\0';
			continue;
		}
		else{
		Scret[i++]=ch;
		printf("*");
		Scret[i]='\0'; 
		}		
	}while(1);	
	while(k!=NULL){
		if((strcmp(ID,k->user_ID)==0)&&(strcmp(Scret,k->user_Secret)==0)){
		    return s=1; 
		}
		k=k->next;
	}
	printf("\n你输入的信息有误请检查输入!!!\n"); 
	system("pause");
	continue;
	if(count==3){
		printf("你输入的次数过多,请稍后在登录");
		break;
	}	
}
return s=0;	
}
//用于控制账户信息的模块
int Cotrol(ID *p)
{
int n=0;
char S[10];
ID *k;
int s;
	system("cls");
	printf("                   ■□■□■菜单□■□■□■                \n");	
	printf("                   □                      □                \n");
	printf("                   □  1.注册     2.登录   □                \n");
	printf("                   □      3.找回密码      □                \n");
	printf("                   □      4.修改密码      □                \n");
    printf("                   □   5.查看所有用户名   □                \n");
	printf("                   ■□■□■□■□■□■□■                \n");
	printf("\n请您选择功能:\n\n");
while(1){
	scanf("%s",S);
	if(strlen(S)>1){
		printf("您输入的指令有误,请重新输入!!!\n");
		system("pause");
		continue;
	}
	if(!(strcmp(S,"1"))||!(strcmp(S,"2"))||!(strcmp(S,"3"))||!(strcmp(S,"4"))||!(strcmp(S,"5"))){
		break;
	}
}
if(!strcmp(S,"1")){
	s=1;
}
if(!strcmp(S,"2")){
	s=2;
}
if(!strcmp(S,"3")){
	s=3;
}
if(!strcmp(S,"4")){
	s=4;
}
if(!strcmp(S,"5")){
	s=5;
}
switch(s){
		case 1:k=Load_All_ID(p);Add_ID(k,p);Preservation_ID(p);break;//注册 
		case 2:Load_All_ID(p);n=Loagin(p);break;//登录 
		case 3:Load_All_ID(p);Seek_ID(p);break;//找回 
		case 4:Load_All_ID(p);Modify(p);Preservation_ID(p);break;//修改 
		case 5:Load_All_ID(p);Printf_All_ID(p);break;//查看 
		default:break;
	}
return n;	
}
int main()
{
	
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);//改变颜色的函数; 
	int x=0;
	STU *N;
	ID *k;
					
while(1){
			k=Create_head();
			x=Cotrol(k);
			if(x==1){
				system("cls");
	printf("                   ■□■□■□■□■□■□■                \n");	
	printf("                   □                      □                \n");
	printf("                   □      登陆成功        □                \n");
	printf("                   □                      □                \n");
	printf("                   ■□■□■□■□■□■□■                \n");
	printf("\n\t\t欢迎 您 来到黄雨森学生管理系统!!!\n\n");
				system("pause"); 
				break;
			}
	}
while(1){				
		N=CreateList();
		x=ContorlFunction(N);
		if(x==0){
			printf("感谢你的本次访问!!!");
			system("pause"); 
			break;
		}
	}			
	return 0;
}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值