Closed. This question is
off-topic。它当前不接受答案。
想改善这个问题吗?
Update the question,所以它是
on-topic,用于堆栈溢出。
4年前关闭。
我正在使用C语言编写一个程序,该程序可以计算地球上任何两个城市之间的地面距离。程序将获得一个.txt输入文件,其中包含城市名称及其相应的坐标(以度和分钟为单位)。从输入文件中,必须通过fscanf将所有数据读入数组。我使用For和If语句基于C的知识编写了该程序,但是输出不正确。
这是我的输入文件:
8
Montreal 45.0 30.5 N 73.0 33.2 W
London 51.0 30.7 N 0.0 7.2 W
Chicago 41.0 52.7 N 87.0 37.8 W
Melbourne 37.0 48.8 S 144.0 57.8 E
Vladivostok 43.0 8.2 N 131.0 54.7 E
Johannesburg 26.0 12.2 S 28.0 2.8 E
Los-Angeles 34.0 2.6 N 118.0 13.8 W
Rio-de-Janeiro 22.0 54.2 S 43.0 12.6 W
到目前为止,这是我的程序,运行时没有语法错误
/******************************************************************************
PROGRAM DESCRIPTION:
This program computes the distance between two cities. Longitude and
latitude coordinates are converted from degrees and minutes to radians
relative to the North pole and the prime meridian which passes through
Greenwich, England. After converting the cities positions to radians,
the angular separation is calculated between two cities. Finally, the
surface distance between the two cities is calculated. The results are
printed to the computer screen.
DESCRIPTION OF VARIABLES FOR STRUCTURE:
NAME | TYPE | DESCRIPTION
----------------------------------------------------------------------------
hem | char | hemisphere of a city in the structure
dir | char | longitude direction of a city in the structure
cname | char | array of city names in the structure
latdeg | double | latitude coordinates in degrees in the structure
latmin | double | latitude coordinates in minutes in the structure
londeg | double | longitude coordinates in degrees in the structure
lonmin | double | longitude coordinates in minutes in the structure
DESCRIPTION OF VARIABLES FOR MAIN FUNCTION:
NAME | TYPE | DESCRIPTION
----------------------------------------------------------------------------
earth_radius | symbolic | radius of the earth
pi | symbolic | math constant
cdata | structure | array of cities read from input file
i | int | loop control variable
ncities | int | number of cites read from the input file
sep_angle | double | computed separation angle between two cities
distance | double | computed surface distance between two cities
alpha1 | double | computed latitude of first city in radians
alpha2 | double | computee latitude of second city in radians
beta1 | double | computed longitude of first city in radians
beta2 | double | computed longitude of second city in radians
city_data | pointer | pointer variable for input file
******************************************************************************/
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "c:\\engr 200\\cities.txt"
#define pi 3.141592654
#define earth_radius 3958.89
/* Define the structure for cities */
struct cities
{
char hem, dir, cname[8];
double latdeg, latmin, londeg, lonmin;
};
/* Main function */
int main(void)
{
/* Declare the structure */
struct cities cdata[8];
/* Declare variables */
double sep_angle, distance, alpha1, alpha2, beta1, beta2;
int i, ncities;
FILE *city_data;
/* Open input file */
city_data = fopen(inputfile,"r");
/* Verify input file and read input data */
if(city_data == NULL)
{
printf("\n\nError opening input file.");
printf("\nProgram terminated.");
printf("\n\n\nPress Enter to quit.\n");
getchar(); getchar();
return 0;
}
else
{
/* Read control number from input file */
fscanf(city_data,"%i",&ncities);
/* Read city data into array structure from input file */
for(i=1; i<=ncities; i++ )
{
fscanf(city_data, "%s %3.0f %3.0f %c %3.0f %3.0f %c",cdata[i].cname,
&cdata[i].latdeg,&cdata[i].latmin,cdata[i].hem,&cdata[i].londeg,
&cdata[i].lonmin,cdata[i].dir);
}
}
/* Print main headings to computer screen */
printf("CITY LATITUDE LONGITUDE\n\n");
/* Compute and print distance between 2 cities */
for(i=1; i<=ncities; i = i + 2 )
{
/* Print two cities to computer screen */
printf("%-15s %3.0fø %4.1f'%c %3.0fø %4.1f'%c"
"\n%-15s %3.0fø %4.1f'%c %3.0fø %4.1f'%c\n",
cdata[i].cname,cdata[i].latdeg,cdata[i].latmin,
cdata[i].hem,cdata[i].londeg,cdata[i].lonmin,cdata[i].dir,
cdata[i+1].cname,cdata[i+1].latdeg,cdata[i+1].latmin,
cdata[i+1].hem,cdata[i+1].londeg,cdata[i+1].lonmin,
cdata[i+1].dir);
/* Convert latitude to radians for city 1 */
if(cdata[i].hem == 'N')
{alpha1 = (90.0-cdata[i].latdeg)*(pi / 180.0 );}
else
{alpha1 = (90.0+cdata[i].latdeg)*(pi / 180.0 );}
/* Convert latitude to radians for city 2 */
if(cdata[i+1].hem == 'N')
{alpha2 = (90.0-cdata[i+1].latdeg)*(pi / 180.0 );}
else
{alpha2 = (90.0+cdata[i+1].latdeg)*(pi / 180.0 );}
/* Convert longitude to radians for city 1 */
if(cdata[i].dir == 'W')
{beta1 = (cdata[i].londeg)*(pi / 180.0 );}
else
{beta1= (360.0-cdata[i].londeg)*(pi / 180.0 );}
/* Convert longitude to radians for city 2 */
if(cdata[i+1].dir == 'W')
{beta2= (cdata[i+1].londeg)*(pi / 180.0 );}
else
{beta2= (360.0-cdata[i+1].londeg)*(pi / 180.0 );}
/* Compute angular separation between city 1 and city 2 */
sep_angle = ((acos(cos(alpha1))*(cos(alpha2)))+((sin(alpha1))*(sin(alpha2))
*(cos(beta2-beta1))));
/* Compute the distance between city 1 and city 2 */
distance = earth_radius*sep_angle;
/* Print distance between city 1 and city 2 to computer screen */
printf("Distance between the two cities = %6.1f miles\n\n\n",distance);
}
/* Close input file */
fclose(city_data);
/* Exit program */
printf("\n\n\nPress Enter to quit.\n");
getchar(); getchar();
return 0;
}
/*********************************************************************/
在编译并运行.exe之后,所有显示的内容是:
CITY LATITUDE LONGITUDE
Montreal 0000000000080980980385690835600000 etc.
其余都是随机零和巨大数字:/
我应该得到这样的东西:
CITY LATITUDE LONGITUDE
Montreal 45° 30.5'N 73° 33.2'W
London 51° 30.7'N 0.0 7.2'W
Distance between the two cities = xxxx.x miles
Chicago 41° 52.7'N 87° 37.8'W
Melbourne 37° 48.8'S 144° 57.8'E
Distance between the two cities = xxxx.x miles
and so on for the rest of the cities in the input file.....
是什么原因造成的?让我知道是否还有其他需要补充的内容。
编辑:我认为他们程序使用fscanf读取数据时会自身脱轨
我是一名优秀的程序员,十分优秀!