With my code can you Implement with linked list, adding the beers one by one and
ID: 3575496 • Letter: W
Question
With my code can you Implement with linked list, adding the beers one by one and inserting them in the correct place to avoid sorting the list
//Libraries and buffersize declartion
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 512
int searchBeer();
void viewInventory();
int placeOrder();
int selectionSort();
struct beer
{
char *name;
char *id;
int quantity;
float price;
};
int main()
{ // declared all of my variables and my temp Buffersize
int i = 0;
int size ;
char tempBuffer[BUFFER_SIZE];
int userChoice = 7;
struct beer * beers;
// Reads in the beer.dat file using the fopen fucntion
FILE *file;
file = fopen("beer.dat", "r");
fscanf(file, "%d", &size);
beers = malloc(sizeof(struct beer) * size);
// created an if statement if files does not excist
if(file == NULL)
{
printf("I Think you had enough ");
}
for(i = 0; i < size; i++)
{
//allocate memory for the name and id arrays
beers[i].name = malloc(sizeof(struct beer) * size);
beers[i].id = malloc(sizeof(struct beer) * size);
// print statements
fscanf(file, "%s", beers[i].name);
fscanf(file, "%s", beers[i].id);
fscanf(file, "%d", &beers[i].quantity);
fscanf(file, "%f", &beers[i].price);
}
while(userChoice != 0)
{
printf(" ====================== Beer ====================== ");
printf("Enter the number of the action you wish to take. ");
printf("1. Search for a Beer ");
printf("2. View Inventory ");
printf("3. Place an Order ");
printf("0. Close the Program ");
printf("================================================== ");
//get which choice the user want
fgets(tempBuffer, BUFFER_SIZE, stdin);
sscanf(tempBuffer, "%d", &userChoice);
if(userChoice == 0)
{
printf(" Closing beer program... ");
}//end if
else if(userChoice < 4)
{
switch(userChoice)
{
case 1://user chooses 1
searchBeer(beers, 0, size);
break;
case 2://user chooses 2
viewInventory(beers, 0, size);
break;
case 3://user chooses 3
placeOrder(beers);
break;
default://user chooses anything else
printf("You're drunk, go home ");
break;
}
}//end else
}//end while
return 0;
}//end main
//funciton to search for a beer in the inventory
int searchBeer(struct beer *beers, int i, int size)
{
char tempBuffer[BUFFER_SIZE];
long int userInput = 5;
while(userInput != -1)
{
printf(" =========================================================== ");
printf("Please enter the ID number of the beer you are looking for. ");
printf("Enter -1 to quit. ");
printf("=========================================================== ");
fgets(tempBuffer, BUFFER_SIZE, stdin);
tempBuffer[strlen(tempBuffer) - 1] = 0;
for(i = 0; i < size; i++)
{
//quit the program
if(strcmp("-1", tempBuffer) == 0)
{
printf(" Closing the search program... ");
return 0;
}//end if
//print the info if the id the user entered equals one of the numbers in the structure
else if(strcmp(beers[i].id, tempBuffer) == 0)
{
printf("Brand Qty. Price ");
printf("%s ", beers[i].name);
printf("%d ", beers[i].quantity);
printf("%.2f ", beers[i].price);
printf(" ");
}//end else if
}//end for
}//end while
return 0;
}//end searchBeer
//function to view the inventory of beer
void viewInventory(struct beer *beers, int i, int size)
{
printf(" ========= Inventory ========= ");
printf("Brand ID Qty. Price ");
for(i = 0; i < size; i++)
{
//sort the beers sorted and print the sorted list
selectionSort(beers, 0, size);
printf("%s ", beers[i].name);
printf("%s ", beers[i].id);
printf("%d ", beers[i].quantity);
printf("%.2f ", beers[i].price);
printf(" ");
}
printf("============================= ");
}//end viewInventory
//function to calculate the total amount a certain number of beers costs
int placeOrder(struct beer *beers, int size)
{
int userInput = 5;
int i = 0;
float total;
int quantity;
char tempBuffer[BUFFER_SIZE];
printf(" ==================== Place an Order ==================== ");
printf("Please enter the ID number of what you would like to buy. ");
printf("Enter -1 to quit. ");
printf("======================================================== ");
//get user input
fgets(tempBuffer, BUFFER_SIZE, stdin);
tempBuffer[strlen(tempBuffer) - 1] = 0;
for(i = 0; i < size; i++)
{
//close the program if the user enters -1
if(strcmp("-1", tempBuffer) == 0)
{
printf(" Closing the order program... ");
return 0;
}//end if
else if(strcmp(beers[i].id, tempBuffer) == 0)
{
printf("How many would you like to buy? ");
//get how many beers the user would like to buy
fgets(tempBuffer, BUFFER_SIZE, stdin);
tempBuffer[strlen(tempBuffer) - 1] = 0;
quantity = strtol(tempBuffer, NULL, 10);
if(quantity <= beers[i].quantity)
{
total = quantity * beers[i].price;
break;
}//end if
//if the user wants to buy more beers than we have in stock
else if(quantity > beers[i].quantity)
{
printf("We do not have that many left in stock ");
}//end else if
}//end else if
}//end for
beers[i].quantity -= quantity;
printf("Brand Qty. Price ");
printf("%s x%d $%.2f ", beers[i].name, quantity, total);
return 0;
}//end placeOrder
int selectionSort(struct beer *beers, int index, int size)
{
float num = 100;
int min;
int i = 0, j;
struct beer temp;
// recursive function
if(index == (size))
return 1;
else
{
for(j = index; j < size; j++)
{
//checking the first number is smaller than index and j is not the same value
if((beers[j].price < beers[index].price) && (j != index) && (num > beers[j].price))
{
min = j;
num = beers[j].price;
}
}
//ensuring junk doesnt get added to the array if it happens to generate in order.
if(num != 100.00)
{
temp = beers[index];
beers[index] = beers[min];
beers[min] = temp;
}
return selectionSort(beers, index + 1, size);
}
}//end selectionSort
Explanation / Answer
If you want to implement linked list in your program to store the data, then below is the code that you need to take into account :
//define a linked list node
struct node
{
beer beerData;
struct node* next;
};
//function to create beerData
struct beer *newBeer(char *name,char *id,int qty,float price)
{
struct beer* bearData =
(struct beer*) malloc(sizeof(struct beer));
beerData->name = name;
beerData->name = id;
beerData->name = qty;
beerData->name = price;
return bearData;
}
//function to create a new LinkedList node
struct node *newLLNode(beer *new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->beerData = new_data;
new_node->next = NULL;
return new_node;
}
//function to print the list
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%s : %f", temp->beerData->name,temp->beerData->price);
temp = temp->next;
}
}
//fucntion to sortedly insert the node based on the price
void sortInsert(struct node** head, struct node* new_node)
{
struct node* current;
if (*head == NULL || (*head)->beerData->price >= new_node->beerData->price)
{
new_node->next = *head;
*head = new_node;
}
else
{
current = *head;
while (current->next!=NULL &&
current->next->beerData->price < new_node->beerData->price)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
//This is the way to create beerData and add beer data to it and insert it into the linked list
struct node* head = NULL;
char name[]="Carlsberg";
char id[]="Carl001";
int qty=10;
float price= 5.5;
struct beer *beerData= newBeer(name,id,qty,price); //create beer data
struct node *new_node = newLLNode(beerData); //create a node with beer data
sortInsert(&head, new_node); //add the node in the sorted list
printList(head);
==============================================================
feel free to ask if you have any doubt :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.