gpt4 book ai didi

c - 低效的学业守则;谁能告诉我这有什么问题吗?

转载 作者:行者123 更新时间:2023-11-30 21:11:25 26 4
gpt4 key购买 nike

所以我一直在为我的学校 C 类编写一个程序(它不漂亮,效率不高,但它需要工作。)任务是创建一个简化的 GPS 跟踪器模拟程序,该程序需要未知的运动坐标 (x,y),并通过在最接近输入坐标的线上创建一个点,将这些坐标映射到 2 条不同的线(道路)上。在线上创建的点是该线最接近输入坐标的点。

该程序应该跟踪所有方向改变的时间,并且还应该跟踪沿着道路“行驶”的总距离。我被困在作业的最后一部分,要求我实现方向改变(从一条路到另一条路)——请注意,道路改变的距离是到道路交叉口的距离以及转弯后行驶的距离.

我遇到的问题是我的 Total_distance 变量没有被更新,我相当确定这是因为我的第 146 行条件分支没有被采用。

Main 不允许修改。

我真的不知道该怎么做,我希望有人能够发现我似乎找不到的逻辑问题。我需要先找出问题所在,然后才能解决它。我感谢您提供的任何帮助。

此外,不允许修改 main()。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Gobal Variables */
double M1; // Road 1 slope
double B1; // Road 1 y-intercept
double M2; // Road 2 slope
double B2; // Road 2 y-intercept

#ifndef SECRET_TESTS
double M1=-1.0, B1=0; // Road 1 slope and intercept
double M2=1.0, B2=0; // Road 2 slope and intercept
#else
// This allows the program to be tested easily with different roads
double M1=SECRET_M1, B1=SECRET_B1;
double M2=SECRET_M2, B2=SECRET_B2;
#endif

int Line=1, First_line=1;
int Changed_direction=0;
double Total_distance=0;

/* -----------------------*/


/* Function Declarations */
void track(double x, double y);
double distance(double x1, double y1, double x2, double y2);
double nearest_point_on_line(int line, double x, double y);
double distance_to_line(int line, double x, double y);
int closest_line(int line, double x, double y);
/* -----------------------*/



int main() {

double x, y;
int ret;

do {
// get x,y coordinate from stdin
ret = scanf("%lf%lf",&x,&y); // ret = number of inputs successfully recieved

if (ret==2)
{
// call into the tracking algorithm
track(x,y);
}

// if we didn't get two doubles, time to quit
} while(ret==2);

// print out final results
printf("Total distance = %.2lf\n",Total_distance);
printf("# of changes in direction = %d\n",Changed_direction);
printf("First Road = %d, Last Road = %d\n",First_line,Line);

return 0;
}

void track(double x, double y){
static int count = 1; // global variable; stores the total number of times the track function has been called
static double Prior_x =0.0; // global variable; storing previous x road coordinate
static double Prior_y = 0.0; // global variable; storing previous y road coordinate
static double Prior_delta = 0.0; // sign of previous (x-Prior_x)
static int Prior_Road = 0;
double Current_delta = 0.0; // sign of previous (x-Prior_x)
double Current_Road_M = 0.0;
double Current_Road_B = 0.0;
double Prior_Road_M = 0.0;
double Prior_Road_B = 0.0;

//CONDITIONALS FOR FIRST TIME TRACKING

//printf("DEBUG: Raw input to Track. x: %lf, y: %lf\n", x, y);
if (count == 1){
//printf("DEBUG: Track is running for the first time.\n");
switch (Line) //Set appropriate slope and y-intercept for road line
{
case 1:
Current_Road_M = M1;
Current_Road_B = B1;
break;
case 2:
Current_Road_M = M2;
Current_Road_B = B2;
break;
}
//printf("DEBUG: Getting slope and y-intercept of Line.\nDEBUG: Line: %d, Current_Road_M: %lf, Current_Road_B: %lf\n", Line, Current_Road_M, Current_Road_B);
First_line = Line = closest_line(Line, x, y);
//printf("DEBUG: D(Prior_Road != Line)etermining true first line. First_line: %d\n", First_line);
switch (Line) //Recalculate appropriate slope and y-intercept for road line
{
case 1:
Current_Road_M = M1;
Current_Road_B = B1;
break;
case 2:
Current_Road_M = M2;
Current_Road_B = B2;
break;
}
//CONVERT INPUT COORDINATES INTO A ROAD POINT
x = nearest_point_on_line(Line, x, y); //get road x-coordinate
y = x * Current_Road_M + Current_Road_B; //calculate road y-coordinate
}
else{
Line = closest_line(Line,x,y);
switch (Line) //Set appropriate slope and y-intercept for road line
{
case 1:
Current_Road_M = M1;
Current_Road_B = B1;
break;
case 2:
Current_Road_M = M2;
Current_Road_B = B2;
break;
}

//CONVERT INPUT COORDINATES INTO A ROAD POINT
x = nearest_point_on_line(Line, x, y); //get road x-coordinate
y = x * Current_Road_M + Current_Road_B; //calculate road y-coordinate

//ADD DISTANCE IF NO ROAD CHANGE (Distance for a road change is done below)
if (Prior_Road == Line){
Total_distance += distance(Prior_x, Prior_y, x, y); //calculate distance between new position and old position; add calculated distance to total distance
}
//printf("DEBUG:Track is running.\n");
//printf("DEBUG: Prior_x: %lf, Prior_y: %lf, x: %lf, y: %lf\n", Prior_x, Prior_y, x, y);

//DEFINE CURRENT_DELTA
if ((x - Prior_x) < 0){
Current_delta = -1;
}
else {
if ((x - Prior_x) > 0){
Current_delta = 1;
}
}

//DEFINE ROAD AND DIRECTION CHANGES
if (count >= 3){
if (((Current_delta != Prior_delta) && (Current_delta !=0) && (Prior_delta != 0)) || (Prior_Road == Line)){
Changed_direction++;
//IF ROAD CHANGE
if (Prior_Road != Line){
//GET PRIOR_ROAD VALUES
switch (Prior_Road) //Set appropriate slope and y-intercept for road line
{
case 1:
Prior_Road_M = M1;
Prior_Road_B = B1;
break;
case 2:
Prior_Road_M = M2;
Prior_Road_B = B2;
break;
}
//printf("DEBUG: Prior_Road: %d, Prior_Road_M: %lf, Prior_Road_B: %lf\n", Prior_Road, Prior_Road_M, Prior_Road_B );
//CALCULATE INTERSECTION
double x_intersect = (Prior_Road_B - Current_Road_B) / (Current_Road_M - Prior_Road_M);
double y_intersect = (x_intersect * Current_Road_M) + Current_Road_B;

//CALCULATE DISTANCE TO ADD
Total_distance += distance(Prior_x, Prior_y, x_intersect, y_intersect);
Total_distance += distance(x_intersect, y_intersect, x, y);

}

}
}
}
Prior_Road = Line;
Prior_delta = Current_delta;
Prior_x = x;
Prior_y = y;
count++;
//DEBUG:printf("Track is running for the first time.\n");

return;}

double distance(double x1, double y1, double x2, double y2){
double delta_x = x2 - x1; //calcuate change in the x position
double delta_y = y2 - y1; //calcuate change in the y position
double distance = sqrt(pow(delta_x,2) + pow(delta_y,2)); //pythagorean theorem to determine distance between two points
return distance;}

double nearest_point_on_line(int line, double x, double y){
//Define a test line containing the input point and
//find the intersection of the test line and the road
double M_test = 0.00;
double B_test = 0.00;
double M_line = 0.00; //holds slope of current road line
double B_line = 0.00; //holds y-intercept of current road line

switch (line) //For line #)
{
case 1:
M_line = M1;
B_line = B1;
break;
case 2:
M_line = M2;
B_line = B2;
break;
}
M_test= -1/M_line;
B_test = -((M_test * x) - y);
x = (B_test - B_line)/(M_line - M_test);
return x;
}

double distance_to_line(int line, double x, double y){
double x_line = 0.0;
double y_line = 0.0;
double M_line = 0.00; //holds slope of current road line
double B_line = 0.00; //holds y-intercept of current road line
switch (line) //For line #)
{
case 1:
M_line = M1;
B_line = B1;
break;
case 2:
M_line = M2;
B_line = B2;
break;
}
x_line = nearest_point_on_line(line, x, y);
y_line = x_line * M_line + B_line;
return distance(x_line, y_line, x, y);
}

int closest_line(int line, double x, double y){
//Compare the distance between the point and each line, return the value of the closest line;
if (distance_to_line(1,x,y) < distance_to_line(2,x,y)){
//printf("DEBUG: closest_line returned 1\n");
return 1;
}
else if (distance_to_line(2,x,y) < distance_to_line(1,x,y)){
//printf("DEBUG: closest_line returned 2\n");
return 2;
}
//if the distances are equal, return the value of the current active line
else if (distance_to_line(2,x,y) == distance_to_line(1,x,y)){
//printf("DEBUG: closest_line returned line\n");
return line; //value of current line
}
// if none of these cases hold, we have some serious issues...
else return -1; //error
}

最佳答案

The problem I'm having is that my Total_distance variable is not being updated, and I am fairly certain it's because my line 146 conditional branch is not being taken

调试知道原因并不难。由于您怀疑第 146 行条件分支未被采用,因此添加一些 printf 来查看发生了什么。

幸好你的程序可以编译,我在这行后面加了printf:

if (((Current_delta != Prior_delta) && (Current_delta !=0) && (Prior_delta != 0)) || (Prior_Road == Line))

再测试一下,就进入了!

所以真正的原因是条件 if (Prior_Road != Line) 始终为 true,这是您想要的吗?因为我发现更新行处于“始终为真状态”(这是大括号问题吗?),所以 Total_distance 没有更改。

一点建议:尽可能调试,如果像这样简单的话,添加一些打印就可以了,或者一步步调试。

关于c - 低效的学业守则;谁能告诉我这有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com