Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Otchyot_OOPro_Lab1

.rtf
Скачиваний:
56
Добавлен:
19.01.2015
Размер:
2.38 Mб
Скачать

Лабораторная работа №3

по дисциплине "Обьектно-ориентированное программирование"

Терещенко Р.В. ИНФ-12-1

Задание:

Разработать класс, представляющий методическое пособие. Пособие характеризуется автором, названием, предметом и списком выдач экземпляров на руки. Выдача характеризуется датой и именем студента. Автор характеризуется именем, фамилией и названием кафедры.

Необходимые операции таковы:

1. узнать имя и фамилию автора пособия

2. узнать название, предмет пособия и кафедру

3. узнать среднее число выдач для всех студентов

4. выдать пособие на руки

5. получить пособие обратно

6. найти студента с наибольшим числом выдач

7. узнать число выдач для заданной даты

Код программы:

Файл "Header.h"

#pragma once

#include<iostream>

#include<list>

#include<vector>

#include<string>

using namespace std;

//Дата

struct Date{

short day;

short month;

short year;

void operator ()(short d,short m, short y);

void operator = (Date &d);

bool operator == (Date &d);

};

ostream& operator<<(ostream& out,Date &d);

//автор

struct Author

{

char name[20];

char surname[30];

char department[20];

void operator ()(char* n, char*s_n, char*dep);

void operator = (Author &a);

};

ostream& operator<<(ostream& out, Author &a);

//студент

struct Student

{

char name[30];

char surname[30];

void operator ()(char* n,char* s_n);

void operator = (Student &s);

bool operator == (Student &s);

};

ostream& operator<<(ostream& out, Student &s);

//выдача

struct Delivery

{

Date date;

Student stud;

void operator ()(Date d,Student s);

};

ostream& operator<<(ostream& out, Delivery &d);

//методическое пособие

class MethodicalManual

{

char name[50];//имя пособия

char subject[20];//предмет

bool is_on_hands;//выдано ли пособие?

Author author;//информация об авторе

list<Delivery> deliveries;//список выдач

public:

MethodicalManual();//конструткор по умолчанию

MethodicalManual(Author a_inf, char*n, char*s);//конструктор с параметрами

~MethodicalManual();// деструктор

void GiveManual(Delivery d);//4)выдать пособие на руки

void GiveManual(Date date, Student s);//4)выдать пособие на руки

void GetBackManual();//5)получить пособие обратно

char* ReturnNameOfManual();//2)получить название пособия

char* ReturnSubjectOfManual();//2)получить предмет пособия

Author ReturnAuthorOfManual();//1)Узнать автора пособия

int ReturnCountOfDeliveries();//3)Получить общее количество выдач

int ReturnCountOfDeliveries(Date d);//7)узнать число выдач для заданной даты

void FindStudent();//6)найти студента с наибольшим числом выдач

friend ostream& operator << (ostream &out, MethodicalManual &m);

};

Файл "Date.cpp"

#include <string>

#include "Header.h"

void Date:: operator ()(short d,short m, short y)

{

if(d>0 && d<32)

day=d;

else

{

cout<<"Error! Invalid \"day\" value "<<d<<"! \"day\" is equal 1\n";

day=1;

}

if(m>0 && m<13)

month=m;

else

{

cout<<"Error! Invalid \"month\" value "<<m<<"! \"month\" is equal 1\n";

month=1;

}

if(y>0)

year=y;

else

{

cout<<"Error! Invalid \"year\" value "<<y<<"! \"year\" is equal 1900\n";

year=1900;

}

}

void Date::operator =(Date &d)

{

day=d.day;

month = d.month;

year = d.year;

}

bool Date::operator ==(Date &d)

{

if(year != d.year || month !=d.month || day!=d.day)

return false;

else

return true;

}

ostream& operator<<(ostream& out,Date &d)

{

if(d.day>9)

out<<d.day;

else

out<<"0"<<d.day;

out<<".";

if(d.month>9)

out<<d.month;

else

out<<"0"<<d.month;

out<<"."<<d.year;

return out;

}

Файл "Author.cpp"

#include "Header.h"

ostream& operator<<(ostream& out, Author &a)

{

out<<"Name: "<<a.name<<"\nSurname: "<<a.surname<<"\nDepartment: "<<a.department<<endl;

return out;

}

void Author:: operator ()(char* n, char*s_n, char*dep)

{

strcpy(name,n);

strcpy(surname,s_n);

strcpy(department,dep);

}

void Author::operator = (Author &a)

{

strcpy(name,a.name);

strcpy(surname,a.surname);

strcpy(department,a.department);

}

Файл "Delivery.cpp"

#include "Header.h"

ostream& operator<<(ostream& out,Delivery &d)

{

out<<"Delivery date: "<<d.date<<"\nStudent:\n"<<d.stud<<endl;

return out;

}

void Delivery:: operator ()(Date d, Student s)

{

date=d;

stud=s;

}

Файл "Student.cpp"

#include "Header.h"

void Student::operator ()(char *n, char *s_n)

{

strcpy(name,n);

strcpy(surname,s_n);

}

void Student::operator =(Student &s)

{

strcpy(name,s.name);

strcpy(surname,s.surname);

}

bool Student::operator ==(Student &s)

{

if(strcmp(name,s.name)!=0 || strcmp(surname,s.name)!=0)

return false;

else

return true;

}

ostream& operator <<(ostream& out, Student &s)

{

out<<s.name<<" "<<s.surname;

return out;

}

Файл "MethodicalManual.cpp"

#include "Header.h"

MethodicalManual::MethodicalManual()

{

Author a;

a("No name","No surname","Unknown");

strcpy(name,"Untitled");

strcpy(subject,"Unknown");

is_on_hands=false;

}

MethodicalManual::MethodicalManual(Author a_inf, char*n, char*s)

{

author=a_inf;

strcpy(name,n);

strcpy(subject,s);

is_on_hands=false;

}

MethodicalManual::~MethodicalManual()

{

deliveries.clear();

}

void MethodicalManual::GiveManual(Date date, Student s)

{

if(!is_on_hands)

{

Delivery d;

d(date,s);

deliveries.push_back(d);

is_on_hands=true;

cout<<"Manual was get to "<<d.stud<<"("<<d.date<<")"<<endl;

}

else

cout<<"Manual is already issued!"<<endl;

}

void MethodicalManual::GiveManual(Delivery d)

{

if(!is_on_hands)

{

deliveries.push_back(d);

is_on_hands=true;

cout<<"Manual was get to "<<d.stud<<"("<<d.date<<")"<<endl;

}

else

cout<<"Manual is already issued!"<<endl;

}

char* MethodicalManual::ReturnNameOfManual()

{

return name;

}

char* MethodicalManual::ReturnSubjectOfManual()

{

return subject;

}

int MethodicalManual::ReturnCountOfDeliveries(Date d)

{

int count=0;

list<Delivery>::iterator iter = deliveries.begin();

cout<<"Deliveries on "<<d<<":";

while(iter!=deliveries.end())

{

Delivery del=*iter;

if(d==del.date)

count++;

++iter;

}

cout<<count<<endl;

return count;

}

Author MethodicalManual::ReturnAuthorOfManual()

{

return author;

}

void MethodicalManual::GetBackManual()

{

if(is_on_hands)

is_on_hands=false;

}

void MethodicalManual::FindStudent()

{

if(deliveries.size()!=0)

{

int max_count=0,temp_count=0;

Student max_stud;

list<Delivery>::iterator iter = deliveries.begin();

list<Delivery>::iterator it = deliveries.begin();

while(iter!=deliveries.end())

{

Delivery d = *iter;

Student s = d.stud;

max_stud = s;

while(it!=deliveries.end())

{

Delivery temp_d = *it;

Student temp_s = temp_d.stud;

if(s==temp_s)

++temp_count;

++it;

}

if(temp_count>max_count)

{

max_count=temp_count;

max_stud = s;

}

++iter;

}

cout<<"Student with max count of deliveries "<<max_stud<<endl;

}

}

int MethodicalManual::ReturnCountOfDeliveries()

{

return (int)deliveries.size();

}

ostream& operator<<(ostream &out, MethodicalManual &m)

{

out<<"Manual Information:\nName: "<<m.name<<"\nSubject: "<<m.subject<<"\nAuthor Info:\n"<<m.author<<endl;

return out;

}

Файл "main.cpp"

#include<iostream>

#include"Header.h"

using namespace std;

void main()

{

setlocale(LC_CTYPE,"rus");

Author a1;

a1("Василий","Пупкин","ИНФ");

MethodicalManual m1(a1,"Философия программирования","Программирование");

cout<<m1;

Student s1,s2;

s1("Петя","Васечкин");

s2("Вася","Петечкин");

Date d1,d2;

d1(31,10,2011);

d2(29,10,2011);

m1.GiveManual(d2,s2);

m1.GetBackManual();

m1.GiveManual(d1,s1);

m1.GetBackManual();

m1.GiveManual(d1,s2);

m1.GetBackManual();

m1.GiveManual(d1,s2);

cout<<"Total count of deliveries:"<<m1.ReturnCountOfDeliveries()<<endl;

m1.ReturnCountOfDeliveries(d1);

m1.FindStudent();

}

Соседние файлы в предмете Объектно ориентированное программирование