Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- #include "iomanip"
- #include "fstream"
- #include "string"
- #include "ctype.h"
- #include "conio.h"
- using namespace std;
- const unsigned MAX=3;
- char *filename = "student.dat"; //��� �� �����
- struct student //��������� �� �������
- {
- string facNo; //���. �
- string Name; //��� �� ��������
- double mark[MAX]; //������ �� ��� ��������
- };
- //������� �� ����������� ����� �� 1 �������
- student set_value(string aFacNo, string aName, double m0, double m1, double m2)
- {
- student stud;
- stud.facNo = aFacNo;
- stud.Name = aName;
- stud.mark[0] = m0;
- stud.mark[1] = m1;
- stud.mark[2] = m2;
- return stud;
- }
- //���� �� ��������� �������
- char Display_menu()
- {cout<<"\n\tMenu\nC - Create file\nW - Write to file\nR - Read from file"
- <<"\nA - Append a redor\nU - Updete a redord\nD - Delete a record"
- <<"\nF - Find data\nE - Exit program\n Youre choice: ";
- char choice;
- choice = _getch();
- choice =toupper(choice);
- return choice;
- }
- //������� �� ��������� �� ������� �� 1 �������
- void Input(student &stud)
- {
- cout<<"\nfacNo: "; getline(cin,stud.facNo,'\n');
- cout<<"name: "; getline(cin,stud.Name,'\n');
- for(int i = 0; i<MAX; i++)
- {
- cout<<"mark["<<i+1<<"] = ";
- cin>>stud.mark[i];
- }
- }
- //������� �� ����������� ������� �� 1 �������
- void Output(student stud)
- {
- cout<<setw(10)<<setiosflags(ios::left)<<stud.facNo
- <<setw(25)<<setiosflags(ios::left)<<stud.Name;
- cout<<resetiosflags(ios::left);
- for(int i=0; i<MAX; i++)
- cout<<setw(6)<<setprecision(2)<<setiosflags(ios::fixed)
- <<stud.mark[i];
- cout<<endl;
- }
- //������� �� ��������� �� ������ ����
- void Create_file(char *fname)
- {
- fstream students;
- student stud;
- students.open(fname,ios::in);
- if(!students) //������ �� ���� �� ���� �������
- {
- cerr<<"File couldn't be opened\n";
- cout<<"Create a new file...\n";
- students.open(fname,ios::out); //������ ��� ����
- students.close();
- return;
- }
- students.seekg(0,ios::end); //���������� get ��������� � ���� �� �����
- streamoff size_file;
- size_file = students.tellg(); //�������� ������� �� �����
- cout<<"file isn't empty. It has "
- <<size_file/sizeof(student)<<" records.\n";
- char Answer;
- cout<<"Delete file? (Y/N): ";
- Answer = _getch();Answer = toupper(Answer);
- if(Answer != 'Y')
- {
- students.close();
- return;
- }
- students.clear();
- students.seekg(0);
- students.close();
- students.open(fname,ios::out|ios::trunc);
- students.close();
- }
- //������� �� ��������� �� ����, ���� �������� ������ ������������ � ����.
- void Write_to_file(char *fname)
- {
- ofstream students;
- student stud;
- students.open(fname,ios::out);
- long i, count;
- cout<<"Enter count of students = ";
- cin>>count;
- for(i=0;i<count; i++)
- {
- cin.ignore();
- Input(stud);
- students.write(reinterpret_cast<char *> (&stud),sizeof(student));
- }
- students.close();
- }
- //������� �� ������ �� ������� �� ���� � ��������� �� �� ������
- void Read_from_file(char *filename)
- {
- ifstream students;
- student s;
- students.open(filename, ios::in);
- students.clear();
- students.seekg(0);
- while(students.read(reinterpret_cast<char*>(&s),sizeof(student)))
- {
- Output(s);
- if(students.eof()) break;
- }
- students.close();
- }
- //������� ���������� �� ����� � ���� �� �����
- void Append_to_file(char *filename)
- {
- ofstream students;
- student stud;
- int i,count;
- students.open (filename,ios::app);
- students.seekp(0,ios::end);
- cout<<"Enter count of students = ";
- cin>>count;
- for(i=0;i<count; i++)
- {
- cin.ignore();
- Input(stud);
- students.write(reinterpret_cast<char *> (&stud),sizeof(student));
- }
- students.close();
- }
- //������� �� ���������� �� ���� ����� ��� �����
- void Update_record(char *filename)
- {
- fstream students;
- student stud;
- students.open(filename, ios::in);
- students.clear();
- students.seekg(0);
- streamoff record;
- cout<<"Enter record "; cin>>record;
- students.seekg((record-1)*sizeof(student));
- students.read(reinterpret_cast<char*>(&stud),sizeof(student));
- Output(stud);
- char Answer;
- cout<<"Update record? (Y/N): ";
- Answer = _getch();Answer = toupper(Answer);
- if(Answer != 'Y')
- {
- students.close();
- return;
- }
- students.close();
- cin.ignore();
- Input(stud);
- students.open(filename, ios::ate| ios::out |ios::_Nocreate);
- students.clear();
- students.seekp((record-1)*sizeof(student));
- students.write(reinterpret_cast<char *> (&stud),sizeof(student));
- students.seekp(0,ios::end);
- students.close();
- }
- //������� �� ��������� �� ���� ����� ��� ����� - �������� ������ � ������ �����
- void Dlete_record(char *filename)
- {
- fstream students;
- student stud;
- students.open(filename, ios::in);
- students.clear();
- students.seekg(0);
- streamoff record;
- cout<<"Enter record "; cin>>record;
- students.seekg((record-1)*sizeof(student));
- students.read(reinterpret_cast<char*>(&stud),sizeof(student));
- Output(stud);
- char Answer;
- cout<<"Delete record? (Y/N): ";
- Answer = _getch();Answer = toupper(Answer);
- if(Answer != 'Y')
- {
- students.close();
- return;
- }
- students.close();
- cin.ignore();
- stud =set_value("no FacNo","No Name",0,0,0);
- students.open(filename, ios::ate| ios::out |ios::_Nocreate);
- students.clear();
- students.seekp((record-1)*sizeof(student));
- students.write(reinterpret_cast<char *> (&stud),sizeof(student));
- students.seekp(0,ios::end);
- students.close();
- }
- //������� �� ���������� ������� ����� �� 1 �������
- double results(student stud)
- {
- double result =0;
- for(int i=0; i<MAX; i++)
- result+=stud.mark[0];
- return result/MAX;
- }
- //������� �� ��������� �� ���������� �� ��������� �������
- // ������� �� ����� ������� �� ������� ���.�
- // ������� ����� �� ������ ��������
- // ��������� ��� �������� ����
- void Find_data(char *filename)
- {
- ifstream students;
- student s;
- students.open(filename, ios::in);
- char ch;
- do
- {
- cout<<"\nI - Information for student\nR - Results for students\n"
- <<"E - End find\nYour choice: ";
- ch = _getch();
- ch=toupper(ch);
- switch(ch)
- {
- case 'I':{cout<<"\nInformation for student\n";//���������� �� 1 �������
- string aFacNo;
- cout<<"Enter FacNo: ";
- getline(cin,aFacNo);
- students.clear();
- students.seekg(0);
- while(students.read(reinterpret_cast<char*>(&s),sizeof(student)))
- {
- if(aFacNo.compare(s.facNo)==0) Output(s);
- if(students.eof()) break;
- }
- break;}
- case 'R':{cout<<"\nR-Results for students\n";
- students.clear();
- students.seekg(0);
- while(students.read(reinterpret_cast<char*>(&s),sizeof(student)))
- {
- cout<<setw(10)<<setiosflags(ios::left)<<s.facNo
- <<setw(30)<<setiosflags(ios::left)<<s.Name
- <<setw(10)<<setprecision(2)
- <<setiosflags(ios::fixed)<<results(s)<<endl;
- if(students.eof()) break;
- }
- break;}
- case 'E':{cout<<"\nEnd search...\n";break;}
- default:{cout<<"\nIncald choice...\n";break;}
- }
- } while(ch !='E');
- students.close();
- }
- int main()
- {
- char choice;
- do
- {
- choice = Display_menu();
- switch(choice)
- {
- case 'C': {
- cout<<"\nCreate empty file\n";
- Create_file(filename);
- break;
- }
- case 'W':{
- cout<<"\nWrite to file\n";
- Write_to_file(filename);
- break;
- }
- case 'R': {
- cout<<"\nRead_from_file\n";
- Read_from_file(filename);
- break;
- }
- case 'A': {
- cout<<"\nAppend a record\n";
- Append_to_file(filename);
- break;
- }
- case 'U': {
- cout<<"\nUpdete a redord\n";
- Update_record(filename);
- break;
- }
- case 'D': {
- cout<<"\nDelete a redord\n";
- Dlete_record(filename);
- break;
- }
- case 'F': {cout<<"\nFind data\n";
- Find_data(filename);
- break;
- }
- case 'E' :{cout<<"\nExit program...\n"; break;}
- default:{cout<<"Invalid choice...\n"; break;}
- }
- }
- while(choice != 'E');
- cout<<"\nPress any key....";
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement