我已经添加,修改了以前的代码。现在我的问题是没有得到正确的布局。我意识到阵列更好。但有没有办法解决它没有数组
文本文件为test0.txt
,如下所示
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
代码是:
#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);
}
目标是将第一列分配到npoints
,将第二列分配到poly_id
,然后将第三列分配到x_point
和y_point
。但每行的x和x都不同y coords。我也为此打印了一张桌子,但我可以管理。
npoints
确定点数(例如3 npoints给出3 x_points和y_points)。
所以我输入执行:
ass_1< test0.txt
所需的输出:
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 |
+-------+-------+-------+-------+-------+
非常感谢任何更正或提示。请注意,我不是在找人告诉我答案,只是指导。
谢谢!
答案 0 :(得分:1)
两个问题:
1)你应该在你的printf中加入某种分隔符(例如&#34; \ n&#34;)
2)您的计数器应为for(i=0; i < npoints; i++)
修改后的代码:
#include<stdio.h>
#include<stdlib.h>
int
main(int argc,char *argv[]){
int npoints,poly_id;
double x_point,y_point;
int i;
while(scanf("%d %d",&npoints,&poly_id)==2){
for(i=0; i < npoints; i++){
scanf("%lf %lf",&x_point,&y_point);
printf("%f %f\n",x_point,y_point);
}
printf("\n");
}
return 0;
}
输出:
3 12867 1.0 2.0 1.0 5.0 4.0 5.0
1.000000 2.000000
1.000000 5.000000
4.000000 5.000000