首頁 > 軟體

基於C語言實現隨機點名器(附原始碼)

2022-07-27 18:02:30

突發奇想寫了個隨機點名器…以供使用

隨機點名器

main函數

#include "myList.h"

#define FILENAME "stu.txt"

void menu();//畫面介面;
void userOptions(Node* headNode);//使用者選項

int main(void) {
	SetConsoleTitle(L"隨機抽查系統");
	Node* List = createrList();
	readInfoFromFile(List, FILENAME);

	while (true) {

		menu();

		userOptions(List);



		system("pause");
		system("cls");

	}

	system("pause");

	return 0;
}




void menu() {
	printf("ttt學生點名系統n");
	printf("tt1)開始隨機抽查"
		"tt2)新增學生n"
		"tt3)刪除學生"
		"tt4)修改學生資訊n"
		"ttq)退出n");
	printf("請輸入你的選項:");

}

void userOptions(Node* List) {
	Student info;

	char choose = '0';

	choose = enter();

	switch (choose) {

	case '1':
		printf("ttt*開始隨機抽查*n");
		
		seekNode(List, rollCall(LengthNode(List)));
		break;
	case '2':

		printf("tttttt已有學生如下n");
		printfNode(List);
		printf("ttt*新增學生*n");
		printf("注意請從%d之後開始也就是%dn", LengthNode(List),LengthNode(List)+1);
		printf("tt請輸入學生序號:");

		scanf_s("%d",&info.num);
		
		printf("tt請輸入學生學號:");
		scanf_s("%ld", &info.number);
		printf("tt請輸入學生姓名:");
		scanf_s("%s", info.name, sizeof(info.name));
		insetNodeByHead(List, info);

		break;
	case '3':
		printf("tttttt已有學生如下n");
		printfNode(List);
		printf("ttt*刪除學生*n");
		printf("tt請輸入學生學號(後兩位即可):");
		scanf_s("%ld", &info.number);
		deleteNodeAppoinNumber(List, info.number);
		break;
	case'4':
		printf("已有學生如下n");
		printfNode(List);
		printf("ttt*修改學生資訊*n");
		printf("tt請輸入學生學號:");
		scanf_s("%ld", &info.number);
		upDataNode(List, info.number);

		break;
	case'q':
		printf("ttquit!n");
		exit(0);
		break;
	default:
		break;




	}
	weiteInfoToFile(List, FILENAME);


}

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 <time.h>
#include <windows.h>
#include "enter.h"
typedef struct student {//型別
	long int number;
	char name[10];

	int  num;//給定一個序號然後新增一個學生後就自加1;

}Student;

typedef struct Node {
	Student data;
	struct Node* next;

}Node;





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

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

	if (headNode) {
		headNode->next = NULL;
	}

	return headNode;
}


//建立結點
Node* createrNode(Student data) {

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

	if (newNode) {
		newNode->data = data;
		newNode->next = NULL;
	}

	return newNode;
}

//插入結點
void insetNodeByHead(Node* headNode, Student data) {

	Node* newNode = createrNode(data);

	newNode->next = headNode->next;
	headNode->next = newNode;

}

//刪除結點
void deleteNodeAppoinNumber(Node* headNode, long int number) {
	Node* posNode = headNode->next;
	Node* posFrontNode = headNode;

	if (posNode == NULL) {
		printf("tt表中沒有學生n");
	}
	else {

		while (posNode->data.number != number) {//沒有找到就繼續找
			posFrontNode = posNode;
			posNode = posNode->next;

			if (posNode == NULL) {//找完最後一個了還沒有
				printf("tt表中沒有該學生n");
				return;
			}
		}

		//找到了,執行刪除操作
		posFrontNode->next = posNode->next;
		free(posNode);
		printf("tt刪除完成!");
	}


}

//修改結點
void upDataNode(Node* headNode, long int number) {
	Node* posNode = headNode->next;
	Node* posFrontNode = headNode;

	char choose = '0';

	if (posNode == NULL) {
		printf("tt該表中沒有學生t");
	}
	else {
		while (posNode->data.number != number) {
			posFrontNode = posNode;
			posNode = posNode->next;

			if (posNode == NULL) {
				printf("tt表中沒有該學生n");
				return;

			}
		}

		while (true) {
			printf("tt請選擇要修改的選項:1)姓名  2)學號  q)退出!n");
			printf("tt請輸入:");
			choose = enter();


			switch (choose) {
			case '1':
				printf("tt請輸入你要更改的名字(原姓名是%s):", posNode->data.name);
				scanf_s("%s", posNode->data.name, sizeof(posNode->data.name));
				system("pause");
				break;
			case '2':
				printf("tt請輸入你要更改的學號(原學號是%ld):", posNode->data.number);
				scanf_s("%ld", &posNode->data.number);
				system("pause");
				break;
			case 'q':
				printf("ttquit!");
				return;
			default:
				printf("請輸入正確選項:");
				break;
			}
		}

	}




}
//列印結點
void printfNode(Node* headNode) {
	Node* pMove = headNode->next;


	printf("tttttttt學號tt姓名n");
	while (pMove != NULL) {
		printf("tttttttt%ldt%sn", pMove->data.number, pMove->data.name);
		pMove = pMove->next;
	}
	printf("n");
}


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

	Student data;
	boolean  one = false;
	FILE* fp;
	fopen_s(&fp, fileName, "r");

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

	}

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

		while (fscanf_s(fp, "%dt%ldt%s"
		, &data.num,&data.number, data.name, sizeof(data.name)) != EOF) {

				insetNodeByHead(headNode, data);
	
	}

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

	return 0;
}

//檔案寫
bool  weiteInfoToFile(Node* headNode, char* fileName) {
	
		

	FILE* fp;
	fopen_s(&fp, fileName, "w");
	Node* pMove = headNode->next;
	if (fp == NULL) { return EOF; }
	while (pMove) {
		
		
		fprintf_s(fp, "%dtt%ldtt%sn", pMove->data.num,pMove->data.number,pMove->data.name);
	
		pMove = pMove->next;
	}

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



//求出連結串列長度然後返回
int LengthNode(struct Node* headNode) {
	int length = 0;

	struct Node* pMove = headNode->next;

	while (pMove) {
		length++;
		pMove = pMove->next;

	}

	return length;
}



//讀取亂數然後選出該學生
void seekNode(Node* headNode, long int rand_1) {
	Node* posNode = headNode->next;
	Node* posFrontNode = headNode;

	if (posNode == NULL) {
		printf("tt該表中沒有學生t");
	}
	else
	{	//這裡的number改為num
		while (posNode->data.num != rand_1) {
			posFrontNode = posNode;
			posNode = posNode->next;
			if (posNode == NULL) {
				printf("tt該表中沒有這這個學號(%ld)的學生n", rand_1);
				return;
			}
		}


			printf("就決定是你了->");
			printf("tt%ldt%snnnnn", posNode->data.number, posNode->data.name);
		
	}


}


//產生亂數
long int rollCall(long int length) {
	long int number;
	srand((unsigned)time(NULL));
	number = rand() % length + 1;//33+40;//length+1
	return number;
}

到此這篇關於基於C語言實現隨機點名器(附原始碼)的文章就介紹到這了,更多相關C語言隨機點名器內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


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