/* db.c */ #include #include #include "types.h" #include "supps.h" #include "parts.h" #include "sp.h" #include "db.h" main() { Snodeptr suppliers[MAXSIZE]; Pnodeptr parts; char line[80]; char ch; int done; makenull_supp(suppliers); makenull_part(&parts); done = FALSE; do { print_menu(); printf("Type in your option: "); getline(line,sizeof(line)); sscanf(line,"%c",&ch); switch (ch) { case '0': ins_supplier(suppliers); printf("\n"); break; case '1': del_supplier(suppliers,parts); printf("\n"); break; case '2': print_suppliers(suppliers); printf("Press RETURN to continue"); getchar(); printf("\n"); break; case '3': ins_part(&parts); printf("\n"); break; case '4': del_part(suppliers,&parts); printf("\n"); break; case '5': print_parts(parts); printf("Press RETURN to continue"); getchar(); printf("\n"); break; case '6': ins_supply(suppliers,parts); printf("\n"); break; case '7': del_supply(suppliers,parts); printf("\n"); break; case '8': prn_suppliers_given_part(suppliers,parts); printf("Press RETURN to continue"); getchar(); printf("\n"); break; case '9': prn_parts_given_supplier(suppliers,parts); printf("Press RETURN to continue"); getchar(); printf("\n"); break; case 'a': case 'A': print_supply(suppliers,parts); printf("Press RETURN to continue"); getchar(); printf("\n"); break; case 'q': case 'Q': done = TRUE; break; default: printf("Type in option again\n"); break; } } while (!done); } void print_menu() { printf("\n\n"); printf("0. INSERT SUPPLIER 3. INSERT PART \n"); printf("1. DELETE SUPPLIER 4. DELETE PART \n"); printf("2. PRINT SUPPLIERS 5. PRINT PARTS \n\n"); printf("6. INSERT A SUPPLY ELEMENT \n"); printf("7. DELETE A SUPPLY ELEMENT \n"); printf("8. PRINT SUPPLIERS GIVEN PART \n"); printf("9. PRINT PARTS GIVEN SUPPLIER \n"); printf("A. PRINT SUPPLY \n\n"); printf("Q. QUIT\n"); printf("\n\n"); } void ins_supplier(Snodeptr suppliers[]) { struct supp_element_type x; char line[80]; printf("Type in Supplier Number: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); printf("Type in Supplier Name: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sname); if (insert_supp(x,suppliers) == TRUE) printf("Supplier %10s has been inserted", x.sno); else printf("Supplier %10s already exists", x.sno); } void del_supplier(Snodeptr suppliers[], Pnodeptr parts) { struct supp_element_type x; char line[80]; printf("Type in SNO to be deleted: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); if (delete_supp(x,suppliers,parts) == TRUE) printf("Supplier %10s has been deleted", x.sno); else printf("Supplier %10s does not exist", x.sno); } void ins_part(Pnodeptr *parts) { struct part_element_type x; char line[80]; printf("Type in Part Number: "); getline(line,sizeof(line)); sscanf(line,"%s",x.pno); printf("Type in Part Name: "); getline(line,sizeof(line)); sscanf(line,"%s",x.pname); if (insert_part(x,&(*parts)) == TRUE) printf("Part %10s has been inserted", x.pno); else printf("Part %10s already exists", x.pno); } void del_part(Snodeptr suppliers[], Pnodeptr *parts) { struct part_element_type x; char line[80]; printf("Type in PNO to be deleted: "); getline(line,sizeof(line)); sscanf(line,"%s",x.pno); if (delete_part(x,suppliers,&(*parts)) == TRUE) printf("Part %10s has been deleted", x.pno); else printf("Part %10s does not exist", x.pno); } void ins_supply(Snodeptr suppliers[], Pnodeptr parts) { struct supp_element_type x; struct part_element_type y; char line[80]; printf("Type in SNO of Supply Element: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); printf("Type in PNO of Supply Element: "); getline(line,sizeof(line)); sscanf(line,"%s",y.pno); if (insert_supply(x,y,suppliers,parts) == TRUE) printf("Supply Element %10s, %10s has been inserted", x.sno, y.pno); else printf("Supply Element %10s, %10s was not inserted", x.sno, y.pno); } void del_supply(Snodeptr suppliers[], Pnodeptr parts) { struct supp_element_type x; struct part_element_type y; char line[80]; printf("Type in SNO of Supply Element: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); printf("Type in PNO of Supply Element: "); getline(line,sizeof(line)); sscanf(line,"%s",y.pno); if (delete_supply(x,y,suppliers,parts) == TRUE) printf("Supply Element %10s, %10s has been deleted", x.sno, y.pno); else printf("Supply Element %10s, %10s was not deleted", x.sno, y.pno); } void prn_suppliers_given_part(Snodeptr suppliers[], Pnodeptr parts) { struct part_element_type x; char line[80]; printf("Type in PNO: "); getline(line,sizeof(line)); sscanf(line,"%s",x.pno); if (print_suppliers_given_part(x,parts) == FALSE) printf("Part %10s does not exist\n",x.pno); } void prn_parts_given_supplier(Snodeptr suppliers[], Pnodeptr parts) { struct supp_element_type x; char line[80]; printf("Type in SNO: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); if (print_parts_given_supplier(x,suppliers) == FALSE) printf("Supplier %10s does not exist\n",x.sno); } int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; }