TeslaCoilGirl

Programming Assignment 1 V2

Sep 10th, 2021 (edited)
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "leak_detector_c.h"
  6.  
  7.  
  8. static FILE *fp;
  9. static FILE *out;
  10.  
  11. typedef struct item {
  12.     int itemID;
  13.     int numParts;
  14. } item;
  15.  
  16. typedef struct recipe {
  17.     int numItems;
  18.     item* itemList;
  19.     int totalParts;
  20. } recipe;
  21.    
  22. char** readIngredients(int *numIngredient){
  23.    
  24.     size_t charsize = *numIngredient*sizeof(char*);
  25.     char** ingredientList = (char**)malloc(charsize); // 1 alloc
  26.     assert(ingredientList != NULL);
  27.  
  28.     for(int i = 0; i < *numIngredient; i++){
  29.         char temp[20];
  30.         fscanf(fp,"%s", temp);
  31.        
  32.        
  33.         charsize = (strlen(temp)+1)*sizeof(char);
  34.         char* currIngredient = (char*)malloc(charsize); // 2 alloc
  35.         assert(currIngredient != NULL);
  36.        
  37.         strcpy(currIngredient,temp);
  38.         ingredientList[i] = currIngredient;
  39.     }  
  40.     return ingredientList;
  41. }
  42.  
  43. recipe* readRecipe(){
  44.     size_t recsize = sizeof(recipe);
  45.     recipe* currentRecipe = (recipe*)malloc(recsize); // 3 alloc
  46.     assert(currentRecipe != NULL);
  47.    
  48.     fscanf(fp, "%d", &currentRecipe->numItems);
  49.    
  50.     recsize = currentRecipe->numItems*sizeof(item);
  51.     currentRecipe->itemList = (item*)malloc(recsize); // 4 alloc
  52.     assert(currentRecipe->itemList != NULL);
  53.    
  54.     currentRecipe->totalParts = 0;
  55.     for(int i = 0; i < currentRecipe->numItems; i++){
  56.         fscanf(fp, "%d %d", &currentRecipe->itemList[i].itemID, &currentRecipe->itemList[i].numParts);
  57.         currentRecipe->totalParts += currentRecipe->itemList[i].numParts;
  58.     }
  59.     return currentRecipe;
  60. }
  61.  
  62. recipe** readAllRecipes(int *numRecipes){
  63.    
  64.     size_t recsize = *numRecipes*sizeof(recipe);
  65.     recipe** recipeList = (recipe**)malloc(recsize); // 5 alloc
  66.     assert(recipeList != NULL);
  67.    
  68.     for(int i = 0; i < *numRecipes; i++){
  69.         recipeList[i] = readRecipe();
  70.     }
  71.    
  72.     return recipeList;
  73. }
  74.  
  75. double* calculateOrder(int ingredientCount, int numSmoothies, recipe** recipeList){
  76.     size_t dSize = ingredientCount*sizeof(double);
  77.     double* orderList = (double*)calloc(ingredientCount, sizeof(double)); // 6 alloc
  78.     assert(orderList != NULL);
  79.  
  80.  
  81.     for(int i = 0; i < numSmoothies; i++){
  82.         int smoothieIdx;
  83.         int smoothieWeight;
  84.         fscanf(fp, "%d %d", &smoothieIdx, &smoothieWeight);
  85.        
  86.         for(int j = 0; j < recipeList[smoothieIdx]->numItems; j++){
  87.             item currItem = recipeList[smoothieIdx]->itemList[j];
  88.             orderList[currItem.itemID] += ((double)currItem.numParts * (double)smoothieWeight)/(double)recipeList[smoothieIdx]->totalParts;            
  89.         }
  90.     }
  91.     return orderList;
  92. }
  93.  
  94. int main(){
  95.     atexit(report_mem_leak);
  96.  
  97.     fp = fopen("in.txt","r");
  98.     out = fopen("out.txt", "w");
  99.    
  100.     if(fp == NULL) {
  101.             fprintf(stderr, "Could not open in file. Exiting now.");
  102.     }
  103.  
  104. /* ============================================================================= */
  105. /*               Part 1: Reading The Possible Ingredients List                   */
  106. /* ============================================================================= */
  107.    
  108.     int numIngredients; // This value ranges from 1 to 10^5.
  109.            
  110.     fscanf(fp, "%d", &numIngredients);
  111.    
  112.     if((numIngredients > 10000) || (numIngredients < 0)){
  113.             fprintf(stderr,"Ingredients list out of bounds. Aborting.");
  114.             abort();
  115.     }
  116.    
  117.     char** ingredientList = readIngredients(&numIngredients);
  118.  
  119. /* ============================================================================= */
  120. /*               Part 2: Reading The Possible Ingredients List                   */
  121. /* ============================================================================= */
  122.  
  123.     int numRecipes; // This value ranges from 1 to 10^5.
  124.        
  125.     fscanf(fp,"%d", &numRecipes);
  126.  
  127.     if((numRecipes > 10000) || (numRecipes < 0)){
  128.             fprintf(stderr,"Recipes list out of bounds. Aborting.");
  129.             abort();
  130.     }
  131.    
  132.     recipe** recipeList = readAllRecipes(&numRecipes);
  133. /* ============================================================================= */
  134. /*               Part 3: Reading The Store List                                  */
  135. /* ============================================================================= */
  136.  
  137.     int numStores; // This value ranges from 1 to 100.
  138.     double* amtOfEachItem;
  139.     fscanf(fp,"%d", &numStores);
  140.  
  141.     if((numStores > 100)||(numStores < 0)){
  142.             fprintf(stderr,"Stores list out of bounds. Aborting.");
  143.             abort();
  144.     }
  145.    
  146.     for (int i = 0; i < numStores; i++){
  147.         int numOrders;
  148.         fscanf(fp, "%d", &numOrders);
  149.          amtOfEachItem = calculateOrder(numIngredients, numOrders, recipeList);
  150.        
  151.         printf("Store #%d:\n", i+1);
  152.         fprintf(out, "Store #%d:\n", i+1);
  153.         for(int j = 0; j < numIngredients; j++){
  154.             if(amtOfEachItem[j] > 0){
  155.                 printf("%s %0.6lf\n", ingredientList[j], amtOfEachItem[j]);
  156.                 fprintf(out, "%s %0.6lf\n", ingredientList[j], amtOfEachItem[j]);
  157.             }
  158.         }
  159.         printf("\n");
  160.         fprintf(out, "\n");
  161.         free(amtOfEachItem);
  162.     }
  163.     printf("\n");
  164.     fprintf(out, "\n");
  165.  
  166.     fclose(fp);
  167.     fclose(out);
  168.  
  169. /* ============================================================================= */
  170. /*               Part 4: Freeing the Malloced Memory                             */
  171. /* ============================================================================= */
  172.  
  173.  
  174.     for(int i = 0; i < numIngredients; i++){
  175.       free(ingredientList[i]);
  176.     }
  177.     free(ingredientList);
  178.  
  179.    
  180.     for(int i = 0; i < numRecipes; i++){
  181.       free(recipeList[i]->itemList);
  182.       free(recipeList[i]);
  183.     }
  184.     free(recipeList);
  185.    
  186.  
  187.  
  188.    return 0;
  189. }
Add Comment
Please, Sign In to add comment