Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 200
  5.  
  6. struct passenger{
  7. char *name;
  8. struct passenger *next;
  9. };
  10.  
  11. struct flightRecord{
  12. int flightNo;
  13. int flightCapacity;
  14. int reservationCount;
  15. struct passenger *passengersOnFlight;
  16. struct passenger *reservesForFlight;
  17. int dailyNo;
  18. };
  19.  
  20. flightRecord* initialise(struct flightRecord *flights){
  21. FILE *text;
  22. int noOfDailyFlights;
  23. text = (fopen("text.txt","r"));
  24. fscanf(text, "%d ", &noOfDailyFlights);
  25. printf("%d flights are in today.\n\n",noOfDailyFlights);
  26. flights =(flightRecord *)malloc(noOfDailyFlights*sizeof(struct flightRecord));
  27. printf("Flight Numbers Flight Capacity\n");
  28. int i = 0;
  29. while (!feof(text)){
  30. fscanf(text,"%d %d ",&(flights+i)->flightNo,&(flights+i)->flightCapacity);
  31. flights[i].dailyNo = noOfDailyFlights;
  32. flights[i].reservationCount = 0;
  33. printf(" %3d %d\n",flights[i].flightNo,flights[i].flightCapacity);
  34. i++;
  35. }
  36. return flights;
  37. }
  38.  
  39. void status(struct flightRecord *flights){
  40. int i = 0;
  41. struct flightRecord *temp= flights;
  42.  
  43. for (i = 0;i < flights->dailyNo ; i++){
  44.  
  45. printf("Flight number is: %d \n",temp[i].flightNo);
  46. printf("Flight capacity is: %d \n",temp[i].flightCapacity);
  47. printf("The people on flight are:\n");
  48. while(temp[i].passengersOnFlight){
  49. printf("%s\n",temp[i].passengersOnFlight->name);
  50. if(temp[i].passengersOnFlight->next)
  51. temp[i].passengersOnFlight = flights[i].passengersOnFlight->next;
  52.  
  53. }
  54. while(temp[i].reservesForFlight){
  55. printf("%s\n",temp[i].reservesForFlight->name);
  56. if(temp[i].reservesForFlight->next)
  57. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  58. }
  59. }
  60. }
  61.  
  62. flightRecord* reserve(char name[],int flightNo,struct flightRecord *flights){
  63. struct flightRecord *temp = flights;
  64. int i = 0,flag = 0;
  65.  
  66. for(i;i < flights->dailyNo;i++){
  67. if(flightNo == temp[i].flightNo){
  68. flag++;
  69.  
  70. }
  71. }
  72.  
  73. if(flag == 0){
  74. printf("Flight is not found.");
  75. }
  76.  
  77. else{
  78.  
  79. for(i = 0;i < flights->dailyNo;i++){
  80.  
  81. if(flightNo == temp[i].flightNo){
  82.  
  83. if(temp[i].reservationCount < temp[i].flightCapacity){
  84. while(temp[i].passengersOnFlight != NULL){
  85. temp[i].passengersOnFlight= temp[i].passengersOnFlight->next;
  86. }
  87. (temp+i)->passengersOnFlight = (struct passenger*)malloc(sizeof(struct passenger));
  88. strcpy(temp[i].passengersOnFlight->name,name);
  89. temp[i].passengersOnFlight->next = NULL;
  90. temp[i].reservationCount++;
  91. }
  92. else if(temp[i].reservationCount > temp[i].flightCapacity){
  93. while(temp[i].reservesForFlight != NULL){
  94. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  95. }
  96. (temp+i)->reservesForFlight = (struct passenger*)malloc(sizeof(struct passenger));
  97. strcpy(temp[i].reservesForFlight->name,name);
  98. temp[i].reservesForFlight->next = NULL;
  99. }
  100.  
  101.  
  102.  
  103. printf("%d\n",temp[i].reservationCount);
  104. }
  105. }
  106. }
  107. for(i = 0;temp[i].passengersOnFlight->next;i++){
  108. printf("%s ",temp[i].passengersOnFlight->name);
  109. temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
  110. }
  111. return flights;
  112. }
  113.  
  114. flightRecord* cancel(char name[],int flightNumber,struct flightRecord *flights){
  115. struct flightRecord *temp = flights;
  116. int i = 0,flag = 0;
  117. for(i;i < flights->dailyNo;i++){
  118. if(flightNumber == temp[i].flightNo){
  119. flag++;
  120. }
  121. }
  122. if(flag == 0){
  123. printf("Flight is not found.\n");
  124. }
  125. else{
  126. for(i = 0;i < temp[i].reservationCount;i++){
  127. if(strcmp(temp[i].passengersOnFlight->name,name) == 0){
  128. temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
  129. temp[i].reservationCount--;
  130. }
  131. else if(strcmp(temp[i].passengersOnFlight->next->name,name)== 0){
  132. temp[i].passengersOnFlight->next = temp[i].passengersOnFlight->next->next;
  133. temp[i].reservationCount--;
  134. }
  135. }
  136. while(temp[i].reservesForFlight->next){
  137. if(strcmp(temp[i].reservesForFlight->name,name)==0){
  138. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  139. }
  140. else if(strcmp(temp[i].reservesForFlight->next->name,name)==0){
  141. temp[i].reservesForFlight->next = temp[i].reservesForFlight->next->next;
  142. }
  143. }
  144. }
  145. }
  146. void inquire(struct flightRecord *flights){
  147. int i;
  148. struct flightRecord *temp = flights;
  149. for(i = 0; i < flights->dailyNo ; i++){
  150. printf("The flight number is %d.\n",temp[i].flightNo);
  151. printf("People on the flights are:\n");
  152. while(temp[i].passengersOnFlight->next){
  153. printf("%s\n",temp[i].passengersOnFlight->name);
  154. temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
  155. }
  156. printf("People are waiting for this flight:\n");
  157. while(temp[i].reservesForFlight->next){
  158. printf("%s\n",temp[i].reservesForFlight->name);
  159. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  160. }
  161. printf("\n");
  162. }
  163. }
  164. void processCommands(struct flightRecord *flights){
  165. printf("\n");
  166. char command[10];
  167. char name[100];
  168. char flightNo[10];
  169. int totalSpaces = 0;
  170. int space = 0;
  171. int i , j , k, l;
  172. while(strcmp(command,"EXIT")!= 0){
  173. i = 0, j = 0 , k= 0 , l = 0;
  174. printf("Enter the command: <COMMAND> <NAME> <FLIGHT NO>: ");
  175. fflush(stdin);
  176. char input[200];
  177. fgets(input, MAX, stdin);
  178. while(input[i]){
  179. if(input[i] == ' ')
  180. totalSpaces++;
  181. i++;
  182. }
  183. i = 0;
  184. while(input[i] != '\n'){
  185. if(input[i] == ' '){
  186. space++;
  187. }
  188. if (space == totalSpaces){
  189. flightNo[l] = input[i];
  190. l++;
  191. }
  192. if(space == 0){
  193. command[j] = input[i];
  194. j++;
  195. }
  196. else if (space > 0){
  197. if(space < totalSpaces){
  198. name[k] = input[i];
  199. k++;
  200. }
  201. }
  202. i++;
  203. }
  204. command[j] = '\0';
  205. name[k] = '\0';
  206. flightNo[l] = '\0';
  207. int flightNumber = atoi(flightNo);
  208. if (strcmp(command,"STATUS") == 0){
  209. status(flights);
  210. }
  211.  
  212. else if(strcmp(command,"RESERVE")== 0){
  213. flights = reserve(name,flightNumber,flights);
  214. }
  215. else if(strcmp(command,"CANCEL")== 0){
  216. flights = cancel(name,flightNumber,flights);
  217. }
  218. else if(strcmp(command,"INQUIRE")==0){
  219. inquire(flights);
  220. }
  221. }
  222. struct flightRecord *temp = flights;
  223. for(i = 0;i < temp[i].reservationCount;i++){
  224. printf("%s",temp[i].passengersOnFlight->name);
  225. }
  226. printf("Thank you for using reservation system.");
  227. }
  228.  
  229. int main(){
  230. struct flightRecord *flights = initialise(flights);
  231. processCommands(flights);
  232.  
  233. return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement