Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

#include #include <ctime> #include <cstdlib> #include <fstream> #include <iostream>…

  1. User

    #include #include #include #include #include

    using namespace std;

    struct date { int day; char *month; int year; };

    struct fio { char *name; char *surname; char *patronymic; };

    struct car { char *brand; fio owner; int engine_power; int mileage; date last_service; };

    void load_data(car *cars, int n, const string &file_name); void add_car(car *cars, int &n); void print_cars(const car *cars, int n); void search_cars(const car *cars, int n, date current_date); void find_brand(const car *cars, int n, const char *brand); void sort_print_high_mileage(car *cars, int n, int mileage_limit); void save_data(const car *cars, int n, const string &file_name); void free_memory(car *cars, int n);

    int main() { int n; cout << "Enter the number of cars: "; cin >> n; cin.ignore();

    car *cars = new car[n];

    load_data(cars, n, “cars_data.txt”);

    bool exit = false; while (!exit) { int choice; cout << “1. Add a new car” << endl; cout << “2. Print car information” << endl; cout << “3. Find cars with more than 18 months since last service” << endl; cout << “4. Find cars of a specified brand” << endl; cout << “5. Find owners with mileage greater than specified (sorted by alphabet)” << endl; cout << “6. Exit” << endl; cout << "Choose an action: "; cin >> choice; cin.ignore();

    time_t now = time(0); tm *ltm = localtime(&now); date current_date; current_date.day = ltm->tm_mday; current_date.month = new char[20]; strcpy(current_date.month, “xx”); current_date.year = 1900 + ltm->tm_year;

    if (choice == 1) { cout << "Enter the number of new cars: "; int k; cin >> k; cin.ignore(); car *new_cars = new car[k]; add_car(new_cars, k); car *temp_cars = new car[n+k]; memcpy(temp_cars, cars, n * sizeof(car)); memcpy(temp_cars + n, new_cars, k * sizeof(car)); n += k; delete[] cars; delete[] new_cars; cars = temp_cars; } else if (choice == 2) { print_cars(cars, n); } else if (choice == 3) { search_cars(cars, n, current_date); } else if (choice == 4) { char *brand = new char[20]; cout << "Enter the brand to search for: "; gets(brand); find_brand(cars, n, brand); delete[] brand; } else if (choice == 5) { int mileage_limit; cout << "Enter the mileage limit: "; cin >> mileage_limit; cin.ignore(); sort_print_high_mileage(cars, n, mileage_limit); } else { exit = true; } }

    save_data(cars, n, “cars_data.txt”); free_memory(cars, n); delete[] cars; delete[] current_date.month;

    return 0; }

    void load_data(car *cars, int n, const string &file_name) { ifstream input_file(file_name); if (!input_file.is_open()) { cerr << “Failed to open file for reading.” << endl; return; }

    for (int i = 0; i < n; i++) { cars[i].brand = new char[20]; input_file.getline(cars[i].brand, 20);

    cars[i].owner.name = new char[20]; input_file.getline(cars[i].owner.name, 20);

    cars[i].owner.surname = new char[20]; input_file.getline(cars[i].owner.surname, 20);

    cars[i].owner.patronymic = new char[20]; input_file.getline(cars[i].owner.patronymic, 20);

    input_file >> cars[i].engine_power; input_file >> cars[i].mileage;

    cars[i].last_service.day = 0; input_file >> cars[i].last_service.day;

    cars[i].last_service.month = new char[20]; input_file.getline(cars[i].last_service.month, 20);

    cars[i].last_service.year = 0; input_file >> cars[i].last_service.year;

    input_file.ignore(); }

    input_file.close(); }

    void add_car(car *cars, int &n) { for (int i = 0; i < n; i++) { cout << "New car: " << endl; cout << "Brand: "; cars[i].brand = new char[20]; cin.getline(cars[i].brand, 20);

    cout << "Owner’s name: "; cars[i].owner.name = new char[20]; cin.getline(cars[i].owner.name, 20);

    cout << "Owner’s surname: "; cars[i].owner.surname = new char[20]; cin.getline(cars[i].owner.surname, 20);

    cout << "Owner’s patronymic: "; cars[i].owner.patronymic = new char[20]; cin.getline(cars[i].owner.patronymic, 20);

    cout << "Engine power: "; cin >> cars[i].engine_power; cin.ignore();

    cout << "Mileage: "; cin >> cars[i].mileage; cin.ignore();

    cout << "Last service date (day, month, year): "; cars[i].last_service.day = 0; cin >> cars[i].last_service.day;

    cars[i].last_service.month = new char[20]; cin.ignore(); cin.getline(cars[i].last_service.month, 20);

    cars[i].last_service.year = 0; cin >> cars[i].last_service.year;

    cout << endl; } }

    void print_cars(const car *cars, int n) { cout << "Car information: " << endl; for (int i = 0; i < n; i++) { cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic << " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " " << cars[i].last_service.month << " " << cars[i].last_service.year << endl; } cout << endl; }

    void search_cars(const car *cars, int n, date current_date) { cout << "Cars that need service: " << endl; for (int i = 0; i < n; i++) { char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; int current_month_index = 0; int last_service_month_index = 0;

    for (int j = 0; j < 12; j++) { if (strcmp(current_date.month, month_names[j]) == 0) { current_month_index = j + 1; } if (strcmp(cars[i].last_service.month, month_names[j]) == 0) { last_service_month_index = j + 1; } }

    int months_since_last_service = (current_date.year - cars[i].last_service.year) * 12 + (current_month_index - last_service_month_index); if (months_since_last_service > 18) { cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic << " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " " << cars[i].last_service.month << " " << cars[i].last_service.year << endl; } } cout << endl; }

    void find_brand(const car *cars, int n, const char *brand) { cout << "Cars with the specified brand: " << endl; for (int i = 0; i < n; i++) { if (strcmp(cars[i].brand, brand) == 0) { cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic << " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " | " << cars[i].last_service.month << " " << cars[i].last_service.year << endl; } } cout << endl; }

    void sort_print_high_mileage(car *cars, int n, int mileage_limit) { car *high_mileage_cars = nullptr; int count = 0;

    for (int i = 0; i < n; i++) { if (cars[i].mileage > mileage_limit) { count++; car *temp = new car[count]; if (high_mileage_cars != nullptr) { memcpy(temp, high_mileage_cars, (count - 1) * sizeof(car)); delete[] high_mileage_cars; } temp[count - 1] = cars[i]; high_mileage_cars = temp; } }

    for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (strcmp(high_mileage_cars[j].owner.surname, high_mileage_cars[j + 1].owner.surname) > 0) { car temp = high_mileage_cars[j]; high_mileage_cars[j] = high_mileage_cars[j + 1]; high_mileage_cars[j + 1] = temp; } } }

    cout << "Owners with mileage greater than the specified limit (sorted by alphabet): " << endl; for (int i = 0; i < count; i++) { cout << high_mileage_cars[i].brand << " | " << high_mileage_cars[i].owner.name << " | " << high_mileage_cars[i].owner.surname << " | " << high_mileage_cars[i].owner.patronymic << " | " << high_mileage_cars[i].engine_power << " | " << high_mileage_cars[i].mileage << " | " << high_mileage_cars[i].last_service.day << " " << high_mileage_cars[i].last_service.month << " " << high_mileage_cars[i].last_service.year << endl; } cout << endl;

    if (high_mileage_cars != nullptr) { delete[] high_mileage_cars; } }

    void save_data(const car *cars, int n, const string &file_name) { ofstream output_file(file_name); if (!output_file.is_open()) { cerr << “Failed to open file for writing.” << endl; return; }

    for (int i = 0; i < n; i++) { output_file << cars[i].brand << endl << cars[i].owner.name << endl << cars[i].owner.surname << endl << cars[i].owner.patronymic << endl << cars[i].engine_power << endl << cars[i].mileage << endl << cars[i].last_service.day << endl << cars[i].last_service.month << endl << cars[i].last_service.year << endl; }

    output_file.close(); }

    void free_memory(car *cars, int n) { for (int i = 0; i < n; i++) { delete[] cars[i].brand; delete[] cars[i].owner.name; delete[] cars[i].owner.surname; delete[] cars[i].owner.patronymic; delete[] cars[i].last_service.month; } }

    error: ‘current_date’ was not declared in this scope 112 | delete[] current_date.month;

  2. Assistant

    This error occurs in the main function because the variable "current_date" was declared inside the while loop, and it is not accessible outside that scope. To fix this error, declare the "current_date" variable outside the while loop, before the "exit" variable. Here's the corrected code:

    ... date current_date; current_date.day = ltm->tm_mday; current_date.month = new char[20]; strcpy(current_date.month, “xx”); current_date.year = 1900 + ltm->tm_year;

    bool exit = false; while (!exit) { ... }

    delete[] current_date.month; ...

  3. User

    как построить в питоне трёхфокусный эллипсоид? дай пример

  4. Assistant

    Для построения трехфокусного эллипсоида в Python можно использовать библиотеку mpl_toolkits.mplot3d.

    Вот пример кода:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # Определяем параметры трехфокусного эллипсоида
    a = 4
    b = 2
    c = 1
    x0 = 1
    y0 = 2
    z0 = 0
    
    # Создаем сетку точек x, y, z
    x = np.linspace(-1, 1, 100)
    y = np.linspace(-1, 1, 100)
    xx, yy = np.meshgrid(x, y)
    zz = np.sqrt((a*a - c*c - b*b) / (2*c*c)) * np.sqrt((xx - x0)**2 + (yy - y0)**2 + c*c) \
        + np.sqrt((a*a - c*c + b*b) / (2*c*c)) * np.sqrt((xx - x0)**2 + (yy - y0)**2 + c*c) \
        + z0
    
    # Отображаем трехфокусный эллипсоид
    ax.plot_surface(xx, yy, zz, alpha=0.5)
    
    plt.show()
    

    В этом примере мы задаем параметры трехфокусного эллипсоида (длины осей a, b и c, а также координаты центра x0, y0 и z0), создаем сетку точек x, y, z и вычисляем значения z на основе уравнения эллипсоида. Затем мы отображаем эллипсоид с помощью функции plot_surface в трехмерном пространстве.

  5. User

    как построить в питоне трёхфокусный эллипсоид? дай пример

  6. Assistant

    Для построения трехфокусного эллипсоида в Python можно использовать библиотеку mpl_toolkits.mplot3d.

    Вот пример кода:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # Определяем параметры трехфокусного эллипсоида
    a = 4
    b = 2
    c = 1
    x0 = 1
    y0 = 2
    z0 = 0
    
    # Создаем сетку точек x, y, z
    x = np.linspace(-1, 1, 100)
    y = np.linspace(-1, 1, 100)
    xx, yy = np.meshgrid(x, y)
    zz = np.sqrt((a*a - c*c - b*b) / (2*c*c)) * np.sqrt((xx - x0)**2 + (yy - y0)**2 + c*c) \
        + np.sqrt((a*a - c*c + b*b) / (2*c*c)) * np.sqrt((xx - x0)**2 + (yy - y0)**2 + c*c) \
        + z0
    
    # Отображаем трехфокусный эллипсоид
    ax.plot_surface(xx, yy, zz, alpha=0.5)
    
    plt.show()
    

    В этом примере мы задаем параметры трехфокусного эллипсоида (длины осей a, b и c, а также координаты центра x0, y0 и z0), создаем сетку точек x, y, z и вычисляем значения z на основе уравнения эллипсоида. Затем мы отображаем эллипсоид с помощью функции plot_surface в трехмерном пространстве.