Write a C program to store the information of vehicles. Use bit fields to store the status of information.
* The types of vehicles information are Two Wheeler=3, Four Wheeler=4, Petrol=0, Diesel=1, Old=5 and New=6
* The bit fields member variables are fuel, type and model
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>//Standart Library
#include <stdio.h>//Standart Input Output
#include <string.h>//
#include <malloc.h>//Alloc the memory
#define SIZE 655 //Size array the vehicles
//Implementation of bits type(Struct the vehicles)
typedef struct Vehicles
{
int fuel;//Petrol=0 or Diesel=1
int type;//Two wheeler=3 or fout wh=4
int model;//Model new=6 or old=5
}Vehicles;
//Input Date
void InputVehicles(Vehicles* v)
{
printf("Please enter the fuel: (Petrol=0 or Diesel=1): ");
scanf("%i", &v->fuel);
printf("Please enter the type: (Two wheeler=3 or four wheeler=4): ");
scanf("%i", &v->type);
printf("Please enter the fuel: (old=5 or new =6): ");
scanf("%i", &v->model);
}
//Out Date
void Outvehicles(Vehicles* v)
{
printf("Type: ");
if (v->type == 3)
printf("Two wheeler");
else
printf("Four wheeler");
printf("\tFuel: ");
if (v->fuel == 0)
printf("Petrol");
else
printf("Diesel");
printf("\tModel: ");
if (v->model == 5)
printf("Old");
else
printf("New");
printf("\n");
}
int main()
{
Vehicles* arrayV = (Vehicles*)malloc(sizeof(Vehicles) * SIZE);
int quit = 0;//Flag quit programm 1-quit 0-disquit
int size = 0;//size array
while (!quit)
{
printf("Menu\n");
printf("\t1 -Add Vehicles\n");//Add the new vehicles
printf("\t2 -Print Information all Vehicles\n");
printf("\t3 -Quit\n");
int cmd;
scanf("%i", &cmd);
switch (cmd)
{
case 1:
{
printf("Enter the information about the vehicles:\n");
InputVehicles(&arrayV[size]);//Input Date
size++;//Increase cnt vehicles
printf("Successfully added!!\n");
break;
}
case 2:
{
printf("======================Information===============================\n");
for (int i = 0; i < size; i++)
{
Outvehicles(&arrayV[i]);
}
printf("==================================================================\n");
break;
}
case 3:
{
printf("Exit\n");
quit = 1;
break;
}
default:
break;
}
}
free(arrayV);
return 0;
}
Comments
Leave a comment