帮我写一个c语言航空订票查询系统代码
时间: 2023-12-24 10:02:02 浏览: 39
好的,下面是一个简单的航空订票查询系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <json-c/json.h>
#define API_URL "https://api.airline.com/flights?from=%s&to=%s&date=%s"
#define MAX_FLIGHTS 100
struct Flight {
char flight_number[10];
char from[10];
char to[10];
char departure_time[20];
char arrival_time[20];
float price;
};
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) {
printf("Not enough memory!\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main() {
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
char from[10], to[10], date[11];
struct Flight flights[MAX_FLIGHTS];
int num_flights = 0;
printf("Welcome to the Airline Booking System!\n");
printf("Please enter your travel details:\n");
printf("From: ");
scanf("%s", from);
printf("To: ");
scanf("%s", to);
printf("Date (YYYY-MM-DD): ");
scanf("%s", date);
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
if (curl_handle) {
char url[100];
snprintf(url, sizeof(url), API_URL, from, to, date);
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
chunk.memory = malloc(1);
chunk.size = 0;
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
res = curl_easy_perform(curl_handle);
if (res != CURLE_OK) {
printf("Error: %s\n", curl_easy_strerror(res));
} else {
json_object *root = json_tokener_parse(chunk.memory);
json_object *flights_array;
if (json_object_object_get_ex(root, "flights", &flights_array)) {
int num_json_flights = json_object_array_length(flights_array);
for (int i = 0; i < num_json_flights && num_flights < MAX_FLIGHTS; i++) {
json_object *json_flight = json_object_array_get_idx(flights_array, i);
json_object *json_flight_number, *json_from, *json_to, *json_departure_time, *json_arrival_time, *json_price;
struct Flight *flight = &flights[num_flights];
if (json_object_object_get_ex(json_flight, "flight_number", &json_flight_number)) {
strncpy(flight->flight_number, json_object_get_string(json_flight_number), sizeof(flight->flight_number));
}
if (json_object_object_get_ex(json_flight, "from", &json_from)) {
strncpy(flight->from, json_object_get_string(json_from), sizeof(flight->from));
}
if (json_object_object_get_ex(json_flight, "to", &json_to)) {
strncpy(flight->to, json_object_get_string(json_to), sizeof(flight->to));
}
if (json_object_object_get_ex(json_flight, "departure_time", &json_departure_time)) {
strncpy(flight->departure_time, json_object_get_string(json_departure_time), sizeof(flight->departure_time));
}
if (json_object_object_get_ex(json_flight, "arrival_time", &json_arrival_time)) {
strncpy(flight->arrival_time, json_object_get_string(json_arrival_time), sizeof(flight->arrival_time));
}
if (json_object_object_get_ex(json_flight, "price", &json_price)) {
flight->price = json_object_get_double(json_price);
}
num_flights++;
}
}
json_object_put(root);
}
curl_easy_cleanup(curl_handle);
free(chunk.memory);
}
curl_global_cleanup();
printf("\n");
printf("Found %d flights:\n", num_flights);
printf("Flight No. From To Departure Time Arrival Time Price\n");
printf("------------------------------------------------------------------\n");
for (int i = 0; i < num_flights; i++) {
struct Flight *flight = &flights[i];
printf("%-10s %-7s %-7s %-16s %-16s $%.2f\n", flight->flight_number, flight->from, flight->to, flight->departure_time, flight->arrival_time, flight->price);
}
return 0;
}
```
在此代码中,我们使用了libcurl库连接了航空公司提供的API接口,获取了航班信息数据。然后,我们解析API返回的JSON数据,提取与航班查询相关的信息,并将查询结果以表格的形式输出到控制台。在此基础上,您可以进一步完善程序,实现订票、取消订票等功能。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)