Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer the Part Two Answer the Part Two using the code for Point2D code below #i

ID: 3849679 • Letter: A

Question

Answer the Part Two

Answer the Part Two using the code for Point2D code below

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

void mallocPoint2D();
void freePoint2D();

struct point2d
{
   float *tofrom;
   float distance;
}c;

void mallocPoint2D()
{
   FILE *fout;
   FILE *fin;
   FILE *f;
   char d;

   int i;

   //memory allocation thing
   c.tofrom=(float *)malloc(sizeof(float));

   printf(" Enter 4 coordinates as X1 Y1 X2 Y2 ");

   //tofrom[0] = x1, tofrom[1] = Y1, tofrom[2] = X2, tofrom[3] = Y2
   for(i = 0; i < 4; i++)
   {
       scanf("%f",&c.tofrom[i]);
   }
  
   c.distance = sqrt((c.tofrom[0]-c.tofrom[2])*(c.tofrom[0]-c.tofrom[2])+(c.tofrom[1]-c.tofrom[3])*(c.tofrom[1]-c.tofrom[3]));//Distance formula sqrt((x2-x1)^2-(y2-y1)^2)

   printf(" Difference between 2 points is : %f ",c.distance);

   i = 0;

   fout = fopen("pointdifferences.txt","a");

   while(i < 4)
   {
       fprintf(fout,"%f ",c.tofrom[i]);
       i++;
   }

   fprintf(fout," ");
   fprintf(fout,"%f",c.distance);
   fprintf(fout," ");
   fclose(fout);

   fin = fopen("pointdifferences.txt","r");//make text file called pointdifferences.txt
  
   fflush(stdin);
  
   fclose(fin);
  
   f = fopen("pointdifferences.txt","rt");//read text file called pointdifferences.txt

   while((d=fgetc(f))!=EOF)
   {
      
       putchar(d);
       d=fgetc(f);
       printf("%c",d);
      
   }
   fclose(f);
}

void freePoint2D()
{
   free(c.tofrom);
}


int main()//driver
{
   char *a;
   int i = 1;

   mallocPoint2D();
   freePoint2D();

   return 0;
}

and the string code below

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


//data type definition thing
struct _string
{
   char *str;
}b;

void mallocString(char *a)
{
   FILE *fin;
   FILE *fout;
   FILE *f;

   char d;
  
   int i;

   char ch;

   i = 0;

   //memory allocating thing
   b.str=(char *)malloc(strlen(a));

   strcpy(b.str,a);

   while(i < strlen(a))
   {
       printf("%c",b.str[i]);
       i++;
   }

   fout= fopen("string.txt","a");

   while(i < strlen(a))
   {
       fprintf(fout,"%c",b.str[i]);
       i++;
   }

   fprintf(fout,a," ");

   fclose(fout);

   fin= fopen("string.txt","r");//open and write to string.txt

   fflush(stdin);

   fclose(fin);

   f = fopen("string.txt","rt");//read from string.txt

   while((d=fgetc(f))!= EOF)
   {
       printf("%c",d);
   }

   fclose(f);
}

void freeString()
{
   free(b.str);
}

int main()
{
   char *a;
   int i = 1;

   mallocString("this is a string ");
   freeString();
  
   return 0;
}

During this class exercise we're going to construct some simple functionality for a geographic information system (GIS). Part One Construct and test two modules: one for a String and a Point2D. For each, build a data type definition and functions to access and manage instances. The intent is that user code does not "touch" the data structure, but that the module code does that. In the case of String you can rely on the existing String library for access and management, however you will need to define a Stringas a char and write the mallocString and freeString functions. The Point2D module will require not only the mallocPoint2D and freePoint2D functions but the ability to set and get values as well as calculate the distance between two points. Your modules should also be able to read and write individual instances from/to a text file. You should write a driver/test program that implements your module Part Two Construct and test two modules: one for a LabelPoint2D and a Line2D. A Label is a Point2D with an associated text string. A Line2D is a linked list of Point2D. There can be an associated LabelPoint2D although the Point defined here is where to locate the string on a map, and is not part of a line. Your modules should also be able to read and write individual instances (LabelPoint2D, Line2D from/to a text file) You should write a driver/test program that implements your module

Explanation / Answer

LabelPoint2DMain.c

#include<stdio.h>
#include"Point2D.h"
#include"String.h"
#include"LabelPoint2D.h"

int main() {
   Point2D *p1, *p2, *p3;
   String s1, s2, s3;
   double x1, y1, x2, y2;
   int* pXErr;
   int* pYErr;
   int xErr, yErr;
   int status;
   LabelPoint2D* pLP2D;
   LabelPoint2D* read;
   FILE* pfile;

   status = 0;

   p1 = createPoint2D(1.0, 2.0);
   s1 = createString("Test (Please don't blow up)");

   pLP2D = createLabelPoint2D(s1, p1);

   p2 = getPoint(pLP2D);
   s2 = getLabel(pLP2D);

   xErr = 0;
   yErr = 0;
   pXErr = &xErr;
   pYErr = &yErr;

   x1 = getXPoint2D(p2, pXErr);
   y1 = getYPoint2D(p2, pYErr);

   printf("Label: '%s' ", s2);
   printf("(%0.2f, %0.2f) ", x1, y1);
   if(xErr == 1 || yErr == 1) {
       printf("D'oh! ");
   }

   p3 = createPoint2D(3.5, 7.2);
   s3 = createString("Test Numero 2 (Please don't blow up)");

   status = setLabel(pLP2D, s3);
   if(status == 1) {
       printf("Something went wrong setting string");
   }

   status = setPoint(pLP2D, p3);
   if(status == 1) {
       printf("Something went wrong setting point");
   }

   p2 = getPoint(pLP2D);
   s2 = getLabel(pLP2D);

   xErr = 0;
   yErr = 0;
   pXErr = &xErr;
   pYErr = &yErr;

   x1 = getXPoint2D(p2, pXErr);
   y1 = getYPoint2D(p2, pYErr);

   printf("Label: '%s' ", s2);
   printf("(%0.2f, %0.2f) ", x1, y1);
   if(xErr == 1 || yErr == 1) {
       printf("D'oh! ");
   }

   p3 = createPoint2D(4.2, 6.9);
   s3 = createString("Test Numero 3 (Please, please don't blow up)");

   status = setLabel(pLP2D, s3);
   if(status == 1) {
       printf("Something went wrong setting string");
   }

   status = setPoint(pLP2D, p3);
   if(status == 1) {
       printf("Something went wrong setting point");
   }

   pfile = fopen("LabelPoint.txt", "w");
   status = fputLabelPoint2D(pfile, pLP2D);
   if(status == 1) {
       printf("Something went wrong printing to file");
   }
   fclose(pfile);

   pfile = fopen("LabelPoint.txt", "r");
   read = fgetLabelPoint2D(pfile, &status);
   if(status == 1) {
       printf("Something went wrong reading from file");
   }
   fclose(pfile);

   p2 = getPoint(read);
   s2 = getLabel(read);

   xErr = 0;
   yErr = 0;
   pXErr = &xErr;
   pYErr = &yErr;

   x1 = getXPoint2D(p2, pXErr);
   y1 = getYPoint2D(p2, pYErr);

   printf("READ FROM FILE ");
   printf("Label: '%s' ", s2);
   printf("(%0.2f, %0.2f) ", x1, y1);
   if(xErr == 1 || yErr == 1) {
       printf("D'oh! ");
   }


   read = freeLabelPoint2D(read);
   if(read == (LabelPoint2D*)NULL){
       printf("Free successful ");
   } else {
       printf("Free not successful ");
   }
}

PartOneDriver.c

#include<stdio.h>
#include"Point2D.h"
#include"String.h"

int main() {
   /* Variable declarations for Point2D test */
   double x, y;
   double xGet, yGet;
   double distance;
   int* pXErr;
   int* pYErr;
   int xErr, yErr;
   Point2D *pPt2D, *pPt2DRead, *pPt2DWrite;
   FILE* pfile;

   /* Variable declarations for String test */
   int strErr;
   String file;
   String testStr;
   String testStr2;

   printf("========== TEST Point2D ========== ");

   x = 15.0;
   y = 20.0;
   xErr = 0;
   yErr = 0;
   pXErr = &xErr;
   pYErr = &yErr;

   /*Create a Point2D by calling the createPoint2D() function*/
   pPt2D = createPoint2D(x, y);
   pPt2DWrite = createPoint2D(2.6, 4.2);

   /*Get and print the x and y of pPt2 to ensure success*/
   xGet = getXPoint2D(pPt2D, pXErr);
   yGet = getYPoint2D(pPt2D, pYErr);

   if(xErr == 1 || yErr == 1) {
       printf("There was an error getting x or y, returning. ");
       return 0;
   }
   printf("First Point2D: x=%0.2f y=%0.2f (Expected: x=15.0 y=25.0) ", xGet, yGet);

   /*Test writing to a file*/
   pfile = fopen("testFile.txt", "w");
   printf("Error Status for file writing (0=success 1=fail): %d ", fputPoint2D(pfile, pPt2DWrite));
   fclose(pfile);

   /*Create a Point2D by reading form a file*/
   pfile = fopen("testFile.txt", "r");
   pPt2DRead = fgetPoint2D(pfile, pXErr);
   fclose(pfile);

   if(xErr == 1) {
       printf("Something went wrong reading the file. ");
       return 0;
   }

   /*Get and print the x and y of pPt2 to ensure success*/
   xGet = getXPoint2D(pPt2DRead, pXErr);
   yGet = getYPoint2D(pPt2DRead, pYErr);

   if(xErr == 1 || yErr == 1) {
       printf("There was an error getting x or y, returning. ");
       return 0;
   }
   printf("Point2D read from file: x=%0.2f y=%0.2f (Expected: x=2.5, y=4.2) ", xGet, yGet);

   /*Test the calculate distance function with our 2 Point2D's*/
   distance = calcDistancePoint2D(pPt2DRead, pPt2D);
   printf("Distance between Point2D and Point2DRead: %0.4f ", distance);

   /* Free a Point2D*/
   pPt2D = freePoint2D(pPt2D);
   if(pPt2D == (Point2D*)NULL) {
       printf("freeing Point2D successful ");
   }
  
   printf("========== TEST String ========== ");

   strErr = 0;
   file = "test.txt";

   /* Test createString */
   testStr = createString("I love C!");
   printf("String created successfully: '%s' ", testStr);

   /* Open file for writing */
   pfile = fopen(file, "w");
   strErr = fputString(pfile, testStr);
   printf("Error Status for file writing (0=success 1=fail): %d ", strErr);
   fclose(pfile);
   strErr = 0;

   /* Open file for reading */
   pfile = fopen(file, "r");
   testStr2 = fgetString(pfile, &strErr);
   printf("Error Status for file reading (0=success 1=fail): %d ", strErr);
   fclose(pfile);

   if(strErr == 0){
           printf("String read from file: '%s' ", testStr2);
   }

   /* Free a String*/
   testStr = freeString(testStr);
   if(testStr == (String)NULL) {
       printf("freeing String successful ");
   }
  

   return 0;
}
Point2DMain.c

#include<stdio.h>
#include"Point2D.h"

