Advertisement
redsees

Untitled

May 2nd, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.21 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. {
  8.     char name[50];
  9.     int id;
  10.     struct node *right;
  11.     struct node *left;
  12. } Node;
  13.  
  14. void          menu(void);
  15. Node*         Insert(Node**,char*,int);
  16. void          Print(Node**,int);
  17. void          print_list(void);
  18. Node*         Search_By_Name(Node**,char*);
  19. Node*         Search_By_ID(Node**,int);
  20. int           load(Node**);
  21. char*         lower_all(char*);
  22.  
  23. int main(void){
  24. Node* root=NULL;
  25. int C2,C,id_temp;
  26. char name_temp[50];
  27. menu();
  28. while(1){
  29. printf("[~]Choice [Main Menu] >");
  30. scanf("%d",&C);
  31. switch(C){
  32. case 0:
  33. menu();break;
  34. case 1:
  35. printf("\n[~]Load \n");
  36. int bol=load(&root);
  37. if(bol) printf("\n[~]File loaded Successfully !\n");
  38. else printf("\n[!]Invalid or Not Found File !\n\n");
  39. break;
  40. case 2:
  41. printf("\n[~]Insertion \n");
  42. printf("\n[I]Student name :");getchar();
  43. fgets(name_temp,sizeof(name_temp)-1,stdin);name_temp[strlen(name_temp)-1]='\0';
  44. printf("\n[I]Student ID :");scanf("%d",&id_temp);
  45. Insert(&root,name_temp,id_temp);
  46. printf("[~]Added Successfully !\n");
  47. break;
  48. case 3:
  49. printf("\n[~]Search \n\n");
  50. char *temp_lower;
  51. struct node *temp_search=NULL;
  52. printf("\n[1]By ID\n[2] By Name\n\n[~]Choice [Search] :");scanf("%d",&C2);
  53. if(C2==1){
  54. printf("\n[I]Student ID :");scanf("%d",&id_temp);
  55. temp_search=Search_By_ID(&root,id_temp);
  56. if(temp_search)printf("\n[~]Found!\n");
  57. else printf("\n[~]Not Found !\n");}
  58. else if(C2==2){
  59. printf("\n[I]Student name :");getchar();
  60. fgets(name_temp,sizeof(name_temp)-1,stdin);name_temp[strlen(name_temp)-1]='\0';
  61. temp_lower=lower_all(name_temp);
  62. temp_search=Search_By_Name(&root,temp_lower);
  63. if(temp_search)printf("\n[~]Found!\n");
  64. else printf("\n[~]Not Found !\n");}
  65. else printf("\n[!]Invalid Entry .\n\n");
  66. break;
  67. case 4:
  68. break;
  69. case 5:
  70. printf("\n[~]Display \n\n");
  71. if(root){
  72. printf("[~]Choices :\n[1] Pre-order\n[2] Post-order\n[3] In-order\n");
  73. printf("\n\n[~]Choice [Display] :");scanf("%d",&C2);
  74. if(C2 >=1 && C2 <=3)Print(&root,C2);
  75. else printf("\n[!]Invalid Entry .\n");}
  76. else printf("[!]List is empty !\n");
  77. break;
  78. case 6:
  79. printf("[~]Exiting ...\n\n");return 0;
  80. default:
  81. printf("\n[!]Invalid choice .\n[~]Please enter a valid one .\n");
  82. }}}
  83. void menu(void){
  84. printf("###########################################################\n");
  85. printf("###         BST Database v0.1       ###\n");
  86. printf("###########################################################\n\n");
  87. printf("Usage :\n");
  88. printf("*******\n");
  89. printf("1)Load Data from an external file .\n");
  90. printf("2)Insert Data entry .\n");
  91. printf("3)Search for an entry .\n");
  92. printf("4)Delete an existing entry .\n");
  93. printf("5)Display current B-tree .\n");
  94. printf("6)Exit application .\n");
  95. printf("0)Re-print menu .\n\n");
  96. }
  97.  
  98. Node* create_node(char *N,int I)
  99. {
  100.     Node *temp=(Node*)malloc(sizeof(Node));
  101.     strcpy(temp->name,N);
  102.     temp->id=I;
  103.     temp->left=NULL,temp->right=NULL;
  104.     return temp;
  105. }
  106.  
  107. Node* Insert(Node** temp,char *N,int I)
  108. {
  109.     if (!(*temp))
  110.     {
  111.         *temp=create_node(N,I);
  112.     }
  113.     else if ((*temp)->id >= I) (*temp)->left=Insert(&(*temp)->left,N,I);
  114.     else (*temp)->right=Insert(&(*temp)->right,N,I);
  115.     return *temp;
  116. }
  117. void Print(Node** temp,int c)
  118. {
  119.     if(c==1){
  120.     if (*temp)
  121.     {
  122.         printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
  123.         Print(&(*temp)->left,c);Print(&(*temp)->right,c);
  124.     }}
  125.     else if(c==2){
  126.     if (*temp)
  127.     {
  128.         Print(&(*temp)->left,c);Print(&(*temp)->right,c);
  129.        printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
  130.     }}
  131.     else {
  132.     if (*temp)
  133.     {
  134.         Print(&(*temp)->left,c);
  135.         printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
  136.         Print(&(*temp)->right,c);
  137.     }}}
  138.  
  139. int load(Node** temp)
  140. {
  141.     char loc_temp[200];
  142.     char name_temp[50];
  143.     int id_temp; FILE *cfPtr;
  144.  
  145.     printf("Choices :\n[1] Load the pre-installed file\n[2] Enter the location of the file\n");
  146.     int C3;
  147.     printf("\n\nChoice [Load] :");
  148.     scanf("%d",&C3);
  149.     switch (C3)
  150.     {
  151.     case 1:
  152.     cfPtr=fopen("New_file.txt","r");
  153.     do
  154.     {
  155.         fscanf(cfPtr,"%[^,]s",name_temp);
  156.         fscanf(cfPtr,",%d\n",&id_temp);
  157.         Insert(&(*temp),name_temp,id_temp);
  158.     }
  159.     while (!feof(cfPtr));return 1;
  160.     break;
  161.     case 2:
  162.         printf("\nEnter the location :");getchar();
  163.         fgets(loc_temp,sizeof(loc_temp)-1,stdin); loc_temp[strlen(loc_temp)-1]='\0';
  164.         cfPtr=fopen(loc_temp,"r");
  165.         if (cfPtr==NULL) {printf("File could not be opened, Try Again.\n");return 0;}
  166.     do
  167.     {
  168.         fscanf(cfPtr,"%[^,]s",name_temp);
  169.         fscanf(cfPtr,",%d\n",&id_temp);
  170.         Insert(&(*temp),name_temp,id_temp);
  171.     }
  172.     while (!feof(cfPtr));return 1;
  173.     return 1;
  174.     break;
  175.     default:printf("\nWrong Choice, Try Again. \n");return 0;
  176. }
  177. }
  178.  
  179. Node* Search_By_ID(Node** temp,int I)
  180. {
  181.     if(!(*temp)) return NULL;
  182.     else if(I==(*temp)->id) return *temp;
  183.     else if(I<(*temp)->id) return Search_By_ID(&(*temp)->left,I);
  184.     else return Search_By_ID(&(*temp)->right,I);
  185. }
  186.  
  187. Node* Search_By_Name(Node** temp,char *N)
  188. {
  189.     if(!(*temp)) return NULL;
  190.     else if(!strcmp((*temp)->name,N)) return *temp;
  191.     else if(strcmp(N,(*temp)->name)<0) return Search_By_Name(&(*temp)->left,N);
  192.     else return Search_By_Name(&(*temp)->right,N);
  193. }
  194.  
  195.  
  196. char* lower_all(char *temp)
  197. {
  198.     for (int i=0;i<strlen(temp);temp[i]=tolower(temp[i]),i++);  return temp;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement