Question: need step by step explanation in the code #include //innehller in- och utmatnigsfunktioner scanf, printf, fgetc, fopen, fread #include //innehller generella hjlpmedel malloc, size_t, atoi
need step by step explanation in the code
#include
#include
#include
#include
#define STR_LANGD 30
#define VEHICLE 10
#define NUMBEROF_PEOPLE 10
typedef struct
{
char name[STR_LANGD];
int age;
}
Person;
typedef struct
{
Person person;
char model[STR_LANGD];
char carbrand[STR_LANGD];
char registrationnumber[STR_LANGD];
}
Vehicle;
void initVehicle(Vehicle myVehicle[])
{
int i;
for (i = 0; i < VEHICLE; i++)
{
myVehicle[i].person.age = 0;
}
}
int menu() {
int choice = 0;
printf ("1. Add a vehicle. ");
printf ("2. Remove a vehicle ");
printf ("3. Sort by car brand. ");
printf ("4. Print vehicle information. ");
printf ("5. Print the entire vehicle register. ");
printf ("0. Exit. ");
scanf("%d", &choice);
return choice;
}
void AddVehicle(Vehicle myVehicle[])
{
Vehicle temp;
int i;
fflush(stdin);
printf("Enter name: ");
scanf("%s", temp.person.name);
printf("Enter personens lder: ");
scanf("%d", &temp.person.age);
printf("Enter Vehiclestyp: ");
scanf("%s", temp.model);
printf("Enter car brand: ");
scanf("%s", temp.carbrand);
printf("Enter registration number: ");
scanf("%s", temp.registrationnumber);
for (i = 0; i < VEHICLE; i++)
{
if (myVehicle[i].person.age == 0)
{
myVehicle[i] = temp;
break;
}
}
}
void showVehicle(Vehicle myVehicle[])
{
int i;
printf(" All vehicle in database ");
printf("------------------- ");
for (i = 0; i < VEHICLE; i++)
{
if (myVehicle[i].person.age != 0)
{
printf("Place: %d ", i);
printf("Name: ");
puts(myVehicle[i].person.name);
printf("lder p garen: %d ", myVehicle[i].person.age);
printf("Car model: %s ", myVehicle[i].model);
printf("Car brand: %s ", myVehicle[i].carbrand);
printf("Registration number: %s ", myVehicle[i].registrationnumber);
}
}
}
void DeleteVehicle(Vehicle myVehicle[VEHICLE])
{
int temp;
printf("P vilken plats (0-10) vill du ta bort Vehicleinfo? Plats: ");
scanf("%d", &temp);
if (myVehicle[temp].person.age != 0)
{
myVehicle[temp].person.age = 0;
}
}
void printinfo(Vehicle myVehicle[VEHICLE])
{
int temp;
printf("P vilken plats (0-10) vill du se Vehicleinfo? Plats: ");
scanf("%d", &temp);
if (myVehicle[temp].person.age != 0)
{
printf(" ------------Vehicleinfo----------- ");
printf("Place: %d", temp);
printf("name: %s ", myVehicle[temp].person.name);
printf("Age on owner: %d ", myVehicle[temp].person.age);
printf("Car model: %s ", myVehicle[temp].model);
printf("Car brand: %s ", myVehicle[temp].carbrand);
printf("Registration number: %s ", myVehicle[temp].registrationnumber);
}
}
void carSort(Vehicle myVehicle[VEHICLE])
{
int i;
int j;
Vehicle temp;
for (i = 1; i < VEHICLE; i++)
{
for (j = 0; j < VEHICLE - i; j++)
{
if (myVehicle[j].carbrand[0] > myVehicle[j+1].carbrand[0])
{
temp = myVehicle[j];
myVehicle[j] = myVehicle[j+1];
myVehicle[j+1] = temp;
}
}
}
}
int main() //main: hr startar alla C-program
{
Vehicle myVehicle[VEHICLE];
int menuchoice = 0;
initVehicle(myVehicle);
do
{
menuchoice = menu();
switch (menuchoice)
{
case 1: AddVehicle(myVehicle); break;
case 2: DeleteVehicle(myVehicle); break;
case 3: carSort(myVehicle); break;
case 4: printinfo(myVehicle); break;
case 5: showVehicle(myVehicle); break;
default: break;
}
}
while (menuchoice !=0);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
