zoukankan      html  css  js  c++  java
  • 全国软件专业人才开发与设计赛题之中等题“统计省份人员信息”

    2.2基于人员名单:

    李爱华,北京

    张立,吉林
    吴祖含,上海
    张颖,河北
    李文虎,北京
    许林,湖南
    赵平复,河北
    唐笑,北京
    刘小明,河北

    董其云,北京

    统计其信息,输出格式如下:

    北京

    李爱华

    李文虎 

    唐笑 

    董其云

    河北

    1

    ……

    其中省份不用考虑顺序,人员之间不用考虑顺序。

     参考程序:

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 FILE *stream;
      6 FILE *streamOut;
      7 
      8 
      9 //构造存储结构
     10 struct Node{
     11     char Pro[20];//存储省份
     12     char Name[50];//存储名字
     13     int counts;
     14     Node* nextPro;//下一省份结点
     15     Node* nextName;//下一人员结点
     16 };
     17 
     18 Node root;//根结点
     19 
     20 //添加新的省份到链表中
     21 void addPro(Node* node)
     22 {
     23     Node* tNode=root.nextPro;
     24     root.nextPro=node;
     25     node->nextPro=tNode;//插入结点
     26 }
     27 
     28 //插入proNode结点后名字
     29 //pronode中的省份跟新插入结点的省份相同
     30 void addName(Node* proNode,Node* node)
     31 {
     32     Node* pNode=proNode->nextName;
     33     proNode->nextName=node;
     34     node->nextName=pNode;
     35     proNode->counts++;
     36 }
     37 
     38 //添加一个结点,根据判断是否之前出现过,进行插入或者新建的工作
     39 //需要调用addName()和addPro()两个函数
     40 void addNode(Node* node)
     41 {
     42     if(root.nextPro==NULL)//首次加入
     43     {
     44         root.nextPro=node;
     45     }
     46     else
     47     {
     48         //循环找到相同的省份头结点
     49         Node* cuNode=root.nextPro;
     50         while(cuNode!=NULL && strcmp(cuNode->Pro,node->Pro)!=0)
     51         {
     52             cuNode=cuNode->nextPro;
     53         }
     54         if(cuNode==NULL)//没有找到相同省份的结点出现
     55         {
     56             addPro(node);
     57         }
     58         else
     59         {
     60             addName(cuNode,node);
     61         }
     62     }
     63 }
     64 
     65 //对同级的结果进行格式化的输出
     66 void outputFile()
     67 {
     68     if ((streamOut = fopen( "人员名单2.txt""w" )) != NULL)
     69     {
     70         Node* pNode=root.nextPro;
     71         while(pNode!=NULL)
     72         {
     73             fprintf(streamOut,"%s",pNode->Pro);
     74             fprintf(streamOut,"%d\n",pNode->counts);
     75             for(Node* t=pNode;t!=NULL;t=t->nextName)
     76             {
     77                 fprintf(streamOut,"%s\n",t->Name);
     78                 if(t->nextName==NULL)
     79                     fprintf(streamOut,"\n");
     80             }
     81             pNode=pNode->nextPro;
     82         }
     83         fclose( streamOut );
     84     }
     85 }
     86 
     87 
     88 void releaseNode()
     89 {
     90 
     91     Node* tNode=root.nextPro;
     92     Node* toDel;
     93     while(tNode!=NULL)
     94     {
     95         Node* delN=tNode->nextName;
     96         while(delN!=NULL)
     97         {
     98             Node* de=delN;
     99             delN=delN->nextName;
    100             free(de);
    101         }
    102         toDel=tNode;
    103         tNode=tNode->nextPro;
    104         free(toDel);
    105     }
    106 }
    107 int main()
    108 {
    109     
    110     char line[100];
    111     int position;
    112 
    113     //初始化
    114     root.nextPro=NULL;
    115     root.nextName=NULL;
    116 
    117     if( (stream = fopen( "人员名单.txt""r" )) != NULL )
    118     {
    119         //printf("打开成功\n");
    120         while(fgets( line, 100, stream ) != NULL)
    121         {
    122             int len=strlen(line);
    123             //printf("%d\n",len);
    124             if(feof(stream))
    125             {
    126                 line[len]='\n';
    127                 line[len+1]='\0';
    128                 len=strlen(line);
    129                 //printf("%d\n",len);
    130             }
    131             
    132             position=strcspn(line,",");
    133             if(position!=0)//分隔成功
    134             {
    135                 line[position]='\0';
    136                 Node* node=(Node*)malloc(sizeof(Node));
    137                 //清空数据
    138                 //memset(node->Pro,0,sizeof(node->Pro));
    139                 //memset(node->Name,0,sizeof(node->Name));
    140                 node->nextPro=NULL;
    141                 node->nextName=NULL;
    142                 node->counts=1;
    143                 strcpy(node->Name,line);
    144                 strcpy(node->Pro,&line[position+1]);
    145                 addNode(node);
    146             }
    147             //printf( "%s", line);
    148         }
    149         outputFile();
    150         releaseNode();
    151         fclose( stream );
    152         
    153     }
    154     return 1;
    155 }
    156 

    版权声明版权归作者WeiSteven所有,转载请注明! 

  • 相关阅读:
    End of 2007, where are you?
    2007年,前进!
    PHP框架Yii快速搭建高并发网站
    需要 gmail 与 wallop 邀请的 请留言给 email
    开始关注Mono了
    百度,阿里巴巴,google
    重返 cnblogs.com
    解决VMware虚拟机桥接不能上网的问题
    SecureCRT
    linux的ssh和sshd配置
  • 原文地址:https://www.cnblogs.com/weisteve/p/1804907.html
Copyright © 2011-2022 走看看