int main() {
   double x, y;
   double xGet, yGet;
   double distance;
   int* pXErr;
   int* pYErr;
   int xErr, yErr;
   Point2D *pPt2D, *pPt2DRead;
   FILE* pfile;

   x = 15.0;
   y = 25.0;
   xErr = 0;
   yErr = 0;
   pXErr = &xErr;
   pYErr = &yErr;

   /*Create a Point2D by calling the createPoint2D() function*/
   pPt2D = createPoint2D(x, y);

   /*Get and print the x and y of pPt2 to ensure success*/
   xGet = getXPoint2D(pPt2D, pXErr);
   yGet = getYPoint2D(pPt2D, pYErr);

   if(xErr == 1 || yErr == 1) {
       printf(" There was an error getting x or y, returning. ");
       return 0;
   }
   printf(" x=%0.2f y=%0.2f for First Point2D ", xGet, yGet);

   /*Test writing to a file*/
   pfile = fopen("testFile.txt", "w");
   printf(" Error Status for file writing: %d 0=success 1=fail ", fputPoint2D(pfile, pPt2D));
   fclose(pfile);

   /*Create a Point2D by reading form a file*/
   pfile = fopen("testFile.txt", "r");
   pPt2DRead = fgetPoint2D(pfile, pXErr);
   fclose(pfile);

   if(xErr == 1) {
       printf(" Something went wrong reading a file. ");
       return 0;
   }

   /*Get and print the x and y of pPt2 to ensure success*/
   xGet = getXPoint2D(pPt2DRead, pXErr);
   yGet = getYPoint2D(pPt2DRead, pYErr);

   if(xErr == 1 || yErr == 1) {
       printf(" There was an error getting x or y, returning. ");
       return 0;
   }
   printf(" x=%0.2f y=%0.2f for the Read in Point2D ", xGet, yGet);

   /*Test the calculate distance function with our 2 Point2D's*/
   distance = calcDistancePoint2D(pPt2DRead, pPt2D);
   printf(" Distance between Point2D and Point2DRead: %0.4f ", distance);

   return 0;
}
StringMain.c

#include<stdio.h>
#include"String.h"

int main() {
   int err;
   FILE* pfile;
   String file;
   String test;
   String test2;

   err = 0;
   file = "test.txt";
   pfile = fopen(file, "w");
   test = createString("$This is a test!");

   fputString(pfile, test);
   fclose(pfile);

   pfile = fopen(file, "r");
   test2 = fgetString(pfile, &err);
   fclose(pfile);

   printf("String: '%s' ", test2);
}
Part2Driver.c


#include<stdio.h>
#include"Point2D.h"
#include"String.h"
#include"LabelPoint2D.h"
#include"Point2DNode.h"
#include"Line2D.h"

int main() {
   /* Label Point test variables */
   Point2D *p1, *p2, *p3;
    String s1, s2, s3;
    double x1, y1, x2, y2;
    int xErr, yErr;
    int status;
    LabelPoint2D* pLP2D;
    LabelPoint2D* read;
    FILE* pfile;

   /* Line Test Variables */
   int err;
   double x, y;
   Point2D* pPt2D1;
   Point2D* pPt2D2;
   Point2D* testPt2D;
   Point2DNode *pPt2DN, *testPt2DN, *testPt2DN2;
   LabelPoint2D *testLP2D, *testLP2D2;
   String str, testStr;
   Line2D *pL2D, *pL2;


   /* Test LabelPoint2D Functions */
   printf("===============LabelPoint2D TEST=============== ");
   status = 0;

   p1 = createPoint2D(1.0, 2.0);
   s1 = createString("Test (Please don't blow up)");

   pLP2D = createLabelPoint2D(s1, p1);

   p2 = getPoint(pLP2D);
   s2 = getLabel(pLP2D);

   xErr = 0;
   yErr = 0;

   x1 = getXPoint2D(p2, &xErr);
   y1 = getYPoint2D(p2, &yErr);

   printf("Label: '%s' ", s2);
   printf("(%0.2f, %0.2f) ", x1, y1);
   if(xErr == 1 || yErr == 1) {
       printf("D'oh! ");
   }

   p3 = createPoint2D(3.5, 7.2);
   s3 = createString("Test Numero 2 (Please don't blow up)");

   status = setLabel(pLP2D, s3);
   if(status == 1) {
       printf("Something went wrong setting string");
   }

   status = setPoint(pLP2D, p3);
   if(status == 1) {
       printf("Something went wrong setting point");
   }

   p2 = getPoint(pLP2D);
   s2 = getLabel(pLP2D);

   xErr = 0;
   yErr = 0;

   x1 = getXPoint2D(p2, &xErr);
   y1 = getYPoint2D(p2, &yErr);

   printf("Label: '%s' ", s2);
   printf("(%0.2f, %0.2f) ", x1, y1);
   if(xErr == 1 || yErr == 1) {
       printf("D'oh! ");
   }

   p3 = createPoint2D(4.2, 6.9);
   s3 = createString("Test Numero 3 (Please, please don't blow up)");

   status = setLabel(pLP2D, s3);
   if(status == 1) {
       printf("Something went wrong setting string");
   }

   status = setPoint(pLP2D, p3);
   if(status == 1) {
       printf("Something went wrong setting point");
   }

   pfile = fopen("LabelPoint.txt", "w");
   status = fputLabelPoint2D(pfile, pLP2D);
   if(status == 1) {
       printf("Something went wrong printing to file");
   }
   fclose(pfile);

   pfile = fopen("LabelPoint.txt", "r");
   read = fgetLabelPoint2D(pfile, &status);
   if(status == 1) {
       printf("Something went wrong reading from file");
   }
   fclose(pfile);

   p2 = getPoint(read);
   s2 = getLabel(read);

   xErr = 0;
   yErr = 0;

   x1 = getXPoint2D(p2, &xErr);
   y1 = getYPoint2D(p2, &yErr);

   printf("READ FROM FILE ");
   printf("Label: '%s' ", s2);
   printf("(%0.2f, %0.2f) ", x1, y1);
   if(xErr == 1 || yErr == 1) {
       printf("D'oh! ");
   }


   read = freeLabelPoint2D(read);
   if(read == (LabelPoint2D*)NULL){
       printf("Free successful ");
   } else {
       printf("Free not successful ");
   }

   /* Test Line2D Functions */
   printf("===============Line2D TEST=============== ");
   err = 0;
   xErr = 0;
   yErr = 0;
   pPt2D1 = createPoint2D(1.0, 2.0);
   pPt2D2 = createPoint2D(6.2, 3.5);
   pPt2DN = createPoint2DNode(pPt2D1);
   testStr = "I love C!";
   pLP2D = createLabelPoint2D(testStr, createPoint2D(3.0, 2.5));

   /* Create first Line */
   pL2D = createLine2D(pLP2D, pPt2DN);
   if(pL2D == (Line2D*)NULL) {
       printf("Line2D creation unsuccessful ");
   } else {
       printf("Line2D creation successful ");
   }

   testPt2DN2 = createPoint2DNode(createPoint2D(1.2, 2.3));
   testLP2D2 = createLabelPoint2D("This is a test!", createPoint2D(3.5, 5.3));
   pL2 = createLine2D(pLP2D, pPt2DN);
   pL2 = freeLine2D(pL2);
   if(pL2 == (Line2D*)NULL) {
       printf("Free Line2D successful ");
   } else {
       printf("Free Line2D not successful ");
   }

   pPt2D1 = createPoint2D(1.0, 2.0);
   pPt2D2 = createPoint2D(6.2, 3.5);
   pPt2DN = createPoint2DNode(pPt2D1);
   pL2D = createLine2D(pLP2D, pPt2DN);
   testLP2D = getLabelLine2D(pL2D, &err);
   if(err == 1) {
       printf("Could not get label point");
       return 0;
   }
   p1 = getPoint(testLP2D);
   if(p1 == (Point2D*)NULL) {
       printf("Could not get point from label");
   }

   testPt2DN = getHeadLine2D(pL2D, &err);
   if(err == 1) {
       printf("Could not get head node");
       return 0;
   }

   testPt2D = getNodePoint(testPt2DN, &err);
   if(err == 1) {
       printf("Could not get label point");
       return 0;
   }

   x = getXPoint2D(p1, &xErr);
   y = getYPoint2D(p1, &yErr);
   if(xErr || yErr) {
       printf("Could not get coordinates from head point");
   }

   x1 = getXPoint2D(testPt2D, &xErr);
   y1 = getYPoint2D(testPt2D, &yErr);
   if(xErr || yErr) {
       printf("Could not get coordinates from head node point");
   }

   printf("Head Point: x=%0.2f, y=%0.2f (Expected: (1.0, 2.0)) ", x1, y1);
   printf("Label String: '%s' ",getLabel(testLP2D));
   printf("Label Point: x=%0.2f, y=%0.2f (Expected: (3.0, 2.5)) ", x, y);

   pfile = fopen("testFile.txt", "w");
   printf("Error Status for writing Line2D to file (0=success 1=fail): %d ", fputLine2D(pfile, pL2D));
   fclose(pfile);

   pfile = fopen("testFile.txt", "r");
   pL2 = fgetLine2D(pfile, &err);
   printf("Error Status for reading Line2D from file (0=success 1=fail): %d ", err);
   fclose(pfile);

   testPt2DN = createPoint2DNode(createPoint2D(5.5, 2.1));
   status = addPtLine2D(pL2D, testPt2DN);
   if(status == 1) {
       printf("Could not add point to line ");
   } else {
       printf("Point added successfully to line ");
   }

   status = setHeadLine2D(pL2D, testPt2DN);
   if(status == 1) {
       printf("Could not set head ");
   } else {
       printf("Head set successfully ");
   }

   pLP2D = createLabelPoint2D("Oh man", createPoint2D(0.3, 5.2));
   status = setLabelLine2D(pL2D, pLP2D);
   if(status == 1) {
       printf("Could not set label point ");
   } else {
       printf("Label point set successfully ");
   }

   printf("===============TEST COMPLETE=============== ");
   return 0;
}

LabelPoint2D.c

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"String.h"
#include"Point2D.h"
#include"LabelPoint2D.h"

LabelPoint2D* mallocLabelPoint2D(int sLength) {
   LabelPoint2D* pLP2D;
   pLP2D = (LabelPoint2D*)malloc(sizeof(LabelPoint2D));
   pLP2D -> pPt2D = mallocPoint2D();
   pLP2D -> str = mallocString(sLength);

   return pLP2D;
}

LabelPoint2D* freeLabelPoint2D(LabelPoint2D* pLP2D) {
   if(pLP2D -> str != (String)NULL){
       pLP2D -> str = freeString(pLP2D -> str);
   }

   if(pLP2D -> pPt2D != (Point2D*)NULL) {
       pLP2D -> pPt2D = freePoint2D(pLP2D -> pPt2D);
   }

   free(pLP2D);

   return (LabelPoint2D*)NULL;
}

int   fputLabelPoint2D(FILE* pfile, LabelPoint2D* pLP2D) {
   String s;
   Point2D* pP;

   s = pLP2D -> str;
   pP = pLP2D -> pPt2D;

   if(fputString(pfile, s)) {
       return 1;
   }

   if(fputPoint2D(pfile, pP)) {
       return 1;
   }

   return 0;
}

LabelPoint2D* fgetLabelPoint2D(FILE* pfile, int* pErr) {
   String s;
   Point2D* pP;

   s = fgetString(pfile, pErr);
   if(*pErr == 1) {
       return (LabelPoint2D*)NULL;
   }

   pP = fgetPoint2D(pfile, pErr);
   if(*pErr == 1) {
       return (LabelPoint2D*)NULL;
   }

   return createLabelPoint2D(s, pP);
}

LabelPoint2D* createLabelPoint2D(String str, Point2D* pPt2D) {
   LabelPoint2D* pLP2D;
   int status;
   int len;

   len = strlen(str);
   pLP2D = mallocLabelPoint2D(len);

   if(setLabel(pLP2D, str)) {
       return (LabelPoint2D*)NULL;
   }

   if(setPoint(pLP2D, pPt2D)) {
       return (LabelPoint2D*)NULL;
   }

   return pLP2D;
}

