Создать запись "Двигатель", которая содержит элементы "Название", "Мощность", "Скорость", "Цена" - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Создать запись "Двигатель", которая содержит элементы "Название", "Мощность", "Скорость", "Цена". На основе записи "Двигатель" создать запись "Электропривод", что дополнительно содержит записи "Цена преобразователя", "Цена датчика скорости", "Цена интерфейса управления". Создать и заполнить базу данных с 4 электроприводов. Найти электропривод, отвечающее заданным с клавиатуры условиям необходимой скорости и мощности, и имеет минимальную цену.

Решение задачи: «Создать запись "Двигатель", которая содержит элементы "Название", "Мощность", "Скорость", "Цена"»

textual
Листинг программы
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define SIZE 100
 
struct drive {
    float price_of_converter; // цена преобразователя
    float price_of_sensor; // цена датчика скорости
    float price_of_control; // цена интерфейса укавления
};
 
struct engine {
    char title[80]; // название
    float power; // мощность
    float speed; // скорость
    float price; // цена
    struct drive cost;
};
 
/* Main functons */
int show_menu(void);
void add_accout(void);
void found_accout(void);
 
/* Helpful functions */
int find_free(void);
void prepare(void);
 
struct engine model[SIZE];
 
int main(void)
{
    char choice;
    
    prepare();
    for ( ; ; ) {
    choice = show_menu();
    switch (choice) {
    case 1:
        add_accout();
        break;
    case 2:
        found_accout();
        break;
    case 3:
        exit(0);
        break;
    default:
        printf("Options <1><2><3>\n");
        break;
    }
    }
    
    return 0;
}
 
int find_free(void)
{
    register int i;
    
    for (i = 0; i < SIZE && model[i].title[0] != '\0'; ++i)
    ;
    if (i == SIZE) {
    printf("No slots in base\n");
    return -1;
    }
    return i;
}
 
void prepare(void)
{
    register int i;
    
    for (i = 0; i < SIZE; ++i)
    model[i].title[0] = '\0';
}
 
int show_menu(void)
{
    char s[80];
    
    printf("\n1: Add new engine to base\n");
    printf("2: Find low cost model\n");
    printf("3: Exit program\n");
    do {
    printf("What you want? <1><2><3>: ");
    gets(s);
    } while (atoi(s) < 1 || atoi(s) > 3);
    return atoi(s);
}
 
void add_accout(void)
{
    int i = find_free();
    if (i < 0)
    return;
    
    char s[80];
    
    printf("Enter new title model: ");
    gets(model[i].title);
    printf("Enter power of new model: ");
    gets(s);
    model[i].power = atof(s);
    printf("Enter speed of new model: ");
    gets(s);
    model[i].speed = atof(s);
    printf("Enter price cost of new model: ");
    gets(s);
    model[i].price = atof(s);
    printf("Enter price of converter of new model: ");
    gets(s);
    model[i].cost.price_of_converter = atof(s);
    printf("Enter price of sensor of new model: ");
    gets(s);
    model[i].cost.price_of_sensor = atof(s);
    printf("Enter price of control panel of new model: ");
    gets(s);
    model[i].cost.price_of_control = atof(s);
    return;
}
 
void found_accout(void)
{
    register int i;
    float s, p, min_cost;
    char str[80];
    
    printf("Enter speed what you wont to find: ");
    scanf("%f", &s);
    printf("Enter power what you wont to find: ");
    scanf("%f", &p);
    
    min_cost = 0.0;
    for (i = 0; i < SIZE; ++i)
    if (model[i].title[0] != '\0')
        if (model[i].speed == s && model[i].power == p)
        if (min_cost < model[i].price) {
            strcpy(str, model[i].title);
            min_cost = model[i].price;
        }
    if (min_cost)
    printf("Model %s have minimal price\n", str);
    else
    printf("No model found\n");
}

Оцени полезность:

8   голосов , оценка 3.875 из 5
Похожие ответы