CSE 240 Homework 6, Fall 2014 (50 points) Due Saturday, October 18, 2014 at 11:5
ID: 3565512 • Letter: C
Question
CSE 240 Homework 6, Fall 2014 (50 points)
Due Saturday, October 18, 2014 at 11:59PM, plus a 24-Hour grace period
Introduction
The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures, including, enumeration and array of structures. By the end of the assignment, you should have
strong concept of ANSI C.
understood structure and the use of linked list
Reading: chapter 2, appendix (sections B.1 and B.2), and course notes (slides).
You are expected to do the majority of the assignment outside the class meetings. Should you need assistance, or have questions about the assignment, please contact the instructor or the TA during their office hours.
You are encouraged to ask and answer questions on the course discussion board. (However, do not share your answers in the course discussion board.)
Programming Exercise (50 points)
In this assignment you will create a shopping list to keep track of the items purchased. Please follow the specification below:
Define a new structure called
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Item{
char name[255];
float price;
int quantity;
struct Item *next;
};
typedef struct Item Item;
Item *shoppingList = NULL;
void insert(){
Item *temp;
temp = (Item *)malloc(sizeof(Item));
printf("Item :");
scanf("%s",temp->name);
printf("Quantity : ");
scanf("%d",&(temp->quantity));
printf("Price : $");
scanf("%f",&(temp->price));
temp->next = NULL;
if(shoppingList == NULL){
shoppingList = temp;
}else{
Item *ptr = shoppingList;
Item *prev = NULL;
while(ptr != NULL){
if(strcmp(temp->name, ptr->name) < 0){
break;
}
prev = ptr;
ptr = ptr->next;
}
if(prev == NULL){
temp->next = shoppingList;
shoppingList = temp;
}else{
temp->next = prev ->next;
prev -> next = temp;
}
}
}
void delet(){
char name[255];
Item *temp = shoppingList;
Item *prev = NULL;
printf("Enter the item you need to delete : ");
scanf("%s",name);
int flag = 0;
while(temp != NULL){
if(strcmp(temp->name, name) == 0){
flag = 1;
break;
}
prev = temp;
temp = temp->next;
}
if(flag == 0){
printf("Item not present in Shopping bag ");
return;
}
if(prev == NULL){
shoppingList = shoppingList->next;
free(temp);
}
else{
prev->next = temp->next;
free(temp);
}
}
void print(){
Item *temp = shoppingList;
float grandTotal = 0;
while(temp != NULL){
printf("Item : %s ",temp->name);
printf("Quantity : %d ",temp->quantity);
printf("Price : %f ",temp->price);
grandTotal += (temp->price)*(temp->quantity);
temp = temp->next;
}
printf("Grand Total : $%f ",grandTotal);
}
int main(){
char choice;
int flag = 0;
do{
printf("Enter Your Selection ");
printf(" (I)nsert an item ");
printf(" (D)elete an item ");
printf(" (P)rint shopping list ");
printf(" (Q)uit Application ");
fflush(stdin);
scanf("%c",&choice);
switch(choice){
case 'I' :
case 'i' :
insert();
printf(" ");
break;
case 'D' :
case 'd' :
delet();
printf(" ");
break;
case 'P' :
case 'p' :
print();
printf(" ");
break;
case 'Q' :
case 'q' :
flag = 1;
printf("Exiting Application ");
break;
default :
printf("Enter valid input and try again ");
}
}while(!flag);
return 0;
}