Advertisement
redsees

Untitled

May 6th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. typedef struct node {
  7. int id;
  8. char *name;
  9. struct node *right;
  10. struct node *left;
  11. } Node;
  12.  
  13. void  menu(void);
  14. void  Print(Node**,int);
  15. Node* Insert(Node**,char*,int);
  16. char* lower_all(char*);
  17. int   Load(Node**,char*);
  18. int   Save(Node**,FILE*);
  19. Node* Search(Node**,char*);
  20. Node* Delete(Node**,char*);
  21. int   isNumeric(const char*);
  22. Node* FindMin(Node**);
  23.  
  24. int main(void){
  25. Node* root=NULL;
  26. int id,c;
  27. char name[50];
  28. menu();
  29. while(1){
  30. printf("\n[I]Choice [Main Menu] > ");
  31. scanf("%d",&c);                         //REPAIR
  32. system("clear");
  33. switch(c){
  34. case 0:
  35. menu();break;
  36. case 1:
  37. //load data
  38. printf("\n\n[~]Loading Data from an external file \n\n");
  39. printf("[I]File to load data from : ");getchar();
  40. fgets(name,sizeof(name)-1,stdin);name[strlen(name)-1]='\0';
  41. if(Load(&root,name))printf("[~]Done loading data from %s .\n\n",name);
  42. else printf("[!]File not found !\n\n");
  43. break;
  44. case 2:
  45. printf("\n\n[~]Data Insertion \n\n[I]Student's Name : ");getchar();
  46. fgets(name,sizeof(name)-1,stdin);name[strlen(name)-1]='\0';
  47. printf("[I]Student's ID : ");
  48. scanf("%d",&id);
  49. Insert(&root,name,id);
  50. printf("[~]Done !\n\n");
  51. break;
  52. case 3:
  53. printf("[~]Data Look up \n\n");
  54. //Search entry
  55. printf("[~]Search :\n\n[1]By Name .\n[2]By ID .\n\n[I]Choice [Search] > ");scanf("%2s",name);getchar();
  56. if(atoi(name)>=1&&atoi(name)<=2){printf("[I]Student's %s",(atoi(name)==1)?("Name : "):("ID : "));scanf("%30[^\n]s",name);printf("\n%-20s%-20s\n%-40s\n","Name","ID","_________________________");Node* lol=Search(&root,name);
  57. printf("\n\n\t%-20s%-20d\n",lol->name,lol->id);}
  58. else printf("\n[!]Invalid Choice !\n\n");
  59. break;
  60. case 4:
  61. printf("[~]Data Deletion \n\n");
  62. //Delete entry
  63. printf("[~]Delete :\n\n[1]By Name .\n[2]By ID .\n\n[I}Choice [Delete] > ");scanf("%2s",name);getchar();
  64. if(atoi(name)>=1&&atoi(name)<=2){printf("[I]Student's %s",(atoi(name)==1)?("Name : "):("ID : "));scanf("%30[^\n]s",name);Delete(&root,name);}
  65. else printf("\n[!]Invalid Choice !\n\n");
  66. break;
  67. case 5:
  68. printf("[~]Displaying Entries \n\n");
  69. printf("[~]Display in :\n[1]Pre-Order\n[2]Post-Order\n[3]In-Order\n\n[I]Choice [Display] > ");
  70. scanf("%d",&id);                        //REPAIR
  71. if(id>=1 && id<=3){
  72. printf("\n%-20s%-20s\n%-40s\n","Name","ID","_________________________");
  73. Print(&root,id);}
  74. else printf("[!]Invalid Choice !\n\n");
  75. printf("\n#####################################\n\n");break;
  76. case 6:
  77. printf("\n\n[~]Saving Data to file \n\n");
  78. printf("[~]Please name the file you want to save data at : ");
  79. scanf("%20s",name);
  80. if(Save(&root,fopen(name,"w")))printf("\n[~]Done !\n\n");
  81. else printf("\n[!]File cannot be created due to directory permissions or invalid renaming characters used !\n\n");
  82. break;
  83. case 7:
  84. printf("[~]Exiting ...\n\n");
  85. return 0;
  86. default:
  87. printf("[!]Invalid Choice !\n\n");
  88. }}}
  89.  
  90. void menu(void){
  91. printf("###########################################################\n");
  92. printf("###                     BST Database v0.1               ###\n");
  93. printf("###########################################################\n\n");
  94. printf("Usage :\n");
  95. printf("*******\n");
  96. printf("1)Load Data from an external file .\n");
  97. printf("2)Insert Data entry .\n");
  98. printf("3)Search for an entry .\n");
  99. printf("4)Delete an existing entry .\n");
  100. printf("5)Display current B-tree .\n");
  101. printf("6)Save Data into a file .\n");
  102. printf("7)Exit application .\n");
  103. printf("0)Re-print menu .\n\n");
  104. }
  105.  
  106. int isNumeric (const char * s){
  107.     if (s == NULL || *s == '\0' || isspace(*s))return 0;
  108.     char *p;
  109.     strtod (s, &p);
  110.     return *p == '\0';
  111. }
  112.  
  113. Node* FindMin(Node** temp){
  114.     if((*temp)==NULL)          return NULL;
  115.     else if((*temp)->left==NULL)   return *temp;
  116.     else               return FindMin(&(*temp)->left);
  117. }
  118. /*
  119. Node* Delete(Node** tmp,char* data){
  120. Node* temp;
  121. if(*tmp){
  122. if(isNumeric(data)){
  123. if((*tmp)->id==atoi(data)){
  124. //Deletion process
  125. if(((*tmp)->left) && ((*tmp)->right )){
  126. temp=FindMin(&(*tmp));
  127. strcpy((*tmp)->name,(temp)->name);
  128. (*tmp)->id=(temp)->id;
  129. temp=Delete(&(*tmp)->right,(*tmp)->name);
  130. }else{
  131. temp=(*tmp);
  132. if(!((*tmp)->left))(*tmp)=(*tmp)->right;
  133. else if(!((*tmp)->right))(*tmp)=(*tmp)->left;
  134. //temp=NULL;
  135. }}
  136. Delete(&(*tmp)->left,data);Delete(&(*tmp)->right,data);
  137. }else{
  138. if(!strcmp((*tmp)->name,data)){
  139. //Deletion process
  140. if(((*tmp)->left) && ((*tmp)->right )){
  141. temp=FindMin(&(*tmp));
  142. strcpy((*tmp)->name,(temp)->name);
  143. (*tmp)->id=(temp)->id;
  144. temp=Delete(&(*tmp)->right,(*tmp)->name);
  145. }else{
  146. temp=(*tmp);
  147. if(!((*tmp)->left))(*tmp)=(*tmp)->right;
  148. else if(!((*tmp)->right))(*tmp)=(*tmp)->left;
  149. //temp=NULL;
  150. }}
  151. Delete(&(*tmp)->left,data);Delete(&(*tmp)->right,data);
  152. }
  153. }return (*tmp);}
  154. */
  155.  
  156. Node* Delete(Node** tmp,char* data){
  157. if(isNumeric(data)){
  158. //Delete using id
  159. if(!(*tmp));
  160. else if(atoi(data)>(*tmp)->id) (*tmp)->right=Delete(&(*tmp)->right,data);
  161. else if(atoi(data)<(*tmp)->id) (*tmp)->left=Delete(&(*tmp)->left,data);
  162. else if((*tmp)->left==NULL)  (*tmp)=(*tmp)->left;
  163. else if((*tmp)->right==NULL) (*tmp)=(*tmp)->right;
  164. else {
  165. Node* temp=FindMin(&(*tmp)->right);
  166. (*tmp)->id=temp->id;
  167. strcpy((*tmp)->name,temp->name);
  168. char buff[10]={};
  169. sprintf(buff,"%d",temp->id);
  170. (*tmp)->right=Delete(&(*tmp)->right,buff);
  171. }
  172. return (*tmp);
  173. }//isNumeric If
  174. else{
  175. //Delete using name
  176. if(!(*tmp));
  177. else if(atoi(data)>(*tmp)->id) (*tmp)->right=Delete(&(*tmp)->right,data);
  178. //else if(atoi(data)<(*tmp)->id) (*tmp)->left=Delete(&(*tmp)->left,data);
  179. else if((*tmp)->left==NULL)  (*tmp)=(*tmp)->left;
  180. else if((*tmp)->right==NULL) (*tmp)=(*tmp)->right;
  181. else {
  182. Node* temp=FindMin(&(*tmp)->right);
  183. (*tmp)->id=temp->id;
  184. strcpy((*tmp)->name,temp->name);
  185. char buff[10]={};
  186. sprintf(buff,"%d",temp->id);
  187. (*tmp)->right=Delete(&(*tmp)->right,buff);
  188. }
  189. return (*tmp);
  190.  
  191. }//isNumeric Else
  192. }//Function End
  193.  
  194. Node* Search(Node** tmp,char* data){
  195. if(*tmp){
  196. if(isNumeric(data)){
  197. if((*tmp)->id==atoi(data)){
  198. printf("%-20s%-20d\n",(*tmp)->name,(*tmp)->id);}
  199. Search(&(*tmp)->left,data);Search(&(*tmp)->right,data);
  200. }else{
  201. if(!strcmp((*tmp)->name,data)){
  202. printf("%-20s%-20d\n",(*tmp)->name,(*tmp)->id);}
  203. Search(&(*tmp)->left,data);Search(&(*tmp)->right,data);
  204. }}return (*tmp);}
  205.  
  206. int Save(Node** tmp,FILE* fname){
  207. if(!fname)return 0;
  208. else{
  209. if(*tmp){
  210. fprintf(fname,"%s ,%d\n",(*tmp)->name,(*tmp)->id);
  211. Save(&(*tmp)->left,fname);Save(&(*tmp)->right,fname);
  212. }}return 1;}
  213.  
  214. int Load(Node** tmp,char* fname){
  215. FILE *fp=fopen(fname,"r");
  216. if(!fp)return 0;
  217. else{
  218. char name[50],id[10];
  219. while(!feof(fp)){
  220. fscanf(fp,"%30[^,]s",name);
  221. fscanf(fp,",%4s\n",id);
  222. Insert(&(*tmp),name,atoi(id));
  223. }return 1;}}
  224.  
  225. Node* Insert(Node** temp,char *N,int I)
  226. {
  227.     Node* create_node(char *N,int I)
  228.     {
  229.     Node *temp=(Node*)malloc(sizeof(Node));
  230.     temp->name=(char*)malloc(strlen(N)+1); //HERE I AM
  231.     strcpy(temp->name,N);
  232.     temp->id=I;
  233.     temp->left=NULL,temp->right=NULL;
  234.     return temp;
  235.     }
  236.  
  237.     if (!(*temp))*temp=create_node(N,I);
  238.     else if ((*temp)->id >= I) (*temp)->left=Insert(&(*temp)->left,N,I);
  239.     else (*temp)->right=Insert(&(*temp)->right,N,I);
  240.     return *temp;
  241. }
  242.  
  243. void Print(Node** temp,int c)
  244. {
  245.     if(c==1){
  246.     if (*temp)
  247.     {
  248.         printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
  249.         Print(&(*temp)->left,c);Print(&(*temp)->right,c);
  250.     }}
  251.     else if(c==2){
  252.     if (*temp)
  253.     {
  254.         Print(&(*temp)->left,c);Print(&(*temp)->right,c);
  255.        printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
  256.     }}
  257.     else {
  258.     if (*temp)
  259.     {
  260.         Print(&(*temp)->left,c);
  261.         printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
  262.         Print(&(*temp)->right,c);
  263. }}}
  264.  
  265. char* lower_all(char *temp)
  266. {
  267.     for(int i=0;i<strlen(temp);temp[i]=tolower(temp[i]),i++); return temp;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement