简体   繁体   English

检查点是否位于 x 轴、y 轴或原点的程序

[英]program to check whether point lies on x-axis , y-axis or origin

I have just started learning c programming.我刚开始学习c编程。

For the question, I wrote the following code.对于这个问题,我写了以下代码。 Can you please help me find the error?你能帮我找出错误吗? I am not getting the desired result and the statement in the last else gets executed always.我没有得到想要的结果,最后一个 else 中的语句总是被执行。

#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();
}

while reading values from keyboard with scanf you need to add & infront of variable.使用scanf从键盘读取值时,您需要在变量前添加&

instead相反

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

Use使用

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

Update更新

you dont have to check every time for both y and x您不必每次都检查yx

instead if(x==0 && y!=0) use only one, if(x==0) or if(y==0) Try:相反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();
}

要检查是否相等,请使用宏或函数

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

Undefined reference to clrscr() function.对 clrscr() 函数的未定义引用。 So, you can try removing clrscr() function from your code.因此,您可以尝试从代码中删除 clrscr() 函数。

#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;
  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();
 }

It will work.它会起作用。

A simple solution may be as一个简单的解决方案可能是

#include <stdio.h>

int main()
{

    int x,y;
    printf("Enter the point ");
    scanf("%d,%d",&x,&y);
    if (y==0)
    {
        if (x==0)
        {
            printf("\nPonit lies on the origin\n");
        }
        else
            printf("\nPoint lies on X-axis\n");
    }
    else if (x==0)
    {
        if (y==0)
        {
            printf("\nPoint lies on the origin\n");
        }
        else
            printf("\nPoint lies on the Y-axis\n");
    }
    else
        printf("\nPoint lies in X-Y coordinate\n");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM