Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include <string>
- using namespace std;
- struct student
- {
- string prof;
- int group;
- int fn;
- char name[30];
- float average;
- };
- fstream fp;
- void input(student st[])
- {
- int i, n;
- do{
- cout << "\n Insert size of a students: ";
- cin >> n;
- } while (n<1 || n>30);
- fp.open("st.dat", ios::binary | ios::out);
- if (!fp){
- cout << "\n Error";
- exit(1);
- }
- for (i = 0; i<n; i++)
- {
- fflush(stdin);
- cout << "\n FN:";
- cin >> st[i].fn;
- cout << "\n Name:";
- cin >> st[i].name;
- cout << "\n Average:";
- cin >> st[i].average;
- cout << "\n Group:";
- cin>> st[i].group;
- if (st[i].group < 1 || st[i].group > 4){
- cout << "ERROR !!!"<<endl;
- }
- cin.ignore();
- cout << "\n Profesiq: ";
- getline (cin, st[i].prof);
- if (!(strcmp("E", st[i].prof.c_str()) == 0 || strcmp("AIUT", st[i].prof.c_str()) == 0)){
- cout << "ERROR !!!"<<endl;
- break;
- }
- }
- fp.write((char*)st, n*sizeof(student));
- fp.close();
- }
- void append()
- {
- student s;
- long pos, n;
- fp.open("st.dat", ios::binary | ios::in);
- if (!fp){
- cout << "\n Error"; exit(1);
- }
- fp.seekg(0L, ios::end);
- pos = fp.tellg(); fp.close();
- fp.open("st.dat", ios::binary | ios::app);
- if (!fp){
- cout << "\n Error"; exit(1);
- }
- n = pos / (sizeof(student));
- cout << "\n Input " << n + 1 << " student \n";
- fflush(stdin);
- cout << "\n FN:"; cin >> s.fn;
- cout << "\n Name:"; cin >> s.name;
- cout << "\n Average:"; cin >> s.average;
- cout << "\n Group:"; cin >> s.group;
- cout << "\n Profesion:"; cin >> s.prof;
- fp.write((char*)&s, sizeof(student));
- fp.close();
- }
- void output(student st[])
- {
- int i, n;
- long pos;
- student s;
- cout << "\n List of students \n ";
- fp.open("st.dat", ios::binary | ios::in);
- if (fp.fail()){ cout << "\n Error"; exit(1); }
- fp.seekg(0L, ios::end);
- pos = fp.tellg();
- fp.seekg(0L, ios::beg);
- n = pos / (sizeof(student));
- for (i = 0; i<n; i++)
- {
- fp.read((char*)&s, sizeof(student));
- st[i] = s;
- }
- fp.close();
- for (i = 0; i<n; i++)
- cout << "\n FN:" << st[i].fn << "\t Name:" << st[i].name << "\t Average:" << st[i].average << "\t Profesion: " << st[i].prof;
- }
- void output_excel(student st[])
- {
- int i, n;
- long pos; // poziciq
- student a;
- fp.open("st.dat", ios::binary | ios::in);
- if (!fp)
- {
- cout << endl << "ERORR!!" << endl;
- exit(1);
- }
- fp.seekg(0L, ios::end); // premestva ukazatelq v kraq na faila ( L sled nulata ne 1 );
- pos = fp.tellg(); // opredelq dyljinata na faila v br baitove
- n = pos / sizeof(student); // opredelq kolko abonata ima v masiva ( delish sichki baitove na goleminata v baitove na edin abonat);
- fp.seekg(0L, ios::beg); // pozicionariash v nachaloto shtoto v momenta e v kraq i nqma da moje da prochete sudurjanieto na faila
- fp.read((char *)st, n * sizeof (student));
- fp.close();
- for ( i = 0; i < n; i++){
- if (st[i].average >= 5.5 && st[i].average <=6.0)
- {
- cout << "\n FN:" << st[i].fn << "\t Name:" << st[i].name << "\t Average:" << st[i].average << "\t Profesion: " << st[i].prof;
- }
- }
- fp.close();
- }
- void output_search(student st[])
- {
- int i, n;
- long pos;
- student s;
- fp.open("st.dat", ios::binary | ios::in);
- if (fp.fail()){ cout << "\n Error"; exit(1); }
- fp.seekg(0L, ios::end);
- pos = fp.tellg();
- fp.seekg(0L, ios::beg);
- n = pos / (sizeof(student));
- for (i = 0; i<n; i++)
- {
- fp.read((char*)&s, sizeof(student));
- st[i] = s;
- }
- fp.close();
- int fn1;
- cout << "\n Search for student with FN : ";
- cin >> fn1;
- for (i = 0; i<n; i++)
- if (st[i].fn == fn1)
- cout << "\n FN:" << st[i].fn << "\t Name:" << st[i].name << "\t Average:" << st[i].average << "\t Profesion: " << st[i].prof;
- }
- void main(){
- student st[30];
- int ch;
- do{
- cout << "\n 1.Create file";
- cout << "\n 2.Append new one ";
- cout << "\n 3.List";
- cout << "\n 4.Exellent students";
- cout << "\n 5.Search on fn";
- cout << "\n 6.Exit";
- cout << "\n Your choice: ";
- cin >> ch;
- switch (ch)
- {
- case 1:input(st); break;
- case 2:append(); break;
- case 3:output(st); break;
- case 4:output_excel(st); break;
- case 5:output_search(st); break;
- }
- } while (ch != 6);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement