Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <stdarg.h>
- using namespace std;
- class Vector;
- bool kollin2 (Vector &j, Vector &m);
- class Vector
- {
- private:
- int amount; //Количество координат
- double coordinates [100]; //Координаты
- public:
- void SetAmount (int n) //Методы для координат и необходимого количества
- {
- amount = n;
- }
- void SetCoordinates (double value, int i)
- {
- coordinates [i] = value;
- }
- double& operator[](int i) {
- return coordinates[i];
- }
- int GetAmount () const
- {
- return amount;
- }
- int GetCoordinates (int i) const //Координаты
- {
- return coordinates [i];
- }
- const Vector operator+ (const Vector& v)
- {
- int i;
- Vector tmp=v;
- for (i=0; i<this->amount; i++)
- tmp.coordinates [i] = coordinates[i]+v.GetCoordinates(i);
- return tmp;
- }
- const Vector operator- (const Vector& v)
- {
- int i;
- Vector tmp=v;
- for (i=0; i<this->amount; i++)
- tmp.coordinates [i] = coordinates[i]-v.GetCoordinates(i);
- return tmp;
- }
- friend Vector operator- (const Vector& v) {
- Vector tmp = v;
- for (int i=0; i<v.amount; i++)
- tmp.coordinates [i] = -v.coordinates[i];
- return tmp;
- }
- friend Vector operator+ (const Vector& v) {
- Vector tmp = v;
- for (int i=0; i<v.amount; i++)
- tmp.coordinates [i] = +v.coordinates[i];
- return tmp;
- }
- bool operator== (const Vector& v)
- {
- int flag = 0;
- for (int i=0; i<this->amount; i++){
- if(this->coordinates[i] == v.coordinates[i]) {}
- else
- flag = 1;
- }
- if (flag != 0)
- return false;
- return true;
- }
- bool operator!= (const Vector& v)
- {
- int flag = 0;
- for (int i=0; i<amount; i++){
- if(coordinates[i] != v.coordinates[i]) {
- flag = 1;
- }
- }
- if (flag == 0)
- return false;
- return true;
- }
- double operator* (const Vector& v)
- {
- int i;
- Vector tmp=v;
- double m=0;
- for (i=0; i<this->amount; i++){
- tmp.coordinates [i] = coordinates[i]*v.GetCoordinates(i);
- m=m+tmp.coordinates[i];};
- return m;
- }
- const Vector operator/ (const Vector& v)
- {
- Vector tmp=v;
- double prom=1;
- for (int i=0; i<this->amount; i++){
- if(coordinates[i]!=0&&v.GetCoordinates(i)!=0) prom=tmp.coordinates[i];}
- for (int i=0; i<this->amount; i++){
- if(coordinates[i]==0&&v.GetCoordinates(i)==0) tmp.coordinates[i]=prom;
- else {tmp.coordinates [i] = coordinates[i]/v.GetCoordinates(i);
- prom=tmp.coordinates[i];}}
- return tmp;
- }
- bool dioph_eq(double a, double b, double c) {
- return true;
- }
- bool ifCollinear (const Vector& v) {
- Vector tmp = *this/v;
- int cdm=tmp.GetAmount()-1;
- for (int i=0; i<cdm; i++){
- if (tmp.GetCoordinates(i)!=tmp.GetCoordinates(i+1)) return false;
- }
- return true;
- }
- int GCD(int a, int b) {
- while(a != b) {
- if(a > b)
- a -= b;
- else
- a += b;
- }
- return a;
- }
- bool ifCoplanar(Vector& v2, Vector& v3) {
- int flag, c, gcdValue;
- flag = 0;
- for (int i=0; i<amount; i++) {
- gcdValue = GCD(v2.coordinates[i], v3.coordinates[i]);
- c = coordinates[i];
- if (gcdValue % c != 0)
- flag = 1;
- }
- if (flag == 0)
- return true;
- return false;
- }
- };
- ostream& operator <<(ostream& out , Vector& v) {
- for(int i = 0; i < v.GetAmount(); i++) {
- out << v[i] << " ";
- }
- return out;
- }
- istream& operator >>(istream& in, Vector& v) {
- for(int i = 0; i < v.GetAmount(); i++) {
- in >> v[i];
- }
- return in;
- }
- bool kollinsys (int n, ...){
- va_list args;
- va_start(args, n);
- Vector v = va_arg(args, Vector);
- for(int i = 0; i < n-1; i++) {
- Vector u = va_arg(args, Vector);
- if(!u.ifCollinear(v)) return false;
- }
- va_end(args);
- return true;
- }
- int main ()
- {
- setlocale (LC_ALL,"rus");
- Vector Vector1;
- Vector Vector2;
- Vector Vector3;
- Vector Vector4;
- Vector Vector5;
- int n;
- int i;
- double value;
- cout << "Введите количество координат векторов:\n";
- cin >> n;
- Vector1.SetAmount (n);
- cout << "Введите координаты вектора 1:\n";
- cin >> Vector1;
- Vector2.SetAmount (n);
- cout << "Введите координаты вектора 2:\n";
- cin >> Vector2;
- Vector3.SetAmount (n);
- cout << "Введите координаты вектора 3:\n";
- cin >> Vector3;
- Vector sumVector;
- Vector resVector;
- sumVector=Vector1+Vector2;
- resVector=Vector1-Vector2;
- cout << "\nВектор 1:\n" << Vector1 << "\n";
- cout << "Вектор 2:\n" << Vector2 << "\n";
- cout << "Вектор 3:\n" << Vector3 << "\n\n";
- cout<<"Вычитаем из вектора 1 вектор 2 = "<<resVector<<"\n";
- cout<<"Складываем вектор 1 с вектором 2 = "<<sumVector<<"\n";
- double m=Vector1*Vector2;
- cout<< "Скалярное произведение векторов 1 и 2: " <<m;
- Vector4 = -Vector1;
- cout<< "\nОтрицательный вектор 1: " << Vector4 << endl;
- bool a, b;
- a=Vector2==Vector1;
- b=Vector2!=Vector1;
- if (a==true && b==false)
- cout<<"Векторы 1 и 2 равны\n";
- else
- cout<<"Векторы 1 и 2 неравны\n";
- cout << "\nВектор 1:\n" << Vector1 << "\n";
- cout << "Вектор 2:\n" << Vector2 << "\n";
- bool coll1=Vector1.ifCollinear(Vector2);
- bool kollinbg=kollinsys(3,Vector1,Vector2,Vector3);
- if (coll1 == true)
- cout<<"Векторы 1 и 2 коллинеарны\n";
- else
- cout<<"Векторы 1 и 2 неколлинеарны\n";
- bool cop = Vector1.ifCoplanar(Vector2, Vector3);
- if (cop == true)
- cout<< "Векторы 1, 2 и 3 компланарны\n";
- else
- cout<< "Векторы 1, 2 и 3 некомпланарны\n";
- cout<<"Проверка на коллинеарность системы из векторов 1, 2, 3 = "<<kollinbg<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement