Home >Backend Development >C++ >How to use C++ to implement a simple flight query system?

How to use C++ to implement a simple flight query system?

WBOY
WBOYOriginal
2023-11-02 13:15:181170browse

How to use C++ to implement a simple flight query system?

How to use C to implement a simple flight query system?

Flight query system is a software system widely used in airlines, travel agencies and other industries. Through this system, users can query flight-related information, including flight number, departure time, arrival time, flight company, etc. Using C language, we can implement a simple and fully functional flight query system.

First, we need to define some data structures to represent flight information. Each flight can be represented by a structure, including fields such as flight number, departure time, arrival time, and flight company. In C, we can use a structure to define these fields:

struct Flight {
    string flightNumber;
    string departureTime;
    string arrivalTime;
    string airline;
};

Next, we can define an array containing flight information. We can use an array to store information for multiple flights. In this example, we assume that there are up to 100 flights:

const int MAX_FLIGHTS = 100;
Flight flights[MAX_FLIGHTS];

Now, we can start to implement the function of the flight query system. First, we need a function to add flight information. Users can add a new flight by entering the flight number, departure time, arrival time and flight company. We can define a function to implement this function:

void addFlight() {
    string flightNumber;
    string departureTime;
    string arrivalTime;
    string airline;
    
    // 获取用户输入的航班信息
    cout << "请输入航班号:";
    cin >> flightNumber;
    cout << "请输入出发时间:";
    cin >> departureTime;
    cout << "请输入到达时间:";
    cin >> arrivalTime;
    cout << "请输入航班公司:";
    cin >> airline;
    
    // 创建一个新的航班对象
    Flight newFlight;
    newFlight.flightNumber = flightNumber;
    newFlight.departureTime = departureTime;
    newFlight.arrivalTime = arrivalTime;
    newFlight.airline = airline;
    
    // 将航班对象添加到航班数组中
    for (int i = 0; i < MAX_FLIGHTS; i++) {
        if (flights[i].flightNumber.empty()) {
            flights[i] = newFlight;
            cout << "航班添加成功!" << endl;
            return;
        }
    }
    
    cout << "航班列表已满,无法添加新的航班!" << endl;
}

In addition to adding flight information, we also need a function to query flights. Users can query flight related information by entering the flight number. We can define a function to implement this function:

void searchFlight() {
    string flightNumber;
    
    // 获取用户输入的航班号
    cout << "请输入要查询的航班号:";
    cin >> flightNumber;
    
    // 查询航班信息
    for (int i = 0; i < MAX_FLIGHTS; i++) {
        if (flights[i].flightNumber == flightNumber) {
            cout << "航班号:" << flights[i].flightNumber << endl;
            cout << "出发时间:" << flights[i].departureTime << endl;
            cout << "到达时间:" << flights[i].arrivalTime << endl;
            cout << "航班公司:" << flights[i].airline << endl;
            return;
        }
    }
    
    cout << "未找到该航班!" << endl;
}

Finally, we can implement the menu function of the flight query system in a main function. Users can choose to add flights, check flights or exit the system. We can use an infinite loop to implement this function:

int main() {
    while (true) {
        int choice;
        
        // 显示菜单选项
        cout << "航班查询系统" << endl;
        cout << "1. 添加航班" << endl;
        cout << "2. 查询航班" << endl;
        cout << "3. 退出系统" << endl;
        cout << "请选择操作:";
        cin >> choice;
        
        // 根据用户选择执行相应的操作
        switch (choice) {
            case 1:
                addFlight();
                break;
            case 2:
                searchFlight();
                break;
            case 3:
                return 0;
            default:
                cout << "无效的操作!" << endl;
                break;
        }
        
        cout << endl;
    }
    
    return 0;
}

With the above code, we can implement a simple flight query system. Users can operate the system by adding flights and querying flights. This system can not only be used in industries such as airlines and travel agencies, but can also be used for the arrangement of personal travel plans. By learning and mastering the C programming language, we can implement various practical software systems.

The above is the detailed content of How to use C++ to implement a simple flight query system?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn