----- Original Message -----
From: "rhese_12_14" <rhese_12_14@yahoo.com>
To: <C_Spice@yahoogroups.com>
Sent: Wednesday, March 03, 2004 3:54 PM
Subject: [C_Spice] help me plssssssssssss.......
> can somebody help me with my case study??? coz im really having a
> hard time doing this.. im a beginner../
> i need to write a program that assign a passenger seats in an
> airplane, assuming that a small airplane with seats numbered as
> follows:
>
> 1 A B C D
> 2 A B C D
> 3 A B C D
> 4 A B C D
> 5 A B C D
> 5 A B C D
> 6 A B C D
> 7 A B C D
>
> the program shld. display the seat pattern, marking with an "X" the
> seats already assigned. For example, after seats 1A, 2B, 4C are
> taken, the display shld. look like
>
> 1 X B C D
> 2 A X C D
> 3 A B C D
> 4 A B X D
> 5 A B C D
> 6 A B C D
> 7 A B C D
>
> After displaying the seats available, the program prompts for the
> seat desired, the user types in a seat, and then display of available
> seats is updated. This continues until all seats are filled or until
> the user signals that the program shld. end. If the user types in a
> seat that is already assigned, the program shld. say that the seat is
> occupied and ask for another choice. the program shld. take into
> consideration the use of arrays and functions as well as the user
> interface.
>
> tnx in advance.. have a nice day!!
>
The following is a minimum working program according to your requirements. It
uses few features of C99.
1 /*
2 * journey.c - Journey Reservation System
3 * Author - Vijay Kumar R Zanvar <vijoeyz@hotmail.com>
4 * Date - March 03, 2004
5 */
6
7 #if !defined ( __STDC__ ) || !defined ( __STDC_VERSION__ )
8 #error The compiler is not C99 compliant
9 #endif
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <assert.h>
16
17 #define DEBUG 0
18
19 #if DEBUG
20 #define debug_prin() printf ( "Entering function: %s...\n", __func__ )
21 #else
22 #define debug_prin()
23 #endif
24
25 #define ROW 9 /* fails if ROW > 9 */
26 #define COL 4 /* fails if COL > 26 */
27
28 typedef
29 struct transport
30 {
31 char seats[ROW][COL];
32 void (*display) ( const struct transport * restrict );
33 int (*prompt) ( void );
34 int (*reservation) ( struct transport * restrict );
35 int (*cancel) ( struct transport * restrict );
36 int (*is_avail) ( const struct transport * restrict, const char * restrict,
37 int, int );
38 int (*master) ( struct transport * restrict );
39 }transport_t;
40
41 static const char * const alpha = ( const char [] ) { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
42
43 inline static void
44 air_display ( const struct transport * plane )
45 {
46 debug_prin ();
47 for ( int i = 0; i < ROW; i++ )
48 {
49 printf ( "%d ", i+1 );
50 for ( int j = 0; j < COL; j++ )
51 putchar ( ' ' ), putchar ( plane -> seats[i][j] );
52 printf ( "\n" );
53 }
54 }
55
56 inline static int
57 air_prompt ( void )
58 {
59 debug_prin ();
60 printf ( "\n"
61 "1: Reservation\n"
62 "2: Cancellation\n"
63 "3: Display\n"
64 "4: Exit\n\n"
65 "Enter your choice: " );
66
67 int ch = 3;
68 int ret, try = 3;
69 while ( try-- )
70 {
71 ret = scanf ( " %d", &ch );
72 if ( ret )
73 return ch;
74 }
75 fprintf ( stderr, "Input failed. Aborting...\n" );
76 abort ();
77 return 0;
78 }
79
80 static int
81 get_x ( const char s[static restrict 3] )
82 {
83 debug_prin ();
84 assert ( !isdigit ( *s - '0' ) );
85 return (int) *s - '0' - 1;
86 }
87
88 static int
89 get_y ( const char s[static restrict 3] )
90 {
91 debug_prin ();
92 assert ( isalpha ( s[1] ) );
93 char *p = strchr ( alpha, toupper ( s[1] ) );
94 assert ( p );
95 return p - alpha;
96 }
97
98 inline static int
99 air_is_avail ( const transport_t * restrict plane,
100 const char s[static restrict 3], int n, int seq )
101 {
102 debug_prin ();
103 assert ( s && n );
104 int x = get_x ( s ),
105 y = get_y ( s );
106
107 if ( x > ROW-1 || y > COL-1 )
108 {
109 fprintf ( stderr, "Invalid range\n" );
110 return 0;
111 }
112 return plane -> seats[x][y] != '#';
113 }
114
115 inline static int
116 air_reservation ( transport_t * restrict plane )
117 {
118 debug_prin ();
119 printf ( "\n"
120 "Enter the seat position: " );
121
122 char pos[5];
123 scanf ( "%s", pos );
124
125 if ( plane -> is_avail ( plane, pos, 1, 0 ) )
126 {
127 int x = get_x ( pos ),
128 y = get_y ( pos );
129
130 plane -> seats[x][y] = '#';
131 plane -> display ( plane );
132 return 0;
133 }
134 fprintf ( stderr, "Seat not available for %s\n", pos );
135 return -1;
136 }
137
138 inline static int
139 air_cancel ( transport_t * restrict plane )
140 {
141 debug_prin ();
142 printf ( "\n"
143 "Enter the seat position: " );
144
145 char pos[5];
146 scanf ( "%s", pos );
147
148 if ( !plane -> is_avail ( plane, pos, 1, 0 ) )
149 {
150 int x = get_x ( pos ),
151 y = get_y ( pos );
152
153 plane -> seats[x][y] = alpha[y];
154 plane -> display ( plane );
155 return 0;
156 }
157 fprintf ( stderr, "Seat not booked for %s\n", pos );
158 return -1;
159 }
160
161 inline static int
162 air_master ( transport_t * restrict plane )
163 {
164 debug_prin ();
165 plane -> display ( plane );
166
167 while ( 1 )
168 switch ( plane -> prompt () )
169 {
170 case 1:
171 plane -> reservation ( plane );
172 break;
173
174 case 2:
175 plane -> cancel ( plane );
176 break;
177
178 case 3:
179 plane -> display ( plane );
180 break;
181
182 case 4: return 0;
183 break;
184
185 default:
186 break;
187 }
188 return 0;
189 }
190
191 int
192 main ( void )
193 {
194 debug_prin ();
195 transport_t *plane = & ( transport_t ) { .display = air_display,
196 .prompt = air_prompt,
197 .reservation = air_reservation,
198 .cancel = air_cancel,
199 .master = air_master,
200 .is_avail = air_is_avail,
201 };
202
203 for ( int i = 0; i < ROW; i++ )
204 for ( int j = 0; j < COL; j++ )
205 plane -> seats[i][j] = alpha[j];
206
207 plane -> master ( plane );
208 return EXIT_SUCCESS;
209 }
Compilation example:
F:\Vijay\C> gcc -std=c99 -O -Wall journey.c
(-O switch is needed to inline the functions)
Points:
-------
* The macro __STDC__ is defined as 1 by a C99 compliant compiler, and
__STDC_VERSION__ is the version of the Standard. (line 7)
* The macro DEBUG is used to turn on/off messages for debugging purposes. De-
fining it to a non-zero number prints the function name. Useful during deb-
ugging. (line 17)
* Macros ROW and COLUMN determines the number of rows, and the number of seats
in each row. This program supports maximum 9 rows, and 26 seats per row.
(line 25 and 26)
* The `transport' structure is very interesting. This is inspired from UNIX
FMS. For any public trasportation system, few facts -- such as reservation,
cancellation, availabilty etc -- are common. Hence, it is a better idea to
have a representation. (line 29)
* We would not discuss the logic of the program, but the features of the lang-
uage used. They are:
+ Compound literals
+ `inline' function specifier
+ `restrict' qualifier
+ C++ style declarations
+ __func__ predefined identifier
+ C99 style array declarators
Compound Literals:
------------------
Compound literals provide a technique of creating temporay objects that are
required only once. Only object of type structure, arrays or union can be
created.
The semantic rule of creating a compound literal is:
type obj = ( type ) { list of initializers };
or,
type *obj = & ( type ) { list of initializers };
`inline' function specifier:
---------------------------
By declaring a function with the `inline' specifier, the compiler can integ-
rate the function's code with the caller function's code. This avoids func-
tion call overhead.
`restrict' qualifier:
---------------------
This qualifier has been introduced to help compiler to perform certain opti-
mizations. It says that the access to the object should occurs directly
or indirectly through the qualified pointer.
C++ style declarations:
-----------------------
The C99 standard permits declarations and statements to be mixed in an
arbitrary manner, as shown.
__func__ predefined identifier:
-------------------------------
__func__ is a block scope identifier which is implicitly defined the the
compiler at the beginning of a function.
void func ( void )
{
const static char __func__ [] = "func";
/* rest of the function */
}
C99 style array declarators:
----------------------------
Type qualifiers can appear inside [ and ]. For example, the function
void func ( char s[static 5] );
says that the array pointed by "s", at least, has 5 characters; so the
compiler can preload the the array into, say, registers.