#include #include #include #include #ifndef __WINAPI_CONSOLE_WRAPPER_H #define __WINAPI_CONSOLE_WRAPPER_H //#include #include int menue(); void gotoxy (int x, int y); void clrscr (void); #endif typedef struct node *ptr; struct node{ char a[15]; // this is making link list ptr next;}; typedef ptr list; typedef ptr position; void printlist(list l); void insertatlast( list l,char a[]); void insertwordtolist(char *name,list alp[]); void radixsort(list alp[]); int isvalid(char *word); void readfromfile(list alp[]); int checkforindex(char word[],int i); void printonfile(list a[]); int main() { int j; int select; char ch; char name[15]; list alp[53];// make an array of alphabet each element corspond to a leter int i=0; for(i=0;i<53;i++){ alp[i]=(list)malloc(sizeof(struct node)); if(alp[i]!=NULL) alp[i]->next=NULL;} for(;;) {clrscr(); select=menue(); if(select==2){ // read words from keyboard printf("\n enter your words and enter number 1 when finished \n "); scanf("%s",name); // this is a condition to stop reading from screen while(strcmp(name,"1")!=0){ if(isvalid(name)){ // this to make sure that the word conain letter only insertwordtolist(name,alp); // function insert the word in the correct list scanf("%s",name);}} } else if(select==1){ // read from file readfromfile(alp); } else if(select==3){ radixsort(alp); // do radix sort } // print nodes on screen else if(select==5){ for(j=1;j<27;j++){ if((alp[j]->next)!=NULL){ printf("\n this is the output for letter %c \n ",(char)(j+64)); printlist(alp[j]);}} for(j=27;j<53;j++){ if((alp[j]->next)!=NULL){ printf("\n this is the output for letter %c \n ",(char)(j+70)); printlist(alp[j]);}} printf("\n press any key to continue \n "); ch=getch(); } else if(select==4){ // print on file printonfile(alp); } else if(select==6) break; // exit the program } return 0; } void gotoxy(int x, int y) { static HANDLE hStdout = NULL; COORD coord; coord.X = x; coord.Y = y; if(!hStdout) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); } SetConsoleCursorPosition(hStdout,coord); } void clrscr(void) { static HANDLE hStdout = NULL; static CONSOLE_SCREEN_BUFFER_INFO csbi; const COORD startCoords = {0,0}; DWORD dummy; if(!hStdout) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdout,&csbi); } FillConsoleOutputCharacter(hStdout, ' ', csbi.dwSize.X * csbi.dwSize.Y, startCoords, &dummy); gotoxy(0,0); } int menue(){ char ch; do { gotoxy(38,10); printf(" Menue \n "); printf(" 1- read data from file \n "); printf(" 2- read data from screen \n "); printf(" 3- sort \n "); printf(" 4- print on a file \n "); printf(" 5-print on screen \n "); printf(" 6-Exit \n "); printf(" select a number "); gotoxy(60,20); ch=getch(); } while(!strchr("123456",ch)); return (ch-48); } void printlist(list l){ position p; p=l->next; while(p!=NULL){ printf("%s\n",p->a); p=p->next; } } void insertatlast( list l,char a[]){ position temp; position current; temp=(position)malloc(sizeof(struct node)); current=l; // make current points to head if(temp!=NULL){ strcpy(temp->a,a); while(current->next!=NULL){ current=current->next; } temp->next=NULL; current->next=temp; } } void insertwordtolist(char name[],list alp[]){ int index=14; if(strlen(name)='A'&&name[index]<='Z'){ insertatlast(alp[name[index]-'A'+1],name); } else // insert the word to one of a-z nodes if(name[index]>='a'&&name[index]<='z'){ insertatlast(alp[name[index]-'a'+27],name); } } void radixsort(list alp[]) { int i=0; // this array has been made just to make swap between the // input array and it list alp2[53]; for(i=0;i<53;i++){ alp2[i]=(list)malloc(sizeof(struct node)); if(alp2[i]!=NULL) alp2[i]->next=NULL;} i=13; // i here make index to the letter we're sorting with respect to it int j=0,choice=1,index; position current,current1,current2; for(;i>=0;i--){ if(choice==1) // this is our flag { for(j=0;j<53;j++){ current=alp[j]->next; while(current!=NULL){ current1=current; // this because i dont want to loose current // this give me where to put the word in any node index=checkforindex(current1->a,i); // this to point to the destenation node where i'll put my word in it current2=alp2[index]; while(current2->next!=NULL) // this to make sure that i insert it at the end current2=current2->next; current2->next=current1; // this will insert the word at the last of the new node alp[j]->next=current1->next; // this will make the previous node poinnt to the next node // so make a jump that;s because our node gone to the other list current=current->next; // this to go to the next node current1->next=NULL; // this to make sure that our node is at the last of the link list } } choice=2; // change the flag so in the next time it'll not come here } else if(choice==2) { for(j=0;j<53;j++){ current=alp2[j]->next; while(current!=NULL){ current1=current; // this give me where to put the word in any node index=checkforindex(current1->a,i); current2=alp[index]; while(current2->next!=NULL) current2=current2->next; current2->next=current1; alp2[j]->next=current1->next; current=current->next; current1->next=NULL; } } choice=1; } }} void readfromfile(list alp[]){ int MAX_LEN=200; FILE*fp ; char line[MAX_LEN], *result,*myword; char filename[15]; printf("\n please enter your file name e.g (me.txt) "); scanf("%s",filename); fp = fopen( filename ,"r" ) ; if( fp == NULL ) { // this when the file isn't exist printf("cannot open file" ) ; exit(0) ; } else{ // read a full line result = fgets(line,MAX_LEN,fp); while(result!=NULL){ // get the words word by word myword=strtok(result," "); while(myword!=NULL) { // this is done for the word at the end of the line if( myword[strlen(myword)-1]=='\n') myword[strlen(myword)-1]='\0'; if(isvalid(myword)){ // this to insert the word to the list insertwordtolist(myword,alp);} myword=strtok(NULL," "); } result = fgets(line,MAX_LEN,fp); }} fclose(fp); } int isvalid(char word[]){ // if the word is more than 15 letters if(strlen(word)>15) return 0; else { int i=0; // this if it contain something not letters for(i=0;i='A'&&word[i]<='Z')|(word[i]>='a'&&word[i]<='z'))) return 0; return 1; }} int checkforindex(char word[],int i){ if (strlen(word)='A'&&word[i]<='Z') // this will make it go to A-Z node return (word[i]-'A'+1); else if(word[i]>='a'&&word[i]<='z') // this will make it go to a-z nodes return (word[i]-'a'+27); // if the word valid it'll never go here return -1; } void printonfile(list alp[]){ int j=0; FILE *fp; char filename[15]; printf("\n please enter your file name to write in e.g (me.txt) "); scanf("%s",filename); fp=fopen(filename, "w"); for(j=1;j<27;j++){ if((alp[j]->next)!=NULL){ fprintf(fp,"\n this is the output for letter %c \n ",(char)(j+64)); position p; p=alp[j]->next; while(p!=NULL){ fprintf(fp,"%s\n",p->a); p=p->next; } } } for(j=27;j<53;j++){ if((alp[j]->next)!=NULL){ fprintf(fp,"\n this is the output for letter %c \n ",(char)(j+70)); position p; p=alp[j]->next; while(p!=NULL){ fprintf(fp,"%s\n",p->a); p=p->next; } } } fclose(fp); }