Advertisement
Guest User

Untitled

a guest
Nov 20th, 2010
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.68 KB | None | 0 0
  1. /*************************************************************************
  2.  
  3.     The 2nd semester Software Technology project reimplemented in C.
  4.                 Coded by Janith Leanage.
  5.  
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19.  ************************************************************************/
  20.  
  21.  
  22. #include<stdio.h>
  23. #include<string.h>
  24.  
  25. typedef struct _node {
  26.     int no;
  27.     char bname[20];
  28.     char manuf[20];
  29.     float price;
  30.     int qty;
  31.  
  32.     struct _node *right;
  33.     struct _node *left;
  34. } node;
  35.  
  36. node *newnode(int n, char bn[], char ma[], float pr, int q)
  37. {
  38.     node *temp;
  39.     temp = (node *) malloc(sizeof(node));
  40.     temp->no = n;
  41.     temp->price = pr;
  42.     temp->qty = q;
  43.     strcpy(temp->bname, bn);
  44.     strcpy(temp->manuf, ma);
  45.  
  46.     temp->right = NULL;
  47.     temp->left = NULL;
  48.  
  49.     return temp;
  50. }
  51.  
  52. node *root;
  53.  
  54. node *insertRec(node *cur, int n, char bn[], char ma[], float pr, int q)
  55. {
  56.     if (cur == NULL)
  57.     {
  58.         node *temp = newnode(n, bn, ma, pr, q);
  59.         return temp;
  60.     }
  61.     else if (cur->no < n)
  62.         cur->right = insertRec(cur->right, n, bn, ma, pr, q);
  63.     else if (cur->no > n)
  64.         cur->left = insertRec(cur->left, n, bn, ma, pr, q);
  65.  
  66.     return cur;
  67. }
  68.  
  69. node *find(int key)
  70. {
  71.     node *cur = root;
  72.     while(cur != NULL)
  73.     {
  74.         if(cur->no == key)
  75.             return cur;
  76.         else if(cur->no < key)
  77.             cur = cur->right;
  78.         else if(cur->no > key)
  79.             cur = cur->left;
  80.     }
  81.  
  82.     return NULL;
  83. }
  84.  
  85. void insert()
  86. {
  87.     int n, q;
  88.     char bn[20];
  89.     char ma[20];
  90.     float pr;
  91.     int ans;
  92.     while(1)
  93.     {
  94.         printf("\nAdd a new item\n\n");
  95.         printf("Item ID      : ");
  96.         scanf("%d", &n);
  97.         printf("Brand name   : ");
  98.         scanf("%s", &bn);
  99.         printf("Manufacturer : ");
  100.         scanf("%s", &ma);
  101.         printf("Quantity     : ");
  102.         scanf("%d", &q);
  103.         printf("Unit price   : ");
  104.         scanf("%f", &pr);
  105.  
  106.         root = insertRec(root, n, bn, ma, pr, q);
  107.  
  108.         printf("Do you want to add more items? Press 1 for yes, any other number for no: ");
  109.         scanf("%d", &ans);
  110.         if (ans == 1)
  111.             continue;
  112.         else
  113.             break;
  114.     }
  115. }
  116.  
  117. void display()
  118. {
  119.     int key;
  120.     printf("Enter item no to display: ");
  121.     scanf("%d", &key);
  122.  
  123.     node *found = find(key);
  124.  
  125.     if(found == NULL)
  126.         printf("Item not found\n");
  127.     else
  128.         printf("%d\t%s\t%s\t%f\t%d\n", found->no, found->bname, found->manuf, found->price, found->qty);
  129. }
  130.  
  131. void postordel(node *cur)
  132. {
  133.     if(cur != NULL)
  134.     {
  135.         postordel(cur->left);
  136.         postordel(cur->right);
  137.         free(cur);
  138.     }
  139. }
  140.  
  141. void modify()
  142. {
  143.     int n, q;
  144.     char bn[20];
  145.     char ma[20];
  146.     float pr;
  147.    
  148.     printf("\nModify an item\n\n");
  149.     printf("Item ID      : ");
  150.     scanf("%d", &n);
  151.  
  152.     node *found = find(n);
  153.  
  154.     if(found == NULL)
  155.         printf("Item not found\n");
  156.     else
  157.     {
  158.         printf("%d\t%s\t%s\t%f\t%d\n", found->no, found->bname, found->manuf, found->price, found->qty);
  159.        
  160.         printf("New brand name   : ");
  161.         scanf("%s", &bn);
  162.         printf("New manufacturer : ");
  163.         scanf("%s", &ma);
  164.         printf("New quantity     : ");
  165.         scanf("%d", &q);
  166.         printf("New unit price   : ");
  167.         scanf("%f", &pr);
  168.  
  169.         found->price = pr;
  170.         found->qty = q;
  171.         strcpy(found->bname, bn);
  172.         strcpy(found->manuf, ma);
  173.     }
  174. }
  175.  
  176. void stockar()
  177. {
  178.     int i, q, add;
  179.  
  180.     printf("\nEnter item ID: ");
  181.     scanf("%d", &i);
  182.  
  183.     node *f = find(i);
  184.  
  185.     if(f == NULL)
  186.         printf("Invalid Item ID.\n");
  187.     else
  188.     {
  189.         printf("Enter 1 to add and 2 to remove items. ");
  190.         scanf("%d", &add);
  191.  
  192.         switch(add)
  193.         {
  194.             case 1: printf("Quantity to add: ");
  195.                 scanf("%d", &q);
  196.                 f->qty += q;
  197.                 printf("\nQuantity added to stock. New level of stock is %d\n", f->qty);
  198.                 break;
  199.             case 2: printf("Quantity to remove: ");
  200.                 scanf("%d", &q);
  201.                 f->qty -= q;
  202.                 printf("\nQuantity removed from stock. New level of stock is %d\n", f->qty);
  203.                 break;
  204.             default: printf("Invalid selection\n");
  205.                  break;
  206.         }      
  207.     }
  208. }
  209.  
  210. node *promote(node *cur)
  211. {
  212.     if(cur->left == NULL)
  213.         return cur;
  214.     else
  215.     {
  216.         node *suc = promote(cur->left);
  217.        
  218.         if(suc == cur->left)
  219.             cur->left == suc->right;
  220.  
  221.         return suc;
  222.     }
  223. }
  224.  
  225. node *deleteRec(int key, node *cur)
  226. {
  227.     if(cur == NULL)
  228.     {
  229.         printf("Invalid Item ID\n");
  230.         return NULL;
  231.     }
  232.     else if(key < cur->no)
  233.     {
  234.         cur->left = deleteRec(key, cur->left);
  235.         return cur;
  236.     }
  237.     else if(key > cur->no)
  238.     {
  239.         cur->right = deleteRec(key, cur->right);
  240.         return cur;
  241.     }
  242.     else if(key == cur->no)
  243.     {
  244.         if(cur->left == NULL && cur->right == NULL)
  245.         {
  246.             free(cur);
  247.             printf("Item %d deleted.\n", key);
  248.             return NULL;
  249.         }
  250.         else if(cur->left == NULL && cur->right != NULL)
  251.         {
  252.             node *temp = cur->right;
  253.             free(cur);
  254.             printf("Item %d deleted.\n", key);
  255.             return temp;
  256.         }
  257.         else if(cur->left != NULL && cur->right == NULL)
  258.         {
  259.             node *temp = cur->left;
  260.             free(cur);
  261.             printf("Item %d deleted.\n", key);
  262.             return temp;
  263.         }
  264.         else
  265.         {
  266.             node *suc = promote(cur->right);
  267.             suc->left = cur->left;
  268.             if(suc != cur->right)
  269.                 suc->right = cur->right;
  270.  
  271.             free(cur);
  272.             printf("Item %d deleted.\n", key);
  273.             return suc;
  274.         }
  275.     }
  276. }
  277. void delete()
  278. {
  279.     int i;
  280.     printf("\nEnter Item ID: ");
  281.     scanf("%d", &i);
  282.  
  283.     root = deleteRec(i, root);
  284. }
  285.  
  286. void stockbalance(node *cur)
  287. {
  288.     if(cur != NULL)
  289.     {
  290.         stockbalance(cur->left);
  291.         printf("%d\t%s\t%s\t%7.2f\t\t%d\n", cur->no, cur->bname, cur->manuf, cur->price, cur->qty);
  292.         stockbalance(cur->right);
  293.     }
  294. }
  295.        
  296.  
  297.  
  298. void mainmenu()
  299. {
  300.     printf("Welcome to Good Health pharmacy\n Inventory control system\n");
  301.     while(1)
  302.     {  
  303.         printf("\nMain menu:\n");
  304.         printf("1) Add a new item\n");
  305.         printf("2) Add stock to an existing item / Remove stock from an existing item\n");
  306.         printf("3) Modify an item\n");
  307.         printf("4) Delete an item\n");
  308.         printf("5) Print stock balance report\n");
  309.         printf("0) Exit\n");
  310.    
  311.         int dec = -1;
  312.         while (1)
  313.         {
  314.             printf("Enter an item number: ");
  315.             scanf("%d", &dec);
  316.  
  317.             if (dec < 0 || dec > 5)
  318.             {
  319.                 printf("Invalid input\n");
  320.                 continue;
  321.             }
  322.             else
  323.                 break;
  324.         }
  325.  
  326.         switch(dec)
  327.         {
  328.             case 0: return;
  329.             case 1: insert(); break;
  330.             case 2: stockar(); break;
  331.             case 3: modify(); break;
  332.             case 4: delete(); break;
  333.             case 5: printf("\nStock balance report\n"); stockbalance(root); break;
  334.         }
  335.     }
  336. }
  337.    
  338.  
  339. int main()
  340. {
  341.     mainmenu();
  342.    
  343.     postordel(root);
  344.     return 0;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement