- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目标状态:我应该显示随机结果,例如集合 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
的末尾 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/
在指向指针的指针上使用指针算术是否定义明确? 例如 int a=some_value; int* p=&a; int**p2=&p; 现在对 p2 执行算术是否是定义明确的行为?(例如 p2+1、p2
我正在尝试使用一个函数来替代 C 中的 scanf()。该函数是由第三方编写的,并进行了相应的定义: ScanDecimal16uNumber - Scans a decimal 16bit unsi
我正在尝试为 Sundials CVODE 编写 CFFI 包装器图书馆。 SWIG 被 Sundial header 阻塞,因为它们相互关联,并且 SWIG 找不到合适的 header ,所以我手工
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: pass by reference not working 我正在阅读一些教程 linklistproblem在互联
我有一个代码片段很难理解。 char *c; // c is uni dimensional table ( single row ) char **p ; // p is a two dimen
我正在将一些代码移植到 Windows 并且被难住了。有一些代码在启动时自动运行以将指针复制到指针,并在退出时再次运行以删除指向指针的指针(如果它不为空)。 我已经创建了一个示例程序来重现该行为 in
将非 const 指针转换为 const 指针是合法的。 那为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; con
将非 const 指针转换为 const 指针是合法的。 那为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; con
将指向非常量的指针转换为指向常数的指针是合法的。 那么为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; const
之间有什么区别 procedure(some_routine), pointer :: ptr ptr => null() 和 procedure(some_routine), pointer ::
只是为了消除一些困惑。我最近遇到了这段代码(使用指针到指针): int encode(unsigned char type, uint64_t input_length, unsigned char*
我已经阅读了我能找到的有关 C/C++ 指针的内容,但其中大部分是介绍性的,虽然它可以帮助您理解它们的使用,但在许多情况下,现有代码会抛出难以破译的示例。 我确实看到了一些例子,他们将一行代码分解成它
我一直在关注的学习数据结构的书使用“单指针”作为函数中的参数,这些函数在链表的不同位置添加新节点,例如在开始,在结束。同样在删除的情况下使用“pointer-to-pointer”。在所有这些情况下,
考虑这段代码: #define MAX 4 ............ ............ int** ptr = (int**)malloc(sizeof(int*)*MAX); *ptr =
如何将指向 void 对象的指针转换为类对象? 最佳答案 使用 static_cast。请注意,只有当指针确实指向指定类型的对象时,您才必须这样做;也就是说,指向 void 的指针的值取自指向此类对象
我假设一种语言的实现允许您将指针视为整数,包括对它们进行标准算术。如果由于硬件限制这是不现实的,请告诉我。如果编程语言通常没有这么强大的指针运算,但是在实践中是可行的,那么我仍然想知道这种实现BigI
我是一名 nodejs 开发人员,我通常为我的应用程序使用一个结构,该结构包含一个配置包/对象,该对象包含对我常用的库和配置选项的引用。通常,此配置对象也包含我的数据库连接,并且可以通过我的应用程序访
我已经在几个上下文中阅读过“胖指针”这个术语,但我不确定它的确切含义以及它何时在 Rust 中使用。指针似乎是普通指针的两倍,但我不明白为什么。它似乎也与特征对象有关。 最佳答案 术语“胖指针”用于指
这是让我困惑的代码。 static char *s[] = {"black", "white", "pink", "violet"}; char **ptr[] = {s+3, s+2, s+1, s
通用指针允许您创建指向指针的指针: void foo(Object **o) {} int main() { Object * o = new Object(); foo(&o); } s
我是一名优秀的程序员,十分优秀!