SoFunction
Updated on 2025-03-09

Implementing vehicle information management system in C language

This article shares the specific code for implementing vehicle information management system in C language for your reference. The specific content is as follows

Vehicle Information Management System

Problem description:Establish a vehicle information management system to realize the addition and maintenance management of vehicle information.

System storage data requirements:Each vehicle needs to contain at least the following attributes, and the information of multiple vehicles is saved in the system:

  • Vehicle models (such as Civic, Corolla, Teana, etc.)
  • Manufacturers (such as GAC Honda, FAW Volkswagen, Dongfeng Nissan, etc.)
  • Model level (such as compact cars, mid-size cars, SUVs, etc.)
  • Number of seats (such as 5 seats, 7 seats, etc.)
  • Displacement (such as 1.2T, 1.4T, 1.5T, 1.6T, etc.)
  • Transmission (such as manual, manual, continuously variable speed, etc.)
  • Body color (white, black, etc.)
  • Vehicle price (unit is ten thousand)

Basic program requirements:

1. To have a system menu, select different options to perform different functions. After each function is executed, you need to return to the system menu and continue to select function.

2. The system needs to complete the following basic functions, and the code for each function is encapsulated in a separate function:

(1) When the system is initialized, at least 10 vehicle record information must be included.

(2) Enter vehicle information: Enter the basic information of a certain vehicle and automatically generate a record number for the new record. There must be a prompt when entering data, and output the added vehicle information on the screen after the entry is completed.

The vehicle information output format is as follows:

Vehicle Model Manufacturer Model Level Seat Displacement Transmission Body Color Vehicle Price
Accord Honda Mid-size Car 5-seater 1.5T continuously variable speed white 199,800

(3) Browse all vehicle information: count how many vehicles are there, and display all vehicle information on the screen.

(4) Query vehicle information according to different fields, including by manufacturer, vehicle type level, vehicle price range (enter the lowest price and the highest price)

(5) Modify vehicle information: Modify the basic information of the vehicle and require the menu to select a certain parameter information to modify it, rather than re-overwrite the entire information.

(6) Delete vehicle information: Delete vehicle information that meets the conditions, including deletion by model and manufacturer. Before requesting to delete, first search for the vehicle information that needs to be deleted according to the model or manufacturer. If it cannot be found, the corresponding prompt information will be given.

Extended requirements:

(1) Implement the sorting function. For example, sort vehicle information by vehicle price, seat number, displacement, etc.

(2) All vehicle information is stored in the file.

(3) Every time you open the program, import the information in the file into the program.

(4) You can define other functional requirements yourself and encode and implement them.

#include<>
#include<>
#include<>
#include <>
    typedef struct car Car,*cheliang;
    struct car
    {
    char brand[20];//model    char manufacturar[15];//Producer    char degree[15];//grade    int seats;//Number of seats    float displacement;//Displacement    char biansuxiang[20];//Transmission    char color[20];//color    char price[30];//price    cheliang next;
    };
cheliang head,tail;
/***********************************************************************
 * Description: Read the vehicle information from it and save it as a single-linked table
 *******************************************************************************/
void read_cars(); 
/***********************************************************************
 * Description: Write vehicle information into
 *******************************************************************************/
void write_cars();
/***********************************************************************
 * Description: Added vehicle information
 *******************************************************************************/
void add_car();//no problem/***********************************************************************
 * Description: Output all vehicle information to the computer
 *******************************************************************************/
void display_cars();//no problem/***********************************************************************
 * Description: Modify vehicle information
 *******************************************************************************/
void change();
/***********************************************************************
 * Description: Find vehicle information
 *******************************************************************************/
void find();
/***********************************************************************
 * Description: Find vehicle information by vehicle model
 *******************************************************************************/
void find_brand();
/***********************************************************************
 * Description: Search vehicle information by manufacturer
 *******************************************************************************/
void find_manufacturar();//no problem/***********************************************************************
 * Description: Find vehicle information by vehicle level
 *******************************************************************************/
