Advertisement
sashachca

Untitled

Nov 19th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Vector
  7. {
  8. private:
  9.     int amount;                 //Количество координат
  10.     int coordinates [100];      //Координаты
  11.    
  12. public:
  13.     Vector ()  //Конструктор
  14.     {
  15.     }
  16.     ~Vector ()  //Деструктор
  17.     {
  18.     }
  19.     void SetAmount (int n) //Методы для координат и необходимого количества
  20.     {
  21.         amount = n;
  22.     }
  23.     // void SetCoordinates (int value, int i)
  24.     // {
  25.     //     coordinates [i] = value;
  26.     // }
  27.     int& operator[](int i) {
  28.         return coordinates[i];
  29.     }
  30.    
  31.     int GetAmount () const
  32.     {
  33.         return amount;
  34.     }
  35.     int GetCoordinates (int i) const  //Координаты
  36.     {
  37.         return coordinates [i];
  38.     }
  39.     const Vector operator+ (const Vector& v)
  40.     {
  41.         int  i;
  42.        
  43.         Vector tmp=v;
  44.        
  45.         for (i=0; i<this->amount; i++)
  46.            
  47.             tmp.coordinates [i] = coordinates[i]+v.GetCoordinates(i);
  48.         return tmp;
  49.     }
  50.    
  51.    
  52.     const Vector operator- (const Vector& v)
  53.     {
  54.         int  i;
  55.        
  56.         Vector tmp=v;
  57.        
  58.         for (i=0; i<this->amount; i++)
  59.            
  60.             tmp.coordinates [i] = coordinates[i]-v.GetCoordinates(i);
  61.         return tmp;
  62.     }
  63.    
  64.     bool operator== (const Vector& v)
  65.     {
  66.         int flag = 0;
  67.         for (int i=0; i<this->amount; i++){
  68.            
  69.             if(this->coordinates[i] == v.coordinates[i]) {}
  70.             else
  71.                 flag = 1;
  72.         }
  73.         if (flag != 0)
  74.             return false;
  75.        
  76.         return true;
  77.     }
  78.    
  79.     bool operator!= (const Vector& v)
  80.     {
  81.         int flag = 0;
  82.         for (int i=0; i<amount; i++){
  83.            
  84.             if(coordinates[i] != v.coordinates[i]) {
  85.                 flag = 1;
  86.             }
  87.         }
  88.         if (flag == 0)
  89.             return false;
  90.        
  91.         return true;
  92.     }
  93.     double operator* (const Vector& v)
  94.     {
  95.         int  i;
  96.        
  97.         Vector tmp=v;
  98.         double m=0;
  99.        
  100.         for (i=0; i<this->amount; i++){
  101.            
  102.             tmp.coordinates [i] = coordinates[i]*v.GetCoordinates(i);
  103.             m=m+tmp.coordinates[i];
  104.            
  105.         }
  106.         //m=sqrt(m);
  107.         return m;
  108.     }
  109. };
  110.  
  111. ostream& operator<< (ostream& out , Vector& v) {
  112.     for(int i = 0; i < v.GetAmount(); i++) {
  113.         out << v[i] << " ";
  114.     }
  115.     return out;
  116. }
  117.  
  118. istream& operator>> (istream& in, Vector& v) {
  119.     for(int i = 0; i < v.GetAmount(); i++) {
  120.         in >> v[i];
  121.     }
  122.     return in;
  123. }
  124.  
  125. int main ()
  126. {
  127.     setlocale (LC_ALL,"rus");
  128.     Vector Vector1;
  129.     Vector Vector2;
  130.     Vector Vector3;
  131.     Vector Vector4;
  132.     Vector Vector5;
  133.    
  134.     int n;
  135.     int i;
  136.     int value;
  137.     cout << "Введите количество координат векторов:\n";
  138.     cin >> n;
  139.     Vector1.SetAmount (n);
  140.     cout << "Введите координаты вектора 1:\n";
  141.     cin >> Vector1;
  142.     Vector2.SetAmount (n);
  143.     cout << "Введите координаты вектора 2:\n";
  144.     cin >> Vector2;
  145.     Vector3=Vector1+Vector2;
  146.     Vector4=Vector1-Vector2;
  147.     cout << "\nВектор 1:\n" << Vector1 << "\n";
  148.     cout << "Вектор 2:\n" << Vector2 << "\n";
  149.     cout << "Вычитаем из вектора 1 вектор 2 = " << Vector4 << "\n";
  150.     cout << "Складываем вектор 1 с вектором 2 = " <<Vector3 << endl;
  151.     bool a = Vector2 == Vector1;
  152.     bool b = Vector2 != Vector1;
  153.     //cout<<a<<b;
  154.     double m=Vector1*Vector2;
  155.     cout << "Умножение векторов: " << m << endl;
  156.    
  157.     if (a == true && b == false)
  158.         cout << "Векторы равны" << endl;
  159.     else
  160.         cout << "Векторы неравны" << endl;
  161.     cout << b;
  162.    
  163.    
  164.    
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement