Код:
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#include "locale.h"
#include"Windows.h"
#define MAX_PRODUCTS_ITEMS_CNT 4
#define MAX_YEAR_ITEM_CNT 4
typedef struct
{
int year_cnt;
float products_qty;
}
year_item_t;
typedef struct
{
char name[30];
year_item_t unit_items[MAX_YEAR_ITEM_CNT];
char unit_of_measure[16];
}
product_item_t;
int main()
{
setlocale(LC_ALL, "rus");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int products_cnt = 0;
int years_cnt = 0;
product_item_t data[MAX_PRODUCTS_ITEMS_CNT] = { 0 };
char answer[4];
do
{
printf("Введите количество лет:");
scanf("%d", &years_cnt);
if (years_cnt <= 0 || years_cnt > MAX_YEAR_ITEM_CNT)
printf("Ошибка ввода\n");
} while (years_cnt <= 0 || years_cnt > MAX_YEAR_ITEM_CNT);
do
{
fflush(stdin);
printf("Введите вид продукции:");
scanf("%s", &data[products_cnt].name);
printf("Введите единицу измерения:");
scanf("%s", &data[products_cnt].unit_of_measure);
for (int year_i = 0; year_i < years_cnt; year_i++)
{
printf("Введите год:");
scanf("%d", &data[products_cnt].unit_items[year_i].year_cnt);
printf("Введите данные за год:");
scanf("%f", &data[products_cnt].unit_items[year_i].products_qty);
}
products_cnt++;
if (products_cnt >= MAX_PRODUCTS_ITEMS_CNT)
break;
printf("Продолжить ввод?[yes для продолжения]");
scanf("%s", &answer);
} while (0 == _stricmp(answer, "yes"));
puts(
"Производство видов продукции машиностроения (средства потребления) с СССР. \n" \
"+----------------+-------------------+------+------+------+------+\n" \
"| Виды продукции | Единицы измерения | 1913 | 1928 | 1940 | 1955 |\n" \
"+----------------+-------------------+------+------+------+------+"
);
for (int product_i = 0; product_i < products_cnt; product_i++)
{
printf("| %-14s | %-17s |%6.1f|%6.1f|%6.1f|%6.1f|\n",
data[product_i].name
,data[product_i].unit_of_measure
,data[product_i].unit_items[0].products_qty
,data[product_i].unit_items[1].products_qty
,data[product_i].unit_items[2].products_qty
,data[product_i].unit_items[3].products_qty
);
puts("+----------------+-------------------+------+------+------+------+");
}
FILE* file = fopen("data.txt", "w");
if (NULL != file)
{
fputs(
"Производство видов продукции машиностроения (средства потребления) с СССР. \n" \
"+----------------+-------------------+------+------+------+------+\n" \
"| Виды продукции | Единицы измерения | 1913 | 1928 | 1940 | 1955 |\n" \
"+----------------+-------------------+------+------+------+------+\n"
, file
);
for (int product_i = 0; product_i < products_cnt; product_i++)
{
fprintf(file, "| %-14s | %-17s |%6.1f|%6.1f|%6.1f|%6.1f|\n",
data[product_i].name
,data[product_i].unit_of_measure
,data[product_i].unit_items[0].products_qty
,data[product_i].unit_items[1].products_qty
,data[product_i].unit_items[2].products_qty
,data[product_i].unit_items[3].products_qty
);
fputs("+----------------+-------------------+------+------+------+------+\n", file);
}
fclose(file);
}
else
puts("Не удалось создать файл");
int max_growth_i = -1;
if (0 == strcmp(data[0].unit_of_measure, "tt" /*"тыс. штук"*/))
{
max_growth_i = 0;
}
{
puts(
"Процент прироста по сравнению с 1928 г. \n" \
"+----------------+------------------+------------------+\n" \
"| Виды продукции | Прирост в 1940 % | Прирост в 1955 % |\n" \
"+----------------+------------------+------------------+\n"
);
for (int product_i = 1; product_i < products_cnt; product_i++)
{
if (0 == strcmp(data[product_i].unit_of_measure, "тыс. штук"))
{
if (-1 == max_growth_i)
{
max_growth_i = product_i;
}
else if (
data[product_i].unit_items[3].products_qty - data[product_i].unit_items[0].products_qty
> data[max_growth_i].unit_items[3].products_qty - data[max_growth_i].unit_items[0].products_qty
)
{
max_growth_i = product_i;
}
}
}
if (-1 != max_growth_i)
{
printf("| %-14s | %16.1f | %16.1f |\n",
data[max_growth_i].name
,100.0 * (data[max_growth_i].unit_items[2].products_qty - data[max_growth_i].unit_items[1].products_qty) / data[max_growth_i].unit_items[1].products_qty
,100.0 * (data[max_growth_i].unit_items[3].products_qty - data[max_growth_i].unit_items[1].products_qty) / data[max_growth_i].unit_items[1].products_qty
);
puts("+----------------+------------------+------------------+");
}
else
puts("Нет продуктов, измеряемых в тыс. штук");
}
FILE* file = fopen("output.txt", "w");
if (NULL != file)
{
fputs(
"Процент прироста по сравнению с 1928 г. \n" \
"+----------------+------------------+------------------+\n" \
"| Виды продукции | Прирост в 1940 % | Прирост в 1955 % |\n" \
"+----------------+------------------+------------------+\n"
, file
);
for (int product_i = 1; product_i < products_cnt; product_i++)
{
if (0 == strcmp(data[product_i].unit_of_measure, "тыс. штук"))
{
if (-1 == max_growth_i)
{
max_growth_i = product_i;
}
else if (
data[product_i].unit_items[3].products_qty - data[product_i].unit_items[0].products_qty
> data[max_growth_i].unit_items[3].products_qty - data[max_growth_i].unit_items[0].products_qty
)
{
max_growth_i = product_i;
}
}
}
if (-1 != max_growth_i)
{
fprintf(file,"| %-14s | %16.1f | %16.1f |\n",
data[max_growth_i].name
,100.0 * (data[max_growth_i].unit_items[2].products_qty - data[max_growth_i].unit_items[1].products_qty) / data[max_growth_i].unit_items[1].products_qty
,100.0 * (data[max_growth_i].unit_items[3].products_qty - data[max_growth_i].unit_items[1].products_qty) / data[max_growth_i].unit_items[1].products_qty
);
fputs("+----------------+------------------+------------------+\n", file);
}
else
fputs("Нет продуктов, измеряемых в тыс. штук", file);
fclose(file);
}
else
puts("Не удалось создать файл");
_getch();
return 0;
}