فكرة البرنامج أنه كراج للسيارات وهو عبارة عن ممر ضيق وله مدخل ومخرج واحد فقط ، وعند دخول كل سيارة يتم إعطائها رقم ثم تدخل ، ( والفكرة عموما تعتمد على المكدسات Stack ) ، سيتم إدخال السيارة باستخدام الدالة Push ويتم إخراجالسيارة باستخدام الدالة Pop ، لكن عند خروج أحد السيارات وبحكم أن الممر ضيق فسيتم إخراج جميع السيارات التي امامها عند المخرج ثم يتم إخراج السيارة وبعدها تعود جميع السيارات المخرجة إلى الكراج مرة أخرى وبنفس الترتيب ، وعند خروج السيارة يتم حساب عدد المرات التي تم فيها تحريك السيارة حتى تخرج السيارة الأخرى ، وهذا هو نص البرنامج :
#include
#include
#include
#define stacksize 10
struct stack
{
int top ;
int car [stacksize];
};
struct stack Parking ;
void menu();
void push ( struct stack * , int , int * );
int empty ( struct stack * );
void pop ( struct stack * , int * , int * );
void print_stack_list ( struct stack * );
void main()
{
Parking.top= -1;
int choico=0 , carnum , x , overflow , underflow ;
while (choico != 10 )
{
menu();
printf ("Enter your choico : ");
scanf ("%d" , &choico ) ;
switch ( choico )
{
case 1 :
{
printf ("Enter car to Enter " );
scanf ("%d", &carnum);
push (&Parking , x , &overflow);
if ( overflow )
{
printf ("%s" , "Parking overflow.");
printf ("Can't Enter car ..Please comeback in other time");
}
else
printf ("Car %d is Entered Successfully onto Parking." , carnum );
break ;
}
case 2 :
{
pop (&Parking , &x , &underflow);
if ( underflow )
{
printf ("nn%snn" , "Parking underflow...");
printf ("Can't pop a Car . Parking is Emoty ! ..");
}
else
printf ("Car %d is exited Successfully from Parkiing." , x );
break ;
}
case 3 :
{
print_stack_list ( &Parking ) ;
break ;
}
} // End switch
getch();
} //end while
return ;
}
/*********** Push Function ***********/
void push ( struct stack *ps , int x , int *poverfl )
{
if ( ps->top == stacksize-1 )
*poverfl = 1 ;
else
{
ps->car[++(ps->top)] = x ;
*poverfl = 0 ;
}
/*********** Empty Function ***********/
int empty ( struct stack *ps )
{
if ( ps->top == -1 )
return (1) ;
else
return (0) ;
//return ( ps->top == -1 ) ;
}
/*********** Pop Function ***********/
void pop ( struct stack *ps , int *px , int *underflow )
{
if ( empty (ps) )
{
*underflow = 1 ;
return;
}
*underflow = 0 ;
*px = ps ->car[ps->top--];
return;
}
/*********** Print Function ***********/
void print_stack_list ( struct stack * pl );
{
printf ("The Parking list : n") ;
for ( int i =0 ; i<=pl->top ;++i )
printf ("%dn" , pl->car ) ;
return ;
}
/*********** Menu Function ***********/
void menu ()
{
printf ("ttt nnn") ;
printf ("ttt Main menu of Parkingn") ;
printf ("ttt ____________________nnn") ;
printf ("ttt1. Push a car to stack.n") ;
printf ("ttt2. Pop a car to stack.n") ;
printf ("ttt3. Print car of Parking.n") ;
printf ("ttt4. Exit.n") ;
return ;
}
وعند التنفيذ يعطيني الأخطاء التالية :
C:Cpp1.cpp(83) : error C2601: 'empty' : local function definitions are illegal
C:Cpp1.cpp(93) : error C2601: 'pop' : local function definitions are illegal
C:Cpp1.cpp(108) : error C2065: 'pl' : undeclared identifier
C:Cpp1.cpp(108) : error C2227: left of '->top' must point to class/struct/union
C:Cpp1.cpp(109) : error C2227: left of '->car' must point to class/struct/union
C:Cpp1.cpp(115) : error C2601: 'menu' : local function definitions are illegal
C:Cpp1.cpp(125) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
أرجوكم أعطوني أي حل للبرنامج فقد سئمت من محاولة حله ، والتسليم غدا ولم أفلح في الحل وحاولت فيه مرات وكرات لكن بدون فائدة