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

标签 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 语句之后放置一个显示“未找到”的 else 语句,那么它会显示为“找到匹配”数字。

我不想要的示例:

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

这是针对初学者的类(class),因此我必须将代码保持在初学者水平。

编辑:

解决方案

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"
}

最佳答案

解决方案

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"
}

关于C - 将数字列表与数组进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28778849/

相关文章:

c - 如果 nelem 或 elsize == 零,为什么 calloc 分配 1 个字节?

c - 如何找出已连接的 tcp 套接字发送的字节数?

python - 正确地为 c 包装器制作 setup.py

c - 单次遍历中链表的中点?

c - 在 RPI 交叉编译器上链接 libwiringPi

c - 使用具有不同结构定义的相同代码

c - 从函数返回矩阵

c - 我的 int isMember() 上的循环函数

c++ - 二维数组的行为

c - 同时访问一个 c union 的不同成员