Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define SIMV "!@#$%^&*():\";'/.,<>[]{}|`~?\\0123456789\n\r- "
- struct slova {
- char word[40];
- };
- int conc(slova*, char*, int);
- char caseup(char);
- void swap(slova *&, int, int);
- void sort(slova *&, int);
- void print(slova *, int);
- int main() {
- slova *slovar;
- FILE *h; int fsize,i,n=0;
- char *str, *pch;
- //open-file
- h=fopen("D:\\a.txt","r");
- if(!h) return 0;
- //file-size
- fseek(h,0,SEEK_END);
- fsize=ftell(h);
- rewind(h);
- //read-file
- str=(char*)malloc(sizeof(char)*fsize);
- fread(str,sizeof(char),fsize,h);
- str[fsize]='\0';
- //print-str
- puts(str);
- //close-file
- fclose(h);
- //case-up
- for(i=0;i<fsize;i++) str[i]=caseup(str[i]);
- //del-symb + create-slovar
- pch=strtok(str,SIMV);
- slovar=(slova*)calloc(1,sizeof(slova));
- while(pch) {
- if(!conc(slovar,pch,n)) { //del-conc
- strcpy(slovar[n++].word,pch); //slovar <- str
- slovar=(slova*)realloc(slovar,(n+1)*sizeof(slova)); //slovar[+1]
- }
- pch=strtok(NULL,SIMV); //next-word
- if(!pch) slovar=(slova*)realloc(slovar,(n--)*sizeof(slova)); //slovar[+1]
- }
- //print
- sort(slovar,n);
- print(slovar,n);
- return 0;
- }
- int conc(slova* slovar, char* pch, int n) {
- for(int i=0;i<n;i++) if(strcmp(slovar[i].word,pch)==0) return 1;
- return 0;
- }
- char caseup(char u) {
- if(u<='z' && u>='a') return u-32;
- return u;
- }
- void swap(slova *& slovar, int x, int y) {
- slova temp;
- temp=slovar[x];
- slovar[x]=slovar[y];
- slovar[y]=temp;
- }
- void sort(slova *& slovar, int n) {
- int max,i,j;
- for(j=0;j<n-1;j++) {
- max=j;
- for(i=j;i<n;i++) {
- if(strcmp(slovar[max].word,slovar[i].word)>0) max=i;
- }
- if(max!=j) swap(slovar,max,j);
- }
- }
- void print(slova * slovar, int n) {
- for(int i=0;i<n;i++) printf("[%3d]%20s\n",i+1,slovar[i].word);
- }
Advertisement
Add Comment
Please, Sign In to add comment