DevioLab CRYPTO TRADING AUTOMATION
LIVE
AUTOMATED CRYPTO TRADING • BINANCE
Автоматизуйте свій криптопортфель
Торгові боти DevioLab аналізують крипторинок, автоматично відкривають і закривають позиції та керують вашим портфелем на Binance 24/7.
CRYPTO 80 Bots
BINANCE Spot Trading
TRADING 24 / 7
Спробувати DevioLab
deviolab.com

Побудова

Інформація про навчальний заклад

ВУЗ:
Національний університет Львівська політехніка
Інститут:
Інститут прикладної математики та фундаментальних наук
Факультет:
Не вказано
Кафедра:
Кафедра прикладної математики

Інформація про роботу

Рік:
2013
Тип роботи:
Лабораторна робота
Предмет:
Об’єктно-орієнтоване програмування

Частина тексту файла (без зображень, графіків і формул):

Міністерство освіти і науки України Національний університет “Львівська політехніка” Інститут прикладної математики та фундаментальних наук Кафедра прикладної математики Лабораторна робота №0 з курсу Об’єктно-орієнтоване програмування Тема : Побудова класів у С++ Мета : Побудувати клас згідно заданої предметної області Хід роботи : Згідно умови задачі потрібно побудувати декілька класів : для роботи з інформацією про студента, групу, у якій знаходяться студенти, порівняння членів класу студент, а також методів сортування. В процесі створення класу потрібно забезпечити можливість введення-виведення студентів та їх сортування. Критерій будь-який (тобто сортувати по імені, прізвищу, групі, середньому значенню оцінок). Створити можливість вибирати один із двох методів сортування – метод “бульбашки” та метод “човник”. У модулі тестування ми реалізували наші потреби за допомогою функцій наших класів, а саме у ході виконання програми ми будемо вводити, сортувати та виводити інформацію про певні складові класу Group. main.cpp #include <iostream> #include "Group.h" void main(void) { Group pm21; pm21.input(); pm21.sort(); pm21.output(); } Group.h #ifndef _Group_ #define _Group_ #include "Student.h" class Group { public: void input(void); void output(void); void sort(void); friend class Sorter; // SYSTEM Group(); Group(int); Group(const Group &); ~Group(); Group & operator=(const Group &); Student & operator[](int i); private: Student *m_ptrMasStudents; int m_kilkStudents; }; #endif Group.cpp #include <iostream> #include <string.h> #include "Group.h" #include "Comparer.h" #include "Sorter.h" void Group::input(void) { cout<<"BBEDiTb 6yDb-LACKA 4UCL0 CTyDEHTiB:"<<endl; cin>>m_kilkStudents; cout<<endl<<"\tBBiD"<<endl; m_ptrMasStudents= new Student [m_kilkStudents]; for(int i=0;i<m_kilkStudents;++i) { cin.ignore(1,'\n'); cout<<"iMJA:"; (*this)[i].setName(); cout<<endl<<"nPU3BUWE:"; (*this)[i].setSurname(); cout<<endl<<"GpynA:"; (*this)[i].setGroup(); cout<<endl<<"0ziHKU:"<<endl;; (*this)[i].setMarks(); cout<<endl; } } void Group::output(void) { for(int i=0;i<m_kilkStudents;++i) { /*char tempBuffer[20]; m_ptrMasStudents[i].getName(tempBuffer); cout<<tempBuffer<<"\t"; m_ptrMasStudents[i].getSurname(tempBuffer); cout<<tempBuffer<<"\t"; m_ptrMasStudents[i].getGroup(tempBuffer); cout<<tempBuffer<<"\t"; int aTemp[5]; m_ptrMasStudents[i].getMarks(aTemp); for(int j=0;j<5;++j) cout<<aTemp[j]<<" "; cout<<endl;*/ cout<<(*this)[i]; } } void Group::sort(void) { cout<<"\nBBEDiTb Cn0Ci6 C0PTyBAHH9l MACUBy:"<<endl <<"1 - 3A iMEHEM \n 2 - 3A nPi3BUWEM \n 3 - 3A GPyn0ju \n 4 - 3A 0ciHKAMU(CEPEDHIj 6AL) \n" <<"iHWE 4UCL0 - 3A iMEHEM\n"; PTR_SORT ptrSort=0; int k; cin>>k; switch(k) { /*case 1 : ptrSort=&Student::sortByName; break; case 2 : ptrSort=&Student::sortBySurname; break; case 3 : ptrSort=&Student::sortByGroup; break; case 4 : ptrSort=&Student::sortByMarks; break; default: ptrSort=&Student::sortByName; break;*/ case 1 : ptrSort=&Comparer::sortByName; break; case 2 : ptrSort=&Comparer::sortBySurname; break; case 3 : ptrSort=&Comparer::sortByGroup; break; case 4 : ptrSort=&Comparer::sortByMarks; break; default: ptrSort=&Comparer::sortByName; break; } cout<<"/nBBEDiTb METOD COPTyBAHH9l"<<endl <<"1 - 6yLb6AWKA \n 2 - LiHIjHA BU6iPKA \n 3 - 40BHUK \n 4 - WBUDKE \n iHWE 4UCLO - 6yLb6AWKA\n\n"; PTR_SORTER ptrSorter=0; cin>>k; switch(k) { case 1 : ptrSorter=&Sorter::sortByBubble; break; case 2 : ptrSorter=&Sorter::sortByLinearChoise; break; case 3 : ptrSorter=&Sorter::sortByBoat; break; //case 4 : ptrSorter=&Sorter::sortByQuick; break; default: ptrSorter=&Sorter::sortByBubble; break; } Sorter * tempPtrSorter=new Sorter; (tempPtrSorter->*ptrSorter)(*this,ptrSort); } // SYSTEM Group::Group() { m_kilkStudents=0; m_ptrMasStudents=0; cout<<"Constuctor Group is WORKING"<<endl; } Group::Group(int temp) { m_kilkStudents=temp; m_ptrMasStudents= new Student [temp]; cout<<"Constuctor Group with int is WORKING"<<endl; } Group::Group(const Group & temp) { cout<<"Copyconstructor is working"<<endl; m_kilkStudents=temp.m_kilkStudents; m_ptrMasStudents= new Student [m_kilkStudents]; for(int i=0;i<m_kilkStudents;++i) (*this)[i]=temp.m_ptrMasStudents[i]; } Group::~Group() { cout<<"destructor Group is working"<<endl; delete [m_kilkStudents] m_ptrMasStudents; } Group & Group::operator=(const Group & refGroup) { //if (*this!=refGroup) { m_kilkStudents=refGroup.m_kilkStudents; m_ptrMasStudents= new Student [m_kilkStudents]; for(int i=0;i<m_kilkStudents;++i) (*this)[i]=refGroup.m_ptrMasStudents[i]; } return (*this); } Student & Group::operator[](int i) { if (i<m_kilkStudents) return (*this).m_ptrMasStudents[i]; Student s; return s; } Student.h #ifndef _Student_ #define _Student_ #include <iostream.h> class Student { public: /* Accessors */ void getName(char *); void getSurname(char *); void getGroup(char *); void getMarks(int *); /* Modificators */ void setName(void); void setSurname(void); void setGroup(void); void setMarks(void); /*comparer functions */ /* int sortByName(Student &); int sortBySurname(Student &); int sortByGroup(Student &); int sortByMarks(Student &);*/ // FRIENDS friend class Group; friend class Comparer; friend ostream & operator <<(ostream & ,Student &); // CONSTRUCTOR Student(); ~Student(); //ostream & operator <<(ostream & OS); private: char m_szName[20]; char m_szSurname[20]; char m_szGroup[20]; int m_aiMarks[5]; }; //typedef int(Student::*PTR_sSORT)(Student &); #endif Student.cpp #include <iostream> #include <string.h> #include "Student.h" // Accessors void Student :: getName(char * szBuffer) { strcpy(szBuffer, m_szName); } void Student::getSurname(char * szBuffer) { strcpy(szBuffer, m_szSurname); } void Student::getGroup(char * szBuffer) { strcpy(szBuffer, m_szGroup); } void Student::getMarks(int * aiBuffer) { for (int i=0; i<5; ++i) aiBuffer[i]=m_aiMarks[i]; } // Modificators void Student::setName(void) { cin.getline(m_szName,20,'\n'); } void Student::setSurname(void) { cin.getline(m_szSurname,20,'\n'); } void Student::setGroup(void) { cin.getline(m_szGroup,20,'\n'); } void Student::setMarks(void) { int temp; for(int i=0;i<5;++i) { cin>>temp; m_aiMarks[i]=temp; } } // COMPARERS /*int Student::sortByName(Student & s) { if (strcmp(this->m_szName,s.m_szName)>0) return 1; return 0; } int Student::sortBySurname(Student & s) { if (strcmp(this->m_szSurname,s.m_szSurname)>0) return 1; return 0; } int Student::sortByGroup(Student & s) { if (strcmp(this->m_szGroup,s.m_szGroup)>0) return 1; return 0; } int Student::sortByMarks(Student & s) { double ser1=0,ser2=0; for(int i=0;i<5;++i) { ser1+=this->m_aiMarks[i]; ser2+=s.m_aiMarks[i]; } ser1/=5; ser2/=5; if (ser1>ser2) return 1; return 0; }*/ // SYSTEM Student::Student() { m_szName[0]='\0'; m_szSurname[0]='\0'; m_szGroup[0]='\0'; for (int i=0; i<5; ++i) m_aiMarks[i]=0; cout<<"Constructor Student is working"<<endl; } Student::~Student() { cout<<"destructor Student is working"<<endl; } /*ostream & Student::operator<<(ostream & OS) { OS<<m_szName<<'\t'<<m_szSurname<<'\t' <<m_szGroup<<'\t'; for (int i=0;i<5;++i) OS<<m_aiMarks[i]<<' '; OS<<endl; return OS; }*/ ostream & operator <<(ostream & OS,Student & s) { OS<<s.m_szName<<'\t'<<s.m_szSurname<<'\t' <<s.m_szGroup<<'\t'; for (int i=0;i<5;++i) OS<<s.m_aiMarks[i]<<' '; OS<<endl; return OS; } Comparer.h #ifndef _Comparer_ #define _Comparer_ #include "Student.h" class Comparer { public: int sortByName(Student & s1,Student & s2); int sortBySurname(Student & s1,Student & s2); int sortByGroup(Student & s1,Student & s2); int sortByMarks(Student & s1,Student & s2); }; typedef int(Comparer::*PTR_SORT)(Student &,Student &); #endif Comparer.cpp #include <string.h> #include "Comparer.h" int Comparer::sortByName(Student & s1,Student & s2) { if (strcmp(s1.m_szName,s2.m_szName)>0) return 1; return 0; } int Comparer::sortBySurname(Student & s1,Student & s2) { if (strcmp(s1.m_szSurname,s2.m_szSurname)>0) return 1; return 0; } int Comparer::sortByGroup(Student & s1,Student & s2) { if (strcmp(s1.m_szGroup,s2.m_szGroup)>0) return 1; return 0; } int Comparer::sortByMarks(Student & s1,Student & s2) { double ser1=0,ser2=0; for(int i=0;i<5;++i) { ser1+=s1.m_aiMarks[i]; ser2+=s2.m_aiMarks[i]; } ser1/=5; ser2/=5; if (ser1>ser2) return 1; return 0; } Sorter.h #ifndef _Sorter_ #define _Sorter_ #include "Comparer.h" class Sorter { public: void sortByBoat(Group &, PTR_SORT); void sortByBubble(Group &, PTR_SORT); void sortByLinearChoise(Group &, PTR_SORT); void sortByQuick(Group &, PTR_SORT); }; typedef void(Sorter::*PTR_SORTER)(Group &,PTR_SORT); #endif Sorter.cpp #include "Sorter.h" #include "Group.h" void Sorter::sortByBubble(Group & G,PTR_SORT sortFunction) { int n=G.m_kilkStudents; int p=1; Comparer * tempPtr=new Comparer; while((n-1)&&p) { p=0; for(int i=0;i<n-1;++i) { if ((tempPtr->*sortFunction)(G.m_ptrMasStudents[i],G.m_ptrMasStudents[i+1])) { p=1; Student temp=G.m_ptrMasStudents[i]; G.m_ptrMasStudents[i]=G.m_ptrMasStudents[i+1]; G.m_ptrMasStudents[i+1]=temp; } } --n; } } void Sorter::sortByLinearChoise(Group & G,PTR_SORT sortFunction) { Comparer * tempPtr=new Comparer; for(int i=0; i<G.m_kilkStudents; ++i) { Student min=G.m_ptrMasStudents[i]; int minIndex=i; for(int j=i+1; j<G.m_kilkStudents; ++j) { if ((tempPtr->*sortFunction)(min,G.m_ptrMasStudents[j])) { min=G.m_ptrMasStudents[j]; minIndex=j; } } G.m_ptrMasStudents[minIndex]=G.m_ptrMasStudents[i]; G.m_ptrMasStudents[i]=min; } } void Sorter::sortByBoat(Group & G, PTR_SORT sortFunction) { Comparer * tempPtr=new Comparer; for(int i=0; i<G.m_kilkStudents-1; ++i) { if((tempPtr->*sortFunction)(G.m_ptrMasStudents[i],G.m_ptrMasStudents[i+1])) { int j=i; do { Student temp=G.m_ptrMasStudents[j]; G.m_ptrMasStudents[j]=G.m_ptrMasStudents[j+1]; G.m_ptrMasStudents[j+1]=temp; --j; } while((tempPtr->*sortFunction)(G.m_ptrMasStudents[j],G.m_ptrMasStudents[j+1])&&j); } } } Висновок В ході даної лабораторної роботи ми навчилися стврювати класи та оперувати елементами типу клас. Тобто ми навчилися інкапсулювати в класах об’єкти та методи їх обробки та ознайомилися із елементами, які клас може в себе включати, використовувати ці знання для реалізації програм. Міністерство освіти і науки України Національний університет “Львівська політехніка” Інститут прикладної математики та фундаментальних наук Кафедра прикладної математики Лабораторна робота №1 з курсу Об’єктно-орієнтоване програмування Виконав: Остапчук Ю.М. Група: ПМ-21 Викладач: Пеняк І.О. Львів-2009 Тема : Побудова класів у С++ Мета : Побудувати клас згідно заданих умов. Хід роботи : Згідно умови задачі потрібно було написати клас для представлення функціональності прямокутників з сторонами, паралельними осям координат. Забезпечити можливість переміщення прямокутника, зміни його розмірів, утворення найменшого нового прямокутника, що містить в собі два довільних (є їх об’єднанням) та прямокутника, що є результатом перетину двох заданих. Написати МТ, що дозволяв би перевірити усю задану функціональність (виклик довільних методів класу). main.cpp #include <iostream> #include <conio.h> #include "Rectangle.h" void main() { char c = 0; while (c != 'e') { Rectangle R, R1, R2; system("cls"); cout<<"Natusnit' vidpovidny klaviwy:"<<endl <<"A: Peremiw4ennja prjamokytnuka."<<endl <<"B: Zmina rozmiriv prjamokytnuka."<<endl <<"C: Stvorennja novogo naimenwogo prjamokytnuka w4o mistut' v sobi dva zadanuh."<<endl <<"D: Stvorennja prjamokytnuka w4o e peretunom dvoh zadanuh."<<endl <<"E: Vujtu z programu."<<endl; c = getch(); system("cls"); switch (c) { case 'a': { R.input("R"); R.move(); R.output("R"); getch(); break; }; case 'b': { R.input("R"); R.setsides(); R.output("R"); getch(); break; } case 'c': { R1.input("R1"); R2.input("R2"); R.createminrec(R1, R2); R.output("R"); getch(); break; }; case 'd': { R1.input("R1"); R2.input("R2"); R.intersection(R1, R2); R.output("R"); getch(); break; }; } } } Rectangle.h #ifndef _Rectangle_ #define _Rectangle_ #include <iostream.h> class Rectangle { public: void output(char[5]); void input(char[5]); void move(void); void setsides(void); Rectangle & createminrec(Rectangle &, Rectangle &); Rectangle & intersection(Rectangle &, Rectangle &); //конструктори Rectangle(); Rectangle(const Rectangle &); //деструктор ~Rectangle(); Rectangle & operator = (const Rectangle &);//оператор присвоєння нового прямокутник Rectangle operator + (const Rectangle &);//утворення найменшого прямокутника з двох заданих Rectangle operator * (const Rectangle &);//перетин двох заданих прямокутників private: int x1; //масив координат прямокутника - x1, y1, x2, y2 int y1; int x2; int y2; }; #endif Rectangle.cpp #include <iostream> #include <math.h> #include "Rectangle.h" void Rectangle::output(char ch[5]) { cout<<"Koordunatu prjamokytnuka "<<ch<<": "<<endl; int temp; if ((*this).x1 > (*this).x2) {temp = (*this).x1; (*this).x1 = (*this).x2; (*this).x2 = temp;} if ((*this).y1 > (*this).y2) {temp = (*this).x1; (*this).x1 = (*this).x2; (*this).x2 = temp;} cout<<"x1 = "<<(*this).x1<<endl <<"y1 = "<<(*this).y1<<endl <<"x2 = "<<(*this).x2<<endl <<"y2 = "<<(*this).y2<<endl; } void Rectangle::input(char ch[5]) { cout<<"Vvedit' koordunatu prjamokytnuka "<<ch<<": "<<endl; cout<<"x1 = "; cin>>(*this).x1; cout<<"y1 = "; cin>>(*this).y1; cout<<"x2 = "; cin>>(*this).x2; cout<<"y2 = "; cin>>(*this).y2; int temp; if ((*this).x1 > (*this).x2) {temp = (*this).x1; (*this).x1 = (*this).x2; (*this).x2 = temp;} if ((*this).y1 > (*this).y2) {temp = (*this).x1; (*this).x1 = (*this).x2; (*this).x2 = temp;} } void Rectangle::move(void) { int x, y; cout<<"Vvedit' zmiw4ennja:"<<endl; cout<<"dx = "; cin>>x; cout<<"dy = "; cin>>y; (*this).x1 += x; (*this).y1 += y; (*this).x2 += x; (*this).y2 += y; } void Rectangle::setsides(void) { int x, y; cout<<"Vvedit' novi zna4ennja storin:"<<endl; cout<<"x = "; cin>>x; cout<<"y = "; cin>>y; if (x < 0) x = -x; if (y < 0) y = -y; (*this).x2 = (*this).x1 + x; (*this).y2 = (*this).y1 + y; } Rectangle & Rectangle::createminrec(Rectangle & R1, Rectangle & R2) { if (R1.x1 < R2.x1) (*this).x1 = R1.x1; else (*this).x1 = R2.x1; if (R1.y1 < R2.y1) (*this).y1 = R1.y1; else (*this).y1 = R2.y1; if (R1.x2 < R2.x2) (*this).x2 = R2.x2; else (*this).x2 = R1.x2; if (R1.y2 < R2.x2) (*this).y2 = R2.y2; else (*this).y2 = R1.y2; return (*this); } Rectangle & Rectangle::intersection(Rectangle & R1, Rectangle & R2) { (*this).x1 = (*this).x2 = (*this).y1 = (*this).y2 = 0; if ((R2.x1 >= R1.x2)||(R2.y1 >= R1.y2)||(R2.x2 <= R1.x1)||(R2.y2 <= R2.y1)) return (*this); if (R2.x1 < R1.x1) (*this).x1 = R1.x1; else (*this).x1 = R2.x1; if (R2.x2 < R1.x2) (*this).x2 = R2.x2; else (*this).x2 = R1.x2; if (R2.y1 < R1.y1) (*this).y1 = R1.y1; else (*this).y1 = R2.y1; if (R2.y2 < R1.y2) (*this).y2 = R2.y2; else (*this).y2 = R1.y2; return (*this); } //конструктори Rectangle::Rectangle() { x1 = y1 = x2 = y2 = 0; //cout<<"Constructor Rectangle is working..."<<endl; } Rectangle::Rectangle(const Rectangle & R) { //cout<<"Copyconstructor is working..."<<endl; (*this).x1 = R.x1; (*this).x2 = R.x2; (*this).y1 = R.y1; (*this).y2 = R.y2; } //деструктор Rectangle::~Rectangle() { //cout<<"Destructor is working..."<<endl; } Rectangle & Rectangle::operator = (const Rectangle & R)//оператор присвоєння нового прямокутник { (*this).x1 = R.x1; (*this).x2 = R.x2; (*this).y1 = R.y1; (*this).y2 = R.y2; return (*this); } Rectangle Rectangle::operator + (const Rectangle & R1)//утворення найменшого прямокутника з двох заданих { Rectangle R; if ((*this).x1 < R1.x1) R.x1 = (*this).x1; else R.x1 = R1.x1; if ((*this).y1 < R1.y1) R.y1 = (*this).y1; else R.y1 = R1.y1; if ((*this).x2 < R1.x2) R.x2 = R1.x2; else R.x2 = R1.x2; if ((*this).y2 < R1.x2) R.y2 = R1.y2; else R.y2 = R1.y2; return R; } Rectangle Rectangle::operator * (const Rectangle & R1)//перетин двох заданих прямокутників { Rectangle R; R.x1 = R.x2 = R.y1 = R.y2 = 0; if ((R1.x1 >= (*this).x2)||(R1.y1 >= (*this).y2)||(R1.x2 <= (*this).x1)||(R1.y2 <= R1.y1)) return R; if (R1.x1 < (*this).x1) R.x1 = (*this).x1; else R.x1 = R1.x1; if (R1.x2 < (*this).x2) R.x2 = R1.x2; else R.x2 = (*this).x2; if (R1.y1 < (*this).y1) R.y1 = (*this).y1; else R.y1 = R1.y1; if (R1.y2 < (*this).y2) R.y2 = R1.y2; else R.y2 = (*this).y2; return R;
Антиботан аватар за замовчуванням

31.03.2013 00:51

Коментарі

Ви не можете залишити коментар. Для цього, будь ласка, увійдіть або зареєструйтесь.

Ділись своїми роботами та отримуй миттєві бонуси!

Нічого не вибрано
0%

Оголошення від адміністратора

Антиботан аватар за замовчуванням

Подякувати Студентському архіву довільною сумою

Admin

26.02.2023 12:38

Дякуємо, що користуєтесь нашим архівом!