跟着哔站的视频复习一遍基础知识,根据老师的思路写双色球项目。相较老师的程序加入了一些功能。没有用到模板知识,欢迎交流。
1、6个红球1个蓝球,红球号码1-33,号码不重复;篮球号码1-15.
2、自选号码功能:要甄别号码是否重复,用了一个数组标记号码是否已出现;越界或者重复情况,清空缓冲区内容,重新输入。掌握了cin.fail()、cin.clear()、cin.ignore(numeric_limits
3、机选号码功能:同样要甄别号码是否重复,使用数组存放号码源,获得随机下标后,取号码,将剩余的号码复制到新数组中,更新号码源。
4、加入了延时开奖功能和倒计时功能:引入了thread中this_thread命名空间下sleep_for()休眠函数;chrono头文件对于时间处理更加便捷。chrono::seconds(1)表示1秒。
5、打印函数中,利用sstream头文件中的ostringstream类创建字符串流,将数组中的内容加入字符串并利用iomanip头文件来控制输出格式,最后获取ostringstream::str()字符串。
上代码:
- #include
- using namespace std;
- #include"PlayGame.h"
- #include
- #include
- int main() {
- srand((unsigned int)time(NULL));
- playgame();
- return 0;
- }
- #pragma once
- #define COUNTBALL 7
- #define REDBALLRANGE 33
- #define BLUEBALLRANGE 15
-
- void playgame(); //主程序函数
- int GetNumber(); //读取数字的工具函数
- int* GetOptionalNum(); //自选号码函数
- int* GetRandomNum(); //机选号码函数
- void printNumBall(int* numball,int len); //打印函数
- int* GetWinNum(); //获得中奖号码
- void Mysort(int* numball, int len); //排序函数
- void BonusCaculation(int* numball, int len,int* WinNum); //开奖函数
- #include "PlayGame.h"
- #include
- using namespace std;
- #include
- #include
- #include
- #include
- void playgame()
- {
- int ExitFlag = 0;
- while (!ExitFlag) {
- cout << "*********************" << endl;
- cout << "1.自选号码" << endl;
- cout << "2.机选号码" << endl;
- cout << "0.退出系统" << endl;
- cout << "*********************" << endl;
- int choice = GetNumber();
- int* numball = NULL;
- switch (choice) {
- case 0:
- ExitFlag = 1;
- break;
- case 1:
- numball = GetOptionalNum();
- break;
- case 2:
- numball = GetRandomNum();
- break;
- default:
- break;
- }
- if (numball) {
- cout << "你选的号码是:\t";
- printNumBall(numball, COUNTBALL);
- cout << "距离开奖还有5秒" << endl;
- int sec = 5;
- while (sec) {
- cout << sec << endl;
- this_thread::sleep_for(chrono::seconds(1));
- sec--;
- }
- cout << "中奖号码是:\t";
-
- int *WinNum = GetWinNum();
- printNumBall(WinNum, COUNTBALL);
- BonusCaculation(numball, COUNTBALL, WinNum);
- }
- system("pause");
- system("cls");
- }
- }
- //数字读取函数
- int GetNumber()
- {
- int res = -1;
- cin >> res;
- while (cin.fail()) {
- cin.clear();
- cin.ignore(1);
- cin >> res;
- }
- return res;
- }
- //用户输入自选号,
- int * GetOptionalNum()
- {
- int* numball = new int[COUNTBALL]; //申请双色球数组,存放用户自选号
- cout << "请输入六个红球数字(1~33):" << endl;
- int numflag[REDBALLRANGE+1] = { 0 }; //标记是否重复1表示已出现,0表示未出现
- for (int i = 0; i < COUNTBALL-1; i++) { //循环读取好吗,前6个是红球
- numball[i] = GetNumber();
- //判断号码是否出界和重复
- if ((numball[i] >= 1 && numball[i] <= REDBALLRANGE && numflag[numball[i]] == 1) || numball[i] < 1 || numball[i]>REDBALLRANGE) {
- cout << "红球输入不合法,请重新输入:" << endl;
- cin.ignore(numeric_limits
::max(),'\n'); //如果重复忽略掉后面所有输入,直至回车 - i = -1; //重置读取循环,重新读取
- for (int& elem : numflag) { //重置重复标志
- elem = 0;
- }
- }
- else if (numball[i] >= 1 && numball[i] <= REDBALLRANGE) { //不重复、不越界,正常读取存放
- numflag[numball[i]] = 1; //已存放的号码球重复标志设置为1
- }
- }
- cin.ignore(numeric_limits
::max(), '\n'); //忽略掉缓冲区多余内容,避免影响后续输入 - cout << "请输入蓝球数字(1~15):";
- numball[6] = GetNumber(); //读取蓝球号码
- while (!(numball[6] >= 1 && numball[6] <= BLUEBALLRANGE)) {
- cout << "蓝球输入不合法,请重新输入:";
- numball[6] = GetNumber();
- }
- cin.ignore(numeric_limits
::max(), '\n'); - Mysort(numball, COUNTBALL - 1); //红球排序
- return numball;
- }
- //获得一组机选号
- int * GetRandomNum()
- {
- int* numball = new int[COUNTBALL]; //申请双色球数组,存放机选号
- int* numsource = new int[REDBALLRANGE]; //申请一个数组,存放号码源,机选号从号码源读取
- for (int i = 0; i < REDBALLRANGE; i++) { //初始化号码源 1-33
- numsource[i] = i + 1;
- }
- for (int i = 0; i < COUNTBALL - 1; i++) { //生成6个红球随机号码
- int index = rand() % (REDBALLRANGE - i); //生成号码源的随机下标
- numball[i] = numsource[index]; //从号码源读取随机下标位置的号码
- int *temp = new int[REDBALLRANGE - i - 1]; //申请一个新数组,比号码源少一个元素
- for (int j = 0,k = 0; j < REDBALLRANGE - i - 1; j++,k++) { //将号码源复制到新数组
- if (k != index) {
- temp[j] = numsource[k];
- }
- else { //当复制到已读取过的元素跳过
- temp[j] = numsource[++k];
- }
- }
- delete numsource;
- numsource = temp;
- }
- numball[6] = rand() % BLUEBALLRANGE + 1;
- Mysort(numball, COUNTBALL - 1); //红球排序
- return numball;
- }
- //打印双色球
- void printNumBall(int * numball, int len)
- {
- ostringstream oss;
- for (int i = 0; i < len; i++) {
- oss << setw(2) << setfill('0') << numball[i] << " ";
- }
- string str = oss.str();
- cout << str << endl;
- }
-
- int * GetWinNum()
- {
- return GetRandomNum();
- }
- //从小到大排序
- void Mysort(int * numball, int len)
- {
- //选择排序
- //for (int i = 0; i < len - 1; i++) {
- // int minindex = i;
- // int j = 0;
- // for (j = i + 1; j < len; j++) {
- // if (numball[j] < numball[i]) {
- // minindex = j;
- // }
- // }
- // if (minindex != i) {
- // int temp = numball[i];
- // numball[i] = numball[minindex];
- // numball[minindex] = temp;
- // }
- //}
-
- //冒泡排序
- for (int i = 0; i < len ; i++) {
- for (int j = 0; j < len - 1 - i; j++) {
- if (numball[j] > numball[j + 1]) {
- int temp = numball[j];
- numball[j] = numball[j + 1];
- numball[j + 1] = temp;
- }
- }
- }
- }
- //+--------------------------------------------+
- //|-------一等奖:6+1-----------500000元-------|
- //|-------二等奖:6或者5+1------200000元-------|
- //|-------三等奖:5或者4+1------50000 元-------|
- //|-------四等奖:4或者3+1------5000 元-------|
- //|-------五等奖:3或者2+1------500 元-------|
- //|-------六等奖:2或者1+1------50 元-------|
- //|-------七等奖:1或者1--------5 元-------|
- //|--------------------------------------------|
- //
- void BonusCaculation(int * numball, int len, int* WinNum)
- {
-
- int cnt = 0, blue = 0, res = 0;
- for (int i = 0; i < len - 1; i++) {
- if (numball[i] == WinNum[i]) {
- cnt++;
- }
- }
- if (numball[len - 1] == WinNum[len - 1]) {
- blue = 1;
- }
- int awards[7] = { 500000,200000,50000,5000,500,50,5 };
- res = cnt + blue;
- if (res) {
- cout << "恭喜你,中" << 8 - res << "等奖,奖金" << awards[7 - res] << "元" << endl;
- }
- else {
- cout << "很遗憾,你没有中奖" << endl;
- }
- }