Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
Отчёт
по лабораторной работе №1
по ООП
Проверил: Митителу В.
Кишинёв 2019
Структура – механизм абстракции
Цели работы:
изучение основ абстракции;
изучение правил определения и использования структур данных;
создание переменных типа структуры, обращение к полям;
изучение принципов программирования, основанных на работе со структурами.
Задание :
Исходный код :
//Пункт А
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
//8 вариант
struct book
{
char *name;
char *author;
char *publishing;
int vol;
int year;
};
void setValues (book &p, char *nm, char *auth, char *publ, int vlm, int yr)
{
p.name = new char[strlen(nm)+1];
strcpy(p.name,nm);
p.author = new char[strlen(auth)+1];
strcpy(p.author,auth);
p.publishing = new char[strlen(publ)+1];
strcpy(p.publishing,publ);
p.vol = vlm;
p.year = yr;
}
sort(p,n);
cout<<"The list of the books after sorting (by volume): "<<endl;
showAll(p,n);
char *a, *b;
cout<<"Enter a name of a book: ";
cin>>a;
putchar('\n');
cout<<"Enter an author's name: ";
cin>>b;
int x1,x2;
putchar('\n');
bool mark = false;
for (int i=0; i<n; i++)
{
// cout<<a<<'='<<p[i].name<<endl<<b<<"="<<p[i].author<<endl;
x1 = strcmp(a,p[i].name);
x2 = strcmp(b,p[i].author);
if (x1+x2==0)
{
cout<<"There is the book"<<endl;
cout<<p[i].name<<", "<<p[i].author<<", "<<p[i].publishing<<",
"<<p[i].vol<<", "<<p[i].year<<endl;
mark = true;
}
}
if (mark == false)
cout<<"There is no book"<<endl;
// search(p,n,a,b);
putchar('\n');
// freeMem(p,n);
// showAll(p,n);
return 0;
//Пункт Б
#include <iostream>
#include <algorithm>
using namespace std;
struct vector
{
int *a;
int n;
};
int main()
{
vector v;
int t,k,p;
bool cmp=false;
int option;
choice:
cout<<"1. Initialize the vector\n2. Delete the vector\n3. Change the size of the
vector\n4. Access a vector element\n5. Calculate the sum of the vector elements\n6.
Compare two vectors\n7. Exit\n";
while(1)
{
cin>>option;
switch (option)
{
case 1:
init(&v,5);
cout<<"Enter the vector elements"<<endl;
for(int i=0; i<5; i++)
{
cin>>v.a[i];
}
goto choice;
break;
case 2:
del(&v);
goto choice;
break;
case 3:
cout<<"Enter a new vector size"<<endl;
cin>>t;
change_size(&v,t);
cout<<"Enter the new vector elements"<<endl;
for(int i=0; i<t; i++)
{
cin>>v.a[i];
}
goto choice;
break;
case 4:
cout<<"Enter the position number of the desired element to access
it"<<endl;
cin>>k;
cout<<access(&v,k-1)<<endl;
goto choice;
break;
case 5:
cout<<"The sum of the even vector elements is "<<evenSum(&v)<<endl;
goto choice;
break;
case 6:
cout<<"Enter a number of elements of an another vector to
compare"<<endl;
cin>>p;
int *m;
m = new int[p];
cout<<"Enter elements of the vector to compare"<<endl;
for(int i=0; i<p; i++)
{
cin>>m[i];
}
for (int i=0; i<p; i++)
{
if (v.n == p)
{
if (v.a[i] != m[i])
{
cmp=true;
cout<<"The vectors are different"<<endl;
break;
}
}
else
{
cmp=true;
cout<<"The vectors are different"<<endl;
break;
}
}
if (cmp==false)
{
cout<<"The vectors are the same"<<endl;
}
goto choice;
break;
case 7:
exit(0);
default:
goto choice;
}
}
return 0;
}
Полученные результаты :
Пункт А
Пункт Б