Callback

Simple Vector

    A. First Edition

    This is really a trivial practice for Vector which I find so difficult to memorize. So, I decide to write a simple

    code to help myself to memorize them.

    B.The problem

What is the basic operation of Vector?

C.The idea of program
¡¡
¡¡
There is only one point worth mentioning: The undetermined argument: .... I wonder if there are many people who
¡¡
are familiar of this kind of argument such that you may input arbitrary number of arguments.
¡¡

    D.The major functions

    ¡¡

    E.Further improvement

    F.File listing

    project client:

    1. vector.h

    2. vector.cpp

    3. main.cpp

    ¡¡

    file name: vector.h

    #include <stdio.h>
    #include <stdlib.h>

    class Vector
    {
    	friend Vector operator *(double scalar, const Vector& v);
    private:
    	int degree;
    	double* tuples;
    	//this is stupid way for passing result in operator +
    	//static Vector result;
    public:
    	void set(double num1,...);
    	//Vector(int defaultDegree=3, ...);
    	Vector(int defaultDegree=3);
    	~Vector();
    	void display();
    	double norm() const;
    	Vector unit();
    	bool orthogonal(const Vector& other);
    	double cos(const Vector& other);
    	Vector project(const Vector& other);
    	double distance(const Vector& other);
    	Vector operator+(const Vector& other);
    	Vector operator-(const Vector& other);
    	const Vector& operator=(const Vector& other);
    	const Vector& operator*(double scalar);
    	double operator*(const Vector& other);
    };

    file name: vector.cpp

    #include "vector.h"
    #include <stdarg.h>
    #include <math.h>

    Vector operator *(double scalar, const Vector& v)
    {
    	Vector result(v.degree);
    	for (int i=0; i<result.degree; i++)
    	{
    		result.tuples[i]=v.tuples[i]*scalar;
    	}
    	return result;
    }

    //Vector Vector::result;

    Vector::~Vector()
    {
    	//delete []tuples;
    	//tuples=NULL;
    }

    Vector::Vector(int defaultDegree):degree(defaultDegree)
    {
    	tuples=new double[degree];
    }

    double Vector::norm() const
    {
    	double result=0;
    	for (int i=0; i<degree; i++)
    	{
    		result+=tuples[i]*tuples[i];
    	}
    	return sqrt(result);
    }

    Vector Vector::operator -(const Vector& other)
    {
    	if (degree!=other.degree)
    	{
    		printf("degree not same\n");
    		exit(1);
    	}
    	Vector result(degree);
    	for (int i=0; i<degree; i++)
    	{
    		result.tuples[i]=tuples[i]-other.tuples[i];
    	}
    	return result;
    }

    Vector Vector::project(const Vector& other)
    {
    	return cos(other) * other;
    }

    double Vector::distance(const Vector& other)
    {
    	return (this->operator -(other)).norm();
    }

    double Vector::cos(const Vector& other)
    {
    	return (this->operator *(other))/(norm()*other.norm());
    }

    bool Vector::orthogonal(const Vector& other)
    {
    	return this->operator *(other)==0;
    }

    Vector Vector::unit()
    {
    	return this->operator *(1/this->norm());
    }

    void Vector::set(double num1, ...)
    {
    	double* ptr=&num1;

    	for (int i=0; i<degree; i++)
    	{
    		tuples[i]=*ptr;
    		ptr++;
    	}

    }

    void Vector::display()
    {
    	printf("(");
    	for (int i=0; i<degree-1; i++)
    	{
    		printf("%.3f,", tuples[i]);
    	}
    	printf("%.3f)\n", tuples[degree-1]);
    }

    /*
    Vector::Vector(int defaultDegree, ...):degree(defaultDegree)
    {
    	char* ptr=(char*)&defaultDegree;
    	tuples=new double[degree];
    	//double d=num1;
    	//va_list marker;

    	//va_start( marker, num1 );
    	ptr+=sizeof(int);
    	for (int i=0; i<degree; i++)
    	{
    		//printf("num %d: %e\n", i, *(double*)(ptr));
    		tuples[i]=*(double*)(ptr);
    		ptr+=sizeof(double);
    	}
    }
    */

    Vector Vector::operator +(const Vector& other)
    {
    	if (degree!=other.degree)
    	{
    		printf("degree not same\n");
    		exit(1);
    	}
    	Vector result(degree);
    	for (int i=0; i<degree; i++)
    	{
    		result.tuples[i]=tuples[i]+other.tuples[i];
    	}
    	return result;
    }

    const Vector& Vector::operator =(const Vector& other)
    {
    	degree=other.degree;
    	for (int i=0; i<degree; i++)
    	{
    		tuples[i]=other.tuples[i];
    	}
    	return *this;
    }

    const Vector& Vector::operator *(double scalar)
    {
    	for (int i=0; i<degree; i++)
    	{
    		tuples[i]*=scalar;
    	}
    	return *this;
    }

    double Vector::operator *(const Vector& other)
    {
    	if (degree!=other.degree)
    	{
    		printf("degree not same!\n");
    		exit(1);
    	}
    	double result=0;
    	for (int i=0; i<degree; i++)
    	{
    		result+=tuples[i]*other.tuples[i];
    	}
    	return result;
    }

    file name: main.cpp

    #include "vector.h"

    int main()
    {
    	Vector v;
    	v.set(3.4, 5.6, 8.0);
    	v.display();
    	Vector u;
    	u=v;

    	u.display();
    	u=u+v;
    	u.display();
    	printf("result of scalar 5.3: %.3f\n", u*v);
    	printf("u.cos(v)=%.3f\n", u.cos(v));
    	return 0;
    }

    What is the result?

(3.400,5.600,8.000)
(3.400,5.600,8.000)
(6.800,11.200,16.000)
result of scalar 5.3: 213.840
u.cos(v)=1.000
Press any key to continue

    ¡¡