C   19
getid
Guest on 20th September 2023 12:12:58 AM


  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4.  
  5. int  getid(char *str);
  6. void add(char *str,int line);
  7. void output(void);
  8.  
  9. int  n=0,vark[110][1100]={{0}};
  10. char var[110][110];
  11.  
  12. int main(void){
  13.         int  k=0,i,j=0,l;
  14.         char str[1100],*p;
  15.         char command[32][20]={"auto","break","case","char","const","continue","default","do",
  16.                                                    "double","else","enum","extern","float","for","goto","if",
  17.                                                    "int","long","register","return","short","signed","sizeof",
  18.                                                    "static","struct","switch","typedef","union","unsigned","void",
  19.                                                    "volatile","while"};
  20.         while(gets(str)!=NULL){
  21.                 k++;
  22.                 p=strtok(str,"\n`~!@#$%^&*()-=+][}{'\"\\;:,|<.>/? \t");
  23.                 while( p ){
  24.             l=strlen(p);
  25.                         for(i=0;i<l;i++,p++){
  26.                                 if(*p<='9'&&*p>='0'){
  27.                                 }
  28.                                 else{   break;
  29.                                 }
  30.                         }
  31.                         if( i!=l ){
  32.                                 for(i=0;i<32;i++){
  33.                                         if(strcmp(p,command[i])==0)j++;
  34.                                 }
  35.                                 if(j==0){
  36.                                         add(p,k);
  37.                                 }
  38.                         }
  39.                 p=strtok(NULL,"\n`~!@#$%^&*()-=+][}{'\"\\;:,|<.>/? \t");
  40.                 }
  41.         }
  42.         output();
  43. //      system("PAUSE");       
  44.         return 0;
  45. }
  46.  
  47. void add(char *str,int line){
  48.         int id;
  49.         id=getid(str);
  50.         vark[id][line]=1;
  51. }
  52.  
  53. int getid(char *str){
  54.         int i;
  55.         for(i=0;i<n;i++){
  56.                 if(strcmp(str,var[i])==0){
  57.                         return i;
  58.                 }
  59.         }
  60.                 strcpy(var[i],str);
  61.                 n++;
  62.                 return n-1;
  63. }
  64.  
  65. void output(void){
  66.         int order[110],i,j=0;
  67.         int swap;
  68.         for(i=0;i<n;i++){
  69.                 order[i]=i;
  70.         }
  71.         while(j<n){
  72.                 i=0;
  73.                 while(i<n-1){
  74.                         if(strcmp(var[order[i]],var[order[i+1]])>0){
  75.                         swap=order[i];
  76.                         order[i]=order[i+1];
  77.                     order[i+1]=swap;
  78.                         }
  79.                         i++;
  80.                 }
  81.                 j++;
  82.         }
  83.         for(i=0;i<n;i++){
  84.                 printf("%s",var[order[i]]);
  85.                 printf(":");
  86.                 for(j=1;j<=n;j++){
  87.                         if(vark[order[i]][j]!=0){
  88.                         printf(" %d",j);
  89.                         j++;
  90.                 }
  91.                 }
  92.                 printf("\n");
  93.         }
  94. }

Raw Paste

Login or Register to edit or fork this paste. It's free.