Risonna

programm languages lab2 zad2(vectors)

Oct 25th, 2020
800
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class vector
  8.  
  9. {
  10.  
  11.     int size;
  12.  
  13.     float* v;
  14.  
  15. public:
  16.  
  17.     vector(int n = 0);
  18.  
  19.     vector(const vector& copy);
  20.  
  21.     ~vector();
  22.  
  23.     void output();
  24.  
  25.     void input(int n);
  26.  
  27.     vector operator+ (const vector& a);
  28.  
  29.     float operator* (const vector& a);
  30.  
  31.     vector operator* (int n);
  32.  
  33.     bool operator== (vector& L);
  34.  
  35.     bool operator!= (vector& L);
  36.  
  37.     bool operator> (vector& L);
  38.  
  39.     bool operator< (vector& L);
  40.  
  41.     float lenght();
  42.  
  43.     friend ostream& operator<< (ostream& out, const vector& c);
  44.  
  45.     friend istream& operator>> (istream& in, vector& c);
  46.  
  47.     float agel(const vector& a);
  48.  
  49. };
  50.  
  51. vector::vector(int n)
  52.  
  53. {
  54.  
  55.     size = n;
  56.  
  57.     v = new float[n];
  58.  
  59. }
  60.  
  61. vector::vector(const vector& copy)
  62.  
  63. {
  64.  
  65.     size = copy.size;
  66.  
  67.     v = new float[size];
  68.  
  69.     for (int i = 0; i < size; i++)
  70.  
  71.         v[i] = copy.v[i];
  72.  
  73. }
  74.  
  75. vector::~vector()
  76.  
  77. {
  78.  
  79.     delete[]v;
  80.  
  81. }
  82.  
  83. void vector::output()
  84.  
  85. {
  86.  
  87.     cout << size << endl;
  88.  
  89.     for (int i = 0; i < size; i++) cout << v[i] << " ";
  90.  
  91. }
  92.  
  93. void vector::input(int n)
  94.  
  95. {
  96.  
  97.     size = n;
  98.  
  99.     v = new float[size];
  100.  
  101.     cout << "Enter vector value: " << endl;
  102.  
  103.     for (int i = 0; i < size; i++)
  104.  
  105.     {
  106.  
  107.         cin >> v[i];
  108.  
  109.     }
  110.  
  111. }
  112.  
  113. vector vector::operator+ (const vector& a)
  114.  
  115. {
  116.  
  117.     vector t(size);
  118.  
  119.     t.size = a.size;
  120.  
  121.  
  122.     for (int i = 0; i < size; i++)
  123.  
  124.         t.v[i] = v[i] + a.v[i];
  125.  
  126.     return t;
  127.  
  128. }
  129.  
  130. vector vector::operator*(int n)
  131.  
  132. {
  133.  
  134.     vector t;
  135.  
  136.     t.size = size;
  137.  
  138.     t.v = new float[size];
  139.  
  140.     for (int i = 0; i < size; i++)
  141.  
  142.     {
  143.  
  144.         t.v[i] = v[i] * n;
  145.  
  146.     }
  147.  
  148.     return t;
  149.  
  150. }
  151.  
  152. float vector::operator*(const vector& a)
  153.  
  154. {
  155.  
  156.     float vek = 0;
  157.  
  158.     for (int i = 0; i < size; i++)
  159.  
  160.     {
  161.  
  162.         vek = vek + (v[i] * a.v[i]);
  163.  
  164.     }
  165.  
  166.     return vek;
  167.  
  168. }
  169.  
  170. float vector::lenght()
  171.  
  172. {
  173.  
  174.     float t = 0;
  175.  
  176.     for (int i = 0; i < size; i++)
  177.  
  178.     {
  179.  
  180.         t = t + (v[i] * v[i]);
  181.  
  182.     }
  183.  
  184.     return t = sqrt(t);
  185.  
  186. }
  187.  
  188. bool vector::operator== (vector& L)
  189.  
  190. {
  191.  
  192.     return lenght() == L.lenght();
  193.  
  194. }
  195.  
  196. bool vector::operator!= (vector& L)
  197.  
  198. {
  199.  
  200.     return lenght() != L.lenght();
  201.  
  202. }
  203.  
  204. bool vector::operator> (vector& L)
  205.  
  206. {
  207.  
  208.     return lenght() > L.lenght();
  209.  
  210. }
  211.  
  212. bool vector::operator< (vector& L)
  213.  
  214. {
  215.  
  216.     return lenght() < L.lenght();
  217.  
  218. }
  219.  
  220. ostream& operator<< (ostream& out, const vector& c)
  221.  
  222. {
  223.  
  224.     out << c.size << endl;
  225.  
  226.     for (int i = 0; i < c.size; i++) out << c.v[i] << " ";
  227.  
  228.     return out;
  229.  
  230. }
  231.  
  232. istream& operator>> (istream& in, vector& c)
  233.  
  234. {
  235.  
  236.     in >> c.size;
  237.  
  238.     c.v = new float[c.size];
  239.  
  240.     cout << "Input vector value: " << endl;
  241.  
  242.     for (int i = 0; i < c.size; i++)
  243.  
  244.     {
  245.  
  246.         in >> c.v[i];
  247.  
  248.     }
  249.  
  250.     return in;
  251.  
  252. }
  253.  
  254. float vector::agel(const vector& a)
  255.  
  256. {
  257.  
  258.     float t = 0, r = 0;
  259.  
  260.     for (int i = 0; i < size; i++)
  261.  
  262.     {
  263.  
  264.         r = r + (v[i] * a.v[i]);
  265.  
  266.     }
  267.  
  268.     float q = 0, w = 0;
  269.  
  270.     for (int i = 0; i < size; i++)
  271.  
  272.     {
  273.  
  274.         q = q + (v[i] * v[i]);
  275.  
  276.     }
  277.  
  278.     q = sqrt(q);
  279.  
  280.     for (int i = 0; i < size; i++)
  281.  
  282.     {
  283.  
  284.         w = w + (a.v[i] * a.v[i]);
  285.  
  286.     }
  287.  
  288.     w = sqrt(w);
  289.  
  290.     t = cos(r / (q * w));
  291.  
  292.     return t;
  293.  
  294. }
  295.  
  296. int main()
  297.  
  298. {
  299.  
  300.     int n = 0, as = 0;
  301.  
  302.     cout << "Enter n:" << endl;
  303.  
  304.     cin >> n;
  305.  
  306.     cout << endl;
  307.  
  308.     vector a, c;
  309.  
  310.     a.input(n);
  311.  
  312.     c.input(n);
  313.  
  314.     cout << "Sum of 2 vectors: " << endl;
  315.  
  316.     a.vector::operator+(c).output();
  317.  
  318.     cout << endl;
  319.  
  320.     cout << "Enter value you want to multiple this vector to: " << endl;
  321.  
  322.     cin >> as;
  323.  
  324.     cout << "Multiplication result: " << endl;
  325.  
  326.     a.operator*(as).output();
  327.  
  328.     cout << endl;
  329.  
  330.     cout << "Result of multiplication vector to vector: " << endl;
  331.  
  332.     cout << a.operator*(c) << endl;
  333.  
  334.     cout << "Vector a's length: " << endl;
  335.  
  336.     cout << a.lenght() << endl;
  337.  
  338.     cout << "Vector c's length: " << endl;
  339.  
  340.     cout << c.lenght() << endl;
  341.  
  342.     cout << "==, !=, >, < :" << endl;
  343.  
  344.     cout << a.operator==(c) << endl;
  345.  
  346.     cout << a.operator!=(c) << endl;
  347.  
  348.     cout << a.operator>(c) << endl;
  349.  
  350.     cout << a.operator<(c) << endl;
  351.  
  352.     cout << endl;
  353.  
  354.     cout << a.agel(c) << endl;
  355.  
  356. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×