📄 dicesimulator.cpp
字号:
// Exercise 12.12: DiceSimulator.cpp
// This application rolls dice and counts the frequency of each side.
#include <iostream> // required to perform C++ stream I/O
#include <string> // required to access string functions
#include <ctime> // contains prototype for function time
// contains function prototypes for functions srand and rand
#include <cstdlib>
using namespace std; // for accessing C++ Standard Library members
// dice face constants
enum Faces { ONE = 1, TWO, THREE, FOUR, FIVE, SIX };
// global variables to store statistical information
int frequency1; // stores the frequency of rolling a 1
int frequency2; // stores the frequency of rolling a 2
int frequency3; // stores the frequency of rolling a 3
int frequency4; // stores the frequency of rolling a 4
int frequency5; // stores the frequency of rolling a 5
int frequency6; // stores the frequency of rolling a 6
int total; // stores the total number of rolls
// function prototypes
void setFrequency( int face );
int rollDie();
// function main begins program execution
int main()
{
// define variables
int numberOfRolls; // stores number of times to roll dice
int die1; // stores the value of the first die
int die2; // stores the value of the second die
// randomize random number generator using current time
srand( time( 0 ) );
// prompt user for number of rolls
cout << "\nEnter the number of times to roll the dice: ";
cin >> numberOfRolls;
// repeat until user guesses correctly or runs out of guesses
for ( int i = 0; i < numberOfRolls; i++ )
{
die1 = rollDie(); // roll first die
die2 = rollDie(); // roll second die
// display die values
cout << "\nRoll " << i + 1 << endl;
cout << "---------" << endl;
cout << "Die 1:\tDie 2:" << endl;
cout << die1 << "\t" << die2 << endl;
} // end for
// display statistics
cout << "\nStatistical information:" << endl;
cout << "Frequency for side one: " << frequency1 << endl;
cout << "Frequency for side two: " << frequency2 << endl;
cout << "Frequency for side three: " << frequency3 << endl;
cout << "Frequency for side four: " << frequency4 << endl;
cout << "Frequency for side five: " << frequency5 << endl;
cout << "Frequency for side six: " << frequency6 << endl;
cout << "Total dice rolled: " << total << endl;
cout << "\n"; // insert newline for readability
return 0; // indicates successful termination
} // end function main
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -