Project 9 code: #include <stdio.h> #include <stdlib.h> #include <string.h> #incl
ID: 3709935 • Letter: P
Question
Project 9 code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 30
//variables and functions
struct equipment{
char type[NAME_LEN+1];
char description[NAME_LEN+1];
int quantity;
struct equipment *next;
};
struct equipment *append_to_list(struct equipment *list);
//voids
void update(struct equipment *list);
void printList(struct equipment *list);
void clearList(struct equipment *list);
int read_line(char str[], int n);
int main(void)
{
//printf/scanf for output/input
char code;
struct equipment *e_list = NULL;
printf("Operation Code: a for appending to the list, u for updating an equipment"
", p for printing the list; q for quit. ");
//for and while loops
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': e_list = append_to_list(e_list);
break;
case 'u': update(e_list);
break;
case 'p': printList(e_list);
break;
case 'q': clearList(e_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
//function
struct equipment *append_to_list(struct equipment *list){
struct equipment *temp = (struct equipment *)malloc(sizeof(struct equipment));
//input
printf(" Enter The Equipment type : ");
scanf("%s",&temp->type);
printf(" Enter The Description of Equipment : ");
scanf("%s",&temp->description);
printf(" Enter The Quantity of Equipment : ");
scanf("%d",&temp->quantity);
if(list == NULL){
list = temp;
temp->next = NULL;
return list;
}
//loop
struct equipment *tt = list;
while(tt != NULL){
if(strcmp(tt->description,temp->description) == 0 && strcmp(tt->type,temp->type) == 0){
printf(" Equipment Already Exists !!! ");
return list;
}
if(tt->next == NULL){
break;
}
tt = tt->next;
}
tt->next = temp;
temp->next = NULL;
return list;
}
void update(struct equipment *list)
{
if(list == NULL){
return;
}
int tquan = 0;
//input with allocated memory
struct equipment *temp = (struct equipment *)malloc(sizeof(struct equipment));
printf(" Enter The Equipment type : ");
scanf("%s",&temp->type);
printf(" Enter The Description of Equipment : ");
scanf("%s",&temp->description);
printf(" Enter The Quantity to update : ");
scanf("%d",&tquan);
struct equipment *tt = list;
while(tt != NULL){
if(strcmp(tt->description,temp->description) == 0 && strcmp(tt->type,temp->type) == 0){
tt->quantity = tt->quantity + tquan;
free(temp);
return;
}
tt = tt->next;
}
printf(" Not Found Equipmnet : ");
free(temp);
}
void printList(struct equipment *list){
struct equipment *p = list;
//while loop
while(p != NULL){
printf(" Type = %s Description = %s Quantity = %d",p->type,p->description,p->quantity);
p = p->next;
}
}
void clearList(struct equipment *list)
{
//variables inlisting
struct equipment *temp = list;
struct equipment *prev = NULL;
while(temp != NULL){
prev = temp;
temp = temp->next;
free(prev);
}
free(list);
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
//End of program
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 30
//variables and functions
struct equipment{
char type[NAME_LEN+1];
char description[NAME_LEN+1];
int quantity;
struct equipment *next;
};
struct equipment *append_to_list(struct equipment *list);
//voids
void update(struct equipment *list);
void printList(struct equipment *list);
void clearList(struct equipment *list);
int read_line(char str[], int n);
int main(void)
{
//printf/scanf for output/input
char code;
struct equipment *e_list = NULL;
printf("Operation Code: a for appending to the list, u for updating an equipment"
", p for printing the list; q for quit. ");
//for and while loops
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': e_list = append_to_list(e_list);
break;
case 'u': update(e_list);
break;
case 'p': printList(e_list);
break;
case 'q': clearList(e_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
//function
struct equipment *append_to_list(struct equipment *list){
struct equipment *temp = (struct equipment *)malloc(sizeof(struct equipment));
//input
printf(" Enter The Equipment type : ");
scanf("%s",&temp->type);
printf(" Enter The Description of Equipment : ");
scanf("%s",&temp->description);
printf(" Enter The Quantity of Equipment : ");
scanf("%d",&temp->quantity);
if(list == NULL){
list = temp;
temp->next = NULL;
return list;
}
//loop
struct equipment *tt = list;
while(tt != NULL){
if(strcmp(tt->description,temp->description) == 0 && strcmp(tt->type,temp->type) == 0){
printf(" Equipment Already Exists !!! ");
return list;
}
if(tt->next == NULL){
break;
}
tt = tt->next;
}
tt->next = temp;
temp->next = NULL;
return list;
}
void update(struct equipment *list)
{
if(list == NULL){
return;
}
int tquan = 0;
//input with allocated memory
struct equipment *temp = (struct equipment *)malloc(sizeof(struct equipment));
printf(" Enter The Equipment type : ");
scanf("%s",&temp->type);
printf(" Enter The Description of Equipment : ");
scanf("%s",&temp->description);
printf(" Enter The Quantity to update : ");
scanf("%d",&tquan);
struct equipment *tt = list;
while(tt != NULL){
if(strcmp(tt->description,temp->description) == 0 && strcmp(tt->type,temp->type) == 0){
tt->quantity = tt->quantity + tquan;
free(temp);
return;
}
tt = tt->next;
}
printf(" Not Found Equipmnet : ");
free(temp);
}
void printList(struct equipment *list){
struct equipment *p = list;
//while loop
while(p != NULL){
printf(" Type = %s Description = %s Quantity = %d",p->type,p->description,p->quantity);
p = p->next;
}
}
void clearList(struct equipment *list)
{
//variables inlisting
struct equipment *temp = list;
struct equipment *prev = NULL;
while(temp != NULL){
prev = temp;
temp = temp->next;
free(prev);
}
free(list);
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
//End of program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.