Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE IN C In this project, you will be making a program to maintain a database o

ID: 3912775 • Letter: C

Question

CODE IN C

In this project, you will be making a program to maintain a database of items found in a convenience store. Each item has a name (assume a maximum of 50 characters) and price associated with it. Your program must use a dynamically allocated linked list to store the item information. Three base files (items.h, items.c and, store.c) are provided to you to start with, study them and complete this project by adding your code to these base files. items.h: This file contains definitions and prototypes of functions that are defined in items.c. The structure item is also defined to store information about a country. You need to have appropriate members for this structure.

items.c: This file contains the function definitions that you need to complete. There are four functions, append_item, search_item, print_items, and clear_items that you need to complete. In each of these function, you should have the following functionality.

• append_item: Ask the user for name of the item. Add the item to the end of the linked list if it doesn't already exist. If the item already exists, do not add anything to the list. This function returns the list. If the list is empty, you should return the pointer to the newly created item, otherwise return the list that was passed into the function.

• search_item: Ask the user for name of an item and search the item with the given name in the linked list. Function should print out the name and price of the item if it exists. If the item doesn't exist, simply print that the item was not found.

• print_items: Print out all the items in the linked list. Each item with its price in printed in a new line.

•clear_items: Clear all the items in the linked list. All dynamically allocated nodes must (individually) be freed.

store.c: This file has the main function. You can see a pointer variable to item structure which should point to the first node in the linked list. You do not need to modify this file.

IMPORTANT: 1. Do not modify the lines (source code) that are provided in the base files, only add to the files.

2. You do not need to modify store.c at all, just complete item structure in items.h and the functions append_item, search_item, print_items, and clear_items in items.c.

3. You need to also have a makefile (for easy compilation) to create an executable program called store along with the three source/header file.

4. Submit a zip file containing items.h, items.c, store.c, and Makefile

5. Test you program with the test script which does the same thing as shown below in example executions.

Example executions: (Assume your current directory has items.h, items.c, store.c, and Makefile. User inputs are in bold and underlined.)

$ make

$ ./store

Append item: a

Search item: s

Print items: p

Quit: q

Enter operation code: a

Enter item name: milk Enter price: 2.97

Enter operation code: a

Enter item name: coffee

Enter price: 6.98

Enter operation code: a

Enter item name: bread

Enter price: 2.56

Enter operation code: a

Enter item name: coffee Item already exists.

Enter operation code: s

Enter item name: coffee

Price of coffee is $6.98

Enter operation code: s Enter item name: egg

Item not found

. Enter operation code: p

Price of milk is $2.97

Price of coffee is $6.98

Price of bread is $2.56

Enter operation code: q

Before you submit: (replace with appropriate filenames) 1. Be sure your Unix source file is read & write protected. Change Unix file permission on Unix: chmod 600 2. Compile with make utility. Be sure it compiles on student cluster (sc.rc.usf.edu) with no errors and no warnings. make 3. Test your program with the shell script on Unix. Chmod +x test_store ./test_store 4. Download the source/header/make files (items.h, items.c, store.c, and Makefile) from the student cluster, compress into a single zip file, and submit on Canvas. DO NOT submit your executable programs (e.g. a.out).

/* items.c */

#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "items.h"


/*********************************************************
* append_item: Add an item to the end of the linked *
* list if it doesn't already exist. If the item already *
* exist, do not add anything to the list. This function *
* returns the list. *
*********************************************************/
struct item *append_item(struct item *item_list)
{
/********* ADD YOUR CODE HERE ******/
}


/*********************************************************
* search_item: Search an item with the given name in *
* the linked list. Function should print out the name *
* and price of the item if it exist. If the item doesn't*
* exist simply print that the item was not found. *
*********************************************************/
void search_item(struct item *item_list)
{
/********* ADD YOUR CODE HERE ******/
}


/*********************************************************
* print_items: Print out all the items in the linked *
* list, each item with its price in a new line. *
*********************************************************/
void print_items(struct item *item_list)
{
/********* ADD YOUR CODE HERE ******/
}


/*********************************************************
* clear_items: Clear all the items in the linked list. *
* All dynamically allocated nodes must be freed. *
*********************************************************/
void clear_items(struct item *item_list)
{
/********* ADD YOUR CODE HERE ******/
}


int read_line(char str[], int n)
{
int ch, i = 0;

while (isspace(ch = getchar()))
;
while (ch != ' ' && ch != EOF) {
if (i < n)
str[i++] = ch;
ch = getchar();
}
str[i] = '';
return i;
}

/* items.h */

#ifndef ITEMS_H
#define ITEMS_H

#define NAME_LEN 50

struct item {
/********* ADD YOUR CODE HERE ******/
};

struct item *append_item(struct item *item_list);
void search_item(struct item *item_list);
void print_items(struct item *item_list);
void clear_items(struct item *item_list);
int read_line(char str[], int n);

#endif

/*********************************************************
* store.c: *
* Program to maintain records of item name price in a *
* convenience store using dynamic linked list *
* *
* *
* compile: gcc -Wall -o store store.c items.c *
* usage: ./store *
*********************************************************/

#include <stdio.h>
#include "items.h"

int main(void)
{
struct item *item_list = NULL; // points to first item
char code;
  
printf("Append item: a ");
printf("Search item: s ");
printf("Print items: p ");
printf("Quit: q ");

for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': item_list = append_item(item_list);
break;
case 's': search_item(item_list);
break;
case 'p': print_items(item_list);
break;
case 'q': clear_items(item_list); return 0;
default: printf("Illegal code ");
}
printf(" ");
}

return 0;
}

Explanation / Answer

struct item{
    char name[50];
    float price;
    struct item *next;
};
struct item *append(struct item *item_list){
   char name[50];
   float price;
   struct item *s;
   printf("Enter item name:");
   scanf("%s",name);
   printf("Enter price:");
   scanf("%f",&price);
   struct item *p;
   int found = 0;
   p = item_list;
   struct item *q = (struct item *)malloc(sizeof(struct item));
   q->next = NULL;
   strcpy(q->name, name);
   q->price = price;
   if (p == NULL){
       item_list = q;
     
   }
   else {
     
       while(p != NULL){
            if (strcmp(name,p->name) == 0){
               found = 1;
               break;
            }
            s = p;
            p = p->next;
       }
       if (found == 0){
          s->next = q;
       }
       else {
          printf("Item already present ");  
       }
   }
   return item_list;
}

void search_item(struct item *item_list){
   char name[50];
   printf("Enter item name:");
   scanf("%s",name);
   struct item *p;
   int found = 0;
   p = item_list;
   if (p == NULL){
       printf("List is empty ");
   }
   else {
       while(p != NULL){
            if (strcmp(name,p->name) == 0){
               found = 1;
               printf("%-20s %20s %5s %10s ","Price of",p->name,"is",p->price);
               break;
            }
            p = p->next;
       }
       if (found == 0){
          printf("Item not found ");
       }
   }
}
void print_items(struct item *item_list){
   struct item *p = item_list;
   if (p == NULL){
       printf("List is empty ");
   }
   else {
       while(p != NULL){
            printf("%-20s %20s %5s %10s ","Price of",p->name,"is",p->price);
            p = p->next;
       }
   }
}
void clear_items(struct item *item_list){
   struct item *p = item_list;
   struct item *q;
   if (p == NULL){
       printf("List is empty ");
   }
   else {
       while(p != NULL){
            q = p;
            p = p->next;
            free(q);
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote