Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************************
- The 2nd semester Software Technology project reimplemented in C.
- Coded by Janith Leanage.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ************************************************************************/
- #include<stdio.h>
- #include<string.h>
- typedef struct _node {
- int no;
- char bname[20];
- char manuf[20];
- float price;
- int qty;
- struct _node *right;
- struct _node *left;
- } node;
- node *newnode(int n, char bn[], char ma[], float pr, int q)
- {
- node *temp;
- temp = (node *) malloc(sizeof(node));
- temp->no = n;
- temp->price = pr;
- temp->qty = q;
- strcpy(temp->bname, bn);
- strcpy(temp->manuf, ma);
- temp->right = NULL;
- temp->left = NULL;
- return temp;
- }
- node *root;
- node *insertRec(node *cur, int n, char bn[], char ma[], float pr, int q)
- {
- if (cur == NULL)
- {
- node *temp = newnode(n, bn, ma, pr, q);
- return temp;
- }
- else if (cur->no < n)
- cur->right = insertRec(cur->right, n, bn, ma, pr, q);
- else if (cur->no > n)
- cur->left = insertRec(cur->left, n, bn, ma, pr, q);
- return cur;
- }
- node *find(int key)
- {
- node *cur = root;
- while(cur != NULL)
- {
- if(cur->no == key)
- return cur;
- else if(cur->no < key)
- cur = cur->right;
- else if(cur->no > key)
- cur = cur->left;
- }
- return NULL;
- }
- void insert()
- {
- int n, q;
- char bn[20];
- char ma[20];
- float pr;
- int ans;
- while(1)
- {
- printf("\nAdd a new item\n\n");
- printf("Item ID : ");
- scanf("%d", &n);
- printf("Brand name : ");
- scanf("%s", &bn);
- printf("Manufacturer : ");
- scanf("%s", &ma);
- printf("Quantity : ");
- scanf("%d", &q);
- printf("Unit price : ");
- scanf("%f", &pr);
- root = insertRec(root, n, bn, ma, pr, q);
- printf("Do you want to add more items? Press 1 for yes, any other number for no: ");
- scanf("%d", &ans);
- if (ans == 1)
- continue;
- else
- break;
- }
- }
- void display()
- {
- int key;
- printf("Enter item no to display: ");
- scanf("%d", &key);
- node *found = find(key);
- if(found == NULL)
- printf("Item not found\n");
- else
- printf("%d\t%s\t%s\t%f\t%d\n", found->no, found->bname, found->manuf, found->price, found->qty);
- }
- void postordel(node *cur)
- {
- if(cur != NULL)
- {
- postordel(cur->left);
- postordel(cur->right);
- free(cur);
- }
- }
- void modify()
- {
- int n, q;
- char bn[20];
- char ma[20];
- float pr;
- printf("\nModify an item\n\n");
- printf("Item ID : ");
- scanf("%d", &n);
- node *found = find(n);
- if(found == NULL)
- printf("Item not found\n");
- else
- {
- printf("%d\t%s\t%s\t%f\t%d\n", found->no, found->bname, found->manuf, found->price, found->qty);
- printf("New brand name : ");
- scanf("%s", &bn);
- printf("New manufacturer : ");
- scanf("%s", &ma);
- printf("New quantity : ");
- scanf("%d", &q);
- printf("New unit price : ");
- scanf("%f", &pr);
- found->price = pr;
- found->qty = q;
- strcpy(found->bname, bn);
- strcpy(found->manuf, ma);
- }
- }
- void stockar()
- {
- int i, q, add;
- printf("\nEnter item ID: ");
- scanf("%d", &i);
- node *f = find(i);
- if(f == NULL)
- printf("Invalid Item ID.\n");
- else
- {
- printf("Enter 1 to add and 2 to remove items. ");
- scanf("%d", &add);
- switch(add)
- {
- case 1: printf("Quantity to add: ");
- scanf("%d", &q);
- f->qty += q;
- printf("\nQuantity added to stock. New level of stock is %d\n", f->qty);
- break;
- case 2: printf("Quantity to remove: ");
- scanf("%d", &q);
- f->qty -= q;
- printf("\nQuantity removed from stock. New level of stock is %d\n", f->qty);
- break;
- default: printf("Invalid selection\n");
- break;
- }
- }
- }
- node *promote(node *cur)
- {
- if(cur->left == NULL)
- return cur;
- else
- {
- node *suc = promote(cur->left);
- if(suc == cur->left)
- cur->left == suc->right;
- return suc;
- }
- }
- node *deleteRec(int key, node *cur)
- {
- if(cur == NULL)
- {
- printf("Invalid Item ID\n");
- return NULL;
- }
- else if(key < cur->no)
- {
- cur->left = deleteRec(key, cur->left);
- return cur;
- }
- else if(key > cur->no)
- {
- cur->right = deleteRec(key, cur->right);
- return cur;
- }
- else if(key == cur->no)
- {
- if(cur->left == NULL && cur->right == NULL)
- {
- free(cur);
- printf("Item %d deleted.\n", key);
- return NULL;
- }
- else if(cur->left == NULL && cur->right != NULL)
- {
- node *temp = cur->right;
- free(cur);
- printf("Item %d deleted.\n", key);
- return temp;
- }
- else if(cur->left != NULL && cur->right == NULL)
- {
- node *temp = cur->left;
- free(cur);
- printf("Item %d deleted.\n", key);
- return temp;
- }
- else
- {
- node *suc = promote(cur->right);
- suc->left = cur->left;
- if(suc != cur->right)
- suc->right = cur->right;
- free(cur);
- printf("Item %d deleted.\n", key);
- return suc;
- }
- }
- }
- void delete()
- {
- int i;
- printf("\nEnter Item ID: ");
- scanf("%d", &i);
- root = deleteRec(i, root);
- }
- void stockbalance(node *cur)
- {
- if(cur != NULL)
- {
- stockbalance(cur->left);
- printf("%d\t%s\t%s\t%7.2f\t\t%d\n", cur->no, cur->bname, cur->manuf, cur->price, cur->qty);
- stockbalance(cur->right);
- }
- }
- void mainmenu()
- {
- printf("Welcome to Good Health pharmacy\n Inventory control system\n");
- while(1)
- {
- printf("\nMain menu:\n");
- printf("1) Add a new item\n");
- printf("2) Add stock to an existing item / Remove stock from an existing item\n");
- printf("3) Modify an item\n");
- printf("4) Delete an item\n");
- printf("5) Print stock balance report\n");
- printf("0) Exit\n");
- int dec = -1;
- while (1)
- {
- printf("Enter an item number: ");
- scanf("%d", &dec);
- if (dec < 0 || dec > 5)
- {
- printf("Invalid input\n");
- continue;
- }
- else
- break;
- }
- switch(dec)
- {
- case 0: return;
- case 1: insert(); break;
- case 2: stockar(); break;
- case 3: modify(); break;
- case 4: delete(); break;
- case 5: printf("\nStock balance report\n"); stockbalance(root); break;
- }
- }
- }
- int main()
- {
- mainmenu();
- postordel(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement