/* Tanel Toova * 6-4 * * */ #include #include #include #define END "LOPP" FILE *inputfile; FILE *outputfile; struct node *first_node=NULL; char input[256]; int i; struct node{ struct node *vasak; struct node *parem; char *word; int kordi; }; //allocates memory for node struct node *memalloc(void){ return (struct node *)malloc(sizeof(struct node)); } //adds node to the binary tree or increases the counter the if word is already there. //if new word should be before the last one in alphabetical order, it is added to the left else to the right struct node *add_node(struct node *pointer, char *w) { if(!pointer){ pointer=memalloc(); pointer->word=strdup(w); pointer->kordi=1; pointer->parem=NULL; pointer->vasak=NULL; }else{ if(strcasecmp(pointer->word,w)==0){ pointer->kordi++; } if(strcasecmp(pointer->word,w)>0){ pointer->vasak=add_node(pointer->vasak,w); } if(strcasecmp(pointer->word,w)<0){ pointer->parem=add_node(pointer->parem,w); } } return pointer; } //prints the binary tree into outputfile (which can be stdout) in alphabetical order int print(struct node *pointer) { if(pointer->vasak!=NULL){print(pointer->vasak);} fprintf(outputfile,"sõna %s esines %i korda\n",pointer->word,pointer->kordi); if(pointer->parem!=NULL){print(pointer->parem);} } //if no inputfile is spetsified data will be read from keyboard until "LOPP" is given int rkeyb(){ fprintf(stdout,"Insert data manually, to stop insert LOPP\n"); while(1){ fprintf(stdout,">"); scanf("%s",input); if(strcmp(END,input)==0){break;} first_node=add_node(first_node,input); } print(first_node); } //if inputfile is spetsified data will be read from it until eof int rfile(char *input){ inputfile=fopen(input,"r"); if(!inputfile){ fprintf(stderr,"Error opening file %s\n",input); exit(1); } while(1){ if ((i = fscanf(inputfile, "%s", input))== EOF){break;} first_node=add_node(first_node,input); } fclose(inputfile); print(first_node); } int main(int argc, char **argv) { if(argc>4){ fprintf(stderr,"Wrong amount of parameters spetisfied\n"); exit(1); } //if 3 command line parameters are given if(argc==4){ if(strcasecmp(argv[3],"-o")==0){ fprintf(stderr,"Illegal use of parameters\n"); exit(1); } if(strcasecmp(argv[1],"-o")==0){ outputfile=fopen(argv[2],"w"); if(!outputfile){ fprintf(stderr,"Can not open output file\n"); exit(2); } rfile(argv[3]); } if(strcasecmp(argv[2],"-o")==0){ outputfile=fopen(argv[3],"w"); if(!outputfile){ fprintf(stderr,"Can not open output file\n"); exit(2); } rfile(argv[1]); } } if(argc==3){ if(strcasecmp(argv[2],"-o")==0){ fprintf(stderr,"Illegal use of parameters\n"); exit(1); } if(strcasecmp(argv[1],"-o")!=0 && strcasecmp(argv[2],"-o")!=0){ fprintf(stderr,"Illegal use of parameters\n"); } if(strcasecmp(argv[1],"-o")==0){ outputfile=fopen(argv[2],"w"); if(!outputfile){ fprintf(stderr,"Can not open output file\n"); exit(2); } rkeyb(); } } if(argc==2){ if(strcasecmp(argv[1],"-o")==0){ fprintf(stderr,"Illegal use of parameters\n"); exit(1); } outputfile=stdout; if(!outputfile){ fprintf(stderr,"Can not open stdout\n"); exit(2); } rfile(argv[1]); } if(argc==1){ outputfile=stdout; if(!outputfile){ fprintf(stderr,"Can not open stdout\n"); exit(2); } rkeyb(); } if(outputfile){fclose(outputfile);} return(0); }