C++隨機點名生成器實例代碼(老師們的福音?。?/h1>
更新時間:2018年12月10日 14:37:50 作者:BeyondLimits
這篇文章主要給大家介紹了關(guān)于C++隨機點名生成器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
用途:
隨機點名
原理:
從exe文件同目錄下的文檔中導(dǎo)入人員信息(可以多重),通過rand+Hash實現(xiàn),按空格鍵即可生成。
C++中rand()函數(shù)可以用來產(chǎn)生隨機數(shù),但是是屬于偽隨機數(shù)。
rand()函數(shù)用法:
在使用rand()函數(shù)的時候,首先需要包含頭文件#include<stdlib.h> ,用法是int rand(void) ,產(chǎn)生的隨機數(shù)范圍是0~65536,類型為unsigned int,不能超過范圍。rand()函數(shù)不接受參數(shù),默認(rèn)以1為種子(即起始值)。 隨機數(shù)生成器總是以相同的種子開始,所以形成的偽隨機數(shù)列也相同,失去了隨機意義。若要不同,此時需要使用函數(shù)srand()進行初始化。
完整示例代碼:
#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;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
-
C++進程的創(chuàng)建和進程ID標(biāo)識詳細(xì)介紹
傳統(tǒng)的C++(C++98)中并沒有引入線程這個概念。linux和unix操作系統(tǒng)的設(shè)計采用的是多進程,進程間的通信十分方便,同時進程之間互相有著獨立的空間,不會污染其他進程的數(shù)據(jù),天然的隔離性給程序的穩(wěn)定性帶來了很大的保障 2022-08-08
-
C++結(jié)構(gòu)體數(shù)組實現(xiàn)貪吃蛇
這篇文章主要為大家詳細(xì)介紹了C++結(jié)構(gòu)體數(shù)組實現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下 2020-03-03
-
C/C++語言中結(jié)構(gòu)體的內(nèi)存分配小例子
當(dāng)未用 #pragma 指令指定編譯器的對齊位數(shù)時,結(jié)構(gòu)體按最長寬度的數(shù)據(jù)成員的寬度對齊;當(dāng)使用了 #pragma 指令指定編譯器的對齊位數(shù)時,結(jié)構(gòu)體按最長寬度的數(shù)據(jù)成員的寬度和 #pragma 指令指定的位數(shù)中的較小值對齊 2013-10-10
最新評論
用途:
隨機點名
原理:
從exe文件同目錄下的文檔中導(dǎo)入人員信息(可以多重),通過rand+Hash實現(xiàn),按空格鍵即可生成。
C++中rand()函數(shù)可以用來產(chǎn)生隨機數(shù),但是是屬于偽隨機數(shù)。
rand()函數(shù)用法:
在使用rand()函數(shù)的時候,首先需要包含頭文件#include<stdlib.h> ,用法是int rand(void) ,產(chǎn)生的隨機數(shù)范圍是0~65536,類型為unsigned int,不能超過范圍。rand()函數(shù)不接受參數(shù),默認(rèn)以1為種子(即起始值)。 隨機數(shù)生成器總是以相同的種子開始,所以形成的偽隨機數(shù)列也相同,失去了隨機意義。若要不同,此時需要使用函數(shù)srand()進行初始化。
完整示例代碼:
#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;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
C++進程的創(chuàng)建和進程ID標(biāo)識詳細(xì)介紹
傳統(tǒng)的C++(C++98)中并沒有引入線程這個概念。linux和unix操作系統(tǒng)的設(shè)計采用的是多進程,進程間的通信十分方便,同時進程之間互相有著獨立的空間,不會污染其他進程的數(shù)據(jù),天然的隔離性給程序的穩(wěn)定性帶來了很大的保障2022-08-08
C++結(jié)構(gòu)體數(shù)組實現(xiàn)貪吃蛇
這篇文章主要為大家詳細(xì)介紹了C++結(jié)構(gòu)體數(shù)組實現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
C/C++語言中結(jié)構(gòu)體的內(nèi)存分配小例子
當(dāng)未用 #pragma 指令指定編譯器的對齊位數(shù)時,結(jié)構(gòu)體按最長寬度的數(shù)據(jù)成員的寬度對齊;當(dāng)使用了 #pragma 指令指定編譯器的對齊位數(shù)時,結(jié)構(gòu)體按最長寬度的數(shù)據(jù)成員的寬度和 #pragma 指令指定的位數(shù)中的較小值對齊2013-10-10

