C - 将数字列表与数组进行比较

时间:2015-02-28 06:22:36

标签: c

我正在获取字符串数字列表,并将它们与数组中的字符串数字列表进行比较。当列表中的数字与数组中的数字匹配时,将显示“匹配找到”。对于匹配的数字,不应显示任何其他内容。对于数字6,显示“未找到匹配”。

表: 12346

阵列: 12345

注意:

while(not end of file)
{
  for(i = 0; i < arraycount; i++)
  {
    if(strcmp(numberlist.numbers,array[i].numbers) == 0)
      //Display "Match Found"
  }
}

我不确定在这一点之后该怎么做,或者我是否接近这个权利。如果我在if语句后面放了一个显示“Not Found”的else语句,那么就会显示“Match Found”数字。

我不想要的例子:

1 Match Found Not Found Not Found Not Found Not Found
2 Not Found Match Found Not Found Not Found Not Found
3 Not Found Not Found Match Found Not Found Not Found
4 Not Found Not Found Not Found Match Found Not Found
6 Not Found Not Found Not Found Not Found Not Found

我想要的例子:

1 Match Found
2 Match Found
3 Match Found
4 Match Found
6 Not Found

这适用于初学者课程,因此我必须将代码保持在初学者水平。

编辑:

解决方案

while(not end of file)
{
  for(i = 0; i < arraycount; i++)
  {
    found = 0;
    if(strcmp(numberlist.numbers,array[i].numbers) == 0)
    found = 1;
    //Display "Match Found"
  }
  if (found != 1)
  //Display "Not Found"
}

1 个答案:

答案 0 :(得分:0)

<强>解决方案

while(not end of file)
{
  for(i = 0; i < arraycount; i++)
  {
    found = 0;
    if(strcmp(numberlist.numbers,array[i].numbers) == 0)
    found = 1;
    //Display "Match Found"
  }
  if (found != 1)
  //Display "Not Found"
}