2017-08-11 117 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))