Question
C Language Program. PLEASE USE DYNAMIC MEMORY AND FOLLOW DIRECTIONS GIVEN. USE FUNCTIONS GIVEN ALSO. Thank you. NO FFLUSH.

Problem 5 (40 points)-Write a program Submit orders.c (Note: you wil be dynamically allocating memory for this problem. The s
Step 1: Define a struct to hold in the information for each order Step 2: Define the following functions: This function shoul
Do you want to see all orders to fill or orders already completed? Type fill or completed Already filled: Francesco Bianchi W
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Order {
   char name[50];
   char paymentType;
   int totalNoOfItems;
   double totalCost;
   int fulfillStatus;
   struct Order *next;
};
typedef struct Order order;

void input_orders(order *o, int size, char *filename) {
   order *orderList = o;
   char c[300], ch;
   int i=0, defaultLineSize = 300;
   FILE *fp;

   strcpy(c,"/home/basant/workspace/HomeworkLib/");
   strcat(c, filename);
   fp = fopen(c, "r");
   if(fp == NULL) {
       printf("Error opening file %s\n", c);
       exit(1);
   }
   memset(c, '\0', 300);

   for(int i=0; i<size; i++) {
       fgets(c, defaultLineSize, fp);

       order *newOrder = orderList;
       int lineIndex = 0;
       char delimiter=',';

       //Reading the name
       while(c[lineIndex] != delimiter) {
           newOrder->name[lineIndex] = c[lineIndex];
           lineIndex++;
       }
       newOrder->name[lineIndex] = '\0';
       lineIndex++;

       //PaymentType
       newOrder->paymentType = c[lineIndex++];
       lineIndex++;

       //No. Of Items
       int totalNoOfItems=0;
       while(c[lineIndex] != delimiter) {
           totalNoOfItems = 10*totalNoOfItems + (c[lineIndex++]-'0');
       }
       newOrder->totalNoOfItems = totalNoOfItems;
       lineIndex++;

       //TotalAmount
       double amount=0;
       while(c[lineIndex] != '.') {
           amount = amount*10 + (c[lineIndex++]-'0');
       }
       lineIndex++;
       int decimalValue=10;
       while(c[lineIndex] != delimiter) {
           amount = amount + (double)(c[lineIndex++]-'0')/decimalValue;
           decimalValue*=10;
       }
       newOrder->totalCost = amount;
       lineIndex++;

       //Fulfill Status
       newOrder->fulfillStatus = c[lineIndex]-'0';

       orderList = orderList->next;
   }
   fclose(fp);
}

void print_out(int status, order *o, int size) {
   order *orderList = o;
   for(int i=0; i<size; i++) {
       if(orderList->fulfillStatus == status) {
           printf("%s\n", orderList->name);
       }
       orderList = orderList->next;
   }
}

int pick_next(order *o, int size) {
   order *orderList = o;
   double maxOrderValue = 0;
   int index = -1;
   for(int i=0; i<size; i++) {
       if(orderList->fulfillStatus == 2) {
           if(orderList->totalCost > maxOrderValue) {
               index = i;
               maxOrderValue = orderList->totalCost;
           }
       }
       orderList = orderList->next;
   }
   return index;
}

void output_file(char *filename, order *o, int size) {
   order *orderList = o;
   FILE *fp;
   char c[300];

   strcpy(c,"/home/basant/workspace/HomeworkLib/");
   strcat(c, filename);
   fp = fopen(c, "w");
   if(fp == NULL) {
       printf("Error opening file %s\n", filename);
       exit(1);
   }

   for(int i=0; i<size; i++) {
       fprintf(fp, "%s,%c,%d,%g,%d\n",
               orderList->name,
               orderList->paymentType,
               orderList->totalNoOfItems,
               orderList->totalCost,
               orderList->fulfillStatus);
       orderList = orderList->next;
   }
   fclose(fp);
}

int main(int argc, char *argv[]) {
   order *orderList;
   char ch, filename[30];

   int size = atoi(argv[1]);
   strcpy(filename, argv[2]);

   //Creating 'size' no. of nodes
   orderList = (order*)malloc(sizeof(order));
   orderList->next = NULL;
   order *head = orderList;
   for(int i=1; i<size; i++) {
       order *newOrder = (order*)malloc(sizeof(order));
       orderList->next = newOrder;
       orderList = orderList->next;
   }
   orderList->next = NULL;
   orderList = head;
   input_orders(orderList, size, filename);

   printf("***Buongiorno Chef Bartolomeo***\n");
   while(1) {
       int input;
       char orderType[20];
       printf("\nWhat to do?\n");
       printf("1. print out orders\n");
       printf("2. pick next order to fulfill\n");
       printf("3. exit\n");
       printf("Your input here: ");
       scanf("%d", &input);

       if(input == 1) {
           printf("Do you want to see all orders to fill or orders already completed? Type fill or completed ");
           scanf("%s",orderType);
           scanf("%c", &ch); //Intentionally placed to flush \n
           if(strcmp(orderType, "fill") == 0) {
               printf("Needed to fill\n");
               print_out(2, orderList, size);
           } else if(strcmp(orderType, "completed") == 0) {
               printf("Already Filled\n");
               print_out(1, orderList, size);
           }
       } else if(input == 2) {
           order *orderToBeFulfilled = NULL;
           int index = pick_next(orderList, size);
           if(index != -1) {
               orderToBeFulfilled = orderList;
               for(int i=0; i<index; i++) {
                   orderToBeFulfilled = orderToBeFulfilled->next;
               }
           }

           if(orderToBeFulfilled == NULL) {
               printf("No orders present to be fulfilled\n");
           } else {
               char ch;
               printf("Next order to fill: %s\n", orderToBeFulfilled->name);
               printf("Go ahead and fulfill this order? y or n ");
               scanf("%c",&ch); //Intentionally placed to flush \n
               scanf("%c",&ch); //Actual reading of input
               if(ch == 'y') {
                   printf("Fulfilled!\n");
                   orderToBeFulfilled->fulfillStatus = 1;
               } else {
                   printf("Not Fulfilled\n");
               }
           }
       } else if(input == 3) {
           printf("Saving Information..Ciao!\n");
           output_file(filename, orderList, size);
           free(orderList);
           break;
       } else {
           printf("Not a valid menu choice\n");
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
C Language Program. PLEASE USE DYNAMIC MEMORY AND FOLLOW DIRECTIONS GIVEN. USE FUNCTIONS GIVEN ALSO. Thank...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • C++ Read and do as instructed on please (C++Program *** use only condition statements, loops, functions,...

    C++ Read and do as instructed on please (C++Program *** use only condition statements, loops, functions, files, and arrays. Do NOT use material such as classes. Make sure to add comments** You are to write an ATM Program. The ATM should allow the user to do the following: 1. Create account 2. Log in 3. Exit When they press 1, they are asked for first name (capital first letter). Then it should ask for password. The computer should give the...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • For your second program, please read the data from the input file directly into an array....

    For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...

  • Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program sh...

    Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program should read in several album names, each album has up to 5 tracks as well as a genre. First declare genre for the album as an enumeration with at least three entries. Then declare an album structure that has five elements to hold the album name, genre, number of tracks, name of those tracks and track location. You can...

  • Order up:: Write a program that will be used to keep track of orders placed at...

    Order up:: Write a program that will be used to keep track of orders placed at a donut shop. There are two types of items that can be ordered: coffee or donuts. Your program will have a class for each order type, and an abstract superclass. Coffee orders are constructed with the following information: quantity (int) - how many coffees are being ordered size (String) - the size of the coffees (all coffees in the order have the same size)...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • C++ Use C++ functions and build a program that does the most basic job all students...

    C++ Use C++ functions and build a program that does the most basic job all students have to contend with, process the grades on a test and produce a summary of the results. The big wrinkle is, it should be a multi-file program. -Requires three simple things: Figure out the best score of all scores produced Figure out the worst score of all scores produced Assign a letter grade for each score produced Complete this lab by writing three functions....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT