首頁 > 軟體

C語言實現學生管理系統的原始碼分享

2022-07-27 18:00:26

注意:沒有用到資料庫使用連結串列完成此係統!

多檔案實現

正式開始

程式碼都可以直接使用

不想看的,直接複製程式碼塊裡面的內容就行!

我用的visual studio 2019   有些使用了 _s  如果是用別的編譯器,可以自行修改!

功能介紹

增,刪,改,查,退出,儲存,以至於格式化!

1.錄入學生資訊

2.檢視錄入的學生資訊(全部學生資訊)

3.修改已錄入的學生資訊(以學號)

4.刪除已錄入的學生資訊(以學號)

5.儲存資訊到檔案

6.指定查詢(以學號)

7.隱藏選項(格式化連結串列--清空)

'q'退出系統

實現功能

建立原始檔main.c 函數部分

//不一一介紹,不懂得去查就行,要學會Google!
#include "myList.h"  //引入自己寫得一個標頭檔案


//選單介面
void menu(void);//函數宣告,選單顯示函數.
//按鈕互動
void keydown(struct Node* List);

int main(void) {
	
	struct  Node* List = createrList();//建立一個叫List的連結串列
	readInfoFromFile(List, "student.txt");//讀取在student.txt的檔案  然後寫入List連結串列中


	while (true) {//一直迴圈,知道使用者不用這個程式後,輸入'q' 退出程式!


		//顯示選單
		menu(); 

		//然後讀取使用者輸入的值,進行操作!
		keydown(List);
		

		system("pause");//暫停程式用的
		system("cls");//執行完一次,就清屏一次,看起來比較舒服
	}


	system("pause");//不閃退!同上面作用

	return 0;
}



//這裡是用一個結構體,到時用來儲存我們的提示資訊
 //結構體  把選單要輸入的內容都放進這裡面
//功能
struct hint_menu {//桌面選單使用!
	char one_menu[25];
	char two_menu[30];
	char three_menu[30];
	char four_menu[30];
	char five_menu[25];
	char six_menu[25];
	char seven_menu[25];

};




//桌面選單
void menu(void) {

	//使用指標 chosen 指向我們的結構體 ,然後給它動態分配空間
// 									型別    						分配的大小
	struct hint_menu* chosen = (struct hint_menu*)malloc(sizeof(struct hint_menu));

	if (chosen) {//這裡的if()可寫可不寫,我寫了是劃分程式碼塊好看點

	//存入要輸入內容
	//這裡修改內容即可  ,  想新增就去結構體那先新增一下陣列
	strcpy_s(chosen->one_menu, sizeof(chosen->one_menu), "1.錄入學生資訊");
	strcpy_s(chosen->two_menu, sizeof(chosen->two_menu), "2.檢視已錄入的學生資訊");
	strcpy_s(chosen->three_menu, sizeof(chosen->three_menu), "3.修改已錄入的學生資訊");
	strcpy_s(chosen->four_menu, sizeof(chosen->four_menu), "4.刪除已錄入的學生資訊");
	strcpy_s(chosen->five_menu, sizeof(chosen->five_menu), "5.儲存至檔案");
	strcpy_s(chosen->six_menu, sizeof(chosen->six_menu), "6.指定查詢");
	strcpy_s(chosen->seven_menu, sizeof(chosen->seven_menu), "q.退出系統");

	//其實這樣比較麻煩

	//你們可以直接定義一個字串
	//char inset[20] = "1.錄入學生資訊";  以此類推



	//輸出你存入的內容
		printf("nnnn");
		printf("tttt******************歡迎進入學生管理系統*******************n");
		printf("tttt*tt %stttt*n", chosen->one_menu);
		printf("tttt*tt %s  tt*n", chosen->two_menu);
		printf("tttt*tt %s  tt*n", chosen->three_menu);
		printf("tttt*tt %s  tt*n", chosen->four_menu);
		printf("tttt*tt %s tttt*n", chosen->five_menu);
		printf("tttt*tt %s tttt*n", chosen->six_menu);
		printf("tttt*tt %s tttt*n", chosen->seven_menu);
		printf("tttt*********************************************************n");
		printf("tttt請輸入你的選項1~6  and  (q quit program):");

		fflush(stdout);//重新整理輸出緩衝流
	}

	

}

                                                                                                                                //功能實現  ,按鈕互動                                                                      
void keydown(struct Node* List) {

	struct  student  info;
	char num[12];
	char choose,ch;
	choose = enter();
	switch (choose) {

	
	case '1':
		printf("tttt******************錄入學生資訊******************n");
		printf("tttt請輸入要錄入的學生:學號t姓名t性別t年齡t電話tn");
		printf("tttt請輸入學號:");

		scanf_s("%s",info.number, (unsigned int)sizeof(info.number));
		
	

		printf("tttt請輸入姓名:");

		scanf_s("%s",info.name,(int)sizeof(info.name));

		printf("tttt請輸入%s的性別:",info.name);


		scanf_s("%s",info.gender,(int)sizeof(info.gender));


		printf("tttt請輸入%s的年齡:", info.name);

		scanf_s("%hd", &info.age);

		printf("tttt請輸入%s的電話:", info.name);

		scanf_s("%s", info.tel, (int)sizeof(info.tel));

		insertNodeByHead(List, info);
		printf("tttt錄入完成! Done!");
		break;


		

	case '2':
		printf("tttt***************檢視已錄入學生資訊************n");
		printfNode(List);
		printf("tttt一共有%d個人n", LengthNode(List));
		break;


	case '3':
		printf("tttt******************修改學生資訊*******************n");
		printfNode(List);
		printf("tttt請輸入需要修改的學生學號:");
		scanf_s("%s", num, (unsigned int)sizeof(num));
		upDataNode(List,num);
		break;


	case '4':
		printf("tttt******************刪除學生資訊*******************n");
		printfNode(List);
		printf("tttt請輸入需要刪除的學生學號:");
		scanf_s("%s",num,(unsigned int)sizeof(num));
		deteleNodeAppoinNumber(List, num);
		break;


	case '5':
		printf("tttt******************儲存至檔案*******************n");
		weiteInfoToFile(List, "use_stu.txt");
		printf("tttt備份完成n");
		printf("ttttDonen");
		break;


	case '6':
		printf("tttt******************指定位置查詢*******************n");
		printf("tttt請輸入想要查詢的學生學號:");
		scanf_s("%s",num, (unsigned int)sizeof(num));
		printfToInput(List,num);
		printf("ttttttttDonen");
		break;


	case '7':
		printf("tttt******************格式化連結串列*******************n");
		printf("tttt*********************************n");
		printf("tttt******************注意***********n");
		printf("tttt********此操作無法撤回!**********n");
		printf("tttt(確認請輸入[Y] 取消請選擇[q]):");
		ch = enter();
		switch (ch)
		{
		case 'Y':
			formattedLinkedList(List);
			weiteInfoToFile(List, "student.txt");
			break;
		case 'q':
			printf("tttt退出成功");
			return;
			break;
		default:
			printf("tttt(確認請輸入[Y] 取消請選擇[q]):");
			break;
		}
		break;


	case 'q':
		printf("tttt正常退出系統成功n");
		exit(0);
		break;


	default:
		printf("ntttt請重新輸入(1~5  and  (q quit program))n");
		break;

	}
	weiteInfoToFile(List,"student.txt");
}

建立原始檔標頭檔案 enter.h 部分

#pragma once  //防止重複參照
#include "myList.h" 



//處理寫入
char enter(void); //函數宣告


char enter(void) {
	short count = 1;//次數

	char input = getchar(); // 讀取單個字元
	fflush(stdin);//清空輸入快取區,防止讀取後,又讀取

	for (int i = 1; i <= 12; i++) {//如果超過誤輸入超過13次,強制退出程式
	
		if (input == 'n') {//如果讀取的一直是回車,就會執行,否則返回該值
			count++;
			
			scanf_s("%c", &input, 3);
			
			fflush(stdin);

			if (count == 5) {
				printf("ntttttt別再調皮了!n");
				continue;
			}
			else if (count == 11) {
				printf("ntttttt別在摁確認鍵了!最後一次機會了n");
				continue;

			}
			else if (count == 13) {

				printf("ntttttt程式已強制退出!byebye");
				exit(0);
			}

		}
		else { return  input; }

	}
	return 0;
}

重頭戲來咯

建立標頭檔案 myList.h

#pragma once
//前面沒有參照是應為這裡都參照了,所以參照一次標頭檔案就歐克了 
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "enter.h"


//定義一個學生型別的結構體         
struct student {

	char name[20]; //姓名
	char gender[3];//性別
	char number[12]; //學號
	char tel[12];//電話號碼
	short age;//年齡

	//需要可以在新增

};

//結點
struct Node {

	struct student data;  //資料域

	struct Node* next;		//指標域
};


//建立連結串列
struct Node* createrList(void) {

	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));

	if (headNode) {
		//初始化
		headNode->next = NULL;

	}

	return headNode;
}

//建立結點
struct Node* createNode(struct student data) {

	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

	if (newNode) {
		//把資料存進去
		newNode->data = data;
		newNode->next = NULL;
	}

	return newNode;	

}

//插入結點				引數:插入哪個連結串列  插入結點的資料是多少
void insertNodeByHead(struct Node* headNode, struct student data) {
	//建立結點
	struct Node* newNode = createNode(data);//賦值
	//使插入的結點接在 headNode後面
	newNode->next = headNode->next;
	headNode->next = newNode;

}



//列印連結串列 也就是遍歷
void  printfNode(struct Node* headNode) {

	struct Node* pMove = headNode->next;

	printf("ttttt學號t姓名t性別t年齡t電話n");
	while (pMove != NULL) {

		printf("nttttt%06st%st%st%hdt%s", pMove->data.number, pMove->data.name, pMove->data.gender, pMove->data.age, pMove->data.tel);

		pMove = pMove->next;
	}

	printf("n");

}



//指定位置刪除
void deteleNodeAppoinNumber(struct Node* headNode, char number[12]) {

	struct Node* posNode = headNode->next;
	struct Node* posFrontNode = headNode;

	if (posNode == NULL) {
		printf("tttttt表中沒有引數n");
	}
	else {


		while (strcmp(posNode->data.number,number)!=0) {//如果不是的話

			posFrontNode = posNode;
			posNode = posNode->next;

			if (posNode == NULL) {//找到最後一個也沒有找到

				printf("tttttt表中沒有該學號的學生.n");
				return;
			}
		}

		//找到了
		posFrontNode->next = posNode->next;

		free(posNode);

		printf("ttttttDone it!n");

	}


}

//更新已錄入內容
void upDataNode(struct Node* headNode, char number[12]) {

	struct Node* posNode = headNode->next;
	struct Node* posFrontNode = headNode;
	struct student info;
	char chosen;

	if (posNode == NULL) {

		printf("tttttt無法修改,該表裡面沒有內容n");

	}
	else {
		while (strcmp(posNode->data.number,number)!=0) {

			posFrontNode = posNode;
			posNode = posNode->next;

			if (posNode == NULL) {
				printf("tttttt該表中沒有此學號的學生,無法修改n");
				return;
			}

		}

		while (true) {
			printf("tttttt請輸入要修改什麼選項:"
				"ntttttt(1.學號t2.姓名t3.性別t4.年齡t5.電話)"
				"ntttttt(如果不需要了選擇'Q')n-->:");
			chosen = enter();

			fflush(stdin);

			switch (chosen) {
			case '1':
				printf("tttttt請輸入需要更改的學生資訊:n");
				printf("tttttt請輸入學號:");
				scanf_s("%s", info.number,(int)sizeof(info.number));
				strcpy_s(posNode->data.number, sizeof(posNode->data.number),info.number);

			
				printf("ttttttDone!n");
				break;

			case '2':
				printf("tttttt請輸入需要更改的學生資訊:n");
				printf("tttttt請輸入姓名:");
				scanf_s("%s", info.name, (int)sizeof(info.name));
				fflush(stdin);
				strcpy_s(posNode->data.name, sizeof(posNode->data.name), info.name);
				printf("ttttttDone!n");
				break;

			case '3':
				printf("tttttt請輸入%s的性別:", posNode->data.name);
				scanf_s("%s", info.gender, (int)sizeof(info.gender));
				fflush(stdin);
				strcpy_s(posNode->data.gender, sizeof(posNode->data.gender), info.gender);
				printf("ttttttDone!n");
				break;

			case '4':
				printf("tttttt請輸入%s的年齡:", posNode->data.name);
				scanf_s("%hd", &info.age);
				posNode->data.age = info.age;
				printf("ttttttDone!n");
				break;

			case '5':
				printf("tttttt請輸入%s的電話:", posNode->data.name);
				scanf_s("%s", info.tel, (int)sizeof(info.tel));
				fflush(stdin);
				strcpy_s(posNode->data.tel, sizeof(posNode->data.tel), info.tel);
				printf("ttttttDone!n");
				break;
			case'Q':
				printf("tttttt退出此選項");
				return;
			default:
				printf("ntttttt請重新輸入(1~5  and  (q quit ))n");
				break;
			}


		}

	}
}

//指定位置檢視
void printfToInput(struct Node* headNode, char number[12]) {

	struct Node* posNode = headNode->next;
	struct Node* posFrontNode = headNode;

	if (posNode == NULL) {
		printf("tttttt該表為空n");

	}else {
		while (strcmp(posNode->data.number,number)!=0) {
			posFrontNode = posNode;
			posNode = posNode->next;
				
			if (posNode == NULL) {
				printf("tttttt沒有找到該學生n");
				return;
			}
		
		}

		printf("tttttt學號t姓名t性別t年齡t電話n");
		printf("ntttttt%06st%st%st%hdt%s", posNode->data.number, posNode->data.name, posNode->data.gender, posNode->data.age, posNode->data.tel);

	}
	printf("n");
}



//讀檔案
bool  readInfoFromFile(struct Node* headNode, char* fileName) {

	struct student data;

	//開啟檔案
	FILE* fp;
	fopen_s(&fp, fileName, "r");

	if (fp == NULL) {
		fopen_s(&fp, fileName, "w+");
	}

	//2操作
	if (fp == NULL) { return EOF; }
	while (fscanf_s(fp, "%st%st%st%hdt%s", data.number,(int)sizeof(data.number), data.name, (int)sizeof(data.name), data.gender, (int)sizeof(data.gender), &data.age, data.tel, (int)sizeof(data.tel)) != EOF) {

		insertNodeByHead(headNode, data);


	}



	//關閉檔案
	if (fp == NULL) { return EOF; }
	fclose(fp);


	return 0;
}

//寫檔案
bool  weiteInfoToFile(struct Node* headNode, char* fileName) {
	FILE* fp;
	fopen_s(&fp, fileName, "w");


	struct Node* pMove = headNode->next;



	while (pMove) {
		if (fp == NULL) { return EOF; }
		fprintf_s(fp, "ntttttt %st%st%st%hdt%s", pMove->data.number, pMove->data.name, pMove->data.gender, pMove->data.age, pMove->data.tel);

		pMove = pMove->next;
	}




	if (fp == NULL) { return EOF; }
	fclose(fp);
	return 0;
}

//求連結串列長度
int LengthNode(struct Node* headNode) {

	int lenth = 0;
	struct Node* pMove = headNode->next;
	while (pMove) {
		lenth++;
		pMove = pMove->next;

	}
	return lenth;
}


//格式化模式!
void formattedLinkedList(struct Node* headNode) {
	struct Node* posNode ;

	if (headNode == NULL) {
		printf("tttttt該表為空n");
	}
	else {
		while (headNode != NULL) {
			posNode = headNode->next;

			free(headNode);
			headNode = posNode;

		}
	}
}

以上就是C語言實現學生管理系統的原始碼分享的詳細內容,更多關於C語言學生管理系統的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com