/*=======================================================================*/ #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "SOLN" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "DEMO" #define REVISION (0) #define TITLE "Structures Demo Program" #define SUBTITLE "Last Modified on 03 DEC 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "pt3d.c" /*=======================================================================*/ /*=======================================================================*/ /* WAIVED COMPILER WARNINGS */ /*=======================================================================*/ /* Linker Warning: No module definition file specified: using defaults Reason for Waiver: Can't suppress. Does not have adverse impact. */ /*=======================================================================*/ /* NOTES TO THE USER/GRADER */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* PROBLEM STATEMENT */ /*=======================================================================*/ /* This program demonstrates the use of a simple structure coupled with the concept of primitive and utility functions. The main() function generates a sequence of points located on the unit circle and outputs them to a text file in the order they were generated. The data can then be imported into a spreadsheet and an x-y scatter plot used to show the resulting connect-the-dots figure. */ /*=======================================================================*/ /* DEVELOPMENT NOTES */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* DEVIATIONS FROM SUBMITTED PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /*=======================================================================*/ /* CODE SECTION */ /*=======================================================================*/ /*=======================================================================*/ /*== INCLUDE FILES ======================================================*/ #include /* stdout, putc(), printf(), scanf() */ /* FILE, fopen(), fread(), fwrite() */ /* SEEK_END, SEEK_SET, SEEK_CUR, ftell(), fseek() */ #include /* EXIT_SUCCESS, EXIT_FAILURE */ #include /* pow(), sqrt() */ /*== MACRO DEFINITIONS (OBJECT-LIKE) ====================================*/ #define FALSE (0) #define TRUE (!FALSE) #define PI (3.1415926) #define BLANKLINE printf("\n") #define MARGIN (2) /* Left Margin in PrintHeader() */ #define INDENT_SIZE (2) /* Indent relative to */ #define LMARG printc(' ', MARGIN) /* Print Left Margin */ #define INDENT printc(' ', INDENT_SIZE) /* Indent relative to margin */ #define EMPTYLOOP {} #define PTS (5) #define SKIP (2) /*== MACRO DEFINITIONS (FUNCTION-LIKE) ==================================*/ /*=======================================================================*/ /* STRUCTURES AND STRUCTURE FUNCTIONS */ /*=======================================================================*/ typedef struct pt3d PT3D; struct pt3d { int pt; double x, y, z; }; /*-----------------------------------------------------------------------*/ /* PT3D Primitive Functions */ /*-----------------------------------------------------------------------*/ int GetPT3D_pt(PT3D *p) { if (NULL == p) return -1; return p->pt; } int SetPT3D_pt(PT3D *p, int v) { if (NULL == p) return -1; p->pt = v; return GetPT3D_pt(p); } double GetPT3D_x(PT3D *p) { if (NULL == p) return -1.0; return p->x; } double SetPT3D_x(PT3D *p, double v) { if (NULL == p) return -1.0; p->x = v; return GetPT3D_x(p); } double GetPT3D_y(PT3D *p) { if (NULL == p) return -1.0; return p->y; } double SetPT3D_y(PT3D *p, double v) { if (NULL == p) return -1.0; p->y = v; return GetPT3D_y(p); } double GetPT3D_z(PT3D *p) { if (NULL == p) return -1.0; return p->z; } double SetPT3D_z(PT3D *p, double v) { if (NULL == p) return -1.0; p->z = v; return GetPT3D_z(p); } /*-----------------------------------------------------------------------*/ /* PT3D Utility Functions */ /*-----------------------------------------------------------------------*/ PT3D NewPoint(int pt, double x, double y, double z) { PT3D temp; SetPT3D_pt(&temp, pt); SetPT3D_x(&temp, x); SetPT3D_y(&temp, y); SetPT3D_z(&temp, z); return temp; } PT3D SubtractPT3D(PT3D ptA, PT3D ptB) { PT3D temp; SetPT3D_pt(&temp, 0); SetPT3D_x(&temp, GetPT3D_x(&ptA) - GetPT3D_x(&ptB)); SetPT3D_y(&temp, GetPT3D_y(&ptA) - GetPT3D_y(&ptB)); SetPT3D_z(&temp, GetPT3D_z(&ptA) - GetPT3D_z(&ptB)); return temp; } double GetPT3D_dimensionN(PT3D *p, int i) { if (NULL == p) return -1; switch (i) { case 0: return GetPT3D_x(p); /* break; */ case 1: return GetPT3D_y(p); /* break; */ case 2: return GetPT3D_z(p); /* break; */ } return -2; } double LengthPT3D(PT3D ptA) { double length; int i; for (i = 0, length = 0.0; i < 3; i++) length += pow(GetPT3D_dimensionN(&ptA, i),2); return sqrt(length); } void fprintPT3D(FILE *fp, PT3D pt) { int i; fprintf(fp, "%i", GetPT3D_pt(&pt)); for (i = 0; i <= 3; i++) fprintf(fp, ", %f", GetPT3D_dimensionN(&pt, i)); fprintf(fp, "\n"); } double DistanceBetweenPT3D(PT3D ptA, PT3D ptB) { double distance; distance = LengthPT3D(SubtractPT3D(ptA, ptB)); return distance; } /*=======================================================================*/ /* SUPPORT FUNCTIONS ( functions not called directly by main() ) */ /*=======================================================================*/ int printc(char c, int n) { while ( (n--) && (c == putc(c, stdout)) ); /* EMPTY LOOP */ return n; } /*=======================================================================*/ /* PRIMARY FUNCTIONS ( functions called directly by main() ) */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Support Functions) ================*/ int printc(char c, int n); /*== PRIMARY FUNCTIONS ==================================================*/ void PrintHeader(void) { LMARG; printc('=', (78 - MARGIN)); putc('\n', stdout); LMARG; printf("Course....... %s-%i (%s %i)\n",COURSE,SECTION,TERM,YEAR); LMARG; printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); LMARG; printf("Assignment... %s (Rev %i)", ASSIGNMENT, REVISION); LMARG; printf("(Source Code in %s)\n", FILENAME); LMARG; printf("Description.. %s\n", TITLE); LMARG; printf(" %s\n", SUBTITLE); LMARG; printc('=', (78 - MARGIN)); putc('\n', stdout); } /*=======================================================================*/ /* MAIN FUNCTION */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Primary Functions) ================*/ void PrintHeader(void); int main(void) { int i; PT3D pt[PTS+1]; double theta; FILE *fp; fp = fopen("points.txt", "wt"); if (NULL == fp) { printf("Error opening file for writing.\n"); return 1; } for (i = 0; i <= PTS; i++) { theta = SKIP * i * (2.0*PI/(double) PTS); pt[i] = NewPoint(i, cos(theta), sin(theta), 0.0); fprintPT3D(fp, pt[i]); } fclose(fp); return 0; }