Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. vectors = []
  2. vectorCounter = -1
  3. class vector:
  4. def __init__(self, x, y, z):
  5. self.x = x
  6. self.y = y
  7. self.z = z
  8. def createVector():
  9. global vectorCounter
  10. vectorCounter +=1
  11. x = int(input('Введите x: '))
  12. y = int(input('Введите y: '))
  13. z = int(input('Введите z: '))
  14. vectors.append(vector(x,y,z))
  15. def showVectors():
  16. for i in range(len(vectors)):
  17. print('Vector{}({}, {}, {})'.format(i,vectors[i].x,vectors[i].y,vectors[i].z))
  18. def summ(vector1,vector2):
  19. global vectorCounter
  20. vectorCounter +=1
  21. x = vectors[vector1].x + vectors[vector2].x
  22. y = vectors[vector1].y + vectors[vector2].y
  23. z = vectors[vector1].z + vectors[vector2].z
  24. vectors.append(vector(x,y,z))
  25. print('Полученный вектор: Vector{}({}, {}, {})'.format(vectorCounter,vectors[vectorCounter].x,vectors[vectorCounter].y,vectors[vectorCounter].z))
  26. def subs(vector1,vector2):
  27. global vectorCounter
  28. vectorCounter +=1
  29. x = vectors[vector1].x - vectors[vector2].x
  30. y = vectors[vector1].y - vectors[vector2].y
  31. z = vectors[vector1].z - vectors[vector2].z
  32. vectors.append(vector(x,y,z))
  33. print('Полученный вектор: Vector{}({}, {}, {})'.format(vectorCounter,vectors[vectorCounter].x,vectors[vectorCounter].y,vectors[vectorCounter].z))
  34. def scalar(vector1,vector2):
  35. result = (vectors[vector1].x * vectors[vector2].x) + (vectors[vector1].y * vectors[vector2].y) + (vectors[vector1].z * vectors[vector2].z)
  36. return result
  37. def module(vectorNumber):
  38. result = ((vectors[vectorNumber].x)**2 + (vectors[vectorNumber].y)**2 + (vectors[vectorNumber].z)**2)**(1/2)
  39. return result
  40. def cos(vector1,vector2):
  41. scal = scalar(vector1,vector2)
  42. result = scal/(module(vector1) * module(vector2))
  43. return result
  44. def menu():
  45. print('========= Меню =========')
  46. print('1 - Создать вектор')
  47. print('2 - Показать все векторы')
  48. print('3 - Сложение векторов')
  49. print('4 - Вычитание векторов')
  50. print('5 - Скалярное произведение векторов')
  51. print('6 - Длина вектора')
  52. print('7 - Косинус угла между векторами')
  53. print('========================')
  54. key = int(input('Введите номер пункта меню: '))
  55. if key == 1:
  56. createVector()
  57. menu()
  58. elif key == 2:
  59. showVectors()
  60. menu()
  61. elif key == 3:
  62. vector1 = int(input('Введите номер первого вектора: '))
  63. vector2 = int(input('Введите номер второго вектора: '))
  64. summ(vector1,vector2)
  65. menu()
  66. elif key == 4:
  67. vector1 = int(input('Введите номер первого вектора: '))
  68. vector2 = int(input('Введите номер второго вектора: '))
  69. subs(vector1,vector2)
  70. menu()
  71. elif key == 5:
  72. vector1 = int(input('Введите номер первого вектора: '))
  73. vector2 = int(input('Введите номер второго вектора: '))
  74. result = scalar(vector1,vector2)
  75. print('Результат скалярного произведения:', result)
  76. menu()
  77. elif key == 6:
  78. vectorNumber = int(input('Введите номер вектора: '))
  79. result = module(vectorNumber)
  80. print('Модуль вектора:', result)
  81. menu()
  82. elif key == 7:
  83. vector1 = int(input('Введите номер первого вектора: '))
  84. vector2 = int(input('Введите номер второго вектора: '))
  85. result = round(cos(vector1,vector2), 5)
  86. print('Косинус угла между векторами:', result)
  87. menu()
  88. else:
  89. menu()
  90. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement