Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <float.h>
- #include <locale.h>
- /* utils */
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
- #define CLS() system("cls")
- #define PAUSE() system("pause")
- /* Control input function */
- #define ESCAPE (27)
- #define BACKSPACE (8)
- #define ENTER (13)
- #define BUFFER_LENGTH (256) /* длина буфера ввода */
- static char __buffer[BUFFER_LENGTH + 1]; /* буфер ввода */
- /* Функция для ограничения ввода */
- static unsigned input(const char* const use, const char* const printAfter)
- {
- int _tmp;
- unsigned i = 0;
- if (!use)
- return 0;
- while (i < BUFFER_LENGTH)
- {
- _tmp = getch();
- if (_tmp == ENTER)
- {
- __buffer[i] = '\0';
- break;
- }
- else if (_tmp == BACKSPACE && i > 0)
- {
- printf("\b \b");
- --i;
- }
- else if (_tmp == ESCAPE)
- {
- exit(0);
- }
- else if (_tmp == 0) /* Если нажата специальная клавиша */
- {
- continue;
- }
- else if (strchr(use, _tmp))
- {
- __buffer[i++] = _tmp;
- putchar(_tmp);
- }
- }
- if (printAfter)
- {
- printf(printAfter);
- }
- return i;
- }
- /* запросить ввод вещественного числа */
- float inputFloat(const char* const printAfter)
- {
- input(",0123456789", printAfter);
- return atof(__buffer);
- }
- enum
- {
- CREATE_FILE = 0,
- PRINT_SOURCE_FILE,
- PROCESS,
- PRINT_DEST_FILE,
- CLOSE,
- STATE_MAX
- };
- static const char* const MENU_NAMES[] =
- {
- "Создать файл",
- "Вывести исходный файл",
- "Обработать выходной файл",
- "Вывести выходной файл",
- "Выход"
- };
- #define FILENAME_LENGTH (260)
- static FILE* destFile = NULL, *srcFile = NULL;
- unsigned char yesNo(void)
- {
- while (1)
- {
- input("YyNn", "\n");
- if (!strcmp(__buffer, "N") || !strcmp(__buffer, "n"))
- {
- return 0;
- }
- else if (!strcmp(__buffer, "Y") || !strcmp(__buffer, "y"))
- {
- return 1;
- }
- }
- }
- static char* fieldNames = NULL;
- static unsigned numFields = 0, allocated = 0;
- #define NUM_COLS (3)
- FILE* OpenFile(void)
- {
- FILE* file;
- char filename[FILENAME_LENGTH];
- while (1)
- {
- printf("Введите имя файла: ");
- scanf("%s", filename);
- if ((file = fopen(filename, "r")))
- {
- fclose(file);
- printf("Файл с именем \"%s\" уже существует. Перезаписать? (Y/N): ", filename);
- if (yesNo())
- {
- file = fopen(filename, "w+");
- break;
- }
- }
- else
- {
- file = fopen(filename, "w+");
- printf("Файл с именем \"%s\" создан.\n", filename);
- break;
- }
- }
- return file;
- }
- void CreateFile(void)
- {
- CLS();
- if (srcFile)
- {
- printf("Исходный файл уже создан! Хотите создать новый? (Y/N): ");
- if (yesNo())
- {
- fclose(srcFile);
- if (destFile)
- {
- fclose(destFile);
- destFile = NULL;
- }
- }
- else
- {
- PAUSE();
- return;
- }
- }
- srcFile = OpenFile();
- CLS();
- /* input data */
- for (unsigned char i = 0; i < numFields; ++i)
- {
- printf("Тип дохода: %s\n", fieldNames[i]);
- fprintf(srcFile, "%s\n", fieldNames[i]);
- printf("1940 год: ");
- fprintf(srcFile, "1940 год: %f\n", inputFloat("\n"));
- printf("1950 год: ");
- fprintf(srcFile, "1950 год: %f\n", inputFloat("\n"));
- printf("1956 год: ");
- fprintf(srcFile, "1956 год: %f\n", inputFloat("\n"));
- }
- rewind(srcFile);
- PAUSE();
- }
- void PrintFile(FILE* const file)
- {
- CLS();
- if (!file)
- puts("Файл не найден! :(");
- char c;
- while ((c = fgetc(file)) != EOF)
- {
- printf("%c", c);
- }
- rewind(file);
- putchar('\n');
- PAUSE();
- }
- void Process(void)
- {
- CLS();
- if (!srcFile)
- {
- puts("Исходный файл не найден :(\n");
- PAUSE();
- return;
- }
- else if (destFile)
- {
- printf("Результат уже посчитан! Хотите пересчитать? (Y/N): ");
- if (yesNo())
- {
- fclose(destFile);
- }
- else
- {
- PAUSE();
- return;
- }
- }
- /* Решение задачи */
- double data[numFields * NUM_COLS];
- for (unsigned char i = 0; i < numFields; ++i)
- {
- fseek(srcFile, strlen(numFields[i]) + 1, SEEK_CUR);
- for (unsigned char j = 0; j < NUM_COLS; ++j)
- {
- fseek(srcFile, strlen("0000 год: "), SEEK_CUR);
- fscanf(srcFile, "%lf\n", &data[i * NUM_COLS + j]);
- printf("%.2lf ", data[i * NUM_COLS + j]);
- }
- puts("");
- }
- rewind(srcFile);
- double minVal = FLT_MAX;
- short minIdx = -1;
- for (unsigned char i = 0; i < numFields; ++i)
- {
- double _tmp = data[i * NUM_COLS + 1] / data[i];
- if (_tmp < minVal)
- {
- minVal = _tmp;
- minIdx = i * NUM_COLS;
- }
- }
- destFile = OpenFile();
- fprintf(destFile, "Название: %s\n", numFields[minIdx / NUM_COLS]);
- fprintf(destFile, "Разница показателей 1940-1950: %lf\n", data[minIdx] - data[minIdx - 1]);
- fprintf(destFile, "Разница показателей 1950-1956: %lf\n", data[minIdx + 1] - data[minIdx]);
- rewind(destFile);
- PAUSE();
- }
- void Close(void)
- {
- if (destFile)
- fclose(destFile);
- if (srcFile)
- fclose(srcFile);
- }
- /* keycodes */
- #define SCROLL_KEY (0xE0)
- #define ARR_UP (72)
- #define ARR_DOWN (80)
- int main(void)
- {
- setlocale(LC_ALL, "Russian");
- unsigned char running = 1, pos = 0;
- int cmd;
- #define ALLOC_STEP (16)
- fieldNames = calloc(ALLOC_STEP, sizeof(data));
- allocated = ALLOC_STEP;
- while (running)
- {
- /* render */
- CLS();
- puts("Что сделать?");
- for (unsigned char i = 0; i < STATE_MAX; ++i)
- {
- printf("%s %s\n", (pos == i ? "[*]" : "[ ]"), MENU_NAMES[i]);
- }
- /* get input */
- cmd = getch();
- switch (cmd)
- {
- case SCROLL_KEY:
- cmd = getch();
- switch (cmd)
- {
- case ARR_UP: pos = MAX(pos - 1, 0); break;
- case ARR_DOWN: pos = MIN(pos + 1, STATE_MAX - 1); break;
- case ESCAPE: Close(); running = 0; break;
- default: break;
- }
- break;
- case ENTER:
- switch (pos)
- {
- case CREATE_FILE: CreateFile(); break;
- case PRINT_SOURCE_FILE: PrintFile(srcFile); break;
- case PROCESS: Process(); break;
- case PRINT_DEST_FILE: PrintFile(destFile); break;
- case CLOSE:
- Close();
- running = 0;
- break;
- default:
- break;
- }
- break;
- default: break;
- }
- }
- free(fieldNames);
- PAUSE();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement