Untitled
unknown
c_cpp
2 years ago
4.9 kB
10
Indexable
#include <stdio.h>
#include <string.h>
#define SIZE 4
struct Tire
{ // One tire
char tireSide[3]; // LF, RF, LR, RR
double pressure;
};
struct Vehicle
{
char brand[21];
char model[21];
double engineCap; // Engine Capacity
struct Tire trs[4]; // 4 tires for the vehicle
};
void displayData(struct Vehicle *veh)
{
int i;
printf("Brand: %s, Model: %s, Engine Cap: %.2lf, ", (*veh).brand, (*veh).model, (*veh).engineCap);
for (i = 0; i < 4; i++)
{
if (strcmp(veh->trs[i].tireSide, "LF") == 0)
{
printf("LF: %.2lf, ", veh->trs[i].pressure);
}
else if (strcmp(veh->trs[i].tireSide, "RF") == 0)
{
printf("RF: %.2lf, ", veh->trs[i].pressure);
}
else if (strcmp(veh->trs[i].tireSide, "LR") == 0)
{
printf("LR: %.2lf, ", veh->trs[i].pressure);
}
else if (strcmp(veh->trs[i].tireSide, "RR") == 0)
{
printf("RR: %.2lf", veh->trs[i].pressure);
}
else
{
}
}
printf("\n");
}
void setTireData(struct Tire *tires, const int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("Enter tire side: ");
scanf("%2[^\n]%*c", tires[i].tireSide);
printf("Enter tire pressure: ");
scanf("%lf%*c", &tires[i].pressure);
}
}
void setData(struct Vehicle *veh)
{
printf("Enter vehicle brand: ");
scanf("%20[^\n]%*c", (*veh).brand);
printf("Enter vehicle model: ");
scanf("%20[^\n]%*c", (*veh).model);
printf("Enter vehicle engineCap: ");
scanf("%lf%*c", &(*veh).engineCap);
setTireData((*veh).trs, SIZE);
printf("\n");
}
void findVehiclesByBrand(struct Vehicle *veh, int size)
{
int i;
char brand[21];
printf("Enter brand: ");
scanf("%20[^\n]", brand);
for (i = 0; i < size; i++)
{
if (strcmp(brand, veh[i].brand) == 0)
{
displayData(&veh[i]);
}
}
}
void findVehiclesByEngineCap(struct Vehicle *veh, int size)
{
int i;
double engineCap;
printf("Enter engine cap: ");
scanf("%lf", &engineCap);
for (i = 0; i < size; i++)
{
if (engineCap == veh[i].engineCap)
{
displayData(&veh[i]);
}
}
}
void sortVehiclesByEngineCap(struct Vehicle *veh, int size)
{
int i, j;
struct Vehicle temp;
for (i = 0; i < size - 1; i++)
{
for (j = 0; j < size - 1; j++)
{
if (veh[j].engineCap > veh[j + 1].engineCap)
{
temp = veh[j];
veh[j] = veh[j + 1];
veh[j + 1] = temp;
}
}
}
for (i = 0; i < size; i++)
{
displayData(&veh[i]);
}
}
int main()
{
int i, selection;
// Create an array of Vehicles names called veh, Size of array should be SIZE
struct Vehicle veh[SIZE];
// Create a function named setData and pass one vehicle data at a time (using
// address) Inside setData, obtain input from user to set vehicle data. For
// setting tire pressure, you need to call another function called setTireData
// from within setData this function will receive an array of Tires that will
// be set from user input for tire location and pressure
for (i = 0; i < SIZE; i++)
{
setData(&veh[i]);
}
// Then display a menu option that allows the following and obtain a user
// selection
// 1. findVehicles by brand
// 2. findVehicles by engineCap
// 3. Sort Vehicles by Engine Capacity
// 4. Quit the Program
// Depending on what the user inputs, your program should either display data
// (as per option 1,2 or 3)or quit(4) Implement the functionality needed for
// selection (1,2 and 3 above) using separate function for each. (You can set
// your own function name)
do
{
printf(
"=========================\n"
"1) findVehicles by brand\n"
"2) findVehicles by engineCap\n"
"3) Sort Vehicles by Engine Capacity\n"
"-------------------------\n"
"4) Quit the Program\n"
"-------------------------\n"
"Selection: ");
scanf("%d%*c", &selection);
putchar('\n');
switch (selection)
{
case 1:
findVehiclesByBrand(veh, SIZE);
break;
case 2:
findVehiclesByEngineCap(veh, SIZE);
break;
case 3:
sortVehiclesByEngineCap(veh, SIZE);
break;
case 4:
break;
}
} while (selection != 4);
// You should also create a displayData utility function that will be used to display the results generate from option 1,2 and 3 above.
// This function will receive one vehicle information at a time as an address.
return 0;
}Editor is loading...