1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
const int MAXNO = 7;
const int MAX = 10;
typedef void* VoidPtr;
enum Fruit {Durian, Papaya, Apple, Orange, Mango, Jackfruit, Berry};
void constructVPArray (VoidPtr *, int);
void printVPArray (VoidPtr*, int);
VoidPtr getAFruitVP ();
Fruit getVP (VoidPtr);
char * getFruitStr (Fruit);
bool isDuplicate(VoidPtr, int size, VoidPtr);
int main ()
{
srand (time (NULL));
VoidPtr* vpArray;
int size = rand () % 7;
cout << "size: " <<size << endl;
vpArray = new VoidPtr [size];
constructVPArray (vpArray, size);
printVPArray (vpArray, size);
}
VoidPtr getAFruitVP ()
{
int k = rand () % MAXNO;
Fruit f = static_cast <Fruit>(k);
Fruit *fruitPtr;
fruitPtr = new Fruit;
*fruitPtr = f;
VoidPtr vp = fruitPtr;
return vp;
}
void constructVPArray (VoidPtr *vpArray, int size)
{
VoidPtr *p = &vpArray [0];
VoidPtr *q = &vpArray [0];
for (int i = 0; i < size; i++)
{
*p = getAFruitVP ();
do
{
*p = getAFruitVP ();
}while (isDuplicate(*q, i, *p));
/*
while (isDuplicate(*q, i, *p))
{
*p = getAFruitVP ();
}
*/
++p;
}
}
void printVPArray (VoidPtr *vpArray, int size)
{
Fruit f;
VoidPtr *p = &vpArray [0];
for (int i = 0; i < size; i++)
{
f = getVP (*p);
cout << f << " --- " << getFruitStr (f) << endl;
++p;
}
}
Fruit getVP (VoidPtr vp)
{
Fruit *fPtr = static_cast <Fruit *>(vp);
Fruit f = *fPtr;
return f;
}
char * getFruitStr (Fruit f)
{
char fruitStr [][MAX] = {"Durian", "Papaya", "Apple", "Orange", "Mango", "Jackfruit", "Berry"};
int k = static_cast <int> (f);
char *str = new char [MAX];
strcpy (str, fruitStr [k]);
return str;
}
bool isDuplicate(VoidPtr q, int size, VoidPtr p)
{
for(int j = 0; j < size; j++)
{
if (q == p)
return true;
else
++q;
}
return false;
}
|