天天看點

C語言學生管理系統

想練習一下連結清單,是以就有了這個用C寫的學生管理系統

沒有把它寫入檔案,才不是因為我懶哈哈哈,主要是為了練習連結清單的

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

typedef struct student
{ // 定義學生的基本資料
	char stuName[10]; // 姓名
	long stuID;  // 學号
	char gender[5];  // 性别
	int score[3];  // 三門課的成績
	struct student* next;
}stu, *LNode;

typedef struct headNode
{ // 定義連結清單的頭結點類型
	int length; // 儲存學生數量
	struct student* next;
}HNode;

HNode* InitList();
void printMenu();
void alternation(HNode* head);
int isExist(HNode* head, long existID);
void addStu(HNode* head);
void deleteStu(HNode* head);
void searchStu(HNode* head);
void modifyStu(HNode* head);
void displayStu(HNode* head);

int main()
{
	HNode* head = InitList();
	printf("************* 歡迎進入學生管理系統 *************\n");
	while (1)
	{
		printMenu();
		alternation(head);
	}
	return 0;
}

void printMenu()
{ // 菜單
	printf("\n - - - - - - - - -\n");
	printf("| 1.添加學生資訊  |\n");
	printf("| 2.删除學生資訊  |\n");
	printf("| 3.修改學生資訊  |\n");
	printf("| 4.查找學生資訊  |\n");
	printf("| 5.顯示全部資訊  |\n");
	printf("| 6.退出系統      |\n");
	printf(" - - - - - - - - \n");
}

void alternation(HNode *head) 
{ //  選擇要執行的操作
	int option;
	printf("請選擇操作:");
	scanf("%d", &option);
	switch (option) 
	{
	case 1:	addStu(head); break;  //添加
	case 2:	deleteStu(head); break;   //删除
	case 3:	modifyStu(head); break;   //修改
	case 4:	searchStu(head); break;	//查詢
	case 5:	displayStu(head); break;	//顯示全部學生資訊
	case 6: printf("退出系統,再見。"); exit(0);
	default: printf("輸入錯誤,請重新輸入:");
	}
}

HNode* InitList()  
{ // 初始化連結清單
	HNode* head = NULL;
	head = (HNode*)malloc(sizeof(HNode));
	head->length = 0;
	head->next = NULL;
	return head;
}

void addStu(HNode* head)  // 添加學生資訊
{ // 前插法添加學生資訊
	LNode p = NULL;
	p = (stu*)malloc(sizeof(stu));
	p->next = NULL;
	//- - - - - - - - 開始錄入資訊 - - - - - - - -//
	printf("請輸入學号(輸入-1錄入結束):");
	scanf("%d", &p->stuID);
	if (p->stuID == -1)
		return;
	if (isExist(head, p->stuID))
	{ // 在添加之前判斷是否已存在該學生
		printf("已存在該學生資訊,無需輸入!");
		return;
	}
	//getchar();
	printf("請輸入姓名:");
	scanf("%s", &p->stuName);
	printf("請輸入性别:");
	scanf("%s", &p->gender);
	printf("請輸入成績(英語、國文、數學):");
	for (int i = 0; i < 3; i++)
	{
		scanf("%d", &p->score[i]);
	}
	//- - - - - - - - 錄入資訊結束 - - - - - - - -//
	head->length ++;
	p->next = head->next; // 更新學生人數
	head->next = p;
	printf("添加成功!");
}

void deleteStu(HNode* head)
{ // 删除某個學生的資訊
	stu* temp = NULL;
	stu* p = head->next;
	long deleteID;
	printf("請輸入要删除學生的學号:");
	scanf("%d", &deleteID);
	if (!isExist(head, deleteID))
	{ // 在删除之前判斷是否已存在該學生
		printf("不存在該學生,删除失敗!");
		return;
	}
	while (p != NULL) 
	{
		if (p->next->stuID == deleteID)
		{ 
			temp = p->next;
			p->next = p->next->next;
			free(temp);
		}
		p = p->next;
	}
	printf("删除成功!");
}

int isExist(HNode* head,long existID)
{  // 判斷學生是否存在
	stu* p = head->next;
	while (p != NULL)
	{
		if (p->stuID == existID)
			return 1;
		p = p->next;
	}
	return 0;
}

void searchStu(HNode* head)
{ // 查找某個學生的資訊
	long searchID;
	stu* p = head->next;
	printf("請輸入要查詢的學号:");
	scanf("%d", &searchID);
	if (!isExist(head, searchID))
	{ // 在查詢之前判斷是否不存在該學生
		printf("不存在該學生,查詢失敗!");
		return;
	}
	while (p != NULL) 
	{
		if (p->stuID == searchID)
		{
			printf("該學生的資訊如下:\n");
			printf("姓名:%s\n學号:%d\n性别:%s\n英語成績:%d\n國文成績:%d\n數學成績:%d\n",
				p->stuName, p->stuID, p->gender, p->score[0], p->score[1], p->score[2]);
			return;
		}
		p = p->next;
	}
}

void modifyStu(HNode* head)
{ // 修改某個學生的資訊,支援單項資訊修改
	stu* p = NULL;
	p = head->next;
	long modifyID;
	printf("輸入要修改資訊的學生的學号:");
	scanf("%d", &modifyID);
	if (!isExist(head, modifyID))
	{ // 在修改之前判斷是否已存在該學生
		printf("不存在該學生,修改失敗!");
		return;
	}
	while (p != NULL) 
	{
		if (p->stuID == modifyID)
		{
			int option;
			long newID;
			char newName[10];
			char newGender[5];
			int newScore;
			printf("請輸入要修改的項目(1.學号  2.姓名  3.性别  4.英語成績  5.國文成績  6.數學成績):");
			scanf("%d", &option);
			switch (option)
			{
			case 1: printf("請輸入新的學号:"); scanf("%d", &newID); p->stuID = newID; break;
			case 2: printf("請輸入新的姓名:"); scanf("%s", newName); strcpy(p->stuName,newName); break;
			case 3: printf("請輸入新的性别:"); scanf("%s", newGender); strcpy(p->gender,newGender); break;
			case 4: printf("請輸入新的英語成績:"); scanf("%d", &newScore); p->score[0] = newScore; break;
			case 5:	printf("請輸入新的國文成績:"); scanf("%d", &newScore); p->score[1] = newScore; break;
			case 6: printf("請輸入新的數學成績:"); scanf("%d", &newScore); p->score[2] = newScore; break;
			}
		}
		p = p->next;
	}
	printf("修改成功!");
}

void displayStu(HNode* head)
{
	stu* p = NULL;
	p = head->next;
	printf("目前一共有%d個學生的資訊,資訊如下:\n",head->length);
	while (p != NULL)
	{
		printf("學号:%d  姓名:%s  性别:%s  英語成績:%d  國文成績:%d 數學成績:%d\n",
			   p->stuID, p->stuName, p->gender, p->score[0], p->score[1], p->score[2]);
		p = p->next;
	}
}