这给了我一套额外的价值观......谁能告诉我为什么?哪里有问题? [英] this is giving me one extra set of values .... can anyone tell why ? where is problem ?

查看:58
本文介绍了这给了我一套额外的价值观......谁能告诉我为什么?哪里有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<#include "stdafx.h"
#include <stdio.h>
#include <math.h>

FILE *pg;
double input_status,
d = 1,
r,
q,
r1,
q1,
total_perimeter;

int t=0  ;

int main()

{
	errno_t err;

	// Open for read (will fail if file "crt_fopen_s.c" does not exist)
	err = fopen_s(&pg, "input.txt", "r");
	if (err == 0)
	{
		printf("The file 'input.txt' was opened\n \n ");
	}
	else
	{
		printf("The file 'input.txt' was not opened\n");
	}
	
	 fopen_s(&pg , "input.txt" , "r");

	 do {

		 input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
		 t = t + 1;
		 printf("point %d : r %lf q %lf \n", t, r, q);
		
		t = t + 1;
		 input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
		 printf("point %d : r %lf q %lf \n", t, r1, q1);

		 d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos(q1 - q)));
		 printf("distance between %d and %d  %lf \n \n ",(t-1) ,t , d);
		
	 } while (input_status != EOF);
	 
    return 0;

}

推荐答案

原因是只有在尝试阅读BEYOND时才会达到EOF条件文件的结尾。



所以你上次读到最后两个值时没有EOF。



所以do循环不会掉出来。



所以它会回来并尝试再读两组但是那时就是达到EOF条件时循环失败了。



但那时你已经尝试过两次读取数据但当然失败了...但是值r和q以及r1和q1已经从上一次读取并且没有更改。



所以你得到的是最后两对再次打印。



您应该测试EOF AFTER试图读取并退出循环



因此在尝试读取r后测试EOF和q然后退出循环(使用break语句),不进行任何打印或p继续阅读r1和q1。



我希望这会有所帮助。



例子......

The reason is that the EOF condition is only reached if you try to read BEYOND the end of the file.

So the last time you read the last two values there was no EOF.

So the do-loop does not fall out.

So it goes back and tries to read two more sets but THEN that is when the EOF condition is reached and the loop falls out.

BUT by then you have already tried to read data twice over but those fail of course... but the values r and q and r1 and q1 have been read from the previous time and are not changed.

So what you are getting is the last two pairs printed again.

You should test for the EOF AFTER having tried to read and exit the loop

So test for the EOF AFTER the attempt to read r and q and just exit the loop (use the break statement) without any printing or proceeding to reading r1 and q1.

I hope this helps.

example...
...
	 do {
 
		 input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
                 if(input_status == EOF) break; //<<---- look here
		 t = t + 1;
		 printf("point %d : r %lf q %lf \n", t, r, q);
		
		t = t + 1;
		 input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
		 printf("point %d : r %lf q %lf \n", t, r1, q1);
 
		 d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos(q1 - q)));
		 printf("distance between %d and %d  %lf \n \n ",(t-1) ,t , d);
		
	 } while (input_status != EOF);
....


这是一个计算周长的解决方案。



它实现了我上面给你的伪代码。



我注意到你的数据点是极坐标,角度是以DEGREES而不是RADIANS给出的。 />


这意味着使用cos()函数计算距离的公式是不正确的,因为cos()函数需要RADIANS中的角度...所以你需要转换( q1-q)在使用cos()之前的弧度。



这是通过:(q1-q)* dtor

完成的

所以现在公式变为

Here is a solution that also calculates the perimeter.

It implements the pseudo-code I gave you above.

I noticed that your data points are polar coordinates and the angles are given in DEGREES not RADIANS.

This means your formula to calculate the distance using the cos() function is incorrect because the cos() function needs angles in RADIANS... so you need to convert the (q1-q) to radians before you use the cos().

This is done by: (q1-q)*dtor

So now the formula becomes
d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));







变量dtor是在程序顶部定义为pi / 180.



这是程序< br $> b $ b




The variable dtor is a defined at the top of the program as pi/180.

Here is the program

#include "stdafx.h"
#include <stdio.h>
#include <math.h>

FILE *pg;
double input_status, d,r0,q0,r,q,r1,q1,total_perimeter=0;
int t=0;
double dtor = 3.14159265358979 / 180.0; //<<---- notice 
int main() 
{
     errno_t err;
     // Open for read (will fail if file "input.txt" does not exist)
     err = fopen_s(&pg, "input.txt", "r");
     if (err == 0)
        printf("The file 'input.txt' was opened\n\n");
     else
     {
        printf("The file 'input.txt' was not opened\n");
        return 0; //exit the program
     }
     //fopen_s(&pg , "input.txt" , "r");   //you really do not need this line
     input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
     r0 = r; q0 = q;  //save the first point for later to close the polygon
     if(input_status==EOF)
     {
        printf("There are no data points in the file");
        return 0; //exit the program
     }
     printf("point %d: r=%lf , q=%lf\n", t, r, q);
     do
     {
          input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
          if(input_status==EOF) 
               break; //if no more points exit the loop
          t++;
          printf("point %d: r=%lf , q=%lf\n", t, r1, q1);
          d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
          printf("distance between %d and %d = %lf \n\n",(t-1),t,d);
          r = r1; q = q1;  //make the previous point become the current point 
          total_perimeter += d; //accumulate the perimeter             
     }while(1);
     r1 = r0; q1 = q0;  //make the new point become the first point to close the polygon
     d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
     printf("distance between %d and 0 = %lf \n\n\n",t,d);
     total_perimeter += d;  //accumulate the perimeter
     printf("Total Perimiter = %lf\n",total_perimeter);     
     return 1; 
}


这篇关于这给了我一套额外的价值观......谁能告诉我为什么?哪里有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