将第一个元素与n个元素进行比较(如果它是唯一的(C中的结构) [英] Compare first element to n elements if it is unique (struct in C)

查看:83
本文介绍了将第一个元素与n个元素进行比较(如果它是唯一的(C中的结构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C中的数据库来提示用户输入产品的唯一标识符代码(UID),例如int值为1,第一项为2,下一项为3 ......等等......



然而,当我尝试比较每个UID if语句没有捕获另一个项具有相同UID的错误。



例如



项目1:UID 1

项目2:UID 1 //这是我正在处理的错误



程序应该警告用户项目2的UID不是唯一的并且输入一个正确的价值,直到他们这样做。 (这是对函数的递归调用)。



结果应该是:



项目1:UID 1

项目2:UID 2等...



我将非常感谢任何帮助



我尝试过:



 //结构模板省略
int num_of_items = 0;
//在
fprintf(stdout,输入项目%d \ n的唯一标识符,num_of_items)下输入项目详细信息;
scanf(%d,& unique_ID);
fflush(stdin);

库存[num_of_items] .item_ID = unique_ID;

//检查每个库存项目的ID是否唯一
if(inventory [num_of_items + 1] .item_ID == inventory [num_of_items] .item_ID)
{
fprintf(stderr,%s \ n,你必须输入一个唯一的标识符);
add_new_item(库存);
}
其他
{
//提示用户输入值
}

解决方案

嗯不,它不会。

只检查你刚插入的元素之后的元素,看看它是否与你刚刚插入的元素相同!



如果你想检查那么你会以错误的方式去做:检查身份证不存在 之前 你把它放在那里!



首先写一个方法找到一个ID在列表中。

< pre lang =c ++> MyStruct * Find( int ID)
{
int i;
for (i = 0 ; i< num_of_items; i ++)
{
if (inventory [i] .item_ID == ID)
{
return &(inventory [i]);
}
}
return (MyStruct *) 0 ;
}

现在调用它,传递新的唯一ID并查看它返回的内容。如果它返回一个空指针,它不在列表中,你可以添加它:

 MyStruct * pms = Find(unique_ID); 
if (pms ==(MyStruct *) 0
{
inventory [num_of_items ++] = ... // 带有ID的新元素。
}
else
{
// 告诉他它存在。
}


您的代码完全错误,但因为它看起来像HomeWork没有完整的爆炸解决方案。

以下是如何组织代码

循环直到用户输入UID 
提示用户为新ID
获取用户输入
循环库存中的每个项目
比较商品ID与新ID
如果匹配,新ID不唯一,再次询问
循环结束
循环结束
在库存中插入新ID





当你不明白你的代码是什么正在做或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


I'm working on a database in C that prompts the user to enter a unique identifier code (UID) for a product e.g. an int value of 1, for the first item, 2 for the next, 3...etc..etc...

However, when I try compare each UID the if statement doesn't catch the "error" that another item has the same UID.

E.g.

Item 1 : UID 1
Item 2 : UID 1 // this is an error that I'm working on at the moment

The program should then warn the user that item 2's UID is not unique and to enter a "correct" value until they do so. (this is the recursive call to the function).

The result should then be:

Item 1 : UID 1
Item 2 : UID 2 etc...

I would appreciate any help greatly

What I have tried:

// struct templates omitted
int num_of_items = 0;
// enter item details below
fprintf(stdout, "Enter unique identifier for item %d\n", num_of_items);
scanf("%d", &unique_ID);
fflush(stdin);

inventory[num_of_items].item_ID = unique_ID;

// check if ID is unique for each inventory item
if (inventory[num_of_items + 1].item_ID == inventory[num_of_items].item_ID)
{
    fprintf(stderr, "%s\n", "You must enter a unique identifier");
    add_new_item(inventory);
}
else
{
    // prompt user for values
}

解决方案

Well no, it won't.
It only checks the element after the one you just inserted to see if that is the same as the one you just stuck in there!

If you want to check then you are going about it the wrong way: check that the ID doesn't exist before you put it in there!

Start by writing a method to locate an ID is in the list.

MyStruct* Find(int ID)
   {
   int i;
   for (i = 0; i < num_of_items; i++)
      {
      if (inventory[i].item_ID == ID)
         {
         return &(inventory[i]);
         }
      }
   return (MyStruct*) 0;   
   }

Now call that, passing it the new "unique" ID and see what it returns. If it returns a null pointer, it's not in the list, and you can add it:

MyStruct* pms = Find(unique_ID);
if (pms == (MyStruct*) 0)
   {
   inventory[num_of_items++] = ... // your new element with the ID.
   }
else
   {
   // Tell him it exists.
   }


Your code is plain wrong, but since it look like HomeWork, no full blowup solution.
Here is how you would organize your code

loop until user have entered a UID
    prompt user for new ID
    get user input
    loop for every item in inventory
        compare item ID with new ID
        if it match, new ID is not unique, ask again
    end of loop
end of loop
insert the new ID in inventory



When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于将第一个元素与n个元素进行比较(如果它是唯一的(C中的结构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