Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 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 =(struct flightRecord *)malloc(noOfDailyFlights*sizeof(struct flightRecord));
  27.  
  28. printf("Flight Numbers Flight Capacity\n");
  29. int i = 0;
  30. while (!feof(text)){
  31. fscanf(text,"%d %d ",&(flights+i)->flightNo,&(flights+i)->flightCapacity);
  32. flights[i].dailyNo = noOfDailyFlights;
  33. flights[i].reservationCount = 0;
  34. flights[i].passengersOnFlight = NULL;
  35. flights[i].reservesForFlight = NULL;
  36. printf(" %3d %d\n",flights[i].flightNo,flights[i].flightCapacity);
  37. i++;
  38. }
  39. return flights;
  40. }
  41.  
  42. void status(struct flightRecord *flights){
  43. int i = 0;
  44. struct flightRecord *temp= flights;
  45.  
  46. for (i = 0;i < flights->dailyNo ; i++){
  47.  
  48. printf("Flight number is: %d \n",temp[i].flightNo);
  49. printf("Flight capacity is: %d \n",temp[i].flightCapacity);
  50. printf("The people on flight are:\n");
  51. while(temp[i].passengersOnFlight != NULL){
  52. printf("%s\n",temp[i].passengersOnFlight->name);
  53. temp[i].passengersOnFlight = flights[i].passengersOnFlight->next;
  54.  
  55. }
  56. while(temp[i].reservesForFlight){
  57. printf("%s\n",temp[i].reservesForFlight->name);
  58. if(temp[i].reservesForFlight->next)
  59. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  60. }
  61. }
  62. }
  63.  
  64. flightRecord* reserve(char name[],int flightNo,struct flightRecord *flights){
  65. struct flightRecord *temp = flights;
  66. struct passenger *temp1 =(struct passenger *)malloc(sizeof(struct passenger));
  67. temp1->name =(char*)malloc((strlen(name)+1)*(sizeof(char)));
  68. int i = 0,flag = 0;
  69.  
  70. for(i;i < flights->dailyNo;i++){
  71. if(flightNo == temp[i].flightNo){
  72. flag++;
  73. }
  74. }
  75.  
  76. if(flag == 0){
  77. printf("Flight is not found.\n");
  78. }
  79.  
  80. else{
  81.  
  82. for(i = 0;i < flights->dailyNo;i++){
  83.  
  84. if(flightNo == temp[i].flightNo){
  85. printf("%d",flightNo);
  86. if(temp[i].reservationCount < temp[i].flightCapacity){
  87.  
  88. struct passenger *tempPas = temp[i].passengersOnFlight;
  89. strcpy(temp1->name,name);
  90.  
  91. temp1->next = NULL;
  92.  
  93. if(tempPas == NULL){
  94. tempPas = temp1;
  95. temp[i].reservationCount++;
  96. printf("%d",temp[i].reservationCount);
  97. }
  98.  
  99.  
  100. else{
  101. while (tempPas->next){
  102. tempPas = tempPas->next;
  103. }
  104. tempPas->next = temp1;
  105. temp[i].reservationCount++;
  106. printf("%d",temp[i].reservationCount);
  107. }
  108. }
  109. else{
  110. struct passenger *tempRes = temp[i].reservesForFlight;
  111. strcpy(temp1->name,name);
  112.  
  113. temp1->next = NULL;
  114. if(tempRes == NULL){
  115. tempRes = temp1;
  116. }
  117. else{
  118. while(tempRes->next){
  119. tempRes = tempRes->next;
  120. }
  121. tempRes->next = temp1;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. for(i = 0; i<temp[i].dailyNo;i++){
  128. while(temp[i].passengersOnFlight){
  129. printf("%s\n",temp[i].passengersOnFlight->name);
  130. temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
  131. }
  132. }
  133. return flights;
  134. }
  135.  
  136. flightRecord* cancel(char name[],int flightNumber,struct flightRecord *flights){
  137. struct flightRecord *temp = flights;
  138. int i = 0,flag = 0;
  139. for(i;i < flights->dailyNo;i++){
  140. if(flightNumber == temp[i].flightNo){
  141. flag++;
  142. }
  143. }
  144.  
  145. if(flag == 0){
  146. printf("Flight is not found.\n");
  147. }
  148. else{
  149.  
  150. for(i = 0;i < temp[i].dailyNo ; i++){
  151. if(flightNumber == temp[i].flightNo){
  152.  
  153. if(temp[i].reservationCount < temp[i].flightCapacity){
  154. struct passenger *cur = temp[i].passengersOnFlight;
  155. struct passenger *temp1;
  156. while(strcpy(cur->name,name)!= 0){
  157. cur = cur->next;
  158. }
  159. if (strcmp(cur->name,name)==0){
  160. temp1 = cur;
  161. cur = cur->next;
  162. }
  163. free(temp);
  164. temp[i].reservationCount--;
  165. cur = temp[i].passengersOnFlight;
  166. if (temp[i].reservesForFlight != NULL){
  167. if(temp[i].reservationCount < temp[i].flightCapacity){
  168. while(cur){
  169. cur = cur->next;
  170. }
  171. cur->next = temp[i].reservesForFlight;
  172. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  173. }
  174. }
  175. }
  176. else{
  177. struct passenger *cur = temp[i].reservesForFlight;
  178. struct passenger *temp1;
  179. while(strcpy(cur->name,name)){
  180. cur = cur->next;
  181. }
  182. if (strcmp(cur->name,name)==0){
  183. temp1 = cur;
  184. cur = cur->next;
  185. }
  186. free(temp);
  187. }
  188.  
  189. }
  190. }
  191. }
  192. return flights;
  193. }
  194. void inquire(struct flightRecord *flights,char name[]){
  195. int i;
  196. struct flightRecord *temp = flights;
  197. for(i = 0; i < flights->dailyNo ; i++){
  198. while(temp[i].passengersOnFlight != NULL){
  199. if(strcmp(temp[i].passengersOnFlight->name,name)==0){
  200. printf("%s reserved on flight %d.",name,temp[i].flightNo);
  201. }
  202. temp[i].passengersOnFlight = flights[i].passengersOnFlight->next;
  203.  
  204. }
  205. while(temp[i].reservesForFlight != NULL){
  206. if(strcmp(temp[i].reservesForFlight->name,name)==0){
  207. printf("%s in reserve list for flight %d.",name,temp[i].flightNo);
  208. }
  209. temp[i].reservesForFlight = temp[i].reservesForFlight->next;
  210. }
  211. printf("\n");
  212. }
  213. }
  214. void processCommands(struct flightRecord *flights){
  215. printf("\n");
  216. char command[10];
  217. char name[100];
  218. char flightNo[10];
  219. int totalSpaces = 0;
  220. int space = 0;
  221. int i , j , k, l;
  222. while(strcmp(command,"EXIT")!= 0){
  223. i = 0, j = 0 , k= 0 , l = 0;
  224. totalSpaces = 0;
  225. space = 0;
  226. memset(command,'\0',10);
  227. memset(name,'\0',100);
  228. memset(flightNo,'\0',10);
  229. printf("Enter the command: <COMMAND> <NAME> <FLIGHT NO>:");
  230.  
  231. char input[200];
  232. fgets(input, MAX, stdin);
  233. fflush(stdin);
  234.  
  235. while(input[i] != '\0'){
  236. if(input[i] == ' ')
  237. totalSpaces++;
  238. i++;
  239. }
  240. i = 0;
  241. while(input[i] != '\n'){
  242. if(input[i] == ' '){
  243. space++;
  244. }
  245. if (space == totalSpaces){
  246. flightNo[l] = input[i];
  247. l++;
  248. }
  249. if(space == 0){
  250. command[j] = input[i];
  251. j++;
  252. }
  253. else if (space > 0){
  254. if(space < totalSpaces){
  255. if(input[i] == ' ' && space == 1 ){
  256. name[k] = input[i+1];
  257. }
  258. else{
  259. name[k] = input[i];
  260. k++;
  261. }
  262. }
  263. }
  264. i++;
  265. }
  266. printf( "%s-%s-\n",name,flightNo);
  267. command[j] = '\0';
  268. name[k] = '\0';
  269. flightNo[l] = '\0';
  270. int flightNumber = atoi(flightNo);
  271.  
  272. if (strcmp(command,"STATUS") == 0){
  273. status(flights);
  274. }
  275.  
  276. else if(strcmp(command,"RESERVE")== 0){
  277. flights = reserve(name,flightNumber,flights);
  278. }
  279. else if(strcmp(command,"CANCEL")== 0){
  280. flights = cancel(name,flightNumber,flights);
  281. }
  282. else if(strcmp(command,"INQUIRE")==0){
  283. inquire(flights,name);
  284. }
  285. }
  286. struct flightRecord *temp = flights;
  287.  
  288. printf("Thank you for using reservation system.");
  289. }
  290.  
  291. int main(){
  292. struct flightRecord *flights;
  293. flights = initialise(flights);
  294. processCommands(flights);
  295.  
  296. return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement