【问题标题】:program to check whether point lies on x-axis , y-axis or origin检查点是否位于 x 轴、y 轴或原点的程序
【发布时间】:2017-08-11 11:14:21
【问题描述】:

我刚刚开始学习 c 编程。

对于这个问题,我编写了以下代码。你能帮我找出错误吗?我没有得到想要的结果,最后一个 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();
}

【问题讨论】:

  • 请编辑您的问题以包括导致​​错误行为的输入,以及实际和预期的输出。
  • 嗨。无论我输入的 2 个数字是什么,语句“该点既不在 x 也不在 y 轴上”都会被执行
  • 这就是你不检查scanf的返回值时必须发生的事情。
  • scanf 的返回值?
  • 永远不要直接比较浮点值以获得(不)相等性:就像这里所说的那样:stackoverflow.com/a/4858547/8051589。这可能是问题所在。

标签: c


【解决方案1】:

使用scanf 从键盘读取值时,您需要在变量前面添加&amp;

改为

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

使用

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

更新

您不必每次都检查yx

if(x==0 &amp;&amp; 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();
}

【讨论】:

  • 现在查看我的答案,一次只需要查看x==0y==0
【解决方案2】:

为了检查是否相等,请使用宏或函数,如

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

【讨论】:

    【解决方案3】:

    对 clrscr() 函数的未定义引用。因此,您可以尝试从代码中删除 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();
     }
    

    它会起作用的。

    【讨论】:

      【解决方案4】:

      一个简单的解决方案可能是

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

      【讨论】:

        猜你喜欢
        • 2022-11-26
        • 2013-12-03
        • 2014-10-30
        • 1970-01-01
        • 2020-09-08
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多