Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- using namespace std;
- FILE *fl;
- FILE *ft;
- struct worker
- {
- char surname[50];
- int birthdate;
- int workdate;
- } *spisok, rabotnik;
- char fname[20];
- void vvod(); //Ввести список
- void chit(); //Прочитать список
- void reze(); //Вывести результат на экран
- void rezf(); //Вывести результат в файл
- int menu(); //Меню
- char * fnam(); //Ввод имени файла
- int main()
- {
- while (true)
- {
- switch (menu())
- {
- case 1: vvod(); break;
- case 2: chit(); break;
- case 3: reze(); break;
- case 4: rezf(); break;
- case 5: return 0;
- default: cout << "Choose correct!" << endl;
- }
- cout << "Press any key" << endl;
- }
- }
- int menu()
- {
- cout << "Choose: " << endl;
- cout << "1.Vvesti spisok" << endl;
- cout << "2.Chitat' spisok" << endl;
- cout << "3.Rez na ekran" << endl;
- cout << "4.Rez v file" << endl;
- cout << "5.Exit" << endl;
- int x;
- cin >> x;
- return x;
- }
- char * nnf()
- {
- if (strlen(fname))
- return fname;
- cout << "Enter the name of file: " << endl;
- cin >> fname;
- return fname;
- }
- void vvod()
- {
- if ((fl = fopen(nnf(), "ab")) == NULL)
- if ((fl = fopen(nnf() , "wb")) == NULL)
- {
- cout << "File is not created" << endl;
- return;
- }
- int n;
- cout << "Enter amount of workers: ";
- cin >> n;
- spisok = new worker[n];
- for (int i = 0; i < n; i++)
- {
- cout << "Enter the surname of worker: ";
- cin >> rabotnik.surname;
- cout << "Enter the birthdate of worker: ";
- cin >> rabotnik.birthdate;
- cout << "Enter the date of first work: ";
- cin >> rabotnik.workdate;
- cout << endl;
- }
- fclose(fl);
- }
- void chit()
- {
- if ((fl = fopen(nnf(), "rb+")) == NULL)
- {
- cout << "File wasn't read" << endl;
- }
- else cout << "File was readed" << endl;
- int n = 0;
- while (fread(&spisok, sizeof(spisok), 1, fl) == 1) //find number of structeres in file
- {
- n++;
- }
- rewind(fl);
- for(int i=0; i<n ; i++)
- {
- fread(&rabotnik, sizeof(spisok), 1, fl);
- cout << "Enter the surname of worker: " << rabotnik.surname << endl;
- cout << "Enter the birthdate of worker: " << rabotnik.birthdate << endl;
- cout << "Enter the date of first work: " << rabotnik.workdate << endl;
- }
- fclose(fl);
- }
- void reze()
- {
- if ((fl = fopen(nnf(), "rb+")) == NULL)
- {
- cout << "File wasn't read" << endl;
- }
- else cout << "File was readed" << endl;
- rewind(fl);
- int n;
- cout << "Enter amount of workers: ";
- cin >> n;
- spisok = new worker[n];
- for (int i = 0; i < n; i++)
- {
- fread (&spisok, sizeof(spisok), 1, fl);
- if (spisok[i].birthdate < 1980)
- {
- cout << "Worker that is not earlier than 1980: " << spisok[i].surname << endl;
- cout << "His birth date: " << spisok[i].birthdate << endl;
- cout << "His date of first work: " << spisok[i].workdate;
- cout << endl;
- }
- else cout << "> 1980";
- }
- delete[] spisok;
- fclose(fl);
- }
- void rezf()
- {
- char fnamet[20];
- cout << "Enter the name of file: ";
- cin >> fnamet;
- if ((ft = fopen(fnamet, "w")) == NULL)
- {
- cout << "File wasn't read" << endl;
- }
- else cout << "File was readed" << endl;
- rewind(ft);
- if ((fl = fopen(nnf(), "rb+")) == NULL)
- {
- cout << "File wasn't read" << endl;
- }
- else cout << "File was readed" << endl;
- rewind(fl);
- int n;
- cout << "Enter amount of workers: ";
- cin >> n;
- spisok = new worker[n];
- for (int i = 0; i < n; i++)
- {
- fread(&spisok, sizeof(spisok), 1, fl);
- if (spisok[i].birthdate < 1980)
- {
- cout << "Worker that is not earlier than 1980: " << spisok[i].surname << endl;
- cout << "His birth date: " << spisok[i].birthdate << endl;
- cout << "His date of first work: " << spisok[i].workdate;
- cout << endl;
- }
- else cout << "> 1980";
- }
- fclose(ft);
- fclose(fl);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement