Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- typedef struct node
- {
- char name[50];
- int id;
- struct node *right;
- struct node *left;
- } Node;
- void menu(void);
- Node* Insert(Node**,char*,int);
- void Print(Node**,int);
- void print_list(void);
- Node* Search_By_Name(Node**,char*);
- Node* Search_By_ID(Node**,int);
- int load(Node**);
- char* lower_all(char*);
- int main(void){
- Node* root=NULL;
- int C2,C,id_temp;
- char name_temp[50];
- menu();
- while(1){
- printf("[~]Choice [Main Menu] >");
- scanf("%d",&C);
- switch(C){
- case 0:
- menu();break;
- case 1:
- printf("\n[~]Load \n");
- int bol=load(&root);
- if(bol) printf("\n[~]File loaded Successfully !\n");
- else printf("\n[!]Invalid or Not Found File !\n\n");
- break;
- case 2:
- printf("\n[~]Insertion \n");
- printf("\n[I]Student name :");getchar();
- fgets(name_temp,sizeof(name_temp)-1,stdin);name_temp[strlen(name_temp)-1]='\0';
- printf("\n[I]Student ID :");scanf("%d",&id_temp);
- Insert(&root,name_temp,id_temp);
- printf("[~]Added Successfully !\n");
- break;
- case 3:
- printf("\n[~]Search \n\n");
- char *temp_lower;
- struct node *temp_search=NULL;
- printf("\n[1]By ID\n[2] By Name\n\n[~]Choice [Search] :");scanf("%d",&C2);
- if(C2==1){
- printf("\n[I]Student ID :");scanf("%d",&id_temp);
- temp_search=Search_By_ID(&root,id_temp);
- if(temp_search)printf("\n[~]Found!\n");
- else printf("\n[~]Not Found !\n");}
- else if(C2==2){
- printf("\n[I]Student name :");getchar();
- fgets(name_temp,sizeof(name_temp)-1,stdin);name_temp[strlen(name_temp)-1]='\0';
- temp_lower=lower_all(name_temp);
- temp_search=Search_By_Name(&root,temp_lower);
- if(temp_search)printf("\n[~]Found!\n");
- else printf("\n[~]Not Found !\n");}
- else printf("\n[!]Invalid Entry .\n\n");
- break;
- case 4:
- break;
- case 5:
- printf("\n[~]Display \n\n");
- if(root){
- printf("[~]Choices :\n[1] Pre-order\n[2] Post-order\n[3] In-order\n");
- printf("\n\n[~]Choice [Display] :");scanf("%d",&C2);
- if(C2 >=1 && C2 <=3)Print(&root,C2);
- else printf("\n[!]Invalid Entry .\n");}
- else printf("[!]List is empty !\n");
- break;
- case 6:
- printf("[~]Exiting ...\n\n");return 0;
- default:
- printf("\n[!]Invalid choice .\n[~]Please enter a valid one .\n");
- }}}
- void menu(void){
- printf("###########################################################\n");
- printf("### BST Database v0.1 ###\n");
- printf("###########################################################\n\n");
- printf("Usage :\n");
- printf("*******\n");
- printf("1)Load Data from an external file .\n");
- printf("2)Insert Data entry .\n");
- printf("3)Search for an entry .\n");
- printf("4)Delete an existing entry .\n");
- printf("5)Display current B-tree .\n");
- printf("6)Exit application .\n");
- printf("0)Re-print menu .\n\n");
- }
- Node* create_node(char *N,int I)
- {
- Node *temp=(Node*)malloc(sizeof(Node));
- strcpy(temp->name,N);
- temp->id=I;
- temp->left=NULL,temp->right=NULL;
- return temp;
- }
- Node* Insert(Node** temp,char *N,int I)
- {
- if (!(*temp))
- {
- *temp=create_node(N,I);
- }
- else if ((*temp)->id >= I) (*temp)->left=Insert(&(*temp)->left,N,I);
- else (*temp)->right=Insert(&(*temp)->right,N,I);
- return *temp;
- }
- void Print(Node** temp,int c)
- {
- if(c==1){
- if (*temp)
- {
- printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
- Print(&(*temp)->left,c);Print(&(*temp)->right,c);
- }}
- else if(c==2){
- if (*temp)
- {
- Print(&(*temp)->left,c);Print(&(*temp)->right,c);
- printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
- }}
- else {
- if (*temp)
- {
- Print(&(*temp)->left,c);
- printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
- Print(&(*temp)->right,c);
- }}}
- int load(Node** temp)
- {
- char loc_temp[200];
- char name_temp[50];
- int id_temp; FILE *cfPtr;
- printf("Choices :\n[1] Load the pre-installed file\n[2] Enter the location of the file\n");
- int C3;
- printf("\n\nChoice [Load] :");
- scanf("%d",&C3);
- switch (C3)
- {
- case 1:
- cfPtr=fopen("New_file.txt","r");
- do
- {
- fscanf(cfPtr,"%[^,]s",name_temp);
- fscanf(cfPtr,",%d\n",&id_temp);
- Insert(&(*temp),name_temp,id_temp);
- }
- while (!feof(cfPtr));return 1;
- break;
- case 2:
- printf("\nEnter the location :");getchar();
- fgets(loc_temp,sizeof(loc_temp)-1,stdin); loc_temp[strlen(loc_temp)-1]='\0';
- cfPtr=fopen(loc_temp,"r");
- if (cfPtr==NULL) {printf("File could not be opened, Try Again.\n");return 0;}
- do
- {
- fscanf(cfPtr,"%[^,]s",name_temp);
- fscanf(cfPtr,",%d\n",&id_temp);
- Insert(&(*temp),name_temp,id_temp);
- }
- while (!feof(cfPtr));return 1;
- return 1;
- break;
- default:printf("\nWrong Choice, Try Again. \n");return 0;
- }
- }
- Node* Search_By_ID(Node** temp,int I)
- {
- if(!(*temp)) return NULL;
- else if(I==(*temp)->id) return *temp;
- else if(I<(*temp)->id) return Search_By_ID(&(*temp)->left,I);
- else return Search_By_ID(&(*temp)->right,I);
- }
- Node* Search_By_Name(Node** temp,char *N)
- {
- if(!(*temp)) return NULL;
- else if(!strcmp((*temp)->name,N)) return *temp;
- else if(strcmp(N,(*temp)->name)<0) return Search_By_Name(&(*temp)->left,N);
- else return Search_By_Name(&(*temp)->right,N);
- }
- char* lower_all(char *temp)
- {
- for (int i=0;i<strlen(temp);temp[i]=tolower(temp[i]),i++); return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement