Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. /*
  2. *the purpose of this program is to show prices of airline tickets during the week
  3. *show the amount of tickets sold on each day for each id during the week with the amount each costs
  4. *and show invalid ticket orders during the week
  5. */
  6.  
  7. /*
  8. * File: Assc2.c
  9. * Author: Jake Dupuis and Navjot Singh
  10. *
  11. * Created on March 1, 2018, 11:21 PM
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #define NUM_FLIGHT_IDS 16
  18. #define DAYS_OF_WEEK 7
  19. int getIdIndex(int fl_num);
  20. int getDayIndex( char day[]);
  21. void loadData(int flightIDs[], int seats[], float price[][DAYS_OF_WEEK], int reservations[][DAYS_OF_WEEK], char invalidFlights[], int numberOfBadTickets[]);
  22. void printFlightPrices(int flightIDs[], float price[][DAYS_OF_WEEK]);
  23. void printFlightSalesData(int flightIDs[], float price[][DAYS_OF_WEEK], int reservations[][DAYS_OF_WEEK]);
  24. void printInvalidFlightOrders(char invalidFlights[], int numberOfBadTickets[]);
  25.  
  26. FILE *flightData, *ticketData, *reportStream;
  27.  
  28. int main( void )
  29. {
  30.  
  31. // array to hold flight information
  32. int flightIDs[NUM_FLIGHT_IDS] = {113,126,139,152,165,178,191,204,217,230,243,256,269,282,295,308};
  33. int seats[NUM_FLIGHT_IDS] = {125,25,100,150,25,150,50,100,100,100,150,125,50,100,125,100};
  34. float price[NUM_FLIGHT_IDS][DAYS_OF_WEEK] = {
  35. {187.50,168.75,151.88,136.69,123.02,187.50,123.02},
  36. { 62.50, 56.25, 50.63, 45.56, 41.01, 62.50, 41.01},
  37. {145.83,131.25,118.13,106.31, 95.68,145.83, 95.68},
  38. {104.17, 93.75, 84.38, 75.94, 68.34,104.17, 68.34},
  39. {145.83,131.25,118.13,106.31, 95.68,145.83, 95.68},
  40. {125.00,112.50,101.25, 91.13, 82.01,125.00, 82.01},
  41. {187.50,168.75,151.88,136.69,123.02,187.50,123.02},
  42. {312.50,281.25,253.13,227.81,205.03,312.50,205.03},
  43. {242.50,218.25,196.43,176.78,159.10,242.50,159.10},
  44. {117.50,105.75, 95.18, 85.66, 77.09,117.50, 77.09},
  45. {200.83,180.75,162.68,146.41,131.77,200.83,131.77},
  46. {159.17,143.25,128.93,116.03,104.43,159.17,104.43},
  47. {200.83,180.75,162.68,146.41,131.77,200.83,131.77},
  48. {180.00,162.00,145.80,131.22,118.10,180.00,118.10},
  49. {242.50,218.25,196.43,176.78,159.10,242.50,159.10},
  50. {367.50,330.75,297.68,267.91,241.12,367.50,241.12}
  51. };
  52. int reservations[NUM_FLIGHT_IDS][DAYS_OF_WEEK] ={ {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  53. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  54. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  55. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  56. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  57. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  58. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
  59. {0,0,0,0,0,0,0},{0,0,0,0,0,0,0}};
  60. char invalidFlights[100000];
  61. int numberOfBadTickets[] = {0};
  62. int option;
  63.  
  64. printf("Opening program!\n\nPlease read the following instructions for the best results while using the program.\n\n"
  65. "The purpose of this program is to show prices of airline tickets during the week, the amount of tickets sold on each day for each id and show invalid ticket orders.\n\n"
  66. "While the program is running the program will prompt the user to use 4 commands ranging from (1 to 4) to display different types of outputs.\n\n"
  67. "Entering One (1)(This is a numerical input for the menu ranging 1-4)on the keyboard while the user is on this menu it will show the prices of flights based on the flight id and the day of the week."
  68. "\n\nEntering Two (2)(This is a numerical input for the menu ranging 1-4) on the keyboard while the user is on this menu it will show the total number of tickets sold on which day and have the amount made from those tickets ."
  69. "\n\nEntering Three(3)(This is a numerical input for the menu ranging 1-4) on the keyboard while the user is on this menu it will show all invalid ticket orders that were made during the week ."
  70. "\n\nEntering four(4)(This is a numerical input for the menu ranging 1-4) on the keyboard if the user enters this number the program will show the termination message and the program will end."
  71. "\n\nPress Enter to proceed to the main program.");
  72. getchar();
  73.  
  74.  
  75. do{
  76. printf("enter the number for what you want:\n1.Flight prices\n2.Flight sales data\n3.Invalid flight orders\n4.Exit program\n");
  77. scanf("%d", &option);
  78. switch(option)
  79. {
  80. case 1:
  81. printFlightPrices(flightIDs,price);
  82. break;
  83. case 2:
  84. loadData(flightIDs,seats,price,reservations,invalidFlights,numberOfBadTickets);
  85. printFlightSalesData(flightIDs,price,reservations);
  86. break;
  87. case 3:
  88. printInvalidFlightOrders(invalidFlights,numberOfBadTickets);
  89. break;
  90. case 4:
  91. printf("exiting program\n\n");
  92. option = 0;
  93. break;
  94. default:
  95. option = 0;
  96. break;
  97. }
  98.  
  99. }while(option > 0 && option < 4);
  100. printf("program completed.\nThank you for using our program :).\n");
  101. return 0;
  102.  
  103. }
  104.  
  105. void printInvalidFlightOrders(char invalidFlights[], int numberOfBadTickets[])
  106. {
  107. if(numberOfBadTickets > 0)
  108. {
  109. printf("Invalid flight ID report:\n");
  110. printf("%s",invalidFlights);
  111. printf("Total invalid tickets: %d\n",numberOfBadTickets[0]);
  112. }
  113. }
  114.  
  115. void printFlightSalesData(int flightIDs[], float price[][DAYS_OF_WEEK], int reservations[][DAYS_OF_WEEK])
  116. {
  117. printf("-------------------------------------------------------------------------------------------------------------\n Tickets (revenue) by flight ID and day of the week\n-------------------------------------------------------------------------------------------------------------\n ID Sunday Monday Tuesday Wednesday Thursday Friday Saturday\n-------------------------------------------------------------------------------------------------------------\n");
  118. int i;
  119. for(i = 0; i < NUM_FLIGHT_IDS; i++)
  120. {
  121. printf("%4d",flightIDs[i]);
  122. int j;
  123. for(j = 0; j < DAYS_OF_WEEK; j++)
  124. {
  125. printf("%4d ($%7.2f)",reservations[i][j],reservations[i][j]*price[i][j]);
  126. }
  127. printf("\n");
  128. }
  129. printf("-------------------------------------------------------------------------------------------------------------\n-------------------------------------------------------------------------------------------------------------\n");
  130. }
  131.  
  132. void printFlightPrices(int flightIDs[], float price[][DAYS_OF_WEEK])
  133. {
  134. printf("---------------------------------------------------------------------------------\n Cost of Flights by flight ID and day of the week\n---------------------------------------------------------------------------------\n ID Sunday Monday Tuesday Wednesday Thursday Friday Saturday\n---------------------------------------------------------------------------------\n");
  135. int i;
  136. for(i = 0; i < NUM_FLIGHT_IDS; i++)
  137. {
  138. printf("%4d",flightIDs[i]);
  139. int j;
  140. for(j = 0; j < DAYS_OF_WEEK; j++)
  141. {
  142. printf(" $%6.2f",price[i][j]);
  143. }
  144. printf("\n");
  145. }
  146. printf("---------------------------------------------------------------------------------\n");
  147. }
  148.  
  149. void loadData(int flightIDs[], int seats[], float price[][DAYS_OF_WEEK], int reservations[][DAYS_OF_WEEK], char invalidFlights[], int numberOfBadTickets[])
  150. {
  151. flightData = fopen("flightData_2_errors.dat","r");
  152. while(!feof(flightData))
  153. {
  154. int ID;
  155. char day[20];
  156. int numTickets;
  157. fscanf(flightData,"%d%s%d",&ID,&day,&numTickets);
  158. int flightIndex = getIdIndex(ID);
  159. int dayIndex = getDayIndex(day);
  160. if(flightIndex != -1 && dayIndex != -1)
  161. {
  162. reservations[flightIndex][dayIndex] += numTickets;
  163. }
  164. else
  165. {
  166. numberOfBadTickets[0] += numTickets;
  167. char badOrder[1000];
  168. sprintf(badOrder, "%d %s %d\n", ID, day, numTickets);
  169. strcat(invalidFlights, badOrder);
  170. }
  171. }
  172. }
  173.  
  174. int getDayIndex(char day[])
  175. {
  176. char daysOfTheWeek[DAYS_OF_WEEK][20] = {{'S','u','n','d','a','y'},
  177. {'M','o','n','d','a','y'},
  178. {'T','u','e','s','d','a','y'},
  179. {'W','e','d','n','e','s','d','a','y'},
  180. {'T','h','u','r','s','d','a','y'},
  181. {'F','r','i','d','a','y'},
  182. {'S','a','t','u','r','d','a','y'}};
  183. int i;
  184. for(i = 0; i < DAYS_OF_WEEK; i++)
  185. {
  186. if(strcmp(day,daysOfTheWeek[i])==0)
  187. {
  188. return i;
  189. }
  190. }
  191. return -1;
  192. }
  193.  
  194. int getIdIndex(int fl_num)
  195. {
  196. switch(fl_num)
  197. {
  198. case 113:
  199. return 0;
  200. case 126:
  201. return 1;
  202. case 139:
  203. return 2;
  204. case 152:
  205. return 3;
  206. case 165:
  207. return 4;
  208. case 178:
  209. return 5;
  210. case 191:
  211. return 6;
  212. case 204:
  213. return 7;
  214. case 217:
  215. return 8;
  216. case 230:
  217. return 9;
  218. case 243:
  219. return 10;
  220. case 256:
  221. return 11;
  222. case 269:
  223. return 12;
  224. case 282:
  225. return 13;
  226. case 295:
  227. return 14;
  228. case 308:
  229. return 15;
  230. }
  231. return -1;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement