2017-08-11 64 views
1

我剛開始學習c編程。程序檢查點是否位於x軸,y軸或原點

對於這個問題,我寫了下面的代碼。你能幫我找到錯誤嗎?我沒有得到期望的結果,並且最後一個語句中的語句總是被執行。

#include<stdio.h> 
#include<conio.h> 

void dummy(float *a) 
{ 
    float b=*a; //perform some floating access 
    dummy (&b); //calling a floating point function 
} 

void main() 
{ 
    double x,y; 

    clrscr(); 

    scanf("%lf %lf",x,y); 

    if(x==0 && y!=0) 
    { 
    printf("The point lies on the y-axis."); 
    } 
    else if(y==0 && x!=0) 
    { 
    printf("The point lies on the x-axis."); 
    } 
    else if(x==0 && y==0) 
    { 
    printf("The point is the origin"); 
    } 
    else 
    { 
    printf("The point lies neither on the x nor the y axis "); 
    } 
    getch(); 
} 
+2

請編輯您的問題,包括輸入導致的錯誤行爲,以及實際和預期產出。 –

+0

嗨。無論是我輸入的2個數字,語句「該點既不在x也不在y軸上」被執行 –

+0

這是當你不需要時發生的事情;不檢查scanf的返回值。 –

回答

3

而與scanf從鍵盤讀取值,你需要添加&盈變量。

代替

scanf("%lf %lf",x,y); 

使用

scanf("%lf %lf",&x,&y); 

更新

你沒有檢查每次兩個yx

代替if(x==0 && y!=0)只使用一個,if(x==0)if(y==0)嘗試:

void main() 
{ 
    double x,y; 
    clrscr(); 

    scanf("%lf %lf",&x,&y); 

    if(x==0 && y==0) 
    { 
     printf("points lies on origin."); 
    } 
    else if(y==0) 
    { 
     printf("points lies on y-axis."); 
    } 
    else if(x==0) 
    { 
     printf("points lies on x-axis"); 
    } 
    else 
    { 
     printf("The point lies neither on the x nor the y axis "); 
    } 
    getch(); 
} 
+0

謝謝。但它仍然不起作用 –

+1

現在檢查我的答案,您需要一次只檢查'x == 0'或'y == 0' –

1

爲了檢查是否等於使用宏或功能類似

#define FEQUAL(x,y,err)   (fabs((x) - (y)) < (err))