Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define LENGTH 80
- #define TRUE
- struct produceItem
- {
- char produce[20];
- char type[20];
- char soldBy[20];
- float price;
- int quantityInStock;
- struct produceItem *next;
- };
- void menu();
- int main() {
- menu();
- return 0;
- }
- void addProduceItem(struct produceItem **, char *, char *, char *, float, int);
- void count(void);
- void display(struct produceItem *);
- void readDataFile(struct produceItem **);
- void recursiveReverse(struct produceItem **);
- char * trim(char *c);
- void writeDataFile(struct produceItem *);
- void addProduceItem (struct produceItem **headRef, char *produce, char *type, char *soldBy, float price, int quantity)
- {
- struct produceItem* temp = (struct produceItem *) malloc(sizeof(struct produceItem));
- strcpy(temp->produce,produce);
- strcpy(temp->type, type);
- strcpy(temp->soldBy, soldBy);
- temp->price = price;
- temp ->quantityInStock = quantity;
- temp->next = (*headRef);
- (*headRef) = temp;
- }
- void display(struct produceItem *headRef)
- {
- struct produceItem *helper = headRef;
- if (helper ==NULL)
- {
- return;
- }
- else
- {
- printf("=======================================================\n");
- printf(" Item # Produce Type Sold By Price In Stock\n");
- printf("=======================================================\n");
- }
- int counter = 1;
- while(helper !=NULL)
- {
- printf("%5d %3s %-13s %-16s %-13s %6.2f %8d \n",
- counter++, " ", helper->produce, helper->type,
- helper->soldBy, helper->price, helper->quantityInStock);
- helper = helper->next;
- }
- printf("\n");
- }
- void readDataFile(struct produceItem **headRef)
- {
- const char comma[2] = ",";
- char dataLine[LENGTH];
- char *produce;
- char *type;
- char *soldBy;
- float price;
- int quantityInStock;
- char *fileName = "Assignment8Input.txt";
- printf("Trying to open file %s\n", fileName);
- FILE *filePointer = fopen("Assignment8Input.txt", "r");
- if (filePointer == 0) {
- perror("fopen");
- exit(1);
- }
- while (fgets(dataLine, LENGTH, filePointer) !=NULL)
- {
- produce = trim(strtok(dataLine, ","));
- type = trim(strtok(NULL, ","));
- soldBy = trim(strtok(NULL,","));
- price = atof(trim(strtok(NULL,",")));
- quantityInStock = atoi (trim(strtok(NULL,",")));
- addProduceItem(headRef, produce, type, soldBy, price, quantityInStock);
- }
- fclose(filePointer);
- }
- void recursiveReverse(struct produceItem** head_ref)
- {
- struct produceItem* first;
- struct produceItem* rest;
- }
- char * trim(char *c)
- {
- char * e = c + strlen(c) - 1;
- while(*c && isspace(*c))
- c++;
- while(e > c && isspace(*e))
- *e-- = '\0';
- return c;
- }
- void menu()
- {
- int menu_selection = 0;
- printf("List Operations\n");
- printf("===============\n");
- printf("1. Stock Produce Department\n");
- printf("2. Display Produce Inventory ~ Stack\n");
- printf("3. Display Produce Inventory ~ Queue\n");
- printf("4. Pop 10 Elements Off Stack and Output\n");
- printf("5. Dequeue 15 Elements Off Queue and Output\n");
- printf("6. Exit Program\n");
- printf("Enter your choice:");
- scanf("%d",&menu_selection);
- switch (menu_selection) {
- case 1:
- readDataFile(0);
- break;
- default:
- printf("Invalid Option");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement