лаба2 3й семестр
.docxСт. Новичонок М.С. Гр. ІНФ-15-1
Лабораторная работа №3
Тема: «Основы разработки классов в языке С++. Наследование, полиморфизм, инкапсуляция, исключения»
Вариант №6
//Создать программу, которая вводит c клавиатуры(с обработкой ошибок) двумерный массив целых чисел a,
//заполняет одномерный массив b :
//-суммами отрицательных элементов строк a, если отрицательных элементов в строке больше,
//чем положительных;
//-минимальным элементом строки, если отрицательных элементов меньше;
//-числом нулевых элементов в строке, если их поровну
//и выводит массив b на экран
//
#include <vector>
#include<iostream>
using namespace std;
using namespace std;
int main()
{
cout<<"Input your vector: ";
int n=3;
int m=4;
vector<vector<int> > stroka(n);
for (int i=0; i<n; i++)
{
vector<int> line(m);
stroka[i] = line;
for (int j=0; j<m; j++)
try{
cin >> stroka[i][j];
for (int i = 0; i < stroka.size(); ++i)
{
if ( ((stroka[i][j] <'0') || (stroka[i][j]>'9')) )
{
throw 'e';
}
} //закончился ввод последовательности
}
catch (const char s) { cout << "Error" <<endl; }
catch(...) {cout<<"Unknown error"<<endl;}
for (int i = 0; i < stroka.size(); i++)
{
for (int j = 0; j < stroka[i].size(); j++)
{
cout << stroka[i][j] << ' ';
} cout << endl;
}
vector<int> b(n);
for (int i = 0; i < stroka.size(); i++)
{int plus=0;
int minus=0;
for (int j = 0; j < stroka[i].size(); j++)
{
if(stroka[i][j]>0)
{plus+=1;}
if(stroka[i][j]<0)
{minus+=1;}
//первое условие
if(minus>plus)
{
int sum=0;
for (int j = 0; j < stroka[i].size(); j++)
{
if(stroka[i][j]<0)
{
sum+=stroka[i][j];
}
}b[i]=sum;
}
//второе условие
if(minus<plus)
{
int min=stroka[i][0];
for (int j = 0; j < stroka[i].size(); j++){
if(min>stroka[i][j])
{
min=stroka[i][j];
}
} b[i]=min;
}
//третье условие
if(minus==plus)
{ int null=0;
for (int j = 0; j < stroka[i].size(); j++){
if(stroka[i][j]==0)
{ null+=1;}
} b[i]=null;}
}//закончился цикл j
}//закончился цикл i
cout<<"Result:";
for (int i = 0; i<stroka.size(); i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}