php点名器代码_C++随机点名生成器实例代码(老师们的福音!)

用途:

随机点名

原理:

从exe文件同目录下的文档中导入人员信息(可以多重),通过rand+Hash实现,按空格键即可生成。

C++中rand()函数可以用来产生随机数,但是是属于伪随机数。

rand()函数用法:

在使用rand()函数的时候,首先需要包含头文件#include ,用法是int rand(void),产生的随机数范围是0~65536,类型为unsigned int,不能超过范围。rand()函数不接受参数,默认以1为种子(即起始值)。 随机数生成器总是以相同的种子开始,所以形成的伪随机数列也相同,失去了随机意义。若要不同,此时需要使用函数srand()进行初始化。

完整示例代码:

#include

#include

#include

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;

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值