我正在尝试比较两个输入文件中的数据,看看一组是否小于另一组

发布于 2024-12-18 18:01:25 字数 907 浏览 1 评论 0原文

我正在尝试将一个文件中的数据与另一个文件中的数据进行比较,看看它是否“适合”另一个文件。我认为一组是“洞”,另一组是“块”。我想看看所有圆块是否都适合至少一个圆孔。如果它们都适合,我想输出“YES”。如果有一个不适合,我想输出“NO,所有圆形块至少不适合一个孔”。

这是我的代码:

    bool fits = false;

    for (int i=0; i<M; i++)
    {
      if (btype[i] == Circles)
      {
        CA[i] = (3.14 * (D[i] * D[i]));
      }

      for (int j=0; j<N; j++)
      {
        if (htype[j] == Circle)
        {
          CA[j] = (3.14 * (A[j] * A[j]));
        }

        if (CA[i] < CA[j])
        {
          fits = true;
          break;
        }
      }

      if (!fits)
      {
        break;
      }   
    }
    if (fits)
    {
      cout << "5. YES, all circle blocks fit a hole. "
           << endl;
    }
    else
    {
      cout << "5. NO, all circle blocks don't fit a hole. "
           << endl;
    }

代码运行不正确。当我将一个圆圈值放入输入文件中时,程序肯定会输出“否”,但它却给出“是”。我想不通。

I'm trying to compare data from one file to another to see if it "fits" inside the other. I'm thinking of one set as "Holes" and the other as "Blocks." I'm trying to see if all circle blocks fit inside at least one circle hole. If they all fit, I want to output "YES." If one does not fit, I want to output "NO, all circle blocks do not fit into at least one hole."

Heres my code:

    bool fits = false;

    for (int i=0; i<M; i++)
    {
      if (btype[i] == Circles)
      {
        CA[i] = (3.14 * (D[i] * D[i]));
      }

      for (int j=0; j<N; j++)
      {
        if (htype[j] == Circle)
        {
          CA[j] = (3.14 * (A[j] * A[j]));
        }

        if (CA[i] < CA[j])
        {
          fits = true;
          break;
        }
      }

      if (!fits)
      {
        break;
      }   
    }
    if (fits)
    {
      cout << "5. YES, all circle blocks fit a hole. "
           << endl;
    }
    else
    {
      cout << "5. NO, all circle blocks don't fit a hole. "
           << endl;
    }

The code is not acting right. When I put a circle value into the input file that should definitely make the program cout a NO, it give me a YES. I can't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-12-25 18:01:25

您有 if 语句在最后一个 for 循环内进行打印。

for (int j=0; j<N; j++)
{
   if (htype[j] == Circle)
   {
      CirArea[j] = (3.14 * (A[j] * A[j]));
   }


   if (CirArea[i] < CirArea[j])
   {
      cout << "5. YES, all circle blocks fit in a hole. " << endl;
   }
   else
   {
      cout << "5. NO, all circle blocks don't fit a hole. " << endl;
   }
}

如果您想根据一条是否不适合来打印消息,如果全部适合则打印另一条消息,您可以这样做。

float fits = 0.0;
float nofit = 0.0;
bool result;

for (int i=0; i<M; i++)
{
  result = false;

  if (btype[i] == Circles)
  {
    CirArea[i] = (3.14 * (D[i] * D[i]));
  }

  for (int j=0; j<N; j++)
  {
    if (htype[j] == Circle)
    {
      CirArea[j] = (3.14 * (A[j] * A[j]));
    }

    if (CirArea[i] < CirArea[j])
    {
       result = true;
       //exit inner loop
       break;
    }
  }

  if (!result)
  {
     //exit outer loop
     break;
  }
}

if (result)
{
    cout << "5. YES, all circle blocks fit in a hole. " << endl;
}
else
{
    cout << "5. NO, all circle blocks don't fit a hole. " << endl;
}

You have the if statement that does the printing inside the last for loop.

for (int j=0; j<N; j++)
{
   if (htype[j] == Circle)
   {
      CirArea[j] = (3.14 * (A[j] * A[j]));
   }


   if (CirArea[i] < CirArea[j])
   {
      cout << "5. YES, all circle blocks fit in a hole. " << endl;
   }
   else
   {
      cout << "5. NO, all circle blocks don't fit a hole. " << endl;
   }
}

If you want to print the message depending on if one doesn't fit, and if all fit then print another message you can do it like this.

float fits = 0.0;
float nofit = 0.0;
bool result;

for (int i=0; i<M; i++)
{
  result = false;

  if (btype[i] == Circles)
  {
    CirArea[i] = (3.14 * (D[i] * D[i]));
  }

  for (int j=0; j<N; j++)
  {
    if (htype[j] == Circle)
    {
      CirArea[j] = (3.14 * (A[j] * A[j]));
    }

    if (CirArea[i] < CirArea[j])
    {
       result = true;
       //exit inner loop
       break;
    }
  }

  if (!result)
  {
     //exit outer loop
     break;
  }
}

if (result)
{
    cout << "5. YES, all circle blocks fit in a hole. " << endl;
}
else
{
    cout << "5. NO, all circle blocks don't fit a hole. " << endl;
}
情释 2024-12-25 18:01:25

由于带有 cout 的行位于具有 N 次迭代的 for 循环内,该循环位于另一个具有 for 循环内strong>M 次迭代,看起来应该打印 M * N

As the lines with cout are inside the for loop with N iterations, which is inside another for loop with M iterations, looks like it should print M * N lines

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文