Advertisement
Guest User

Untitled

a guest
May 25th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Vector
  6. {
  7. private:
  8. int *m;
  9. int abscissa;
  10. int ordinata;
  11. public:
  12. Vector(int x, int y)
  13. {
  14. abscissa = x; ordinata = y;
  15. m = new int[2];
  16. };
  17. Vector operator +(Vector &v1);
  18. Vector operator -(Vector&v1);
  19. double operator *(Vector&v1);
  20. Vector operator *(double s);
  21. friend ostream& operator<< (ostream &out, const Vector &v);
  22. friend istream& operator>> (istream &in, const Vector &v);
  23. };
  24. ostream&operator<<(ostream &out, const Vector &v) {
  25. for (int i = 0; i < 2; i++)
  26. out << v.m[i] << endl;
  27. return out;
  28. };
  29. istream& operator>> (istream &in, const Vector &v) {
  30. for (int i = 0; i < 2; i++)
  31. in >> v.m[i];
  32. return in;
  33. };
  34. Vector Vector :: operator+(Vector&v1) {
  35. Vector tmp(abscissa, ordinata);
  36. for (int i = 0; i < 2; i++)
  37. tmp.m[i] = m[i] + v1.m[i];
  38.  
  39. return tmp;
  40. };
  41. Vector Vector::operator-(Vector&v1) {
  42. Vector tmp(abscissa, ordinata);
  43. for (int i = 0; i < 2; i++)
  44. tmp.m[i] = m[i] - v1.m[i];
  45.  
  46. return tmp;
  47. };
  48. double Vector::operator*(Vector&v1) {
  49. double k = 0;
  50. k = (m[0] * v1.m[0] - m[1] * v1.m[1]) + (m[0] * v1.m[1] + m[1] * v1.m[0]);
  51. return k;
  52. };
  53. Vector Vector::operator*(double s) {
  54. Vector tmp(abscissa, ordinata);
  55. for (int i = 0; i < 2; i++)
  56. tmp.m[i] = m[i] * s;
  57.  
  58. return tmp;
  59. }
  60.  
  61. int main()
  62. {
  63. setlocale(LC_ALL, "ru");
  64. int k;
  65. double d = 0;
  66. int s = 0, p = 0;
  67. int x = 0;
  68. int y = 0;
  69. Vector v1(x, y);
  70. Vector v2(x, y);
  71. Vector m(x, y);
  72. while (1)
  73. {
  74. cout << "\n\t Меню \t\n";
  75. cout << "\t 1 - Ввод координат вектора v1 \t\n";
  76. cout << "\t 2 - Ввод координат вектора v2 \t\n";
  77. cout << "\t 3 - Вывод координат на экран \t\n";
  78. cout << "\t 4 - Сложение векторов v1+v2 \t\n";
  79. cout << "\t 5 - Вычитание векторов v1 - v2 \t\n";
  80. cout << "\t 6 - Скалярное умножение векторов v1 * v2 \t\n";
  81. cout << "\t 7 - Умножение вектора v1 на скаляр \t\n";
  82. cout << "\t 8 - Умножение вектора v2 на скаляр \t\n";
  83. cout << "\t 0 - Выход \t\n";
  84. cin >> k;
  85. switch (k)
  86. {
  87. case 1: {
  88. system("cls");
  89. cin >> v1;
  90. cout << endl;
  91. break; };
  92. case 2: {
  93. system("cls");
  94. cin >> v2;
  95. cout << endl;
  96. break; };
  97. case 3: {
  98. system("cls");
  99. cout << v1 << endl;
  100. cout << v2 << endl;
  101. cout << endl;
  102. break; };
  103. case 4: {
  104. system("cls");
  105. m = v1.operator+(v2);
  106. cout << m;
  107. cout << endl;
  108. break; };
  109. case 5: {
  110. system("cls");
  111. m = v1.operator-(v2);
  112. cout << m;
  113. cout << endl;
  114. break; };
  115. case 6: {
  116. system("cls");
  117. d = v1.operator*(v2);
  118. cout << d;
  119. cout << endl;
  120. break; };
  121. case 7: {
  122. system("cls");
  123. cout << "Ведите скаляр" << endl;
  124. cin >> s;
  125. m = v1.operator*(s);
  126. cout <<endl<< m;
  127. cout << endl;
  128. break; };
  129. case 8: {
  130. system("cls");
  131. cout << "Ведите скаляр";
  132. cin >> s;
  133. m = v2.operator*(s);
  134. cout <<endl<< m;
  135. cout << endl;
  136. break; };
  137. case 0: {exit(0); }
  138. default: {
  139. system("cls");
  140. cout << "Введено недопустимое значение, нажмите на любую клавишу для продолжения \n";
  141. break; };
  142. };
  143. };
  144.  
  145. return 0;
  146. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement