airlines.c (2687B)
1 /* Exercise 6.21 */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 #define POS 10 7 8 int getpost(int, int []); 9 10 int main() 11 { 12 int positions[POS+1] = { 0 }; /* positions[0] do a lot of things :-) */ 13 int c; /* 1 = first class, 2 = economy. */ 14 15 srand( time(NULL) ); /* for the flight numbers ;) */ 16 17 /* available classes (2 + 1): ac[0] is not a class */ 18 int ac[3] = { 0, 1, 1 }; 19 20 int chclass = 0; /* for auto-change of class check */ 21 22 while(1) { 23 24 if(!chclass) { 25 printf("\n" 26 "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" 27 "American airlines international airport :-)\n" 28 "AIRLINES PRENOTATION SYSTEM! (C)Claudio M.\n" 29 "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" 30 "\nPlease type 1 for \"first class\"\n" 31 "Please type 2 for \"economy\"\n" 32 "Choose: "); 33 scanf("%d", &c); 34 printf("\n"); 35 } 36 else 37 --chclass; 38 39 positions[0] = getpos(c, positions); 40 41 if(positions[0] == -1) { 42 printf("The class %d is not exists, choose another class.\n", c); 43 continue; 44 } 45 else if(positions[0]) { 46 printf("<==================[ BOARDING PAPER ]==================>\n" 47 "Class: %d\n" 48 "Positions: %d\n" 49 "Fly number: *%d\n\n" 50 "Thank you for fly with us!\n" 51 "Hope you enjoy with our company.\n" 52 "<==================[ BOARDING PAPER ]==================>\n" 53 ,c, positions[0], 6789 + rand() & 1000); 54 positions[positions[0]] = 1; 55 56 printf("\nPress a key for reserve a flight.\n"); 57 getchar(); /* ignore the newline */ 58 getchar(); /* wait for a key */ 59 } 60 else { /* there are not positions available in the class */ 61 ac[c] = 0; 62 63 /* check if there is at least one available class */ 64 for(ac[0] = 0, positions[0] = 1; positions[0] <= 2; positions[0]++) 65 ac[0] += ac[positions[0]]; 66 67 if(!ac[0]) { 68 printf("Sorry! There are not classes available.\n"); 69 printf("Take your car or run ;)\n"); 70 break; 71 } 72 73 while( (positions[0] = getchar()) != 'n' && positions[0] != 'y') { 74 printf("Class %d is full! Want you try to check class %d?\n" 75 "Choose (y / n): ", c, c == 1 ? 2 : 1); 76 } 77 78 if(positions[0] == 'n') { 79 printf("Next flight leaves in 3 hours\n"); 80 break; 81 } 82 c = c == 1 ? 2 : 1; 83 ++chclass; 84 } 85 } /* end while(1) */ 86 87 return 0; 88 } /* E0F main */ 89 90 /* Return the first free position of a class */ 91 int getpos(int class, int v[]) 92 { 93 if(class != 1 && class != 2) { 94 return -1; 95 } 96 97 int i; 98 int x = class == 1 ? 1 : 6; 99 int y = class == 1 ? 5 : 10; 100 101 for(i = x; i <= y; i++) { 102 if(!v[i]) { 103 return i; 104 } 105 } /* end for (i) */ 106 107 return 0; 108 } /* eof getpos() */ 109