Mitrezzz

Vector

May 6th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. class MyVector{
  5. private:
  6.     int niza[15];
  7.     int broj;
  8. public:
  9.     MyVector(){
  10.         this->broj=0;
  11.     }
  12.     MyVector(int *niza,int broj){
  13.         this->broj=broj;
  14.         for(int i=0;i<broj;i++)
  15.         this->niza[i]=niza[i];
  16.     }
  17.     friend istream& operator>>(istream &i,MyVector &v){
  18.         i>>v.broj;
  19.         for(int k=0;k<v.broj;k++)
  20.             i>>v.niza[k];
  21.         return i;        
  22.     }
  23.     friend ostream& operator<<(ostream &o,MyVector &v){
  24.         o<<"<";int i;
  25.         for(i=0;i<v.broj-1;i++)
  26.             o<<v.niza[i]<<",";
  27.         o<<v.niza[i]<<">"<<endl;
  28.         return o;        
  29.     }
  30.     MyVector& operator-=(MyVector &v){
  31.     for(int i=0;i<broj;i++)
  32.         niza[i]-=v.niza[i];
  33.       return *this;
  34.     }
  35.     friend int operator*(MyVector &v1,MyVector &v2){
  36.     int suma=0;
  37.         MyVector rez;
  38.         rez.broj=v1.broj;
  39.         for(int i=0;i<v1.broj;i++)
  40.         suma+=v1.niza[i]*v2.niza[i];
  41.         return suma;
  42.     }
  43.     bool operator<=(MyVector &v){
  44.     int suma1=0,suma2=0;
  45.         for(int i=0;i<broj;i++)
  46.             suma1+=niza[i];
  47.         for(int i=0;i<v.broj;i++)
  48.             suma2+=v.niza[i];
  49.         if(suma1<=suma2)
  50.             return true;
  51.         else return false;
  52.     }
  53.     int& operator[](int index) {
  54.     int pom = -1;
  55.     if (index>=0&&index<broj)
  56.         return niza [index];
  57.     else {
  58.         cout << "Index out of bounds" << endl ;
  59.         return pom;
  60.       }
  61.     }
  62.     MyVector& operator--(){
  63.      for(int i=0;i<broj;i++)
  64.         if(niza[i]>0)
  65.          niza[i]--;
  66.          return *this;
  67.     }
  68.     MyVector operator++(int){
  69.         MyVector tmp(*this);
  70.         for(int i=0;i<broj;i++)
  71.         if(niza[i]<0)
  72.         niza[i]++;
  73.         return tmp;
  74.     }
  75. };
  76.  
  77. int main() {
  78. int n;
  79.  
  80. cin >> n;
  81. if (n == 1) {
  82.   cout << "Testiranje na operatorite >> i <<" << endl;
  83.   cout << "==============================\n";
  84.   MyVector a;
  85.   MyVector b;
  86.   cin >> a;
  87.   cin >> b;
  88.   cout << a;
  89.   cout << b;
  90. }
  91. else if (n == 2) {
  92.   cout << "Testiranje na operatorot -="<< endl;
  93.   cout << "==============================\n";
  94.   int m;
  95.   int niza[15];
  96.   cin >> m;
  97.   for (int i=0; i < m; ++i){
  98.     cin >> niza[i];
  99.   }
  100.   MyVector a(niza,m);
  101.   for (int i=0; i < m; ++i) {
  102.     cin >> niza[i];
  103.   }
  104.   MyVector b(niza,m);
  105.   a -= b;
  106.   cout << a;
  107. }
  108. else if (n == 3) {
  109.   cout << "Testiranje na operatorot *"<< endl;
  110.   cout << "==============================\n";
  111.   int m;
  112.   int niza[15];
  113.   cin >> m;
  114.   for (int i=0; i < m; ++i){
  115.     cin >> niza[i];
  116.   }
  117.   MyVector a(niza,m);
  118.   for (int i=0; i < m; ++i) {
  119.     cin >> niza[i];
  120.   }
  121.   MyVector b(niza,m);
  122.   cout << a * b;
  123. }
  124. else if (n == 4){
  125.   cout << "Testiranje na operatorot <="<< endl;
  126.   cout << "==============================\n";
  127.   int m;
  128.   int l;
  129.   int niza[15];
  130.   cin >> m;
  131.   for (int i=0; i < m; ++i){
  132.     cin >> niza[i];
  133.   }
  134.   MyVector a(niza,m);
  135.   cin >> l;
  136.   for (int i=0; i < l; ++i) {
  137.     cin >> niza[i];
  138.   }
  139.   MyVector b(niza,l);
  140.   cout << (a <= b);
  141. }
  142. else if (n == 5){
  143.   cout << "Testiranje na operatorot []"<< endl;
  144.   cout << "==============================\n";
  145.   int m;
  146.   int niza[15];
  147.   cin >> m;
  148.   for (int i=0; i < m; ++i){
  149.     cin >> niza[i];
  150.   }
  151.   MyVector a(niza,m);
  152.   for (int i=0; i < m; ++i) {
  153.     cin >> niza[i];
  154.   }
  155.   MyVector b(niza,m);
  156.   cout << a[2];
  157.   cout << endl;
  158.   cout << b[3];
  159. }
  160. else if (n == 6) {
  161.   cout << "Testiranje na operatorot --"<< endl;
  162.   cout << "==============================\n";
  163.   int m;
  164.   int niza[15];
  165.   cin >> m;
  166.   for (int i=0; i < m; ++i){
  167.     cin >> niza[i];
  168.   }
  169.   MyVector a(niza,m);
  170.   --a;
  171.   cout << a;
  172. }
  173. else if (n == 7) {
  174.   cout << "Testiranje na operatorot ++"<< endl;
  175.   cout << "==============================\n";
  176.   int m;
  177.   int niza[15];
  178.   cin >> m;
  179.   for (int i=0; i < m; ++i){
  180.     cin >> niza[i];
  181.   }
  182.   MyVector a(niza,m);
  183.   MyVector b = a++;
  184.   cout << a;
  185.   cout << b;
  186. }
  187.     return 0;
  188. }
Add Comment
Please, Sign In to add comment