Untitled

 avatar
unknown
abap
4 years ago
2.9 kB
11
Indexable
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include<stdio.h>
#include<conio.h>

using namespace std;

FILE* fl;
typedef struct {

	char naz[30];
	char autor[30];
	int year;
	int page;
}
Books;

Books book[30]; //Массив структур

char name[20]; //Имя файла
int nst;
int n = 0;
int years;
int i;
char namet[30];
int menu(); // Меню
void spisok(); //Ввести список
void resf();//Вывести результат в файл
void resc(); //Вывести на экран за последний месяц
void VivodNaEcran(); // Вывести на экран все

int main()
{
	while (true)
	{
		switch (menu())

		{
		case 1: spisok(); break;
		case 2: resf(); break;
		case 3: VivodNaEcran(); break;
		case 4: resc(); break;
			return 0;
		default: "Viberite pravilno!";
		}
	
	}
}

int menu() //Меню

{
	cout << "1. Vvesti spisok" << endl;
	cout << "2. Vivesti v fail" << endl;
	cout << "3. Vivesti na ekran vse" << endl;
	cout << "4. Vivesti book" << endl;
	int i;
	cin >> i;
	return i;
}

void spisok() // Ввести список
{
	cout << "Vvedite kolichestvo" << endl;
	cin >> nst;
	for (int i = 0; i < nst; i++)
	{
		cout << "Vvedite nazvanie:" << endl;
		cin >> book[i].naz;
		cout << "Vvedite autor:" << endl;
		cin >> book[i].autor;
		cout << "Vvedite page:" << endl;
		cin >> book[i].page;
		cout << "Vvedite year:" << endl;
		cin >> book[i].year;
	}
}

void resf() // Вывести результат в файл
{
	FILE* fl;
	fopen_s(&fl, "file.txt", "w");             // Здесь нужно было присвоить fopen для fl, также заменил fopen_s на fopen
	for (int i = 0; i < nst; i++)
	{
		if (book[i].year != 0) {			
			fprintf(fl, "%s %s %u %u", book[i].naz, book[i].autor, book[i].page, book[i].year);

		}
		cout << endl;
	}
	fclose(fl);
}
void VivodNaEcran() //Вывести все на экран
{
	n = 0;
	FILE* fl; fopen_s(&fl, "file.txt", "r");
	if (fl != NULL)
	{
		do {
			n++;
			fscanf(fl, "%s %s %d %d", book[n].naz, book[n].autor, &book[n].page, &book[n].year);
			cout << book[n].naz << " " << book[n].autor << " " << book[n].page << " " << book[n].year << endl;
		} while (!feof(fl));
		
	}
	else {
		cout << "ERROR";
	}
	fclose(fl);
}
void resc() //Вывести на экран книги после заданного года
{
	n = 0;
	int PrevYear; 
	cout << " Enter year before " << endl;
	cin >> PrevYear;

	FILE* fl; fopen_s(&fl, "file.txt", "r");
	if (fl != NULL)
	{
		do {
			n++;
			fscanf(fl, "%s %s %d %d", book[n].naz, book[n].autor, &book[n].page, &book[n].year);
		} while (!feof(fl));

	}
	for (int i = 0; i <= n; i++)
	{
		if (PrevYear < book[i].year)
			{
			cout << book[i].naz << " " << book[i].autor << " " << book[i].page << " " << book[i].year << endl;
			}
	}
	fclose(fl);

}
Editor is loading...