String getLabel(LabelPoint2D* pLP2D) {
   return pLP2D -> str;
}

Point2D* getPoint(LabelPoint2D* pLP2D) {
   return pLP2D -> pPt2D;
}

int   setLabel(LabelPoint2D* pLP2D, String inStr) {
   if(pLP2D == (LabelPoint2D*)NULL){
       return 1;
   } else {
       pLP2D -> str = inStr;
       return 0;
   }
}

int   setPoint(LabelPoint2D* pLP2D, Point2D* pInPt2D) {
   if(pLP2D == (LabelPoint2D*)NULL){
       return 1;
   } else {
       pLP2D -> pPt2D = pInPt2D;
       return 0;
   }
}

LabelPoint2D.h

typedef struct {
   Point2D* pPt2D;
   String str;
} LabelPoint2D;

LabelPoint2D* mallocLabelPoint2D(int sLength);
LabelPoint2D* freeLabelPoint2D(LabelPoint2D* pLP2D);
int   fputLabelPoint2D(FILE* pfile, LabelPoint2D* pLP2D);
LabelPoint2D* fgetLabelPoint2D(FILE* pfile, int* pErr);
LabelPoint2D* createLabelPoint2D(String str, Point2D* pPT2D);
String getLabel(LabelPoint2D* pLP2D);
Point2D* getPoint(LabelPoint2D* pLP2D);
int   setLabel(LabelPoint2D* pLP2D, String inStr);
int   setPoint(LabelPoint2D* pLP2D, Point2D* pInPt2D);


Line2D.c


#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"Point2D.h"
#include"String.h"
#include"LabelPoint2D.h"
#include"Point2DNode.h"
#include"Line2D.h"

Line2D* mallocLine2D(int sLength) {
   Line2D* pLine2D;
   pLine2D = (Line2D*)malloc(sizeof(Line2D));
   pLine2D->lbl = mallocLabelPoint2D(sLength);
   pLine2D->pHead = mallocPoint2DNode();
   return pLine2D;
}

Line2D* freeLine2D(Line2D* pLine2D) {
   Point2DNode* pTempNode;
   Point2DNode* pWorkNode;
  
   pWorkNode = pLine2D->pHead;
   while(pWorkNode != (Point2DNode*)NULL)   {
       pTempNode = pWorkNode->nextNode;
       freePoint2DNode(pWorkNode);
       pWorkNode = pTempNode;
   }

   return (Line2D*)NULL;
}

Line2D* createLine2D(LabelPoint2D* pLP2D, Point2DNode* pPt2DNode) {
   Line2D* pLine2D;
   int len;

   len = (strlen(pLP2D->str));
   pLine2D = mallocLine2D(len);
   if(pLine2D == (Line2D*)NULL) {
       return pLine2D;
   }
   setHeadLine2D(pLine2D, pPt2DNode);
   setLabelLine2D(pLine2D, pLP2D);
   return pLine2D;
}

int addPtLine2D(Line2D* pLine2D, Point2DNode* pPt2DNode) {
   Point2DNode* pWorking;
   if(pLine2D == (Line2D*)NULL || pPt2DNode == (Point2DNode*)NULL) {
       return 1;
   }

   pWorking = pLine2D->pHead;
   while(pWorking->nextNode != (Point2DNode*)NULL) {
       pWorking = pWorking->nextNode;
   }

   pWorking->nextNode = pPt2DNode;
   return 0;
}

Point2DNode* getHeadLine2D(Line2D* pLine2D, int* pErr) {
   if(pLine2D == (Line2D*)NULL) {
       *pErr = 1;
       return (Point2DNode*)NULL;
   }

   return pLine2D->pHead;
}

int setHeadLine2D(Line2D* pLine2D, Point2DNode* pPt2DNode) {
   if(pLine2D == (Line2D*)NULL) {
       return 1;
   }

   pLine2D->pHead = pPt2DNode;
   return 0;
}

LabelPoint2D* getLabelLine2D(Line2D* pLine2D, int* pErr) {
   if(pLine2D == (Line2D*)NULL) {
       *pErr = 1;
       return (LabelPoint2D*)NULL;
   }

   return pLine2D->lbl;
}

int setLabelLine2D(Line2D* pLine2D, LabelPoint2D* pLP2D) {
   if(pLine2D == (Line2D*)NULL) {
       return 1;
   }

   pLine2D->lbl = pLP2D;
   return 0;
}

Line2D* fgetLine2D(FILE* pfile, int* pErr) {
   Line2D *pLine2D;
   Point2D* pPt2D;
   LabelPoint2D* pLP2D;
   Point2DNode* pP2DN;
   double x, y;
   int status;

   if(pfile == (FILE*)NULL) {
       *pErr = 1;
   }

   pLP2D = fgetLabelPoint2D(pfile, pErr);

   status = fscanf(pfile, "%lf %lf", &x, &y);
   if(status < 2) {
       *pErr = 1;
   }
   pPt2D = createPoint2D(x, y);
   pP2DN = createPoint2DNode(pPt2D);

   pLine2D = createLine2D(pLP2D, pP2DN);

   status = fscanf(pfile, "%lf %lf", &x, &y);
   while(status == 2) {
       pPt2D = createPoint2D(x, y);
       pP2DN = createPoint2DNode(pPt2D);
       *pErr = addPtLine2D(pLine2D, pP2DN);

       status = fscanf(pfile, "%lf %lf", &x, &y);
   }

   return   pLine2D;
}

int fputLine2D(FILE* pfile, Line2D* pLine2D) {
   Point2DNode* pPt2DN;
   Point2D* pPt2DOut;
   int status;
   int* pXErr;
   int* pYErr;
   int err, xErr, yErr;
   double x, y;

   if(pfile == (FILE*)NULL || pLine2D == (Line2D*)NULL) {
       return 1;
   }
   status = fputLabelPoint2D(pfile, pLine2D->lbl);
   if(status == 1){
       return 1;
   }

   pXErr = &xErr;
   pYErr = &yErr;
   err = 0;
   pPt2DN = pLine2D->pHead;

   while(pPt2DN != (Point2DNode*)NULL) {
       xErr = 0;
       yErr = 0;

       x = getXPoint2D(getNodePoint(pPt2DN, &err), pXErr);
       y = getYPoint2D(getNodePoint(pPt2DN, &err), pYErr);
       if(xErr == 1 || yErr == 1 || err == 1) {
           return 1;
       }

       fprintf(pfile, "%f", x);
       fputc(' ', pfile);
       fprintf(pfile, "%f", y);
       status = fputc(' ', pfile);

       if(status == EOF) {
           return 1;
       }

       pPt2DN = pPt2DN->nextNode;
   }

}

Line2D.h

typedef struct {
   LabelPoint2D* lbl;
   Point2DNode* pHead;
} Line2D;

Line2D* mallocLine2D(int sLength);
Line2D* freeLine2D(Line2D* pLine2D);
Line2D* createLine2D(LabelPoint2D* pLP2D, Point2DNode* pPt2DNode);
int addPtLine2D(Line2D* pLine2D, Point2DNode* pPt2DNode);
Point2DNode* getHeadLine2D(Line2D* pLine2D, int* pErr);
int setHeadLine2D(Line2D* pLine2D, Point2DNode* pPt2DNode);
LabelPoint2D* getLabelLine2D(Line2D* pLine2D, int* pErr);
int setLabelLine2D(Line2D* pLine2D, LabelPoint2D* pLP2D);
Line2D* fgetLine2D(FILE* pfile, int* pErr);
int fputLine2D(FILE* pfile, Line2D* pLine2D);