/* Title: /* Title: Vectors.c Author: Xiaoyang Yu Revised: 22-NOV-2002 Module Interface Specification: INTERFACE Vectors; TYPE vector; CONST zero: vector; PROCEDURE getvec(x,y: float): vector; PROCEDURE xval(v: vector): float; PROCEDURE yval(v: vector): float; PROCEDURE add(u,v: vector): vector; PROCEDURE mul(r: float; v: vector): vector; END Vectors. */ #include "stdio.h" #include "stdlib.h" #include "string.h" #include "vectors.h" struct rec{ float x,y; struct rec *next; }; vector head = NULL; /* return the pointer of vector ( x, y ) */ vector getvec(float x, float y) { vector before, after, v; before = NULL; after = head; /* find where to insert new vector */ while ( after != NULL && ((x > after->x) || (x == after->x && y > after->y)) ) { before = after; after = after->next; } /* vector already in list */ if ( after != NULL && (x == after->x && y == after->y) ) return after; /* create new vector */ v = (struct rec *)malloc(sizeof(struct rec)); v->x = x; v->y = y; v->next = after; /* update pointer in list */ if ( before != NULL ) before->next = v; else head = v; return v; } /* end getvec */ /* return xval of vector v */ float xval(vector v) { return v->x; } /* end xval */ /* return yval of vector v */ float yval(vector v) { return v->y; } /* end yval */ /* add vector u and vector v */ vector add(vector u,vector v) { float x,y; x = u->x + v->x; y = u->y + v->y; return getvec(x,y); } /* end add */ /* multiple r and vector v */ vector mul(float r,vector v) { float x,y; x = v->x * r; y = v->y * r; return getvec(x,y); } /* end mul */ /* show linklist:test only,delete later */ void showlist() { vector ptr; printf("\n*** the following is the ordered linklist ***\n\n"); ptr = head; while ( ptr != NULL ) { if( ptr->next != NULL ) printf("(%f,%f)-->",ptr->x,ptr->y); else printf("(%f,%f)\n",ptr->x,ptr->y); ptr = ptr->next; } } /* end showlist */ /* the end of vectors.c */