Task: Vector

C: #include<stdio.h>
#include<math.h>

#define pi M_PI

typedef struct{
	double x,y;
}vector;

vector initVector(double r,double theta){
	vector c;
	
	c.x = r*cos(theta);
	c.y = r*sin(theta);
	
	return c;
}

vector addVector(vector a,vector b){
	vector c;
	
	c.x = a.x + b.x;
	c.y = a.y + b.y;
	
	return c;
}

vector subtractVector(vector a,vector b){
	vector c;
	
	c.x = a.x - b.x;
	c.y = a.y - b.y;
	
	return c;
}

vector multiplyVector(vector a,double b){
	vector c;
	
	c.x = b*a.x;
	c.y = b*a.y;
	
	return c;
}

vector divideVector(vector a,double b){
	vector c;
	
	c.x = a.x/b;
	c.y = a.y/b;
	
	return c;
}

void printVector(vector a){
	printf("%lf %c %c %lf %c",a.x,140,(a.y>=0)?'+':'-',(a.y>=0)?a.y:fabs(a.y),150);
}

int main()
{
	vector a = initVector(3,pi/6);
	vector b = initVector(5,2*pi/3);
	
	printf("\nVector a : ");
	printVector(a);
	
	printf("\n\nVector b : ");
	printVector(b);
	
	printf("\n\nSum of vectors a and b : ");
	printVector(addVector(a,b));
	
	printf("\n\nDifference of vectors a and b : ");
	printVector(subtractVector(a,b));
	
	printf("\n\nMultiplying vector a by 3 : ");
	printVector(multiplyVector(a,3));
	
	printf("\n\nDividing vector b by 2.5 : ");
	printVector(divideVector(b,2.5));
	
	return 0;
}










Fortran: MODULE ROSETTA_VECTOR
    IMPLICIT NONE

    TYPE VECTOR
        REAL :: X, Y
    END TYPE VECTOR


    INTERFACE OPERATOR(+)
       MODULE PROCEDURE VECTOR_ADD
    END INTERFACE

    INTERFACE OPERATOR(-)
       MODULE PROCEDURE VECTOR_SUB
    END INTERFACE

    INTERFACE OPERATOR(/)
       MODULE PROCEDURE VECTOR_DIV
    END INTERFACE

    INTERFACE OPERATOR(*)
       MODULE PROCEDURE VECTOR_MULT
    END INTERFACE

    CONTAINS

    FUNCTION VECTOR_ADD(VECTOR_1, VECTOR_2)
        TYPE(VECTOR), INTENT(IN) :: VECTOR_1, VECTOR_2
        TYPE(VECTOR) :: VECTOR_ADD
        VECTOR_ADD%X = VECTOR_1%X+VECTOR_2%X
        VECTOR_ADD%Y = VECTOR_1%Y+VECTOR_2%Y
    END FUNCTION VECTOR_ADD

    FUNCTION VECTOR_SUB(VECTOR_1, VECTOR_2)
        TYPE(VECTOR), INTENT(IN) :: VECTOR_1, VECTOR_2
        TYPE(VECTOR) :: VECTOR_SUB
        VECTOR_SUB%X = VECTOR_1%X-VECTOR_2%X
        VECTOR_SUB%Y = VECTOR_1%Y-VECTOR_2%Y
    END FUNCTION VECTOR_SUB

    FUNCTION VECTOR_DIV(VEC, SCALAR)
        TYPE(VECTOR), INTENT(IN) :: VEC
        REAL, INTENT(IN) :: SCALAR
        TYPE(VECTOR) :: VECTOR_DIV
        VECTOR_DIV%X = VEC%X/SCALAR
        VECTOR_DIV%Y = VEC%Y/SCALAR
    END FUNCTION VECTOR_DIV

    FUNCTION VECTOR_MULT(VEC, SCALAR)
        TYPE(VECTOR), INTENT(IN) :: VEC
        REAL, INTENT(IN) :: SCALAR
        TYPE(VECTOR) :: VECTOR_MULT
        VECTOR_MULT%X = VEC%X*SCALAR
        VECTOR_MULT%Y = VEC%Y*SCALAR
    END FUNCTION VECTOR_MULT

    FUNCTION FROM_RTHETA(R, THETA)
        REAL :: R, THETA
        TYPE(VECTOR) :: FROM_RTHETA
        FROM_RTHETA%X = R*SIN(THETA)
        FROM_RTHETA%Y = R*COS(THETA)
    END FUNCTION FROM_RTHETA

    FUNCTION FROM_XY(X, Y)
        REAL :: X, Y
        TYPE(VECTOR) :: FROM_XY
        FROM_XY%X = X
        FROM_XY%Y = Y
    END FUNCTION FROM_XY

    FUNCTION PRETTY_PRINT(VEC)
        TYPE(VECTOR), INTENT(IN) :: VEC
        CHARACTER(LEN=100) PRETTY_PRINT
        WRITE(PRETTY_PRINT,"(A, F0.5, A, F0.5, A)") "[", VEC%X, ", ", VEC%Y, "]"
    END FUNCTION PRETTY_PRINT
END MODULE ROSETTA_VECTOR

PROGRAM VECTOR_DEMO
    USE ROSETTA_VECTOR
    IMPLICIT NONE

    TYPE(VECTOR) :: VECTOR_1, VECTOR_2
    REAL, PARAMETER :: PI = 4*ATAN(1.0)
    REAL :: SCALAR

    SCALAR = 2.0

    VECTOR_1 = FROM_XY(2.0, 3.0)
    VECTOR_2 = FROM_RTHETA(2.0, PI/6.0)

    WRITE(*,*) "VECTOR_1 (X: 2.0, Y: 3.0)      : ", PRETTY_PRINT(VECTOR_1)
    WRITE(*,*) "VECTOR_2 (R: 2.0, THETA: PI/6) : ", PRETTY_PRINT(VECTOR_2)
    WRITE(*,*) NEW_LINE('A')
    WRITE(*,*) "VECTOR_1  +  VECTOR_2          = ", PRETTY_PRINT(VECTOR_1+VECTOR_2)
    WRITE(*,*) "VECTOR_1  -  VECTOR_2          = ", PRETTY_PRINT(VECTOR_1-VECTOR_2)
    WRITE(*,*) "VECTOR_1  /  2.0               = ", PRETTY_PRINT(VECTOR_1/SCALAR)
    WRITE(*,*) "VECTOR_1  *  2.0               = ", PRETTY_PRINT(VECTOR_1*SCALAR)
END PROGRAM VECTOR_DEMO