低效的学校工作守则;有谁能告诉我这有什么问题?

时间:2014-03-26 01:11:52

标签: c runtime-error

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

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

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

不允许修改主页。

我真的不知道该怎么做,我希望有人能够发现我似乎无法找到的逻辑问题。在解决问题之前,我需要弄清楚问题。我感谢您提供的任何帮助。

此外,不允许修改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
}

1 个答案:

答案 0 :(得分:0)

  

我遇到的问题是我的Total_distance变量不存在   更新了,我相当肯定,因为我的第146行是有条件的   分支没有被采取

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

幸运的是你的程序可以编译,我在行后添加了printf:

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

然后进行测试,它就进入了!

所以真正的原因是条件if (Prior_Road != Line)总是如此,你想要的是什么?因为我发现更新行处于&#34;始终为真的状态&#34; (是支撑问题吗?),因此Total_distance没有改变。

一点建议: 尽可能调试,如果这样简单,添加一些打印可以工作,或者一步一步调试。