C++隨機(jī)點(diǎn)名生成器實(shí)例代碼(老師們的福音?。?/h1>
來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次
用途:
隨機(jī)點(diǎn)名
原理:
從exe文件同目錄下的文檔中導(dǎo)入人員信息(可以多重),通過rand+Hash實(shí)現(xiàn),按空格鍵即可生成。
C++中rand()函數(shù)可以用來產(chǎn)生隨機(jī)數(shù),但是是屬于偽隨機(jī)數(shù)。
rand()函數(shù)用法:
在使用rand()函數(shù)的時(shí)候,首先需要包含頭文件#include<stdlib.h>
,用法是int rand(void)
,產(chǎn)生的隨機(jī)數(shù)范圍是0~65536,類型為unsigned int,不能超過范圍。rand()函數(shù)不接受參數(shù),默認(rèn)以1為種子(即起始值)。 隨機(jī)數(shù)生成器總是以相同的種子開始,所以形成的偽隨機(jī)數(shù)列也相同,失去了隨機(jī)意義。若要不同,此時(shí)需要使用函數(shù)srand()進(jìn)行初始化。
完整示例代碼:
#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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持。
用途:
隨機(jī)點(diǎn)名
原理:
從exe文件同目錄下的文檔中導(dǎo)入人員信息(可以多重),通過rand+Hash實(shí)現(xiàn),按空格鍵即可生成。
C++中rand()函數(shù)可以用來產(chǎn)生隨機(jī)數(shù),但是是屬于偽隨機(jī)數(shù)。
rand()函數(shù)用法:
在使用rand()函數(shù)的時(shí)候,首先需要包含頭文件#include<stdlib.h>
,用法是int rand(void)
,產(chǎn)生的隨機(jī)數(shù)范圍是0~65536,類型為unsigned int,不能超過范圍。rand()函數(shù)不接受參數(shù),默認(rèn)以1為種子(即起始值)。 隨機(jī)數(shù)生成器總是以相同的種子開始,所以形成的偽隨機(jī)數(shù)列也相同,失去了隨機(jī)意義。若要不同,此時(shí)需要使用函數(shù)srand()進(jìn)行初始化。
完整示例代碼:
#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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持。
上一篇:C語言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
欄 目:C語言
本文標(biāo)題:C++隨機(jī)點(diǎn)名生成器實(shí)例代碼(老師們的福音?。?/a>
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/638.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改