【问题标题】:Correction and tips for my code我的代码的更正和提示
【发布时间】:2015-06-20 20:24:21
【问题描述】:

我已经添加、修改了之前的代码。现在我的问题是没有得到正确的布局。我意识到数组更好。但是有没有办法在没有数组的情况下修复它 文本文件为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_pointy_point。但是每一行都有不同数量的x和y坐标。我也为此打印了一张表格,但我可以管理。

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 |
+-------+-------+-------+-------+-------+

  

非常感谢任何更正或提示。请注意,我不是在找人告诉我答案,只是指导。

谢谢!

【问题讨论】:

  • 再次检查for 循环条件,并尝试找出它真正做了多少次迭代。您还可以检查内部 scanf 函数返回的内容。
  • 会有两个编译器警告,都是针对未使用的参数:argc 和 argv[] 建议使用 'int main()' 来避免这个问题。
  • 你真的编译过这个吗?是否启用了所有编译器警告?第一个问题是变量 poly_id 在自动变量(堆栈变量)中声明了两次。这一行: 'for(i = 0; i
  • 首先,您的缩进遍布整个商店
  • @EdHeal 你能暂时忽略这个吗?

标签: c while-loop scanf


【解决方案1】:

两个问题:

1) 你应该在你的 printf 中加入某种分隔符(例如“\n”)

2) 你的柜台应该是for(i=0; i &lt; 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

【讨论】:

  • 实际上,根据建议的代码,这个输出行“3 12867 1.0 2.0 1.0 5.0 4.0 5.0”是不存在的,因为它从来没有被输入过,也没有被打印出来
猜你喜欢
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2013-04-14
相关资源
最近更新 更多