Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h>// header file
#include<string.h>
void gear_down();// void as no return type and no arguments
void check_before_takeoff();
void raising_gear();
void gear_up();
void check_before_landing();
void lowering_gear();
typedef enum
{
geardown,
checkbeforetakeoff,
raisinggear,
gearup,
checkbeforelanding,
loweringgear
}state_type;
typedef enum
{
open,
close,
}switch_state;
typedef enum
{
down,
up,
rising,
falling
}lever;
typedef enum
{
Disabled,
Enabled,
}hydraulic;
switch_state squat,limit;
lever pilot;
state_type current_state;
typedef struct
{
char* statename;
char* light;
char* directionvalve;
char* hydraulic_valve;
}detail;
//accessing function by an array of function
static void(*state_table[])(void)={gear_down,check_before_takeoff,raising_gear,gear_up,
check_before_landing,lowering_gear};// static is used as to restrict the function
detail state_machine[6]=
{
{"Gear Down", "Green", "Down", "Enabled"},
{"Checking before takeoff", "Green","Down", "Enabled"},
{"Raising Gear", "Red","Up", "Enabled"},
{"Gear up", "off", "Null", "Disabled"},
{"Checking before landing", "Red","Down", "Enabled" },
{"Lowering gear", "Green", "Down","Enabled"}
};
int main()
{printf("Welcome to Air Arabia\n");
//current_state=geardown;
while(1)
{
state_table[current_state]();
printf("\n enter the next operation \n");
}
}
void gear_down()
{
current_state=geardown;
printf("current state is %s",state_machine[current_state].statename);
printf("\nis pilot lever rising =2 or falling=3:-");
fflush(stdout);
scanf("%d",&pilot);
printf("is squat switch opened=0 or closed=1:-");
fflush(stdout);
scanf("%d",&squat);
if((pilot==rising)&&(squat==open))
{
current_state=checkbeforetakeoff;// resetting for array of function as well as for
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\nhydraulic valve is %s\n\n",state_machine[current_state].hydraulic_valve);
}
else
{
gear_down();// if we enter falling so it will called the function and print the current state
}
}
void check_before_takeoff()
{
printf("is pilot lever up=1 or falling=3");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==up)
{
current_state=raisinggear;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\n hydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
else if(pilot==falling)
{
gear_down();
}
else
{
check_before_takeoff();//if we press any other value rather than 1 or three it will return to function
}
}
void raising_gear()
{
current_state=raisinggear;
printf("is pilot lever or falling=3 or up=1");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==falling)
{
current_state=checkbeforelanding;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\nhydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
else if(pilot==up)
{
current_state=gearup;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\nhydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
else
{
raising_gear();
}
}
void gear_up()
{
current_state=gearup;
printf("is pilot lever falling=3");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==falling)
{
current_state=checkbeforelanding;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\nhydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
else
{
gear_up();
}
}
void check_before_landing()
{
current_state=checkbeforelanding;
printf("is pilot lever down=0");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==down)
{
current_state=loweringgear;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\nhydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
else
{
check_before_landing();
}
}
void lowering_gear()
{
current_state=loweringgear;
printf("\nis pilot lever rising =2 ");
fflush(stdout);
scanf("%d",&pilot);
printf("is limit switch opened=0");
fflush(stdout);
scanf("%d",&limit);
if(pilot==rising)
{
current_state=checkbeforetakeoff;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\n hydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
else //(limit==open)
{
current_state=geardown;
printf("current state is %s",state_machine[current_state].statename);
printf("\nlight is %s",state_machine[current_state].light);
printf("\ndirection valve is %s",state_machine[current_state].directionvalve);
printf("\nhydraulic valve is %s\n",state_machine[current_state].hydraulic_valve);
}
}