2012-03-03 28 views
0

這是一個簡單的程序來計算行的長度。該計劃的所有計算方面都已完成並正在開展工作。 但是我遇到了一個非常簡單的問題,第10行聲明瞭變量「percent_detoured」,第83行使用了變量,並在第84行打印出來。問題是執行時,其他一切正常運行,但percent_detoured始終返回0.000。調試時,percent_detoured甚至不會在「本地」中顯示爲變量。 如果有幫助,我正在使用Pelles C IDE。聲明變量不顯示在調試器的本地

感謝您的幫助!

{ 
float line_value;  
int x_1, y_1, x_2, y_2;   
float y_intercept;  
float distance, greatest_distance, total; 
float percent_detoured;    // <-- DECLARED HERE (SCROLL ALL THE WAY DOWN TO SEE WHERE IT IS USED) 
int count, detour_count;   

x_1=1; y_1=1; x_2=1; y_2=1;  
greatest_distance = 0; total = 0; count = 0; detour_count = 0; 

/* Ask for user input in setting the coordinates of the line */ 
printf("Enter in the length of the line\n"); 
scanf("%f", &line_value); 


/* If user enters in y coordinates for the line such that the line length is greater 
    than 10 units, then enter loop and continue looping until the length is smaller or 
    equal to 10 */ 
while (line_value > 10 || line_value < 0) 
{ 
    printf("Try Again. Value should be smaller or equal to 10 units\n"); 
    scanf("%f", &line_value); 
} 

line_value = line_value/2;   //Define the top of the line 

printf("Enter x and y coordinates for point 1 and 2\n"); 
scanf("%d %d %d %d", &x_1, &y_1, &x_2, &y_2); 

if (x_1 == 0 && x_2 == 0 && y_1 == 0 && y_2 == 0)  //If user enters in 0 0 0 0 right away, then immediately end the program 
{ 
    printf("No path lengths have been calculated\n"); 
} 

else   
{ 
    /* While the user does not enter in the values 0 0 0 0 for all coordinates, continue while loop */ 
    while ((x_1 != 0) || (y_1 != 0) || (x_2 != 0) || (y_2 != 0)) 
    { 
     y_intercept = y_1 - ((y_2 - y_1)/(x_2 - x_1) * x_1); //Rearrange (y=mx+b) to get (b=y-mx) where b is the y intercept 

     if (y_intercept <= line_value && y_intercept >= 0) //If the y-intercept of the line from point a to b is between 
     {               //the top of L and 0, then calculate distance between a and top of L 
      distance = sqrt(pow(x_1, 2) + pow((y_1 - line_value), 2)) 
         + sqrt(pow(x_2, 2) + pow((line_value - y_2), 2)); 
      detour_count++;     //Updating the detour counter 
     } 
     else if (y_intercept >= (line_value * -1) && y_intercept < 0) //If the y-intercept of the line from point a to b is between 
     {                 //the bottom of L and 0, then calculate distance between a and bottom of L 
      distance = sqrt(pow(x_1, 2) + pow((y_1 + line_value), 2)) 
         + sqrt(pow(x_2, 2) + pow((y_2 - (line_value * -1)), 2)); 
      detour_count++;     //Updating the detour counter 
     } 
     else 
     { 
      distance = sqrt(pow((x_2 - x_1), 2) + pow((y_2 - y_1), 2)); //If shortest path between point a and b does not cross L, 
     }                 //then the distance is just the sum of squares of rise and run 

     if (distance > greatest_distance) 
     { 
      greatest_distance = distance; //If the current distance is greater than the previous, set it as the greatest distance 
     } 

     total = total + distance;  //Total count of distance 
     count++;      //Update general counter 

     printf("The shortest path is: %f\n", distance); 

     printf("Enter x and y coordinates for point 1 and 2\n"); 
     scanf("%d %d %d %d", &x_1, &y_1, &x_2, &y_2); 
    } 

} 

printf("\nThe average path length: %f\n", (total/count));  
printf("The length of the longest path: %f\n", greatest_distance); 

percent_detoured = ((detour_count/count) * 100);  // <-- USED IT HERE 
printf("The percentage of paths involving a detour: %f\n", percent_detoured); // <-- SHOULD BE PRINTED AS ITS VALUE HERE 

return 0; 

}

回答

0

您應該使用

 
percent_detoured = ((float)detour_count/count) * 100.0); 

否則,既detour_count和計數是整數,和INT通過INT分會給一個int。因此,假設detour_count <計數,detour_count/count將舍入爲0,再乘以100(仍爲0),並最終以四捨五入方式將其舍入爲float(percent_detoured)。

我從來沒有使用過你提到過的IDE,所以我不知道爲什麼它可能不會顯示在調試器中。 :)

+0

太棒了! – 2012-03-03 17:39:54

+0

謝謝,我現在明白了。該變量仍然不顯示在調試中,但沒關係! – 2012-03-03 17:41:21