老師的福音!C++隨機點名生成器
noip2018剛過,寫了點小東西來愉悅身心。
用途:隨機點名
原理:從exe檔案同目錄下的文件中匯入人員資訊(可以多重),通過rand+Hash實現,按空格鍵即可生成。
程式碼:
#include <bits/stdc++.h> #include <conio.h> #include <windows.h> static const int MAXN=101;//limit using namespace std; struct Information { char name[MAXN]; }stu[MAXN]; bool vis[MAXN]; FILE *fp; int num,cnt,randnum; char ch,filename[MAXN],line[MAXN]; inline void copyright() { puts("Program Name: Random Name.\n"); Sleep(1000); puts("Design By:BeyondLimits.\n"); Sleep(1000); puts("All rights reserved.\n"); Sleep(1000); } inline void input() { puts("Please input the file name of the name list.\n"); gets(filename); fp=fopen(filename,"r"); puts("Everything is ready.\n"); } inline void work() { while(fgets(line,sizeof(line)-1,fp)) if(line[0]!='\n'&&line[0]!=' ') sscanf(line,"%s\n",stu[cnt++].name);//input information srand(0); puts("Press Space to get a random name or press any other key to exit.\n"); while((ch=getch())==' ') { randnum=rand()%cnt; while(vis[randnum]) randnum=rand()%cnt; vis[randnum]=true; printf("%s\n",stu[randnum].name); if(++num==cnt) { puts("Program has been exited.\n"); puts("Thank you for your using.\n"); return ; } } } int main() { copyright();//copyright announce input();//input file name work();//main work return 0; }