Добавил:
okley
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:4 семестр / sr27
.cpp#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <locale.h>
// 25.02: форматирование, пример см. на https://disk.yandex.ru/d/uG72xGr2dRnSmg
/* 23.02: доделан 7 пункт (работа с файлами).
Методы, связанные с работой с файлами (вывод output и ввод input), обозначены буквой 'f' в начале.
*/
/* 19.02:
В качестве примера для шестого пункта был создан преподаватель (Сергей), и два ученика из двух разных групп
Предметы, которые ведёт Сергей - аналитическая механика (есть у первого уч.) и экология (у второго)
При "добавлении" групп к Сергею он автоматически добавляется в качестве преподавателя к первому ученику и второму.
Сама реализация следующая: при активации конструктора для Student все студенты автоматически попадают
в глобальный массив всех студентов.
При каждом добавлении группы к преподавателю в соотв. методе идёт пробежка по всем студентам, их дисциплинам и их группам,
а также дисциплинам преподавателя.
Если добавляемая группа совпадает с группой студента, какая-то из дисциплин студента совпадает
с какой-то из дисциплин преподавателя (а их несколько), то преподаватель добавляется в список преподавателей данного студента
*/
using namespace std;
int studGlob=0; // общее кол-во студентов
class Person { // персона +
protected:
char name[64];
char sex;
int year_of_birth;
public:
Person(){ // конструктор по умолчанию
strcpy(name, "\0");
sex='n';
year_of_birth=0;
}
Person(char* a, char b, int c){ // конструктор с параметрами
strcpy(name, a);
sex=b;
year_of_birth=c;
}
fPersonInput(){
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Person")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth;
}
in.close();
}
fPersonOutput(){
ofstream out;
out.open("testout.txt");
if (out.is_open()){
out.width(12);
out << "Name";
out.width(4);
out << "Sex";
out.width(5);
out << "Year";
out.width(3);
out << "St";
out.width(7);
out << "Group";
out.width(24);
out << "Disciplines";
out.width(24);
out << "Teachers";
out.width(18);
out << "Theme/Profile";
out.width(24);
out << "Tutor";
out.width(24);
out << "Job";
out.width(4);
out << "Wrk" << endl;
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth << endl;
}
out.close();
}
void View(){ // вывод
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl << "\n";
}
};
void Table(){
ofstream out;
out.open("testout.txt");
if (out.is_open()){
out.width(12);
out << "Name";
out.width(3);
out << "MF";
out.width(5);
out << "Year";
out.width(3);
out << "St";
out.width(7);
out << "Group";
out.width(24);
out << "Disciplines";
out.width(24);
out << "Teachers";
out.width(18);
out << "Theme/Profile";
out.width(24);
out << "Tutor";
out.width(24);
out << "Job";
out.width(7);
out << "Work" << endl;
}
out.close();
}
class Studying : public Person { // учащийся +
protected:
int year;
char group[7];
public:
Studying(): Person(){ // по умолчанию
year=0;
strcpy(group, "\0");
}
Studying(char* a, char b, int c, int d, char* g): Person(a, b, c){ // конструктор
year=d;
strcpy(group, g);
}
fStudyingInput(){
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Studying")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth >> year >> group;
}
in.close();
}
fStudyingOutput(){
ofstream out;
out.open("testout.txt", ios::app);
if (out.is_open()){
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth;
out.width(3);
out << year;
out.width(7);
out << group << endl;
}
out.close();
}
void View(){ // вывод
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl;
cout << "Year of application: " << year << endl;
cout << "Group: " << group << endl << "\n";
}
};
class Worker : public Person { // сотрудник +
protected:
int workyears;
char job[64];
public:
Worker(): Person(){
workyears=0;
strcpy(job, "\0");
}
Worker(char* a): Person(){
workyears=0;
strcpy(job, "\0");
strcpy(name, a);
}
Worker(char* a, char b, int c, int d, char* g): Person(a, b, c){
workyears=d;
strcpy(job, g);
}
fWorkerInput(){
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Worker")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth >> workyears;
in.ignore();
in.getline(job, 64);
}
in.close();
}
fWorkerOutput(){
ofstream out;
out.open("testout.txt", ios::app);
if (out.is_open()){
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth;
out.width(124);
out << job;
out.width(4);
out << workyears << endl;
}
out.close();
}
char* getName(){
return(name);
}
void View(){
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl;
cout << "Job: " << job << endl;
cout << "Work years: " << workyears << endl << "\n";
}
};
class Teacher : public Worker { // преподаватель
protected:
int discNum, groupNum;
char disciplines[32][32];
char groups[7][8];
public:
Teacher(): Worker(){
discNum=0;
groupNum=0;
}
Teacher(char *a): Worker(a){
discNum=0;
groupNum=0;
}
Teacher(char* a, char b, int c, int d, char* g): Worker(a, b, c, d, g){
discNum=0;
groupNum=0;
}
fTeacherInput(){
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Teacher")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth >> workyears;
in.ignore();
in.getline(job, 64);
in >> discNum;
in.ignore();
for(int i=0; i<discNum; i++){
in.getline(disciplines[i], 64);
}
in >> groupNum;
in.ignore();
for(int i=0; i<groupNum; i++){
in.getline(groups[i], 64);
}
}
in.close();
}
fTeacherOutput(){
ofstream out;
out.open("testout.txt", ios::app);
if (out.is_open()){
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth;
out.width(10);
out << groupNum;
out.width(24);
out << discNum;
out.width(90);
out << job;
out.width(4);
out << workyears << endl;
for (int i=0; i<groupNum; i++){
out.width(31);
out << groups[i];
if(i<discNum){
out.width(24);
out << disciplines[i];
}
out << endl;
}
for (int i=groupNum; i<discNum; i++){
out.width(55);
out << disciplines[i] << endl;
}
}
out.close();
}
void AddDiscipline(char* a){
strcpy(disciplines[discNum], a);
discNum++;
}
void AddGroups(char* a); // прототип добавления группы
void ViewDisciplines(){
cout << "List of disciplines:" << endl;
for (int i=0; i<discNum; i++){
cout << " - " << disciplines[i] << endl;
}
}
char* getDiscT(int i){
return(disciplines[i]);
}
int getDiscNumT(){
return(discNum);
}
void ViewGroups(){
cout << "List of groups:" << endl;
for (int i=0; i<groupNum; i++){
cout << " - " << groups[i] << endl;
}
}
void View(){
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl;
cout << "Job: " << job << endl;
cout << "Work years: " << workyears << endl;
ViewDisciplines();
ViewGroups();
cout << endl;
}
};
class Scientist : public Worker { // научный работник +
protected:
char profile[64]; // область исследований
public:
Scientist(): Worker(){
}
Scientist(char* a, char b, int c, int d, char* g, char* e): Worker(a, b, c, d, g){
strcpy(profile, e);
}
fScientistInput(){
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Scientist")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth >> workyears;
in.ignore();
in.getline(job, 64);
in.getline(profile, 64);
}
in.close();
}
fScientistOutput(){
ofstream out;
out.open("testout.txt", ios::app);
if (out.is_open()){
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth;
out.width(76);
out << profile;
out.width(48);
out << job;
out.width(4);
out << workyears << endl;
}
out.close();
}
void View(){
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl;
cout << "Job: " << job << endl;
cout << "Work years: " << workyears << endl;
cout << "Main profile: " << profile << endl << "\n";
}
};
class Student : public Studying { // студент
protected:
char disciplines[32][32];
int discNum, teachNum;
Teacher teachers[12];
public:
Student(char* a, char b, int c, int d, char* g); // прототип конструктора
Student(): Studying(){ // конструктор по умолчанию
discNum=0;
teachNum=0;
Teacher();
}
fStudentInput(){
int j;
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Student")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth >> year >> group >> discNum;
in.ignore();
for(int i=0; i<discNum; i++){
in.getline(disciplines[i], 64);
}
in >> j;
in.ignore();
for(int i=0; i<j; i++){
in.getline(srav, 64);
AddTeacher(Teacher(srav));
}
}
in.close();
}
fStudentOutput(){
ofstream out;
out.open("testout.txt", ios::app);
if (out.is_open()){
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth;
out.width(3);
out << year;
out.width(7);
out << group;
out.width(24);
out << discNum;
out.width(24);
out << teachNum << endl;
for (int i=0; i<discNum; i++){
out.width(55);
out << disciplines[i];
if(i<teachNum){
out.width(24);
out << teachers[i].getName();
}
out << endl;
}
for (int i=discNum; i<teachNum; i++){
out.width(79);
out << teachers[i].getName() << endl;
}
}
out.close();
}
void AddDiscipline(char* a){
strcpy(disciplines[discNum], a);
discNum++;
}
void AddTeacher(Teacher a){
teachers[teachNum]=a;
teachNum++;
}
char* getGroup(){
return(group);
}
char* getDisc(int i){
return(disciplines[i]);
}
int getDiscNum(){
return(discNum);
}
void ViewDisciplines(){
cout << "List of disciplines:" << endl;
for (int i=0; i<discNum; i++){
cout << " - " << disciplines[i] << endl;
}
}
void ViewTeachers(){
cout << "List of teachers:" << endl;
for (int i=0; i<teachNum; i++){
cout << " - " << teachers[i].getName() << "\n";
}
}
void stView(){
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl;
cout << "Year of application: " << year << endl;
cout << "Group: " << group << endl;
ViewDisciplines();
ViewTeachers();
cout << "\n";
}
};
class Aspirant : public Studying { // аспирант +
protected:
char theme[64];
Worker tutor;
public:
Aspirant(): Studying(){
tutor=Worker();
}
Aspirant(char* a, char b, int c, int d, char* g, char* t): Studying(a, b, c, d, g){ // конструктор
strcpy(theme, t);
}
void setWorker(Worker a){
tutor=a;
}
fAspirantInput(){
char srav[64];
ifstream in("test.txt");
if (in.is_open()){
in >> srav;
while ((strcmp(srav, "Aspirant")!=0)&&(!in.eof( ))){
in >> srav;
}
if (!in.eof( )) in >> name >> sex >> year_of_birth >> year >> group;
in.ignore();
in.getline(theme, 64);
in.getline(srav, 64);
tutor=Worker(srav);
}
in.close();
}
fAspirantOutput(){
ofstream out;
out.open("testout.txt", ios::app);
if (out.is_open()){
out.width(12);
out << name << " ";
out.width(3);
out << sex;
out.width(5);
out << year_of_birth;
out.width(3);
out << year;
out.width(7);
out << group;
out.width(66);
out << theme;
out.width(24);
out << tutor.getName() << endl;
}
out.close();
}
void View(){
cout << "Person " << name << ":" << endl;
cout << "Sex: " << sex << endl;
cout << "Year of birth: " << year_of_birth << endl;
cout << "Year of application: " << year << endl;
cout << "Group: " << group << endl;
cout << "Profile: " << theme << endl;
cout << "Director: " << tutor.getName();
cout << "\n\n";
}
};
Student* studentList[24]; // глобальный массив, состоящий из всех студентов
Student::Student(char* a, char b, int c, int d, char* g): Studying(a, b, c, d, g){ // конструктор для студента
discNum=0;
teachNum=0;
Teacher();
studentList[studGlob]=this;
studGlob++;
}
void Teacher::AddGroups(char* a){
strcpy(groups[groupNum], a);
groupNum++;
for(int j=0; j<studGlob; j++){
if (strcmp(studentList[j]->getGroup(), a)==0){
for(int k=0; k<studentList[j]->getDiscNum(); k++){
for(int l=0; l<(*this).getDiscNumT(); l++){
if(strcmp(studentList[j]->getDisc(k), (*this).getDiscT(l))==0){
studentList[j]->AddTeacher(*this);
}
}
}
}
}
}
int main(void){
setlocale(LC_ALL, "Rus");
Table();
Person a1;
a1.fPersonInput();
a1.View();
a1.fPersonOutput();
Studying a2;
a2.fStudyingInput();
a2.View();
a2.fStudyingOutput();
Student a3;
a3.fStudentInput();
a3.stView();
a3.fStudentOutput();
Aspirant a4;
a4.fAspirantInput();
a4.View();
a4.fAspirantOutput();
Worker a5;
a5.fWorkerInput();
a5.View();
a5.fWorkerOutput();
Teacher a6;
a6.fTeacherInput();
a6.View();
a6.fTeacherOutput();
Scientist a7;
a7.fScientistInput();
a7.View();
a7.fScientistOutput();
/*
cout << " - Пример обычного человека:" << endl;
Person a1("Anatoly", 'm', 2025); // человек
a1.View();
cout << " - Пример обучающегося:" << endl;
Studying a2("Gena", 'm', 2005, 2024, "B23105"); // обучающийся
a2.View();
cout << " - Пример сотрудника:" << endl;
Worker a3("Katya", 'f', 1996, 4, "Nuclear physicist"); // сотрудник
a3.View();
cout << " - Пример научного работника:" << endl;
Scientist a4("Nadya", 'f', 1997, 6, "Nuclear chemist", "Quantum chemistry"); // научный работник
a4.View();
cout << " - Пример аспиранта с назначенным научруком (графа director):" << endl;
Aspirant a5("Tolya", 'm', 1845, 25, "A25934\0", "Philosophy\0");
a5.setWorker(a3);
a5.View();
Student a6("Nikolay", 'm', 1687, 2012, "B21953"); // студент
Student a8("Nikolay 2", 'm', 1687, 2012, "B22953"); // студент 2
a6.AddDiscipline("Analytical Mechanics");
a8.AddDiscipline("Ecology");
Teacher a7("Sergey", 'm', 1987, 10, "Physics teacher"); // учитель
Teacher a9("Sergey", 'm', 1987, 10, "Physics teacher");
a7.AddDiscipline("Analytical Mechanics");
a7.AddDiscipline("Ecology");
a7.AddGroups("B21953");
a7.AddGroups("B22953");
cout << " - Пример учителя с назначенными группами и предметами:" << endl;
a7.View();
cout << " - Пример студентов с назначенными предметами и преподавателями:" << endl;
for(int i=0; i<studGlob; i++){
studentList[i]->stView();
}
*/
}
