網站首頁 編程語言 正文
突發奇想寫了個隨機點名器…以供使用
隨機點名器
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("\t\t\t學生點名系統\n");
printf("\t\t1)開始隨機抽查"
"\t\t2)添加學生\n"
"\t\t3)刪除學生"
"\t\t4)修改學生信息\n"
"\t\tq)退出\n");
printf("請輸入你的選項:");
}
void userOptions(Node* List) {
Student info;
char choose = '0';
choose = enter();
switch (choose) {
case '1':
printf("\t\t\t*開始隨機抽查*\n");
seekNode(List, rollCall(LengthNode(List)));
break;
case '2':
printf("\t\t\t\t\t\t已有學生如下\n");
printfNode(List);
printf("\t\t\t*添加學生*\n");
printf("注意請從%d之后開始也就是%d\n", LengthNode(List),LengthNode(List)+1);
printf("\t\t請輸入學生序號:");
scanf_s("%d",&info.num);
printf("\t\t請輸入學生學號:");
scanf_s("%ld", &info.number);
printf("\t\t請輸入學生姓名:");
scanf_s("%s", info.name, sizeof(info.name));
insetNodeByHead(List, info);
break;
case '3':
printf("\t\t\t\t\t\t已有學生如下\n");
printfNode(List);
printf("\t\t\t*刪除學生*\n");
printf("\t\t請輸入學生學號(后兩位即可):");
scanf_s("%ld", &info.number);
deleteNodeAppoinNumber(List, info.number);
break;
case'4':
printf("已有學生如下\n");
printfNode(List);
printf("\t\t\t*修改學生信息*\n");
printf("\t\t請輸入學生學號:");
scanf_s("%ld", &info.number);
upDataNode(List, info.number);
break;
case'q':
printf("\t\tquit!\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("\n\t\t\t\t\t\t別再調皮了!\n");
continue;
}
else if (count == 11) {
printf("\n\t\t\t\t\t\t別在摁回車鍵了!最后一次機會了\n");
continue;
}
else if (count == 13) {
printf("\n\t\t\t\t\t\t程序已強制退出!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("\t\t表中沒有學生\n");
}
else {
while (posNode->data.number != number) {//沒有找到就繼續找
posFrontNode = posNode;
posNode = posNode->next;
if (posNode == NULL) {//找完最后一個了還沒有
printf("\t\t表中沒有該學生\n");
return;
}
}
//找到了,執行刪除操作
posFrontNode->next = posNode->next;
free(posNode);
printf("\t\t刪除完成!");
}
}
//修改結點
void upDataNode(Node* headNode, long int number) {
Node* posNode = headNode->next;
Node* posFrontNode = headNode;
char choose = '0';
if (posNode == NULL) {
printf("\t\t該表中沒有學生\t");
}
else {
while (posNode->data.number != number) {
posFrontNode = posNode;
posNode = posNode->next;
if (posNode == NULL) {
printf("\t\t表中沒有該學生\n");
return;
}
}
while (true) {
printf("\t\t請選擇要修改的選項:1)姓名 2)學號 q)退出!\n");
printf("\t\t請輸入:");
choose = enter();
switch (choose) {
case '1':
printf("\t\t請輸入你要更改的名字(原姓名是%s):", posNode->data.name);
scanf_s("%s", posNode->data.name, sizeof(posNode->data.name));
system("pause");
break;
case '2':
printf("\t\t請輸入你要更改的學號(原學號是%ld):", posNode->data.number);
scanf_s("%ld", &posNode->data.number);
system("pause");
break;
case 'q':
printf("\t\tquit!");
return;
default:
printf("請輸入正確選項:");
break;
}
}
}
}
//打印結點
void printfNode(Node* headNode) {
Node* pMove = headNode->next;
printf("\t\t\t\t\t\t\t\t學號\t\t姓名\n");
while (pMove != NULL) {
printf("\t\t\t\t\t\t\t\t%ld\t%s\n", 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, "%d\t%ld\t%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, "%d\t\t%ld\t\t%s\n", 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("\t\t該表中沒有學生\t");
}
else
{ //這里的number改為num
while (posNode->data.num != rand_1) {
posFrontNode = posNode;
posNode = posNode->next;
if (posNode == NULL) {
printf("\t\t該表中沒有這這個學號(%ld)的學生\n", rand_1);
return;
}
}
printf("就決定是你了->");
printf("\t\t%ld\t%s\n\n\n\n\n", 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;
}
原文鏈接:https://blog.csdn.net/m0_58520840/article/details/123851543
相關推薦
- 2022-09-07 Golang使用CGO與Plugin技術運行加載C動態庫_Golang
- 2022-09-17 c語言實現從源文件從文本到可執行文件經歷的過程_C 語言
- 2024-03-05 git的使用
- 2022-04-24 C語言中的時間函數clock()和time()你都了解嗎_C 語言
- 2023-04-03 python中super().__init__()作用詳解_python
- 2023-03-15 Pandas操作兩個Excel實現數據對應行的合并_python
- 2022-03-20 android自定義對話框實例代碼_Android
- 2022-04-28 Python?常用的print輸出函數和input輸入函數_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支