随机生成坐次表
#include <stdio.h>#include <string.h>
#include <stdlib.h>
#include <time.h>
#define YES 1
#define NO 0
struct node {
int num;
struct node* next;
char word[16];
};
typedef struct node* list;
list head;
int count=0;
list init()
{list tail;
head=(list)malloc(sizeof(struct node));
tail=head;
tail->next=NULL;
return tail;
}
list load(list tail)
{
FILE *fp;
list p;
char word[20];
int inword=NO;
char filename[40];
int i=0;
char ch;
printf("please input filename (include file path):",filename);
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{printf("fail to open file!");
exit(1);}
system("cls");
while( (ch=fgetc(fp))!=EOF)
{ if((ch==' ')||(ch=='\t')||(ch=='\n'))
{
if(inword==YES)
{ word[i]='\0';
p=(list)malloc(sizeof(struct node));
p->num=count;
strcpy(p->word,word);
tail->next=p;
tail=p;
i=0;
}
inword=NO;
}
else if(inword==NO)
{inword=YES;
count ++;
word[0]=ch;
i++;
}
else
{
word[i]=ch;
i++;
}
}
word[i]='\0';
p=(list)malloc(sizeof(struct node));
p->num=count;
strcpy(p->word,word);
tail->next=p;
tail=p;
tail->next=NULL;
fclose(fp);
return tail;
}
int* shuijishu(int *num)
{
int i,k;
num=(int*) malloc (sizeof(int)*(count+1));
for( i=1;i<=count;i++)
num[i]=1;
srand((unsigned int)time(0));
for(i=1;i<=count;i++)
{
k=rand();
k=k%(count+1);
if(k!=0)
{for(;num[k]!=1;k=k%(count+1),k++){}
num[k]=i;}
else
i--;
}
return num;
}
void save()
{
FILE *fp;
char filename[40];
char *word;
int *num=NULL;
list p;
int i,cols,counter=0;
printf("please input the people of every group:");
scanf("%d",&cols);
printf("please input save filename (include file path):",filename);
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{printf("fail to create file!");
exit(1);
}
num=shuijishu(num);
for(i=1;i<=count;i++)
printf("%d ",num[i]);
printf("\n");
for(i=1;i<=count;i++)
{
p=head->next;
while(p!=NULL)
{ printf("%d---%s\n",p->num,p->word);
if(p->num==num[i])
{ int j=0;
while(p->word[j]!='\0')
j++;
word=(char*) malloc (sizeof(char)*(j+1));
memcpy(word,p->word,j);
counter++;
fwrite(word,sizeof(char),j,fp);
fputc(' ',fp);fputc(' ',fp);
if(counter%cols==0)
fputc('\n',fp);
break;
}
p=p->next;
}
}
fclose(fp);
}
void dislpay(list head)
{ list p;
p=head->next;
while(p!=NULL)
{printf("%d---%s\n",p->num,p->word);
p=p->next;
}
}
char menu()
{ int i;
system("cls");
printf("\n\n\n\n\n\n");
printf(" **********************************\n");
printf(" | seat table |\n");
printf(" **********************************\n");
printf(" | |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | 1.load |\n");
printf(" | |\n");
printf(" | 2.exit |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | |\n");
printf(" |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |\n");
printf("\n\n");
printf("please choice(1--2)\n");
i=getchar();
return i;
}
int main()
{
int c;
list tail;
tail=init();
do{
c=menu();
if((c<'1')&&(c>'4'))
{printf("input error!");
return 0;}
switch(c)
{
case '1':
tail=load(tail);
dislpay(head);
system("pause>nul");
save();
c='2';
}
}while(c!='2');
return 0;
}
请多多指正
seat table.rar
(2.03 KB)