C 我的代码的更正和提示
我已经添加、修改了以前的代码。现在我的问题是没有得到正确的布局。我意识到阵列更好。但是有没有办法在没有阵列的情况下修复它呢 文本文件是C 我的代码的更正和提示,c,while-loop,scanf,C,While Loop,Scanf,我已经添加、修改了以前的代码。现在我的问题是没有得到正确的布局。我意识到阵列更好。但是有没有办法在没有阵列的情况下修复它呢 文本文件是test0.txt,如下所示 3 12867 1.0 2.0 1.0 5.0 4.0 5.0 5156431.024.057.835.050.041.040.4 4186741.0.40.40.40.43.61.03.6 0 代码是: #include<stdio.h> #include<stdlib.h> #include&
test0.txt
,如下所示
3 12867 1.0 2.0 1.0 5.0 4.0 5.0
5156431.024.057.835.050.041.040.4
4186741.0.40.40.40.43.61.03.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);
}
所需输出:
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 |
+-------+-------+-------+-------+-------+
如有任何更正或提示,我们将不胜感激。请注意,我不是在找人告诉我答案,只是寻求指导
谢谢大家! 两个问题:
1) 您应该在printf中添加某种分隔符(例如“\n”)
2) 您的计数器应为(i=0;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;
}
再次检查循环条件的
,并尝试计算它实际执行了多少次迭代。您还可以检查内部scanf
函数返回的内容。对于未使用的参数,将出现两个编译器警告:argc和argv[]建议使用“int main()”以避免出现问题。您确实编译过这个吗?是否启用了所有编译器警告?第一个问题是变量poly_id在自动变量(堆栈变量)中声明了两次。这一行:'对于(i=0;i作为开始,您的缩进遍布整个shop@EdHeal请暂时忽略这一点好吗?实际上,根据建议的代码,这一输出行“3 12867 1.0 2.0 1.0 5.0 4.0 5.0”将不存在,因为它从未被全部输入或打印过
#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