Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<fstream>
- #include<string>
- #include<Windows.h>
- using namespace std;
- struct book
- {
- char name[100]{ 0 };
- unsigned year;
- unsigned circulation;
- };
- int count_if(book* b, int sz, int from, int to)
- {
- int cn = 0;
- for(int i = 0; i < sz; ++i)
- {
- if(b[i].year >= from && b[i].year <= to)
- {
- ++cn;
- }
- }
- return cn;
- }
- book* get_books_list(book* arr, int n, int from, int to, int& out_sz)
- {
- out_sz = count_if(arr, n, from, to);
- if(out_sz == 0)
- {
- return nullptr;
- }
- book* array = new book[out_sz];
- int idx = 0;
- for(int i = 0; i < n; ++i)
- {
- if(arr[i].year >= from && arr[i].year <= to)
- {
- strcpy_s(array[idx].name, arr[i].name);
- array[idx].year = arr[i].year;
- array[idx].circulation = arr[i].circulation;
- ++idx;
- }
- }
- return array;
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string kng = "TextFile1.txt";
- int N;
- book *arr;
- int otkr;
- cout << "ВЫБЕРИТЕ СПОСОБ ВВОДА. 0 - С КЛАВИАТУРЫ, 1 - ИЗ ФАЙЛА \n";
- cin >> otkr;
- if(otkr == 0)
- {
- cout << "введите размер массива \n";
- cin >> N;
- arr = new book[N];
- for(int i = 0; i < N; ++i)
- {
- cout << "Введите название книги\n";
- cin >> arr[i].name;
- cout << "Введите тираж \n";
- cin >> arr[i].circulation;
- cout << "Введите год издания \n";
- cin >> arr[i].year;
- }
- for(int i = 0; i < N; ++i)
- {
- cout << arr[i].name << " " << arr[i].circulation << " " << arr[i].year << endl;
- }
- }
- else
- {
- fstream knigi;
- knigi.open("TextFile1.txt");
- knigi >> N;
- arr = new book[N];
- for(int i = 0; i < N; i++)
- {
- knigi >> arr[i].name >> arr[i].circulation >> arr[i].year;
- }
- }
- int sz = 0;
- book* books = get_books_list(arr, N, 2000, 2010, sz);
- delete[] arr;
- if(books)
- {
- cout << "НАЗВАНИЕ И ТИРАЖ КНИГ С ИЗДАННЫХ С 2000-2010 ГГ:\n";
- for(int i = 0; i < sz; ++i)
- {
- cout << books[i].name << " " << books[i].circulation << "\n";
- }
- delete[] books;
- }
- else
- {
- cout << "ТАКИХ НЕТ.";
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement