dimanche 19 avril 2015

Correction and tips for my code

I have added, modified the previous code. Now my problem is not getting the right layout. I realise arrays is better. BUT is there any way to fix it without arrays The text file is test0.txt as shown below



3 12867 1.0 2.0 1.0 5.0 4.0 5.0


5 15643 1.0 2.0 4.0 5.0 7.8 3.5 5.0 0.4 1.0 0.4


4 18674 1.0 0.4 0.4 0.4 0.4 3.6 1.0 3.6


0



The code is:



#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX_POINTS 100




double length(double x1,double x2,double y1,double y2);
double area_bits(double x1,double x2,double y1,double y2);



int
main(int argc,char *argv[]){
int npoints;
double x_point,y_point;
double x_prev=0,y_prev=0,x_first=0,y_first=0;
int poly_id;
int k,l,m=0;



double perimeter=0;
double area=0;
int primed=0;

/*Error check and echo-control*/
for(;m<=2;m++){

if(m==1){
printf("Stage 2\n");
printf("=======\n");
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");
printf("| id | nval | perim | area | eccen |\n");
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");
}
if(m==0){
printf("Stage 1\n");
printf("======\n");
}
while(scanf("%d %d",&npoints,&poly_id)==2){
if(npoints>MAX_POINTS){
printf("Exceeded limit\n");
exit(EXIT_FAILURE);

}
if(m==0){
printf("First polygon is %d\n",poly_id);
printf(" x_val y_val\n");

}

printf("| %5d | %5d |",poly_id,npoints);

perimeter=0;
area=0;
for(k=0;k<npoints;k++){
if (scanf("%lf %lf",&x_point,&y_point)==2){
if(m==0){
printf("%8.1f %8.1f\n",x_point,y_point);
}
if(k==0){
x_first=x_point;
y_first=y_point;
x_prev=x_point;
y_prev=y_point;

}
if(primed){
perimeter+=(length(x_point,x_prev,y_point,y_prev));
area+=area_bits(x_point,x_prev,y_point,y_prev);
x_prev=x_point;
y_prev=y_point;

}
primed=1;
}

}
perimeter+=length(x_first,x_prev,y_first,y_prev);
area+=area_bits(x_first,x_prev,y_first,y_prev);
if(m==0){
printf("perimeter = %.2f m\n",perimeter);
printf("area = %.2f m^2\n",area/2);
printf("eccentricity = %.2f\n",( pow(perimeter,2)/(area/2))/(4*M_PI));
}

printf("%6.2f |%6.2f |%6.2f |\n",perimeter,area/2,( pow(perimeter,2)/(area/2))/(4*M_PI));



}
if(m==1){
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");

}
}
return 0;
}

double length(double x1,double x2,double y1,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double area_bits(double x1,double x2,double y1,double y2){
return (x1-x2)*(y1+y2);
}


The goal is to assign the first column to npoints,second to poly_id and third onwards to x_point and y_point.But each row has different number of x and y coords. I have a print a table for this as well, but that I can manage.


npoints determine the number of points(eg 3 npoints give 3 x_points and y_points).


So I type in to execute:



ass_1< test0.txt


The desired output:



Stage 1
=======
First polygon is 12867
x_val y_val
1.0 2.0
1.0 5.0
4.0 5.0
perimeter = 10.24 m
area = 4.50 m^2
eccentricity = 1.86

Stage 2
=======
+-------+-------+-------+-------+-------+
| id | nval | perim | area | eccen |
+-------+-------+-------+-------+-------+
| 12867 | 3 | 10.24 | 4.50 | 1.86 |
| 15643 | 5 | 18.11 | 19.59 | 1.33 |
| 18674 | 4 | 7.60 | 1.92 | 2.39 |
+-------+-------+-------+-------+-------+


Any corrections or tips is highly appreciated. Note that I am not looking for someone to tell me the answer, just guidance.


Thank You!


Aucun commentaire:

Enregistrer un commentaire