0

как мне отсортировать введённые данные по возрастанию, то есть передать data в функцию sort и отсортировать data.perc и переставить местами data.month_num соответственно, затем вернуть data. Перепробовал многие варианты, но не получалось, хочу узнать как именно это сделать.

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

const int n = 12, m = 10, kst = 12;

struct business {
    char months[n][m] = { "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" };
    int month_num;
    int plan;
    int act;
    float perc;
} data[kst];

int main() {

    int i, count=0;

    puts("Enter the data based on the results of enterprise for several months");
    for (i = 0; i < kst; i++) {
        printf("Enter the number of the month: ");
        if (scanf("%d", &data[i].month_num) == 0) break;

        printf("Enter the planned result: ");
        scanf("%d", &data[i].plan);    

        printf("Enter the actual result: ");
        scanf("%d", &data[i].act);
        count++;
    }

    for (i = 0; i < count; i++) {
        data[i].perc = 100.0 * data[i].act / data[i].plan;
        //printf("\n%.1f%%", data[i].perc);
    }

    struct business sort_data();
    printf("\n\n\n%d, %f", data[0].month_num, data[0].perc);

    return 0;
}


struct business sort(int count) {
    int temp;
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if (data[j].perc > data[j + 1].perc) {

                temp = data[j].perc;
                data[j].perc = data[j + 1].perc;
                data[j + 1].perc = temp;

                temp = data[j].month_num;
                data[j].month_num = data[j + 1].month_num;
                data[j + 1].month_num = temp;

            }
        }
   }

    return *data;
}

1 ответ 1

2

Структуру struct business можно копировать полностью, одной командой =. И в переменной int temp; надо поменять тип на struct business temp;.

void sort(int count) {
    struct business temp;
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if (data[j].perc > data[j + 1].perc) {

                temp = data[j];
                data[j] = data[j + 1];
                data[j + 1] = temp;

            }
        }
   }
}

Возвращать копию первой структуры не имеет смысла. return * data;


Непонятен элемент в структуре month как матрица, а инициализирован как вектор. Это будет в каждом экземпляре. Не много?

struct business {
  char months[n][m] = { "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" };
1
  • Спасибо, у меня всё заработало, сортирует правильно, насчёт month, она в структуре стоит ошибочно, уже убрал.
    – Aeros
    14 фев 2021 в 22:00

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.