void find_degree();
/***********************************************************************
 * Description: Find vehicle information by vehicle price
 *******************************************************************************/
void find_price();
/***********************************************************************
 * Description: Delete vehicle information
 *******************************************************************************/
void delete_car();
/***********************************************************************
 * Description: Delete vehicle information by vehicle model
 *******************************************************************************/
void delete_car_brand();
/***********************************************************************
 * Description: Delete vehicle information through the manufacturer
 *******************************************************************************/
void delete_car_manufacturar();
int main()
{
    read_cars();//Read the file    int i;
    while(1)
    {
        while(1)
        {
            printf("Please select relevant operations:\n1. Add vehicle information\n2. View vehicle information\n");
            printf("3. Query vehicle information\n4. Modify vehicle information\n5. Delete vehicle information\n6. Save data and exit the management system\n");
            printf("Please enter a number to select the function to implement:\n");
            scanf("%d",&i);
            if(i>=1&&i<=6)
            {
                break;
            }
            else
            {
                printf("Number error, please re-enter");
            }
        }
        switch(i)
        {
        case 1: add_car();break;
        case 2: display_cars();break;
        case 3: find();break;
        case 4: change();break;
        case 5: delete_car();break;
        case 6: write_cars();//Save data in file            printf("Thank you for using it!\n");
            exit(0);
        }
    }
    return 0;
}
void read_cars()
{
    FILE *fp;
    if (0 != fopen_s(&fp, "", "a+b"))//The return value of the fopen_s method is 0 or non-0. 0 means successful opening, and non-0 means failed opening    {
        perror("The file failed to open because");//perror(s) is used to output the cause of the error in the previous function to the standard device (stderr)        getch();
        exit(1);
    }
    cheliang car;
    head = (cheliang)malloc(sizeof(Car));
    if (NULL == head)
    {
        printf("Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }
    head->next = NULL;
    tail = head;
    while (1)
    {
        car = (cheliang)malloc(sizeof(Car));
        if (fread(car, sizeof(Car), 1, fp) == 0)
        {
            free(car);
            break;
        }
        car->next = NULL;
        tail->next = car;
        tail = car;
    }
    fclose(fp);
}
void write_cars()
{
    FILE *fp;
    if (0 != fopen_s(&fp, "", "w+"))//Open the file    {
        perror("The file failed to open because");
        _getch();
        return;
    }
    cheliang car = head->next;
    while (car != NULL)
    {
        fwrite(car, sizeof(Car), 1, fp);
        car = car->next;
    }
    fclose(fp);//Close the file}
void add_car()
{
    cheliang p,temp;
    p=(cheliang)malloc(sizeof(Car));
    if(p==NULL)
    {
        printf("Memory allocation failed");
        exit(1);
    }
    printf("Please enter vehicle information\nModel:");
    scanf("%s",p->brand);
    printf("Merchant:");
    scanf("%s",p->manufacturar);
    printf("grade:");
    scanf("%s",p->degree);
    printf("Number of seats:");
    scanf("%d",&p->seats);
    printf("Displacement:");
    scanf("%f",&p->displacement);
    printf("Transmission:");
    scanf("%s",p->biansuxiang);
    printf("color:");
    scanf("%s",p->color);
    printf("price:");
    scanf("%s",p->price);
    if(NULL == head->next)
    {
    head->next = p;
    p->next = NULL;
    }
    else
    {
    temp = head->next;//Head insertion method    head->next = p;
    p->next = temp;
    }
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",p->brand,p->manufacturar,p->degree,p->seats,p->displacement,p->biansuxiang,p->color,p->price);
    
}
void display_cars()
{
    int a=0;
    cheliang p=head->next;
    if(NULL == head->next)
    {
    printf("No vehicle information\n");
    return ;
    }
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    for(;p!=NULL;p=p->next)
    {
        a++;
        printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",p->brand,p->manufacturar,p->degree,p->seats,p->displacement,p->biansuxiang,p->color,p->price);
        
    }
    printf("There are %d vehicles in total",a);
}
void find()
{
    int k;
    while(1)
    {
        printf("Please select the search method: 1. Search by model 2. Search by manufacturer 3. Search by level 4. Search by price");
        printf("Please select:");
        scanf("%d",&k);
        if(k>=1&&k<=4){
            break;
        }
        else{
            printf("Number error, please re-enter");
        }
    }
    switch(k)
    {
    case 1: find_brand();break;
    case 2: find_manufacturar();break;
    case 3: find_degree();break;
    case 4:find_price();break;
    }
}
void find_brand()
{
    cheliang p;
    p=head->next;
    printf("Please enter the model you are looking for:");
    char brand[20];
    scanf("%s",brand);
    int j=0;
    printf("The information you are looking for is:\n");
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(p!=NULL)
    {
        if(strcmp(p->brand,brand)==0)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",p->brand,p->manufacturar,p->degree,p->seats,p->displacement,p->biansuxiang,p->color,p->price);
            p=p->next;
            j++;
        }
        else
         p=p->next;
    }
        if(j==0)
        {
            printf("The system does not have vehicle information for the manufacturer");
        }
}
void find_manufacturar()
{
    cheliang p;
    p=head->next;
    printf("Please enter the manufacturer you are looking for:");
    char manufacturar[15];
    int j=0;
    scanf("%s",manufacturar);
     fflush(stdin);
    printf("The information you are looking for is:\n");
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(p!=NULL)
    {
        if(strcmp(p->manufacturar,manufacturar)==0)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",p->brand,p->manufacturar,p->degree,p->seats,p->displacement,p->biansuxiang,p->color,p->price);
            p=p->next;
            j++;
        }
        else
         p=p->next;
    }
        if(j==0)
        {
            printf("The system does not have vehicle information for the manufacturer");
        }
}
void find_degree()
{
    cheliang p;
    p=head->next;
    printf("Please enter the level to look for:");
    char degree[15];
    int j=0;
    scanf("%s",degree);
    printf("The information you are looking for is:\n");
     fflush(stdin);
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(p!=NULL)
    {
        if(strcmp(p->degree,degree)==0)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",p->brand,p->manufacturar,p->degree,p->seats,p->displacement,p->biansuxiang,p->color,p->price);
            p=p->next;
            j++;
        }
        else
            p=p->next;

    }
        if(j==0)
        {
            printf("The system does not have vehicle information of this level");
        }
}
void find_price()
{
    cheliang p;
    p=head->next;
    //char price_max[30],price_min[30];
    int price_max,price_min;
    int j=0;
    printf("Please enter the maximum price:");
    scanf("%d",&price_max);
     fflush(stdin);
    printf("Please enter the minimum price:");
    scanf("%d",&price_min);
     fflush(stdin);
    printf("The information you are looking for is:\n");
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(p!=NULL)
    {
        if(atoi(p->price)>price_min && atoi(p->price)<price_max)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",p->brand,p->manufacturar,p->degree,p->seats,p->displacement,p->biansuxiang,p->color,p->price);
            j++;
        }
            p=p->next;
    }
    if(j==0)
    {
        printf("The system does not have vehicle information for this price range");
    }
}
void change()
{
    cheliang q;
    int i;
    q=head->next;
    int j=0;
    printf("Please enter the model of the vehicle you want to modify:\n");
    char brand[20];
    scanf("%s",brand);
     fflush(stdin);
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(q!=NULL)
    {
        if(strcmp(q->brand,brand)==0)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",q->brand,q->manufacturar,q->degree,q->seats,q->displacement,q->biansuxiang,q->color,q->price);
            j++;
            while(1){
                printf("1. Change model 2. Change manufacturer 3. Change grade 4. Change seat number 5. Change displacement 6. Change gearbox 7. Change color 8. Change price");
                printf("Please enter the type of vehicle information to be modified:");
                scanf("%d",&i);
                if(i>=1&&i<=8){
                    break;
                }
                else{
                    printf("Please enter the correct number");
                }
            }
              switch(i)
              {
              case 1:
                   printf("model:");
                   char str1[20];
                   scanf("%s",str1);
                   strcpy(q->brand,str1);break;
              case 2:
                   printf("Producer:");
                   char str2[15];
                   scanf("%s",str2);
                   strcpy(q->manufacturar,str2);break;
              case 3:
                     printf("grade:");
                    char str3[15];
                    scanf("%s",str3);
                   strcpy(q->degree,str3);break;
              case 4:
                    printf("Number of seats:");
                    int str4;
                    scanf("%d",&str4);
                   q->seats=str4;break;
              case 5:
                    printf("Displacement:");
                    float str5;
                    scanf("%f",&str5);
                    q->displacement=str5;break;
              case 6:
                    printf("Transmission:");
                    char str6[20];
                    scanf("%s",str6);
                   strcpy(q->biansuxiang,str6);break;
              case 7:
                    printf("color:");
                    char str7[20];
                    scanf("%s",str7);
                   strcpy(q->color,str7);break;
              case 8:
                    printf("price:");
                    char str8[30];
                    scanf("%s",str8);
                   strcpy(q->price,str8);break;
              }
            printf("Modification is successful\n");
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",q->brand,q->manufacturar,q->degree,q->seats,q->displacement,q->biansuxiang,q->color,q->price);
            q=q->next;
            break;
        }
        
        else
        {
            q=q->next;
        }
    }
    if(j==0)
    {
         printf("The model you want was not found");
    }
}
void delete_car()
{
    int i;
    while(true){
        printf("What method is to find a vehicle: 1. Model 2. Manufacturer\n");
        scanf("%d",&i);
        if(i>=1&&i<=2){
           break;
        }
        else{
            printf("Number error, please re-enter");
        }
    }
    switch(i)
    {
    case 1: delete_car_brand();break;
    case 2: delete_car_manufacturar();break;
    }
}
void delete_car_brand()
{
    cheliang p,q;
    int j=0;
    char brand[20];
    printf("Please enter the model of the vehicle you want to delete:");
    scanf("%s",brand);
     fflush(stdin);
    //p=head;
    q=head;
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(q->next!=NULL)
    {
        if(strcmp(q->next->brand,brand)==0)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",q->next->brand,q->next->manufacturar,q->next->degree,q->next->seats,q->next->displacement,q->next->biansuxiang,q->next->color,q->next->price);
            j++;
            p=q->next;
            q->next = q->next->next;
            printf("Data deletion succeeded!\n");
            free(p);
            break;
        }
        else
         q=q->next;
    }
    if(j==0)
    {
        printf("The system does not have the vehicle information you want to delete!\n");
    }
}
void delete_car_manufacturar()
{
    cheliang p,q;
    int j=0;
    char manufacturar[15];
    printf("Please enter the manufacturer of the vehicle you want to delete:");
    scanf("%s",manufacturar);
     fflush(stdin);
    q=head;
    printf("%-15s|%-15s|%-15s|%5s|%5s|%-15s|%-15s|%-5s\n",
        "Vehicle Model", "Manufacturer", "Car Type", "Number of seats", "Displacement(L)", "Transmission", "Body color", "Vehicle price(Ten thousand)");
    while(q->next!=NULL)
    {
        if(strcmp(q->next->manufacturar,manufacturar)==0)
        {
            printf("%-16s%-16s%-16s%-7d%-8.1f%-16s%-16s%-5s\n",q->next->brand,q->next->manufacturar,q->next->degree,q->next->seats,q->next->displacement,q->next->biansuxiang,q->next->color,q->next->price);
            j++;
            p=q->next;
            q->next = q->next->next;
            printf("Data deletion succeeded!\n");
            free(p);
            break;
        }
        else
         q=q->next;
    }
    if(j==0)
    {
        printf("The system does not have the vehicle information you want to delete!\n");
            exit(0);
    }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.