#include <iostream>
#include "vector.h"

Vector& Vector::operator+=(const Vector& rhs)
{
	x+=rhs.x;
	y+=rhs.y;
	z+=rhs.z;
	return *this;
}

Vector& Vector::operator-=(const Vector& rhs)
{
	x-=rhs.x;
	y-=rhs.y;
	z-=rhs.z;
	return *this;
}
void Vector::operator=(const Vector& rhs)
{x=rhs.x;y=rhs.y;z=rhs.z;}

Vector operator+(const Vector& lhs,const Vector& rhs)
{
	Vector result=lhs;
	return result+=rhs;
}
Vector operator-(const Vector& lhs,const Vector& rhs)
{
	Vector result=lhs;
	return result-=rhs;
}
float operator*(const Vector& lhs,const Vector& rhs)
{
	return lhs.x*rhs.x+lhs.y*rhs.y+lhs.z*rhs.z;
}


Vector operator%(const Vector& lhs,const Vector& rhs)
{
	Vector result;
	result.x=lhs.y*rhs.z-lhs.z*rhs.y;
    result.y=lhs.z*rhs.x-lhs.x*rhs.z;
	result.z=lhs.x*rhs.y-lhs.y*rhs.x;
	return result;
}
Vector& Vector::operator%=(const Vector& rhs)
{   
	Vector result;
	result.x=y*rhs.z-z*rhs.y;
    result.y=z*rhs.x-x*rhs.z;
	result.z=x*rhs.y-y*rhs.x;
	x=result.x;y=result.y;z=result.z;
	return *this;
}
bool Vector::operator==(const Vector& rhs)
{
	return x==rhs.x&&y==rhs.y&&z==rhs.z;
}


std::ostream& operator<<(std::ostream& os,const Vector& hs)
{
	os<<'(';
	os << hs.x;
	os <<',';
	os<<hs.y;
	os<<',';
	os<<hs.z;
	os<<')';
	return os;
}

std::istream& operator>>(std::istream& in,Vector& rhs)
{
	char c;
    for(;;)
	{	
		in >>c;
	    if(!in) return in;
        if(c=='(')
		{
		
			in >>rhs.x;
		    if(!in) return in;
		    in>>c;
		    if(!in)return in;
		    if(c!=',')
			{
			    in.putback(c);
                in.clear(std::ios::failbit);
			    return in;
			}
		    in>>rhs.y;
		    if(!in)return in;
            in>>c;
		    if(!in)return in;
		    if(c!=',')
			{
			    in.putback(c);
                in.clear(std::ios::failbit);
			    return in;
			}
		    in>>rhs.z;
		    if(!in)return in;
		    in>>c;
		    if(!in)return in;
		    if(c!=')')
			{
			    in.putback(c);
			    in.clear(std::ios::failbit);
			    return in;
			}
		    return in;
		}
	    in.putback(c);
	    in.clear(std::ios::failbit);
	    return in;
	}
}
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
///test.cpp

#include <iostream>
#include "vector.h"

Vector read_number(std::istream is)
{
	Vector value;
	while(!(is>>value))
	{
		std::cerr<<"Bad input format -- try again"<<std::endl;
		is.clear(0);
		is.ignore(255,'\n');
		is.clear(0);
	}
	return value;
}

int main()
{
	Vector v1,v2,v3;float f;bool b;

	
	for(;;)
	{
		std::cout<<"Enter a pair of Vector numbers:"<<std::endl;
	    std::cout<<"Enter all zeros to quit"<<std::endl;
		v1=read_number(std::cin);
		v2=read_number(std::cin);
		if(v1==Vector(0,0,0)&&v2==Vector(0,0,0))break;
		v3=v1+v2;
		std::cout << v1 << "+" << v2 <<"==="<<v3<<std::endl;
        v3=v1-v2;
		std::cout<<v1<<"-"<<v2<<"==="<<v3<<std::endl;
        f=v1*v2;
		std::cout<<v1<<"*"<<v2<<"==="<<f<<std::endl;
        v3=v1%v2;
		std::cout<<v1<<"%"<<v2<<"==="<<v3<<std::endl;
        v3=v1;
		v3+=v2;
		std::cout<<v1<<"+="<<v2<<"==="<<v3<<std::endl;
        v3=v1;
		v3-=v2;
		std::cout<<v1<<"-="<<v2<<"==="<<v3<<std::endl;
        v3=v1;
		v3%=v2;
		std::cout<<v1<<"%="<<v2<<"==="<<v3<<std::endl;
		b=(v1==v2);
		std::cout<<"("<<v1<<"=="<<v2<<")"<<"==="<<b<<std::endl;

	
	}
	
    
	return 0;

}


