QQ网名大全

急 求c语言随机抽取姓名源码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct
{
char str[20]; //名字
int no;  //号码
}a[99]={{" ",0}};  //初始化
int main()
{
FILE *fp;
char ch;
int i,n=0,choice,who;
fp=fopen("name.txt","r");
if(fp==NULL)
{
printf("can not open the file\n");
return 1;
}
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')n++; //一行一个名字,计算人数
}
rewind(fp);
for(i=0;i<n;i++)
{
a[i].no=i; //每人对应的号码
fscanf(fp,"%s\n",a[i].str);

}
srand(time(NULL));
for(i=1;i<n;i++)
{
printf("=============\n1.抽取 0.退出\n=============\n");
scanf("%d",&choice);
if(choice==1)
{
loop:
who=rand()%n+1;
if(a[who].no==0)goto loop;
puts(a[who].str);
a[who].no=0; //抽取过的标志为0
}
else break;
}
fclose(fp);
return 0;
}

 

佚名
2024-06-17 13:52:35
最佳回答
类似问题(10)