欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

大一新生,学生管理系统

程序员文章站 2022-07-15 08:46:41
...

大一新手,给大家借鉴一下。呜呜,求个点赞和关注。

/* 
1。界面
2.数据结构设计
3。交互
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

//结构体保存管理员信息 
struct guanli
 {
 	char gname[15];
 	char gid[15];
 	char tel[10];
 };
 //结构体保存学生信息 
struct student
{
	char id[15]; 
	char name[10];
    char specialty[10];
	char sex[5];
};

//结点 
struct Node
{
	struct student date;
	struct Node *next;
};

//创建头结点 
struct Node *createList() 
{
	struct Node *headNode = (struct Node *)malloc(sizeof(struct Node));
	headNode->next = NULL;
	return headNode;
}

//创建结点 
struct Node *createNode(struct student date) 
{
	struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
	newNode->date = date;
	newNode->next = NULL;
	return newNode;
}

//不同功能使用函数完成

//插入结点 
//1录入学生信息 
void insertNodeByHead(struct Node *headNode, struct student date)
{
	struct Node *newNode = createNode(date);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

//2打印全部学生信息 
void printList(struct Node *headNode)
{
	struct Node *pMove = headNode->next;
	printf("学号\t\t姓名\t专业\t性别\n");
	 while(pMove)
	 {
	 	printf("%s\t%s\t%s\t%s\n",pMove->date.id,pMove->date.name,
		 pMove->date.specialty,pMove->date.sex);
	 	pMove = pMove->next;
	} 
	printf("\n"); 
}

//3根据学号搜索学生信息 
struct Node *searchInfoByDate(struct Node *headNode, char *id)
{
	struct Node *pMove = headNode->next;
	while(pMove != NULL)
	{
		if(strcmp(pMove->date.id,id) == 0)
		{
			return pMove;
		}
		else
		{
			pMove = pMove->next;
		}
	}
	return pMove;
}

//4根据学号修改学生信息 
struct Node *amend(struct Node *headNode,char *id)
{

	struct Node *pMove = headNode->next;
	while( pMove != NULL)
	{
		if(strcmp(pMove->date.id,id)==0) 
		break; 
	    else
	    pMove = pMove->next;
	}
	if(pMove!=NULL)
	{
		printf("请输入修改后的信息:\n");
	    printf("请输入学生信息:\n学号\t\t姓名\t专业\t性别:\n");
		fflush(stdin);
		scanf("%s %s %s %s",pMove->date.id,pMove->date.name,
		 pMove->date.specialty,pMove->date.sex);
	}
	else
	{
		printf("没有找到该学生信息!请重新输入!\n");
	}
}

//5根据学号指定删除学生信息
void deleteNode(struct Node *headNode, char *id) 
{
	struct Node *posNode = headNode->next;
	struct Node *posFrontNode = headNode;
	if(posNode == NULL)
	{
		printf("数据为空,无法删除!\n");
		return ;
	}
	while(strcmp(posNode->date.id,id) != 0)
	{
		posFrontNode = posNode;
		posNode = posNode->next;
		if(posNode == NULL)
		{
			printf("没有该名学生,请重新输入!\n");
			return ;
		}
    }
	posFrontNode->next = posNode->next;
	printf("该名学生信息已经被删除!");
	free(posNode);
}

//5删除全部学生信息 
int deleteall(struct Node *headNode) 
{
	printf("你确定需要删除全部学生信息吗?确定请输入1返回请输入0\n"); 
	int q;
	scanf("%d",&q);
	if(q)
	{
	struct Node *posNode = headNode->next;
	while(posNode)
	{
			posNode = posNode->next;
	}
	headNode->next = posNode;
	printf("已经删除全部学生信息!"); 
    } 
    
}

//6按照专业进行分类 
void fenlei(struct Node *headNode,char *specialty)
{
    struct Node *pMove = headNode->next;
    printf("学号\t\t姓名\t专业\t性别\n");
    int k = 1;
	while(pMove)
	{
		if(strcmp(pMove->date.specialty,specialty) == 0)
		{
			printf("%s\t%s\t%s\t%s\n",pMove->date.id,pMove->date.name,
		    pMove->date.specialty,pMove->date.sex);
		    k = 0;
		}
		pMove = pMove->next;
	}
	if(k == 1)
		{
			printf("系统里没有储存该专业学生信息。\n");
		} 
}

//7按照性别进行分类
void fenleibysex(struct Node *headNode,char *sex)
{
    struct Node *pMove = headNode->next;
    printf("学号\t\t姓名\t专业\t性别\n");
    int k = 1;
	while(pMove)
	{
		if(strcmp(pMove->date.sex,sex) == 0)
		{
			printf("%s\t%s\t%s\t%s\n",pMove->date.id,pMove->date.name,
		    pMove->date.specialty,pMove->date.sex);
		    k = 0;
		}
		pMove = pMove->next;
	}
	if(k)
	{
		printf("系统里没有储存该性别学生信息。\n");
	}
}

//8按照学号从小到大排序 
void BubbleSortByName(struct Node *headNode)
{
	
	for (struct Node* p = headNode->next; p != NULL; p = p->next)
	{
		for (struct Node* q = headNode->next; q->next != NULL; q = q->next)
		{
			if (strcmp(q->date.id, q->next->date.id)>0)
			{
				struct student tempData = q->date;
				q->date = q->next->date;
				q->next->date = tempData;
			}
		}
	}
	printList(headNode);
}

//输入密码登陆 
int password()
{
	char gname[]={"辜果"};
	char gid[]={"202031061014"};
	char name[10];
	char id[20];
	struct guanli number[10];
	FILE* fp = fopen("E:\\管理.txt", "r");  
	if (fp == NULL) 
	{	
		fp = fopen("E:\\管理.txt", "w+");
	}
	int i = 0;
	while (fscanf(fp, "%s %s\n", number[i].gname,number[i].gid) != EOF)
	{
		i++;
	}
	fclose(fp);
	int n = 3;
	while(n)
	{
		printf("您还有%d次机会登陆\n",n);
		printf("请输入姓名和密码值!(备注:初始姓名和密码是最帅气的那一位)\n");
		scanf("%s %s",name,id);
		if((strcmp(gname,name)==0)&&(strcmp(gid,id)==0))
		{
			printf("登陆成功!加油奥力给!\n");
	        return 1;		
		}
		int k = 0;
		while(k<i)
		{
		 	if((strcmp(number[k].gname,name)==0)&&(strcmp(number[k].gid,id)==0))
		 	{
		 		printf("登陆成功!加油奥力给!\n");
		 		return 1;
		    }
		    else
		 	k++;
		}
		n--;
	}
	if(n == 0)
	printf("密码错误三次!正常退出系统!");
	exit(0);
}

//注册管理员信息 
int sign_in ()
{
	char gname[]={"辜果"};
	char gid[]={"202031061014"};
	char name[10];
	char id[20];
	struct guanli number;
	struct guanli tnumber[10];
    FILE *fp = fopen("E:\\管理.txt","r");
	if(fp == NULL)
	{
		fp = fopen("E:\\管理.txt","w+");
	}
	int i = 0;
	while (fscanf(fp, "%s %s %s\n", tnumber[i].gname,tnumber[i].gid,tnumber[i].tel) != EOF)
	{
		i++;
	}
	fclose(fp);
	fp = fopen("E:\\管理.txt", "a");
	printf("最多可以新添10位管理员信息。(现在已新添%d名管理)\n",i); 
	printf("请输入初始姓名和密码!\n");
	scanf("%s %s",name,id);
	if((strcmp(gname,name)==0)&&(strcmp(gid,id)==0)) 
	{
		printf("输入需要注册的姓名密码和号码:\n");
		scanf("%s %s %s",number.gname,number.gid,number.tel); 
		fprintf(fp,"%s %s %s\n",number.gname,number.gid,number.tel);
		fclose(fp);
		printf("注册完成!重新登陆!\n");
		return -1;
    }
    else
    {
      	printf("初始密码错误!请重新输入!");
      	return -1;
	}
}

//管理员信息查看
void information()
{
	struct guanli number[10];
	FILE* fp = fopen("E:\\管理.txt", "r");  
	if (fp == NULL) 
	{	
		fp = fopen("E:\\管理.txt", "w+");
	}
	int i = 0;
	while (fscanf(fp, "%s %s %s\n", number[i].gname,number[i].gid,number[i].tel) != EOF)
	{
		i++;
	}
	i--;
	fclose(fp);
	while(i>=0)
	{
		printf("%s %s\n",number[i].gname,number[i].tel);
		i--;
	}
} 

//管理员登陆界面 
int  login()
{
	
	printf("\t\t\t\t\t╭╮ __\|/__╭╮\n");
	printf("\t\t\t\t\t│   ~│       |\n");
	printf("\t\t\t\t\t│ ≥ ╭———╮ ≤│    \n");
	printf("\t\t\t\t\t│//  │0 0  │//│\n ");
	printf("\t\t\t\t\t│    ╰—— —    │\n");
	printf("\t\t\t\t\t╰——┬O————┬—O╯\n");
	printf("\t\t\t\t\t●│    │ \n");
	printf("\t\t\t\t\t╰│  O  │ \n");
	printf("\t\t\t\t\t╰||-||╯\n");
	printf("\n\n"); 
	printf("\t\t\t-------------亲爱的管理员你好啊m( =. ω.= )m-----------\n"); 
	printf("\n"); 
	printf("\t\t\t--------------   今天也要元气满满哟    ---------------\n"); 
	printf("\n");
	printf("\n");
	printf("\t\t\t\t\t 管理登陆请输入(1)\n\t\t\t\t\t 管理注册请输入(2)\n\t\t\t\t\t 任意输入其他值退出\n");
	int i;
	scanf("%d",&i);
	if(i == 1)
	{	
	    password();
	}
	else if(i == 2)
	{
	    sign_in();
	}
	else
	{
		printf("正常退出!"); 
		exit(0);
	}
		
}

	
//界面 
void menu()
{
	printf("            ☆☆☆  ∧_∧  ☆☆ ☆\n");
	
	printf("---------------{学生管理系统}-----------\n\n");
	printf("                 0.退出系统             \n\n");
	printf("                 1.录入信息             \n\n");
	printf("                 2.浏览信息             \n\n");
	printf("                 3.查找信息             \n\n");
	printf("                 4.修改信息             \n\n");
	printf("                 5.删除信息             \n\n");
	printf("                 6.按照专业分类         \n\n");
	printf("                 7.按照性别分类         \n\n");
	printf("                 8.按照学号排序从小到大 \n\n");
    printf("                 9.查看管理员信息       \n\n");
    printf("         每次进行一次输入后使用空格隔开\n");
	printf("---------------{学生管理系统}-----------\n");
	
}

//创建一个头结点,便于后面操作 
struct Node *list = createList(); 
//链表与文件的读入信息和读出信息 
//文件写操作 
void saveInfoToFile(const char* fileName, struct Node* headNode) 
{
	FILE *fp = fopen(fileName, "w");
	struct Node* pMove = headNode->next;
	while (pMove != NULL) 
	{
		fprintf(fp, "%s\t%s\t%s\t%s\n", pMove->date.id, pMove->date.name, 
		pMove->date.specialty,pMove->date.sex);
		pMove = pMove->next;
	}
	fclose(fp);
}

//文件读操作
void readInfoFromFile(const char* fileName, struct Node* headNode) 
{
	FILE* fp = fopen(fileName, "r");  //第一次打开文件肯定不存在
	if (fp == NULL) 
	{	
		fp = fopen(fileName, "w+");//不存在就创建出来这个文件
	}
	struct student tempData;
	while (fscanf(fp, "%s\t%s\t%s\t%s\n", tempData.id,tempData.name,
	 tempData.specialty,tempData.sex) != EOF) 
	{
		insertNodeByHead(list, tempData);
	}
	fclose(fp);
}

//根据菜单项,做相应的事情
void keyDown()
{
	
	int choice ;
	struct student date;
	struct Node *pMove = NULL;
	scanf("%d",&choice);
	switch(choice)
	{
		
		case 0:
			system("color 75");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("正常退出!\n");
			system("pause");
			exit(0);
		break;
		
		case 1:
			system("color 72");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("----------录入信息---------\n"); 
		    printf("请输入需要添加信息的学生人数:");
            int n;
            scanf("%d",&n);
            printf("\n");
            printf("请输入学生信息:\n学号\t\t姓名\t专业\t性别:\n");
            
            for(int i = 1;i<=n;i++)
           {
	            fflush(stdin);
	            scanf("%s%s%s%s",date.id,date.name,
		        date.specialty,date.sex);
	            insertNodeByHead(list,date);
	            saveInfoToFile("student.txt", list);
            }
	    break;
	    
		case 2:
			system("color 74");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("----------浏览信息---------\n");
			printf("浏览学生信息:\n");
			printList(list);
		break;
		
		case 3:
			system("color 73");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("----------查找信息---------\n");
			printf("请输入需要查找的的学生学号:\n");
			scanf("%s",date.id);
			pMove = searchInfoByDate(list,date.id);
			if(pMove==NULL) 
			{
				printf("未找到该学生\n");
				//system("pause");
			}
			else
			{
				printf("学号\t\t姓名\t专业\t性别:\n");
				printf("%s\t%s\t%s\t%s\n",pMove->date.id,pMove->date.name,
		 pMove->date.specialty,pMove->date.sex);
			}
		break;
		
		case 4:
			system("color 7A");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("----------修改信息---------\n");
			printf("请输入需要修改的学生学号:\n");
			scanf("%s",date.id);
			amend(list,date.id);
		    saveInfoToFile("student.txt", list);
		break;
		
		case 5:
			system("color 74");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("----------删除信息---------\n");
			printf("\t1.指定删除 。   \t2.全部删除。");
			printf("\n");
            int j;
            scanf("%d",&j);
            if(j == 1)
            {
	        printf("请输入需要删除的学生学号:\n");
			scanf("%s",date.id);
			deleteNode(list,date.id);
	        saveInfoToFile("student.txt", list);
           } 
            else if(j == 2)
          {
	         deleteall(list);
	         saveInfoToFile("student.txt", list);
          }
           else
          {
	      printf("你输错了,请重新输入!"); 
          } 
		break;
		
		case 6:
			system("color 79");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
		    printf("请输入需要查看的专业:");
			scanf("%s",date.specialty);
			fenlei(list,date.specialty);
		break;
		 
		case 7:
			system("color 79");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
		    printf("请输入需要查看的性别:");
			scanf("%s",date.sex);
			fenleibysex(list,date.sex);
		break;
		
		case 8:
			system("color 76");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("按照学号排序:\n");
			BubbleSortByName(list);
		break;
		
		case 9:
		    system("color 7");
			printf("☆☆☆  ∧_∧  ☆☆ ☆\n");
			printf("查看管理员信息:\n");
			information(); 
			printf("遇到困难可以联系以上管理员!");
			break;
			
		default:
			printf("输入错误,请重新输入!\n");
		break;
	} 
 } 

//主函数 
int main()
{
	system("color 30");
	readInfoFromFile("student.txt", list);
	
		while(1)
		{
	        int k = login(); 
			while(k == 1)
			{
			system("pause");
		    system("cls"); 
			    while(1)
			    {
				printf("\## 标题t请进行你的操作!(只能输入0-8哟)\n"); 
				menu();
				keyDown();
				system("pause");
				system("cls"); 
			    }
		    }
		    system("pause");
			system("cls"); 
  	   }
   
}
相关标签: 链表 结构体