2017-05-05 4 views
0

目標状態:私は結果をランダムに表示します。 Set S = {犬、牛、鶏...}無作為化されたサイズは1〜12で動物は複製できないので、牛がいったん登場するとSet Sには別の牛がいなくなります。エラー:ポインタを使用して重複結果を表示します。

エラー:私は1から12の正しい無作為のサイズを表示しています。しかし、動物がセットSに挿入される前にセットSに動物が存在するかどうかをチェックしようとしても、私は動物を複製しました。

制約:私はポインターとポインタを比較するために動的にポインタを使用する必要があります。 アレイに使用されているすべてのストレージを動的に作成し、それらが不要になったときに削除する必要があります。 アレイの要素にアクセスするときは、ポインタを使用してアクセスする必要があります。 この参照の逆参照たとえば、[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は、種類に建てられ、その後一貫のtypedefを使用していないが、超混乱してtypedeffing私見 –

+0

を重複している最後の要素でない限り、それ以外の場合は、常にfalseを返します、trueを返す必要があります。例えば。 '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つの異なる主要な問題を抱えています。最初は既にフェデリコによって与えられています:checkElementは、1つの要素が見つかるとすぐに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)を呼び出す必要があることを意味します。これは、現在の要素のインデックスが既に入力されている項目の数であるためです。だから、期待される結果を与えるためにあなたのプログラムのための十分なはずです。この1

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

中に何が見つかりましたか? –

+0

私は新しい制約があるので、私のコードを見直してもよろしいですか?歓声の兄弟 –

1

主な問題は、それが要素を見つけたら、 'ブレーク')(checkElementに欠落していることです。ループを壊さないと、他のインデックスと比較して 'found'フラグを上書きします。

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

私はそれにマイナーな編集をしたので私のコードを再訪してもいいですか? –

関連する問題