所以我对 C 编程非常陌生,并且陷入困境。
我正在编写一些代码,从文件中读取某些数据(由我的 read_file 函数定义)。我的任务之一是还隐藏 2 个给定点之间的距离(如 void distance 所示)并将其转换为弧度(如 toRadian 所示)。
但是问题出在第 48 行(在代码中突出显示),我在其中得到
"error: expected expression chord_length = pow(sin(toRadian(lat_2 - LAT_1)/2) + cos(toRadian
program.c:6:15: note: expanded from macro 'LAT_1'
define LAT_1 −37.798185"
我做错了什么?我就是想不出来。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LAT_1 -78.98214
#define LONG_1 -7.31600
#define R_PI 3.14159
#define DEGREES 180
int read_file(int i);
void toRadian(double theta);
void distance(double d);
int main()
{
int i = 0;
return read_file(i);
}
void toRadian(double theta)
{
double x = 0;
x = R_PI/DEGREES;
}
void distance(double d)
{
/* This function is designed to calculate the distance between the check-in
POI and the reference point provided*/
double dist, angle_distance, chord_length;
char *lat_2, *long_2;
char length[256];
char *token[6];
if (fgets(length, 256, stdin) != NULL)
{
token[0] = strtok(length, " ");
int i = 0;
double dist;
for (i = 1; i < 6; i++)
{
lat_2 = token[1];
long_2 = token[2];
}
chord_length = pow(sin(toRadian(lat_2 - LAT_1)/2) + cos(toRadian
(LAT_1)) * cos(toRadian(lat_2)) * pow(sin(toRadian(long_2 -
LONG_1))));
angle_distance = 2 * atan2(sqrt(chrod_length), sqrt(1 - cord_length));
dist = 6371 * angle_dist;
}
int read_file(int i)
{
/* This function takes the data from the input file,reading and printing the
User ID, Location (longitude and latitude), Date, Time, and Distance*/
char length[256];
char *token[6];
if (fgets(length, 256, stdin) != NULL)
{
token[0] = strtok(length, " ");
int i = 0;
double dist;
for (i = 1; i < 6; i++)
token[i] = strtok(NULL, " "); /*C programming is fun*/
printf("Stage 1\n==========\n");
printf("User: #%s\n", token[0]);
printf("Location: <%s, %s>\n", token[1], token[2]);
printf("Date: %s\n", token[3]);
printf("Time: %s\n", token[4]);
printf("Distance to reference: %2.2f\n", distance(dist));
}
else
{
printf("Error opening file. Check file and try again.");
exit(EXIT_FAILURE);
}
return 0;
}
最佳答案
您正在从指针 (lat_2 - LAT_1) 中减去 float 。这没有任何意义。
此外,您将其传递给 toRadian() ,它需要一个 double 值并且不返回任何内容......这都是错误的
关于c - '错误 : expected expression' keeps returning. 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029909/