基于C语言实现随机点名器(附源码)
突发奇想写了个随机点名器…以供使用
随机点名器
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; }
到此这篇关于基于C语言实现随机点名器(附源码)的文章就介绍到这了,更多相关C语言随机点名器内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)