Advertisement
NickAndNick

Ввод и сортировка массива структур

Jun 8th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. #define SIZE 3
  7.  
  8. enum MSG {
  9.     SHEET_ERROR,
  10.     SHEET_NUMBER,
  11.     SHEET_SURNAME,
  12.     SHEET_TIME,
  13.     SHEET_PAYMENT
  14. };
  15.  
  16. typedef struct {
  17.     char number[17], surname[25], time[6], payment[6];
  18. } SHEET;
  19.  
  20. typedef SHEET* PSHEET;
  21.  
  22. void msg(enum MSG command) {
  23.     switch (command) {
  24.         case SHEET_ERROR:
  25.             while (getchar() != '\n');
  26.             printf("Ошибка!\n");
  27.             break;
  28.         case SHEET_NUMBER: printf("Номер страховки: "); break;
  29.         case SHEET_SURNAME: printf("Фамилия: "); break;
  30.         case SHEET_TIME: printf("Количество отработанных часов: "); break;
  31.         case SHEET_PAYMENT: printf("Почасовая оплата: "); break;
  32.         default: printf("Входная команда функции msg(enum MSG command) не реализована!\n");
  33.     }
  34. }
  35.  
  36. void input(PSHEET psheet, int size) {
  37.     for (int i = 0; i != size; ++i) {
  38.         printf("\n\t Введите данные о %d-ой графе\n", i + 1);
  39.         msg(SHEET_NUMBER);
  40.         while (!scanf_s("%s", psheet[i].number, _countof(psheet[i].number))) {
  41.             msg(SHEET_ERROR);
  42.             msg(SHEET_NUMBER);
  43.         }
  44.         msg(SHEET_SURNAME);
  45.         while (!scanf_s("%s", psheet[i].surname, _countof(psheet[i].surname))) {
  46.             msg(SHEET_ERROR);
  47.             msg(SHEET_SURNAME);
  48.         }
  49.         msg(SHEET_TIME);
  50.         while (!scanf_s("%s", psheet[i].time, _countof(psheet[i].time))) {
  51.             msg(SHEET_ERROR);
  52.             msg(SHEET_TIME);
  53.         }
  54.         msg(SHEET_PAYMENT);
  55.         while (!scanf_s("%s", psheet[i].payment, _countof(psheet[i].payment))) {
  56.             msg(SHEET_ERROR);
  57.             msg(SHEET_PAYMENT);
  58.         }
  59.     }
  60. }
  61.  
  62. void swap(PSHEET a, PSHEET b) {
  63.     SHEET tmp;
  64.     strcpy_s(tmp.number, _countof(tmp.number), a->number);
  65.     strcpy_s(tmp.surname, _countof(tmp.surname), a->surname);
  66.     strcpy_s(tmp.time, _countof(tmp.time), a->time);
  67.     strcpy_s(tmp.payment, _countof(tmp.payment), a->payment);
  68.     strcpy_s(a->number, _countof(a->number), b->number);
  69.     strcpy_s(a->surname, _countof(a->surname), b->surname);
  70.     strcpy_s(a->time, _countof(a->time), b->time);
  71.     strcpy_s(a->payment, _countof(a->payment), b->payment);
  72.     strcpy_s(b->number, _countof(b->number), tmp.number);
  73.     strcpy_s(b->surname, _countof(b->surname), tmp.surname);
  74.     strcpy_s(b->time, _countof(b->time), tmp.time);
  75.     strcpy_s(b->payment, _countof(b->payment), tmp.payment);
  76. }
  77.  
  78. void bubble(PSHEET psheet, int size) {
  79.     for (int i = size - 1; i >= 0; --i) {
  80.         for (int j = 0; j < i; ++j) {
  81.             int cmp = strcmp(psheet[j].number, psheet[j + 1].number);
  82.             if (cmp > 0) swap(&psheet[j], &psheet[j + 1]);
  83.         }
  84.     }
  85. }
  86.  
  87. void output(PSHEET psheet, int size) {
  88.     const char* line = "---------------------------------------------------------------------------------\n";
  89.     const char* format = "|%-17s|%-25s|%-19s|%-15s|\n";
  90.     printf(line);
  91.     printf("|%44s%37s", "Ведомость", "|\n");
  92.     printf(line);
  93.     printf("| Номер страховки |    Фамилия работника    | Отработанные часы |     Оплата    |\n");
  94.     //printf("| Номер страховки | Фамилия | Количество отработанных часов  | Почасовая оплата |\n");
  95.     printf(line);
  96.     for (int i = 0; i < size; i++) {
  97.         printf(format, psheet[i].number, psheet[i].surname, psheet[i].time, psheet[i].payment);
  98.         printf(line);
  99.     }
  100. }
  101.  
  102. int main(void) {
  103.     PSHEET psheet = (PSHEET)malloc(SIZE * sizeof(SHEET));
  104.     system("chcp 1251");
  105.     system("cls");
  106.     setlocale(LC_ALL, "Russian");
  107.     if (psheet != NULL) {
  108.         input(psheet, SIZE);
  109.         bubble(psheet, SIZE);
  110.         output(psheet, SIZE);
  111.         free(psheet);
  112.     } else {
  113.         puts("Недостаточно памяти!");
  114.     }
  115.     system("pause");
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement