Hellko

last

Dec 12th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  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("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.     //close-file
  37.     fclose(h);
  38.  
  39.     //case-up
  40.     for(i=0;i<fsize;i++) str[i]=caseup(str[i]);
  41.  
  42.     //del-symb + create-slovar
  43.     pch=strtok(str,SIMV);
  44.     slovar=(slova*)calloc(1,sizeof(slova));
  45.     while(pch) {
  46.         if(!conc(slovar,pch,n)) { //del-conc
  47.         strcpy(slovar[n++].word,pch); //slovar <- str
  48.         slovar=(slova*)realloc(slovar,(n+1)*sizeof(slova)); //slovar[+1]
  49.         }
  50.         pch=strtok(NULL,SIMV); //next-word
  51.     }
  52.  
  53.     //print
  54.     sort(slovar,n);
  55.     print(slovar,n);
  56.  
  57.     return 0;
  58. }
  59.  
  60. int conc(slova* slovar, char* pch, int n) {
  61.     for(int i=0;i<n;i++) if(strcmp(slovar[i].word,pch)==0) return 1;
  62.     return 0;
  63. }
  64.  
  65. char caseup(char u) {
  66.     if(u<='z' && u>='a') return u-32;
  67.     return u;
  68. }
  69.  
  70. void swap(slova *& slovar, int x, int y) {
  71.     slova temp;
  72.     temp=slovar[x];
  73.     slovar[x]=slovar[y];
  74.     slovar[y]=temp;
  75. }
  76.  
  77. void sort(slova *& slovar, int n) {
  78.     int max;
  79.     for(int j=0;j<n;j++) {
  80.         max=j;
  81.         for(int i=j;i<n;i++) {
  82.             if(slovar[max].word<slovar[i].word) max=i;
  83.         }
  84.         swap(slovar,max,j);
  85.     }
  86. }
  87.  
  88. void print(slova * slovar, int n) {
  89.     for(int i=0;i<n;i++) printf("[%3d]%20s\n",i,slovar[i].word);
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment