Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <float.h>
  6. #include <locale.h>
  7.  
  8. /* utils */
  9. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  10. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  11. #define CLS() system("cls")
  12. #define PAUSE() system("pause")
  13.  
  14. /* Control input function */
  15. #define ESCAPE (27)
  16. #define BACKSPACE (8)
  17. #define ENTER (13)
  18.  
  19. #define BUFFER_LENGTH (256) /* длина буфера ввода */
  20. static char __buffer[BUFFER_LENGTH + 1]; /* буфер ввода */
  21.  
  22. /* Функция для ограничения ввода */
  23. static unsigned input(const char* const use, const char* const printAfter)
  24. {
  25.     int _tmp;
  26.     unsigned i = 0;
  27.    
  28.     if (!use)
  29.         return 0;
  30.  
  31.     while (i < BUFFER_LENGTH)
  32.     {
  33.         _tmp = getch();
  34.         if (_tmp == ENTER)
  35.         {
  36.             __buffer[i] = '\0';
  37.             break;
  38.         }
  39.         else if (_tmp == BACKSPACE && i > 0)
  40.         {
  41.             printf("\b \b");
  42.             --i;
  43.         }
  44.         else if (_tmp == ESCAPE)
  45.         {
  46.             exit(0);
  47.         }
  48.         else if (_tmp == 0) /* Если нажата специальная клавиша */
  49.         {
  50.             continue;
  51.         }
  52.         else if (strchr(use, _tmp))
  53.         {
  54.             __buffer[i++] = _tmp;
  55.             putchar(_tmp);
  56.         }
  57.     }
  58.  
  59.     if (printAfter)
  60.     {
  61.         printf(printAfter);
  62.     }
  63.  
  64.     return i;
  65. }
  66.  
  67. /* запросить ввод вещественного числа */
  68. float inputFloat(const char* const printAfter)
  69. {
  70.     input(",0123456789", printAfter);
  71.     return atof(__buffer);
  72. }
  73.  
  74. enum
  75. {
  76.     CREATE_FILE = 0,
  77.     PRINT_SOURCE_FILE,
  78.     PROCESS,
  79.     PRINT_DEST_FILE,
  80.     CLOSE,
  81.     STATE_MAX
  82. };
  83.  
  84. static const char* const MENU_NAMES[] =
  85. {
  86.     "Создать файл",
  87.     "Вывести исходный файл",
  88.     "Обработать выходной файл",
  89.     "Вывести выходной файл",
  90.     "Выход"
  91. };
  92.  
  93. #define FILENAME_LENGTH (260)
  94. static FILE* destFile = NULL, *srcFile = NULL;
  95.  
  96. unsigned char yesNo(void)
  97. {
  98.     while (1)
  99.     {
  100.         input("YyNn", "\n");
  101.         if (!strcmp(__buffer, "N") || !strcmp(__buffer, "n"))
  102.         {
  103.             return 0;
  104.         }
  105.         else if (!strcmp(__buffer, "Y") || !strcmp(__buffer, "y"))
  106.         {
  107.             return 1;
  108.         }
  109.     }
  110. }
  111.  
  112. static char* fieldNames = NULL;
  113. static unsigned numFields = 0, allocated = 0;
  114. #define NUM_COLS    (3)
  115.  
  116. FILE* OpenFile(void)
  117. {
  118.     FILE* file;
  119.     char filename[FILENAME_LENGTH];
  120.     while (1)
  121.     {
  122.         printf("Введите имя файла: ");
  123.         scanf("%s", filename);
  124.         if ((file = fopen(filename, "r")))
  125.         {
  126.             fclose(file);
  127.             printf("Файл с именем \"%s\" уже существует. Перезаписать? (Y/N): ", filename);
  128.             if (yesNo())
  129.             {
  130.                 file = fopen(filename, "w+");
  131.                 break;
  132.             }
  133.         }
  134.         else
  135.         {
  136.             file = fopen(filename, "w+");
  137.             printf("Файл с именем \"%s\" создан.\n", filename);
  138.             break;
  139.         }
  140.     }
  141.  
  142.     return file;
  143. }
  144.  
  145. void CreateFile(void)
  146. {
  147.     CLS();
  148.     if (srcFile)
  149.     {
  150.         printf("Исходный файл уже создан! Хотите создать новый? (Y/N): ");
  151.         if (yesNo())
  152.         {
  153.             fclose(srcFile);
  154.             if (destFile)
  155.             {
  156.                 fclose(destFile);
  157.                 destFile = NULL;
  158.             }
  159.         }
  160.         else
  161.         {
  162.             PAUSE();
  163.             return;
  164.         }
  165.     }
  166.    
  167.     srcFile = OpenFile();
  168.  
  169.     CLS();
  170.    
  171.     /* input data */
  172.     for (unsigned char i = 0; i < numFields; ++i)
  173.     {
  174.         printf("Тип дохода: %s\n", fieldNames[i]);
  175.         fprintf(srcFile, "%s\n", fieldNames[i]);
  176.         printf("1940 год: ");
  177.         fprintf(srcFile, "1940 год: %f\n", inputFloat("\n"));
  178.         printf("1950 год: ");
  179.         fprintf(srcFile, "1950 год: %f\n", inputFloat("\n"));
  180.         printf("1956 год: ");
  181.         fprintf(srcFile, "1956 год: %f\n", inputFloat("\n"));
  182.     }
  183.     rewind(srcFile);
  184.  
  185.     PAUSE();
  186. }
  187.  
  188. void PrintFile(FILE* const file)
  189. {
  190.     CLS();
  191.     if (!file)
  192.         puts("Файл не найден! :(");
  193.  
  194.     char c;
  195.     while ((c = fgetc(file)) != EOF)
  196.     {
  197.         printf("%c", c);
  198.     }
  199.     rewind(file);
  200.  
  201.     putchar('\n');
  202.     PAUSE();
  203. }
  204.  
  205. void Process(void)
  206. {
  207.     CLS();
  208.     if (!srcFile)
  209.     {
  210.         puts("Исходный файл не найден :(\n");
  211.         PAUSE();
  212.         return;
  213.     }
  214.     else if (destFile)
  215.     {
  216.         printf("Результат уже посчитан! Хотите пересчитать? (Y/N): ");
  217.         if (yesNo())
  218.         {
  219.             fclose(destFile);
  220.         }
  221.         else
  222.         {
  223.             PAUSE();
  224.             return;
  225.         }
  226.     }
  227.  
  228.     /* Решение задачи */
  229.     double data[numFields * NUM_COLS]; 
  230.     for (unsigned char i = 0; i < numFields; ++i)
  231.     {
  232.         fseek(srcFile, strlen(numFields[i]) + 1, SEEK_CUR);
  233.         for (unsigned char j = 0; j < NUM_COLS; ++j)
  234.         {
  235.             fseek(srcFile, strlen("0000 год: "), SEEK_CUR);
  236.             fscanf(srcFile, "%lf\n", &data[i * NUM_COLS + j]);
  237.             printf("%.2lf ", data[i * NUM_COLS + j]);
  238.         }
  239.         puts("");
  240.     }
  241.     rewind(srcFile);
  242.    
  243.     double minVal = FLT_MAX;
  244.     short minIdx = -1;
  245.  
  246.     for (unsigned char i = 0; i < numFields; ++i)
  247.     {
  248.         double _tmp = data[i * NUM_COLS + 1] / data[i];
  249.         if (_tmp < minVal)
  250.         {
  251.             minVal = _tmp;
  252.             minIdx = i * NUM_COLS;
  253.         }
  254.     }
  255.  
  256.     destFile = OpenFile();
  257.     fprintf(destFile, "Название: %s\n", numFields[minIdx / NUM_COLS]);
  258.     fprintf(destFile, "Разница показателей 1940-1950: %lf\n", data[minIdx] - data[minIdx - 1]);  
  259.     fprintf(destFile, "Разница показателей 1950-1956: %lf\n", data[minIdx + 1] - data[minIdx]);  
  260.     rewind(destFile);
  261.     PAUSE();
  262. }
  263.  
  264. void Close(void)
  265. {
  266.     if (destFile)
  267.         fclose(destFile);
  268.     if (srcFile)
  269.         fclose(srcFile);
  270. }
  271.  
  272. /* keycodes */
  273. #define SCROLL_KEY  (0xE0)
  274. #define ARR_UP      (72)
  275. #define ARR_DOWN    (80)
  276.  
  277. int main(void)
  278. {
  279.     setlocale(LC_ALL, "Russian");
  280.     unsigned char running = 1, pos = 0;
  281.     int cmd;
  282.  
  283.     #define ALLOC_STEP (16)
  284.     fieldNames = calloc(ALLOC_STEP, sizeof(data));
  285.     allocated = ALLOC_STEP;
  286.  
  287.     while (running)
  288.     {
  289.         /* render */
  290.         CLS();
  291.         puts("Что сделать?");
  292.         for (unsigned char i = 0; i < STATE_MAX; ++i)
  293.         {
  294.             printf("%s %s\n", (pos == i ? "[*]" : "[ ]"), MENU_NAMES[i]);
  295.         }
  296.  
  297.         /* get input */
  298.         cmd = getch();
  299.         switch (cmd)
  300.         {
  301.             case SCROLL_KEY:
  302.                 cmd = getch();
  303.                 switch (cmd)
  304.                 {
  305.                     case ARR_UP: pos = MAX(pos - 1, 0); break;
  306.                     case ARR_DOWN: pos = MIN(pos + 1, STATE_MAX - 1); break;
  307.                     case ESCAPE: Close(); running = 0; break;
  308.                     default: break;
  309.                 }
  310.                 break;
  311.             case ENTER:
  312.                 switch (pos)
  313.                 {
  314.                     case CREATE_FILE: CreateFile(); break;
  315.                     case PRINT_SOURCE_FILE: PrintFile(srcFile); break;
  316.                     case PROCESS: Process(); break;
  317.                     case PRINT_DEST_FILE: PrintFile(destFile); break;
  318.                     case CLOSE:
  319.                         Close();
  320.                         running = 0;
  321.                         break;
  322.                     default:
  323.                         break;
  324.                 }
  325.                 break;
  326.             default: break;
  327.         }
  328.     }  
  329.  
  330.     free(fieldNames);
  331.     PAUSE();
  332.     return 0;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement