📄 pex3_10.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "random.h"
class Dice
{
private:
int n; // number of dice
int diceTotal; // sum of the n die
int diceList[20]; // listing of the die faces
// random number generator class used for dice toss. made
// static to assure that all instances of Dice use the dame
// random sequence.
static RandomNumber rnd;
public:
// constructor
Dice(int numberDie = 2);
void SetNumber(int numberDie);
// dice handling methods
void Toss(void);
int Total(void) const;
void DisplayToss(void) const;
};
// constructor. let random number generator use automatic seeding
Dice::Dice(int numberDie): n(numberDie)
{}
void Dice::SetNumber(int numberDie)
{
n = numberDie;
}
// toss and determine dietotal
void Dice::Toss(void)
{
diceTotal = 0;
for (int i = 0; i < n; i++)
{
diceList[i] = rnd.Random(6) + 1;
diceTotal = diceTotal + diceList[i];
}
}
// return the value of dietotal
int Dice::Total(void) const
{
return diceTotal;
}
// print the dice faces
void Dice::DisplayToss(void) const
{
for (int i = 0; i < n; i++)
cout << diceList[i] << " ";
}
// declare the static private Dice member rnd
RandomNumber Dice::rnd;
// sort the n element Dice array a in ascending order
// by die count
void Sort(Dice a[], int n)
{
int i, j;
Dice tmp;
// implement n-1 passes
for(i = 0; i < n-1; i++)
// put object with maximum total within the collection
// a[i+1]...a[n-1] in a[i]
for(j = i+1; j < n; j++)
// exchange if a[i].Total() > a[j].Total()
if (a[i].Total() > a[j].Total())
{
tmp = a[i];
a[i]=a[j];
a[j] = tmp;
}
}
void main(void)
{
// default constructor used (n = 2)
Dice d[30];
int toss12 = 0;
int i;
int max = -1, maxindex, target, repeats;
// change the number of dice tossed to 5 and toss each
// die. compute the max of the 30 tosses and keep track
// of the index of the object
for (i = 0; i < 30; i++)
{
d[i].SetNumber(5);
d[i].Toss();
if (d[i].Total() > max)
{
max = d[i].Total();
maxindex = i;
}
}
// initial target
target = d[0].Total();
// an immediate match of d[0] with target sets
// repeats to 0
repeats = -1;
for (i = 0; i < 30; i++)
{
// keep track of number of 12's thrown
if (d[i].Total() == 12)
toss12++;
// count a repeating total or reset the target
if (d[i].Total() == target)
repeats++;
else
target = d[i].Total();
}
cout << "The die totals are:" << endl;
for (i = 0; i < 30; i++)
{
if (i % 10 == 0)
cout << endl;
cout << d[i].Total() << " ";
}
cout << endl << endl;
cout << "The total 12 was thrown " << toss12 << " times." << endl;
cout << "Number of repeats is " << repeats << endl;
cout << "The maximum dice total is " << max << " on a toss (";
d[maxindex].DisplayToss();
cout << ')' << endl;
Sort(d,30);
cout << endl << "The sorted die totals are:" << endl;
for (i = 0; i < 30; i++)
{
if (i % 10 == 0)
cout << endl;
cout << d[i].Total() << " ";
}
cout << endl;
}
/*
<Run>
The die totals are:
20 18 16 18 21 23 23 23 22 13
13 25 19 15 17 22 16 18 20 23
12 20 17 9 15 16 15 15 11 20
The total 12 was thrown 1 times.
Number of repeats is 4
The maximum dice total is 25 on a toss (6 3 6 5 5 )
The sorted die totals are:
9 11 12 13 13 15 15 15 15 16
16 16 17 17 18 18 18 19 20 20
20 20 21 22 22 23 23 23 23 25
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -