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

Вариант 5 контрольная ТРПОСУ

.doc
Скачиваний:
40
Добавлен:
01.04.2014
Размер:
75.26 Кб
Скачать

Вариант 5.

Задание №1.

Заданы два массива А(5) и В(5). В каждом из массивов найти среднее арифметическое всех элементов массивов. На печать вывести исходные массивы и найденные значения

. #include <iostream.h>

#include <stdlib.h>

#include <math.h>

void print(int *A1)

{ int x;

for (x=0;x<5;x++)

{

cout<<A1[x]<<" ";

}

cout<<endl;

}

void main(void)

{ int *A,*B;

int i,sum1,sum2;

double sr1, sr2;

A=new int[5];

B=new int[5];

sum1=0;

sum2=0;

for (i=0;i<5;i++)

{cout<<"Vvedite "<<i+1<<" element massiva A"<<endl;

cin>>A[i];

sum1=sum1+A[i];

}

for (i=0;i<5;i++)

{cout<<"Vvedite "<<i+1<<" element massiva B"<<endl;

cin>>B[i];

sum2=sum2+B[i];

}

sr1=sum1/5;

sr2=sum2/5;

cout<<endl;

cout<<"V massive A srednee ="<<sr1;

cout<<"\nV massive B srednee ="<<sr2<<endl;

cout<<"\nMassiv A"<<endl;

print(A);

cout<<"\nMassiv B"<<endl;

print(B);

delete[] A;

delete[] B;

}

Задание №2.

При х=-15.246, y=0.04642, z=2000.1, α=-182.036

#include <iostream.h>

#include <math.h>

#include <stdlib.h>

class X

{

double *x, *y, *z, *f;

public:

X (double x1, double y1, double z1);

~X();

friend void print(X& );

friend void run(X& );

};

X::X(double x1, double y1, double z1)

{

x = new double;

y = new double;

z = new double;

f = new double;

*X::x=x1;

*X::y=y1;

*X::z=z1;

*X::f=0;

}

X::~X()

{

double x, y, z, f;

}

void run(X& abc)

{ *abc.f=log(pow(*abc.y,(-sqrt(fabs(*abc.x)))))*(*abc.x-*abc.y/2)+pow(sin(atan(*abc.z)),2);

}

void print(X& abc)

{

cout<<*abc.x<<*abc.y<<*abc.z<<endl;

cout<<*abc.f<<endl;

}

void main()

{ X obj (-15.246, 0.04642, 2000.1);

run(obj);

print (obj);

}

Задание №3.

Длина L >5-и, то выделяется подстрока до первого пробела.

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <iostream.h>

void main()

{

char buffer[128],*p;

int i=0,l;

gets(buffer);

cout << "\n Ishodnaya Stroka: " << buffer << endl;

l=strlen(buffer);

if (l>5) {

cout << endl;

p = strtok (buffer," ");

p = strtok (NULL," ");

while (p)

{

cout << p<< endl;

p = strtok (NULL," ");

}}

else cout <<"Stroka nechetnaya" << endl;

getch();

}

Задание №4.

Найти значение (х1+х2)/у

#include <iostream.h>

#include <math.h>

class X

{

public:

X(int,int);

~X();

virtual void Set(int ,int);

virtual void Get(int&,int&);

protected:

int x1,x2;

};

X::X(int x1,int x2)

{

X::x1=x1;

X::x2=x2;

};

X::~X()

{

cout<<"Class X destroyed"<<endl;

};

void X::Set(int x1,int x2)

{

X::x1=x1;

X::x2=x2;

};

void X::Get(int& x1,int& x2)

{

x1=X::x1;

x2=X::x2;

};

class Y:public X

{

public:

Y(int,int,int);

~Y();

virtual void Set(int ,int,int);

virtual void Get(int&,int&,int&);

int Run();

private:

int y;

};

Y::Y(int x1,int x2,int y):X(x1,x2)

{

Y::y=y;

};

Y::~Y()

{

cout<<"Class Y Destroyed"<<endl;

};

void Y::Set(int x1,int x2,int y)

{

Y::y=y;

X::Set(x1,x2);

};

void Y::Get(int& x1,int& x2,int& y)

{

y=Y::y;

X::Get(x1,x2);

};

int Y::Run()

{

return (x1+x2)/y;

}

void main()

{

int a,b,c;

cout<<"Enter x1,x2,y"<<endl;

cin>>a>>b>>c;

Y *p=new Y(a,b,c);

p->Get(a,b,c);

cout<<"Function 1="<<p->Run()<<endl;

cout<<"Enter new x1,x2,y"<<endl;

cin>>a>>b>>c;

p->Set(a,b,c);

p->Get(a,b,c);

cout<<"Function 2="<<p->Run()<<endl;

delete p;

}

Задание №5.

Дано: число N и последовательность a1, a2, … aN Создать шаблон класса, порождающий динамические одномерные массивы с элементами различных типов (вещественные, целочисленные, символьные и т.д.). Тип данных и результат являются параметрами по отношению к классу, программа должна иметь методы инициализации, конструктор, деструктор, метод просмотра значений созданного массива, согласно заданному алгоритму.

(a1+a2), (a2+a3),… ,(aN-1+aN);

#include <iostream.h>

#include<math.h>

template <class Type> class Tarr {

Type* ad;

int size;

public:

Tarr(int);

~Tarr();

Type* Run();

void Set(Type a,int in);

Type Get(int in);

};

template<class Type> Tarr<Type>::Tarr(int n){

size=n;

ad=new Type[n];

};

template<class Type> Tarr<Type>::~Tarr()

{

delete[] ad;

cout<<"Klass Likvidirovan"<<endl;

};

template<class Type> Type* Tarr<Type>::Run(){

Type *result=new Type[size];

result[0]=abs(ad[0]);

for (int i=1;i<size;i++)

result[i]=abs(result[i-1]+ad[i]);

return result;

};

template<class Type> void Tarr<Type>::Set(Type a,int in){

ad[in]=a;

};

template<class Type> Type Tarr<Type>::Get(int in){

return ad[in];

};

int main(){

int n;

cout<<"Vvedite N"<<endl;

cin>>n;

Tarr<int> aray(n);

Tarr<double> arayD(n);

Tarr<char> arayC(n);

cout<<endl<<"Vvedite Massiv Celyh - "<<n<<" Elementov"<<endl;

for (int i=0;i<n;i++){

int t;

cin>>t;

aray.Set(t,i);

}

cout<<endl<<" Massiv:"<<endl;

for (i=0;i<n;i++)

cout<<" "<<aray.Get(i);

cout<<endl;

int *tt=aray.Run();

cout<<endl<<" Rezul'tat:"<<endl;

for (i=0;i<n;i++)

cout<<" "<<tt[i];

cout<<endl;

cout<<endl<<"Vvedite Massiv Drobnyh - "<<n<<" Elementov"<<endl;

for (i=0;i<n;i++){

double t;

cin>>t;

arayD.Set(t,i);

}

cout<<endl<<" Massiv:"<<endl;

for (i=0;i<n;i++)

cout<<" "<<arayD.Get(i);

cout<<endl;

double *tD=arayD.Run();

cout<<endl<<" Rezul'tat:"<<endl;

for (i=0;i<n;i++)

cout<<" "<<tD[i];

cout<<endl;

cout<<endl<<"Vvedite Massiv simvolov iz - "<<n<<" Elementov"<<endl;

for (i=0;i<n;i++){

char t;

cin>>t;

arayC.Set(t,i);

}

cout<<endl<<" Massiv:"<<endl;

for (i=0;i<n;i++)

cout<<" "<<arayC.Get(i);

cout<<endl;

char *tC=arayC.Run();

cout<<endl<<" Rezul'tat:"<<endl;

for (i=0;i<n;i++)

cout<<" "<<tC[i];

cout<<endl;

return 0;

}