gpt4 book ai didi

c++ - 错误 : Display duplicated results via pointer

转载 作者:行者123 更新时间:2023-11-30 03:31:59 24 4
gpt4 key购买 nike

目标状态:我应该显示随机结果,例如集合 S = {dog, cow, chicken...} 其中随机大小可以是 1-12 并且动物不能被复制所以一旦有牛,集合 S 中就不能再有另一头牛。

错误:我一直在显示 1-12 的正确随机大小。但是,即使我在将动物插入集合 S 之前尝试检查该动物是否存在于集合 S 中,我也有重复的动物。

更新:在 stackoverflow 同行进行各种更新后,我无法让它运行。

约束:我必须使用指针与指针进行比较——动态地。“重要的提示用于阵列的所有存储都应该动态创建;并在什么时候删除它们他们不再需要。访问数组的元素时,您应该通过指针访问它,即通过取消引用此指针。使用符号,例如 set [k] 或 *(set + k)不允许访问集合的第 k 个元素。”

希望听到你的建议, friend 们!

最好的问候,毫米

/* 
MarcusMoo_A2.cpp by Marcus Moo
Full Time Student
I did not pass my assignment to anyone in the class or copy anyone’s work;
and I'm willing to accept whatever penalty given to you and
also to all the related parties involved
*/

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

/* Global Declaration */
const int MAX = 12; // 12 animals
const int MAXSTR = 10;

typedef char * Element;
static Element UniversalSet [MAX] = {"Rat", "Ox", "Tiger", "Rabbit", "Dragon",
"Snake", "Horse", "Sheep", "Monkey", "Rooster", "Dog", "Pig"};

/* Functions */

// Construct a set
void option0(int); // Menu Option 0
void constructSet (Element *, int); // Construct a set
bool checkElement (Element *, Element *, int); // Check element for replicates

int main()
{
// Declarations
int mainSelect;

int size=rand()%12+1; // Random construct


srand (time(NULL)); // Even better randomization

cout << "Welcome to MARCUS MOO Learning Center" << endl;

do
{
cout << "0. An example of set" << endl;
cout << "1. Union" << endl;
cout << "2. Intersection" << endl;
cout << "3. Complement" << endl;
cout << "4. Subset of" << endl;
cout << "5. Equality" << endl;
cout << "6. Difference " << endl;
cout << "7. Distributive Law" << endl;
cout << "9. Quit" << endl;
cout << endl;

if (mainSelect==0)
{
option0(size);
}

cout << "Your option: ";
cin >> mainSelect;
cout << endl;

} while(mainSelect!=9);

return 0;
}

/* Functions */

// Option 0 - An example of set
void option0 (int size)
{
// Mini Declaration
int again;
Element *S;

do
{
cout << "Here is an example on set of animals" << endl;
cout << endl;

// Build set S

constructSet (S,size);


// Display set S
Element *S = &S[0];

cout << "Set S = {";

for (int i = 0; i < size; i++)
{
if (i!=size)
{
cout << *S
<< ", ";
}
else
{
cout << *S
<< "}"
<< endl;
}

S++;
}


cout << endl;
cout << "Note that elements in S are distinct are not in order" << endl;
cout << endl;

// Option 0 2nd Part
cout << "Wish to try the following operations?" << endl;
cout << "1. Add an element to the set" << endl;
cout << "2. Check the element in the set" << endl;
cout << "3. Check the cardinality" << endl;
cout << "9. Quit" << endl;
cout << endl;
cout << "Your choice: ";
cin >> again;

} while (again!=9);
}

// Construct a set
void constructSet (Element *set, int size)
{
// Declarations
Element *ptrWalk;
ptrWalk = &set[0];
int randomA=0;

for (int i = 0;i<size;i++)
{
bool found = true;
while (found)
{
randomA = rand()%MAX; // avoid magic numbers in code...
*ptrWalk = UniversalSet [randomA];

// Ensure no replicated animals in set S
found = checkElement (ptrWalk, set, i);
}
set=ptrWalk;
set++;
}
}

bool checkElement (Element *ptrWalk, Element *set, int size)
{
for (int j=0; j<size;j++)
{
if (ptrWalk==&set[j])
{
return true;
}
}
return false;
}

最佳答案

您的代码中有 2 个不同的主要问题。 Federico 已经给出了第一个:checkElement应在找到一个元素后立即返回 true。代码应该变得简单(但请注意 < 中的 j<size ):

bool checkElement (char *ptrWalk, int size)
{
for (int j=0; j<size;j++)
{
if (ptrWalk==S[j])
{
return true;
}
}
return false;
}

第二个问题是你不应该搜索整个数组,而应该只搜索已经填充的部分。这意味着在 constructSet你应该调用checkElement(ptrWalk, i)因为当前元素的索引是已经填充的项目数。所以你必须更换两次线

    found = checkElement (*ptrWalk, size);

这个

    found = checkElement (*ptrWalk, i);

这应该足以让您的程序产生预期的结果。不过想要好看的话,还是有一些改进的:

  • 你正确地声明了int main()但忘了 return 0;main 的末尾
  • 在函数定义之前调用它们时,您未能转发声明函数(至少应该引起警告...)
  • 你大量使用全局变量,这不是一个好习惯,因为它不允许简单的测试
  • 应简化您的算法以遵循“不要重复自己”原则。代码重复不利于 future 的维护,因为如果强制在不同的地方应用代码更改而遗漏这样做会导致讨厌的错误(看起来这很糟糕但我已经修复了它 - 是的但只在一个地方......)

constructSet可以简单地是:

// Construct a set 
void constructSet (Element *set, int size)
{
// Declarations
//Element *ptrBase;
voidPtr *ptrWalk;
ptrWalk = &set[0];
int randomA=0;

for (int i = 0;i<size;i++)
{
bool found = true;
while (found) {
randomA = rand()%MAX; // avoid magic numbers in code...
*ptrWalk = UniversalSet [randomA];

// Ensure no replicated animals in set S
found = checkElement (*ptrWalk, i);
}
ptrWalk++;
}
}

关于c++ - 错误 : Display duplicated results via pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com