2017-05-05 24 views
0

目標狀態:我應該顯示一個結果,設置S = {狗,牛,雞...}隨機大小可以是1-12,並且動物不能被複制,因此一旦有牛,就不會有S組中的另一頭牛了。錯誤:通過指針顯示重複結果

錯誤:我一直在顯示正確的隨機大小1-12。不過我有重複,即使我試圖檢查動物是否在集合S存在之前,我將其插入集合S

UPDATE動物:我不能讓它通過計算器同行的各種更新後運行。我不得不使用指針來與指針進行比較 - 動態。 「重要注意事項 用於陣列的所有存儲應該動態創建,並在不再需要它們時,將其刪除 訪問數組元素時,應該通過指針訪問它,即通過 解除引用此指針。使用符號,例如set [k]或*(set + k) 訪問該集合的第k個元素是不允許的。

希望聽到你的建議,好朋友!

最好的問候, MM

/* 
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; 
} 
+3

checkElement應該儘快找到一個重複返回true,否則將始終返回false,除非它被複制 –

+0

恕我直言typedeffing內建類型,然後不使用的typedef始終是超級混亂的最後一個元素。例如。 'ptrWalk == S [j]'比較'Element'和'char *',它們實際上是相同的 – user463035818

+1

@ tobi303瞭解! –

回答

0

我已經在我的C++講師的指導下解決了這個問題!你們可能會從中得到一個參考來解決你下一次指向困境的指針!乾杯!

/* 
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 

// This function is to get a random element 
// with storage allocated 
Element getAnElement() 
{ 
    Element *p = &UniversalSet [0]; 
    int k = rand() % MAX; 

    for (int i = 0; i < k; i++) 
     ++p; 

    Element e = new char [MAXSTR]; 
    strcpy (e, *p); 

    return e; 
} 

int main() 
{ 
    // Declarations 
    int mainSelect; 

    int size=rand()%12; // 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; 

    // You need to assign storage 
    S = new Element [MAX]; 
    for (int i = 0; i < MAX; i++) 
     S [i] = new char [MAXSTR]; 


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

     // Build set S 

     constructSet (S,size); 


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

     cout << "Set S = {"; 

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

      p++;  
     } 


     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; 

    Element temp = new char [MAXSTR]; 

    for (int i = 0;i<size;i++) 
    { 
     bool found = true; 
     while (found) 
     { 
      // randomA = rand()%MAX; .. 
      temp = getAnElement(); 

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

     // set=ptrWalk; 
     // set++; 


     strcpy (*ptrWalk, temp); 
     ++ptrWalk;   
    } 
} 

bool checkElement (Element ptrWalk, Element *set, int size) 
{ 
    Element *p = &set[0]; 

    for (int j=0; j<size;j++) 
    { 
     if (strcmp (ptrWalk, *p) == 0) 
     { 
      return true; 
     } 

     p++; 
    } 
    return false; 
} 
1

你在你的代碼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, i); 

這應該是足夠您的程序給預期的結果,以取代兩次行

found = checkElement (*ptrWalk, size); 

。但是,如果你希望它是好的,但仍有一些改進:

  • 你正確聲明int main()但在main
  • 結束忘了return 0;你未提出,而你之前打電話給他們申報的功能的高清(至少應該引起預警...)
  • 你犯了一個大量使用全局變量這是不是一個好的做法,因爲它不允許容易檢測
  • 您的算法應該簡化跟隨一次且僅一次原理。代碼複製對於未來的維護是不利的,因爲如果代碼變更的力量在不同的地方發生變化並且這樣做會導致令人討厭的錯誤(看起來像這樣很糟糕,但我已經修復了它 - 是的,但只在一個地方......)

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++;   
    } 
} 
+0

while(found)是什麼意思? –

+0

您是否介意重新訪問我的代碼,因爲我有新的限制!歡呼兄弟 –

1

主要問題是 '休息' 中缺少checkElement()一旦它找到的元素。如果你不打破循環,它將與其他指標進行比較並覆蓋「找到」標誌。

if (ptrWalk==S[j]) 
{ 
    found = true; 
    break; 
} 

此外,使用ptrWalk作爲臨時變量來容納字符串。只有在確認它不存在的情況下,才能將字符串添加到S中。

void constructSet (Element *set, int size) 
{ 
// Declarations 
//Element *ptrBase; 
Element ptrWalk; 
//ptrWalk = &set[0]; 
int randomA=0; 
int randomB=0; 
bool found = false; 

for (int i = 0;i<size;i++) 
{ 
    randomA = rand()%12; 
    ptrWalk = UniversalSet [randomA]; 

    // Ensure no replicated animals in set S 
    found = checkElement (ptrWalk, i); 
    if (found==true) 
    { 
     do 
     { 
      // Define value for S 
      randomB = rand()%12; 
      ptrWalk = UniversalSet [randomB]; 
      found = checkElement (ptrWalk, i); 
     } while(found==true); 
     S[i] = UniversalSet [randomB]; 
     //ptrWalk++; 
    } 
    else 
    {   
     // Define value for S 
     S[i] = UniversalSet [randomA]; 
     //ptrWalk++;   
    } 
} 

}

你需要刪除不必要的變量,並使其不太複雜的優化你的代碼。

+0

你介意重新審視我的代碼,因爲我對它進行了微小的修改 –