| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 895 人关注过本帖
标题:随机生成坐次表
只看楼主 加入收藏
xiaoyong12
Rank: 2
等 级:论坛游民
帖 子:25
专家分:14
注 册:2008-9-12
结帖率:0
收藏
 问题点数:0 回复次数:0 
随机生成坐次表
#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)
搜索更多相关主题的帖子: 坐次 随机 
2008-10-27 16:02
快速回复:随机生成坐次表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050590 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved