我不知道为什么错误会在标题中提到。数据都是数字,并且在没有指针的情况下测试时函数会毫无问题地读取数据。
注意:我不需要使用' malloc'或任何其他的。我试图弄清楚结构的内容。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#define MAX_POINTS 100
#define MAX_POLYGONS 100
typedef struct{
int polyid;
int npoints;
double x[MAX_POINTS];
//Stage1
double y[MAX_POINTS];
double perimeter;
double eccentricity;
double area;
} Polygon;
typedef struct {
Polygon npolygon[MAX_POLYGONS];
}polygons;
//function prototypes
void process_file(polygons *Total_poly);
int largestvalue_index(double A[],int m);
int largest_poly_id(double A[],int m, int B[]);
double area(double x[MAX_POINTS],double y[MAX_POINTS],int n);
double perimeter(double x[MAX_POINTS],double y[MAX_POINTS],int n);
double eccentricity(double area,double perimeter);
int main(int argc, char *argv[]) {
int i,j,k,l;
polygons Total_poly;
process_file(Total_poly);
printf("Stage 1\n");
printf("=====\n");
printf("First Polygon %d\n",Total_poly->npolygon[0]->polyid);
printf("x_val y_val\n");
for(i=0;i<Total_poly->npolygon[0].npoints;i++){
printf("%8.2f %8.2f\n",Total_poly->npolygon[0]->x[i],
Total_poly->npolygon[0]->y[i]);
}
printf("area=%.2f\n",area(Total_poly->npolygon[0].x,
Total_poly->npolygon[0]->y,Total_poly->npolygon[0]->npoints));
printf("perimeter=%.2f\n",perimeter(Total_poly->npolygon[0]->x,
Total_poly->npolygon[0]->y,Total_poly->npolygon[0]->npoints));
printf("eccentricity=%.2f\n",Total_poly->npolygon[0]->eccentricity);
//Stage2
printf("Stage 2");
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");
for(k=0;k<count;k++){
printf("| %5d | %5d |%6.2f |%6.2f |%6.2f |\n",
Total_poly->npolygon[k]->polyid,Total_poly->npolygon[k]->npoints,
Total_poly->npolygon[k]->perimeter,Total_poly->npolygon[k]->area,
Total_poly->npolygon[k]->eccentricity);
}
for(l=1;l<=5;l++){
printf("+-------");
}
//Stage3
return 0;
}
void process_file(polygons *Total_poly){
int count=0;
int i;
while(scanf("%d %d",&Total_poly.npolygon[count].npoints,
&Total_poly.npolygon[count].polyid)=2){
for(i=0;i<Total_poly.npolygon[count].npoints;i++){
if( scanf("%lf %lf",&Total_poly.npolygon[count].x[i],
&Total_poly.npolygon[count].y[i])=!2)
{
printf("Error");
exit(EXIT_FAILURE);
}else{
scanf("%lf %lf",&Total_poly.npolygon[count].x[i],
&Total_poly.npolygon[count].y[i]);
}
}
count++;
}
}
double area(double x[MAX_POINTS],double y[MAX_POINTS],int n){
int i,j;
double area=0;
j=n-1;
for(i=0;i<n;i++){
area+=(x[i]-x[j])*(y[i]+y[j]);
j=i;
}
return 0.5*fabs(area);
}
double perimeter(double x[MAX_POINTS],double y[MAX_POINTS],int n){
int i,j;
double length=0;
j=n-1;
for(i=0;i<n;i++){
length+=fabs(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
j=i;
}
return length;
}
double eccentricity(double area,double perimeter){
return perimeter*perimeter/area/(4*M_PI);
}
int largest_poly_id(double A[],int m, int B[]){
int i,j;
int poly_index=0,largest = A[0];
for(i=1;i<m;i++){
if(A[i]>largest){
largest=A[i];
poly_index=i;
}
}
j=B[poly_index];
return j;
}
int largestvalue_index(double A[],int m){
int i;
int index=0,largest=A[0];
for(i=1;i<m;i++){
if (A[i]>largest){
largest=A[i];
index=i;
}
}
return index;
}
答案 0 :(得分:1)
您正在将结构传递给process_file
函数。但是,它期望指向结构的指针。
更改此行:
process_file(Total_poly);
到此:
process_file(&Total_poly);
此外,您需要将->
功能printf
语句中的main
运算符更改为.
运算符。