Hellko

last

Dec 12th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIMV "!@#$%^&*():\";'/.,<>[]{}|`~?\\0123456789\n\r- "
  6.  
  7. struct slova {
  8.         char word[40];
  9. };
  10.  
  11. int conc(slova*, char*, int);
  12. char caseup(char);
  13. void swap(slova *&, int, int);
  14. void sort(slova *&, int);
  15. void print(slova *, int);
  16.  
  17. int main() {
  18.         slova *slovar;
  19.         FILE *h; int fsize,i,n=0;
  20.         char *str, *pch;
  21.  
  22.         //open-file
  23.         h=fopen("D:\\a.txt","r");
  24.         if(!h) return 0;
  25.  
  26.         //file-size
  27.         fseek(h,0,SEEK_END);
  28.         fsize=ftell(h);
  29.         rewind(h);
  30.  
  31.         //read-file
  32.         str=(char*)malloc(sizeof(char)*fsize);
  33.         fread(str,sizeof(char),fsize,h);
  34.         str[fsize]='\0';
  35.  
  36.         //print-str
  37.         puts(str);
  38.  
  39.         //close-file
  40.         fclose(h);
  41.  
  42.         //case-up
  43.         for(i=0;i<fsize;i++) str[i]=caseup(str[i]);
  44.  
  45.         //del-symb + create-slovar
  46.         pch=strtok(str,SIMV);
  47.         slovar=(slova*)calloc(1,sizeof(slova));
  48.         while(pch) {
  49.                 if(!conc(slovar,pch,n)) { //del-conc
  50.                 strcpy(slovar[n++].word,pch); //slovar <- str
  51.                 slovar=(slova*)realloc(slovar,(n+1)*sizeof(slova)); //slovar[+1]
  52.                 }
  53.                 pch=strtok(NULL,SIMV); //next-word
  54.                 if(!pch) slovar=(slova*)realloc(slovar,(n--)*sizeof(slova)); //slovar[+1]
  55.         }
  56.  
  57.         //print
  58.         sort(slovar,n);
  59.         print(slovar,n);
  60.  
  61.         return 0;
  62. }
  63.  
  64. int conc(slova* slovar, char* pch, int n) {
  65.         for(int i=0;i<n;i++) if(strcmp(slovar[i].word,pch)==0) return 1;
  66.         return 0;
  67. }
  68.  
  69. char caseup(char u) {
  70.         if(u<='z' && u>='a') return u-32;
  71.         return u;
  72. }
  73.  
  74. void swap(slova *& slovar, int x, int y) {
  75.         slova temp;
  76.         temp=slovar[x];
  77.         slovar[x]=slovar[y];
  78.         slovar[y]=temp;
  79. }
  80.  
  81. void sort(slova *& slovar, int n) {
  82.         int max,i,j;
  83.         for(j=0;j<n-1;j++) {
  84.                 max=j;
  85.                 for(i=j;i<n;i++) {
  86.                         if(strcmp(slovar[max].word,slovar[i].word)>0) max=i;
  87.                 }
  88.                 if(max!=j) swap(slovar,max,j);
  89.         }
  90. }
  91.  
  92. void print(slova * slovar, int n) {
  93.         for(int i=0;i<n;i++) printf("[%3d]%20s\n",i+1,slovar[i].word);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment