Advertisement
Razali

Store List

Nov 13th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. 5a)
  2.  
  3. typedef struct
  4. {
  5.     char pName[31];
  6.     float cost, retail;
  7. }product_t;
  8.  
  9. 5b)
  10.  
  11. // Read the number of stores and details of each store
  12. // into array storeList.
  13. // Return the number of stores.
  14. int readStores(store_t storeList[])
  15. {
  16.     int numStores, i, j;
  17.     FILE * fp;
  18.    
  19.     fp = fopen("stores.in", "r");
  20.    
  21.     /* Read number of stores */
  22.     fscanf(fp, "%d", &numStores);
  23.    
  24.     /* For each Store */
  25.     for(i = 0; i < numStores; i++)
  26.     {
  27.         fscanf(fp, "%s %d %d %f %d", storeList[i].sname, &storeList[i].x, &storeList[i].y, &storeList[i].radius, &storeList[i].numProduct);
  28.        
  29.         /* For each product */
  30.         for(j=0; j<storeList[i].numProduct; j++)
  31.             fscanf(fp, "%s %f %f", storeList[i].products[j].pName, &storeList[i].products[j].cost, &storeList[i].products[j].retail);
  32.     }
  33.    
  34.     fclose(fp);
  35.    
  36.     return numStores;  
  37. }
  38.  
  39. 5c)
  40.  
  41. // Return 1 if user is within store's radius of influence,
  42. // or 0 otherwise.
  43. int withinRadius(int x, int y, store_t store)
  44. {
  45.     float distance = sqrt(pow(store.x - x, 2) + pow(store.y - y, 2));
  46.    
  47.     return (store.radius >= distance);
  48. }
  49.  
  50.  
  51. 5d)
  52.  
  53. // Print the names of stores where the user's current
  54. // location is within their circles of influence.
  55. void printNearbyStores(int x, int y, store_t storeList[], int numStore)
  56. {
  57.     int i;
  58.    
  59.     for(i = 0; i < numStore; i++)
  60.         if(withinRadius(x, y, storeList[i]))
  61.             puts(storeList[i].sname);
  62. }
  63.  
  64. 5e)
  65.  
  66. // Find and label the products that are good buys.
  67. // You are to decide on the return type and parameters of
  68. // this function.
  69. /* Return "void" as functions objective is to just label */
  70. void labelGoodBuys(store_t storeList[], int numStore)
  71. {
  72.     int i, j;
  73.     float difference;
  74.    
  75.     /* For each store */
  76.     for(i = 0; i < numStore; i++)
  77.     {
  78.         /* For each product */
  79.         for(j = 0; j < storeList[i].numProduct; j++)
  80.         {
  81.             difference = storeList[i].products[j].retail - storeList[i].products[j].cost;
  82.            
  83.             if(difference <= (0.05 * storeList[i].products[j].cost))
  84.                 strcat(storeList[i].products[j].pName, "***Must Buy***");
  85.         }
  86.     }
  87. }
  88.  
  89. 5f)
  90.  
  91. // Determine which store has the lowest retail price
  92. // for a given product. You are to decide on the
  93. // return type and parameters of this function.
  94. int findCheapestStore(char productName[], store_t storeList[], int numStore)
  95. {
  96.     int i, j, storeIndex;
  97.     char newProductName[31];
  98.     float lowestRetail = 1001;
  99.    
  100.     /* If product name has label.. */
  101.     strcpy(newProductName, productName);
  102.     strcat(newProductName, "***Must Buy***");
  103.    
  104.     /* For each store */
  105.     for(i = 0; i < numStore; i++)
  106.     {
  107.         /* For each product */
  108.         for(j = 0; j < storeList[i].numProduct; j++)
  109.         {
  110.             /* Check for matching product name with or w/o label */
  111.             if(strcmp(productName, storeList[i].products[j].pName) == 0 || strcmp(newProductName, storeList[i].products[j].pName) == 0)
  112.               {
  113.                 if(storeList[i].products[j].retail < lowestRetail)
  114.                     storeIndex = i;    
  115.               }
  116.         }
  117.     }
  118.    
  119.     return storeIndex;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement