Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- node_id* root_id;
- node_id* root_name;
- root_name = NULL;
- root_id = NULL;
- printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
- printf("choose:\n");
- printf("1- insert data\n");
- printf("2- write file to tree\n3- print\n");
- printf("4- delete\n");
- printf("5- search\n");
- printf("6- read file to tree\n7- quit\n\n");
- int command;
- char co[20];
- char name[40];
- char id_2[10];
- int id;
- char c;
- printf("Enter command: ");
- scanf(" %s", co);
- command = atoi(co);
- do
- {
- if(command<1 || command>7)
- {
- system("cls");
- printf("enter valid number\n\n");
- }
- else
- switch (command)
- {
- case 7:
- {
- system("cls");
- printf("THANK YOU!\n\n");
- printf("Ahmad Omar 2715 & Ahmad Abdulaziz 2210\n\n");
- exit(0);
- break;
- }
- case 1:
- {
- system("cls");
- printf("Enter name: ");
- while((c= getchar()) != '\n' && c != EOF);
- //gets(name);
- int j,f;
- scanf(" %[^\n]",name);
- printf("Enter id: ");
- scanf(" %[^\n]",id_2);
- int l=strlen(name);
- for(j=0; j<l; j++)
- if ( !(name[j]=='\n' || name[j] == 32 || (name[j] >= 65 && name[j] <= 90) || (name[j] >= 97 && name[j] <= 122) ))
- {
- printf("ERROR in name\n");
- f=1;
- break;
- }
- if(f==1)
- break;
- node_id *test = search_id(&root_id,atoi(id_2));
- if(test != NULL && atoi(id_2) == test->id)
- printf("ERROR, id already exist!\n");
- else if(atoi(name)==0 && atoi(id_2)> 0 && atoi(id_2) == atof(id_2))
- {
- id= atoi(id_2);
- insert(&root_id, &root_name, id, name);
- system("cls");
- printf("%d %s inserted succesfuly\n\n",id,name);
- }
- else
- {
- printf("\nERROR in name or id!\n");
- }
- break;
- }
- case 2:
- {
- system("cls");
- char file_name[50];
- printf("Enter file name: ");
- scanf("%s", file_name);
- FILE* file = fopen(file_name, "w");
- write_tree_to_file(root_id, file);
- fclose(file);
- break;
- }
- case 3:
- {
- char z[15];
- system("cls");
- printf("choose type of print:\n");
- printf("1- print preorder by id\n");
- printf("2- print preorder by name\n3- print post order by id\n");
- printf("4- print post order by name\n5- inorder by id\n");
- printf("6- print inorder by name\n");
- printf("your choice: ");
- scanf(" %[^\n]s",z);
- int x=atoi(z);
- if(x==1)
- print_preorder(root_id,0);
- else if(x==2)
- print_preorder(root_name,0);
- else if(x==3)
- print_postorder(root_id,0);
- else if(x==4)
- print_postorder(root_name,0);
- else if(x==5)
- print_inorder(root_id,0);
- else if(x==6)
- print_inorder(root_name,0);
- else
- {
- system("cls");
- printf("enter a valid choice!\n");
- break;
- }
- break;
- }
- case 4:
- {
- char z2[15];
- system("cls");
- printf("1- by name\n2- by id\n");
- printf("your choice: ");
- scanf(" %[^\n]s",z2);
- int x2=atoi(z2);
- if(x2==1)
- {
- system("cls");
- printf("Enter name: ");
- while((c= getchar()) != '\n' && c != EOF);
- gets(name);
- node_id* temp = search_name(&root_name, name);
- if (temp != NULL)
- {
- id = temp->id;
- }
- if(temp == NULL)
- printf("NOT FOUND.\n\n");
- else
- {
- root_id = delete_id(root_id, id);
- root_name = delete_name(root_name, name);
- printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
- }
- }
- else if(x2==2)
- {
- system("cls");
- printf("Enter id: ");
- scanf("%d", &id);
- node_id* temp = search_id(&root_id, id);
- if (temp != NULL)
- {
- strcpy(name, temp->name);
- }
- if(temp==NULL)
- printf("NOT FOUND.\n\n");
- else
- {
- root_id = delete_id(root_id, id);
- root_name = delete_name(root_name,name);
- printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
- }
- }
- else
- {
- system("cls");
- printf("enter a valid choice.\n");
- break;
- }
- break;
- }
- case 5:
- {
- system("cls");
- char z3[15];
- printf("1- by id\n2- by name\n");
- printf("your choice: ");
- scanf("% [^\n]s",z3);
- int x3=atoi(z3);
- if(x3==2)
- {
- system("cls");
- printf("Enter name: ");
- while((c= getchar()) != '\n' && c != EOF);
- gets(name);
- search_name_2(&root_name, name);
- }
- if(x3==1)
- {
- system("cls");
- printf("Enter id: ");
- scanf("%d", &id);
- search_id_2(&root_id, id);
- }
- else
- {
- system("cls");
- printf("enter a valid choice!\n");
- break;
- }
- break;
- }
- case 6:
- {
- system("cls");
- char file_name[50];
- printf("Enter file name: ");
- scanf("%s", file_name);
- reed_file_to_tree(&root_id, &root_name, file_name);
- break;
- }
- }
- printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
- printf("choose:\n");
- printf("1- insert data\n");
- printf("2- write file to tree\n3- print\n");
- printf("4- delete\n");
- printf("5- search\n");
- printf("6- read file to tree\n7- quit\n\n");
- printf("Enter command: ");
- scanf(" %s", co);
- command = atoi(co);
- }
- while (command != ~(1<<31));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement