随机购买n注彩票,或者手动输入彩票数字,看看你的幸运值如何?
对体彩超级大乐透的前区5个数字和后区2个数字进行区分,前区(1-35),后区(1-12),避免错误输入,避免输入数字超过范围,避免前区或后区中有重复数字,判断获奖情况。
代码如下
#include <iostream>
#include<ctime>
#include<stdlib.h>
#include<vector>
using namespace std;//生成随机数
void produceNum(vector<int> &A);
//检测数字是否存在
bool checkSamenum(vector<int>A,int temp);
//打印数字
void printNum(vector<int> A);
//检测单注前后区中奖数字个数
vector<int> correctNum(vector<int> predictNum,vector<int> chooseNum);
//选择购买数字
void chooseNum(vector<vector<int>>&buyNum);
//保存所有中奖情况
vector<vector<int>> winSum(vector<int> &winNum,vector<vector<int>> buyNum);
//获奖情况
void awards(vector<vector<int>> &winSum);
//随机购买数字
void randomBuyNum(vector<vector<int>>&buyNum);void produceNum(vector<int> &A){//srand((unsigned)time(NULL));int a = 1, b = 35,c=12;bool label=false;bool label1=false;for(int i=0;i<5;i++){// 生成[a,b]的随机数int temp=(rand() % (b-a+1)) + a;if(i==0){A.push_back(temp);}else{label=checkSamenum(A,temp);while(label==true){temp=(rand() % (b-a+1)) + a;//cout<<"same number: "<< temp<<endl;label=checkSamenum(A,temp);}A.push_back(temp);}}//printNum(storagea);// cout<<"second area"<<endl;vector<int> B;for(int j=0;j<2;j++){int temp1=(rand() % (c-a+1)) + a;if(j==0){B.push_back(temp1);}else{label1=checkSamenum(B,temp1);while(label1==true){temp1=(rand() % (c-a+1)) + a;//cout<<"same number2: "<< temp1<<endl;label1=checkSamenum(B,temp1);}B.push_back(temp1);}}A.push_back(B[0]);A.push_back(B[1]);//cout<<"size:"<<storageb.size()<<endl;//cout<<"<<<<winning number>>>>"<<endl;printNum(A);}
bool checkSamenum(vector<int>A,int temp){int label=false;int size=A.size();for(int i=0;i<size;i++){if(A[i]==temp){label=true;return label;}}return label;
}
void printNum(vector<int> A){for(vector<int>::iterator it=A.begin();it!=A.end();it++){cout<<*it<<" ";}cout<<endl;}
vector<int> correctNum(vector<int> predictNum,vector<int> chooseNum){vector<int> rightSum;int corct1=0,corct2=0;for(int i=0;i<5;i++){for(int j=0;j<5;j++){if(chooseNum[j]==predictNum[i]){corct1+=1;}}}for(int i=5;i<7;i++){for(int j=5;j<7;j++){if(chooseNum[j]==predictNum[i]){corct2+=1;}}}rightSum.push_back(corct1);rightSum.push_back(corct2);return rightSum;}void randomBuyNum(vector<vector<int>>&buyNum){cout<<"random buy lottery tickets"<<endl;cout<<"How many lottery tickets do you want to buy? (0~100)"<<endl;int totalNum;cin>>totalNum;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number :"<<endl;cin>>totalNum;}while(totalNum<0||totalNum>100){cout<<"wrong number,please rechoose lottery tickets: (0~100)"<<endl;cin>>totalNum;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number :"<<endl;cin>>totalNum;}}cout<<"the number you buy:"<<endl;for(int i=0;i<totalNum;i++){vector<int> single;produceNum(single);buyNum.push_back(single);}// for(int i=0;i<totalNum;i++){
// printNum( buyNum[i]);
// }}void chooseNum(vector<vector<int>>&buyNum){cout<<"How many lottery tickets do you want to buy (0~30) ?"<<endl;int totalNum;cin>>totalNum;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~30)"<<endl;cin>>totalNum;}while(totalNum<0||totalNum>30){cout<<"wrong number,please rechoose lottery tickets: "<<endl;cin>>totalNum;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~30)"<<endl;cin>>totalNum;}}for(int i=0;i<totalNum;i++){cout<<"choose the "<<i+1<<" ticket"<<endl;vector<int> single;bool label=false;cout<<"cin 5 first number (1~35)"<<endl;for(int i=0;i<5;i++){int d=0;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~35)"<<endl;cin>>d;}if(i==0){while(d<0||d>35){cout<<"wrong number,please choose again a"<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~35)!"<<endl;cin>>d;}}single.push_back(d);}else{while(d<0||d>35){cout<<"wrong number,please choose again "<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~35) "<<endl;cin>>d;}}label=checkSamenum(single,d);while(label==true){cout<<"same number: "<< d<<" .please choose again"<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~35)!"<<endl;cin>>d;}while(d<0||d>35){cout<<"wrong number,please choose again "<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~35)!"<<endl;cin>>d;}}label=checkSamenum(single,d);}single.push_back(d);}}
// cout<<"single[5]"<<endl;
// printNum(single);cout<<"cin 2 second number (1~12):"<<endl;vector<int> A;for(int i=0;i<2;i++){int d=0;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~12)"<<endl;cin>>d;}if(i==0){while(d<0||d>12){cout<<"wrong number,please choose again"<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~12)"<<endl;cin>>d;}}A.push_back(d);}else{while(d<0||d>12){cout<<"wrong number,please choose again"<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~12)!"<<endl;cin>>d;}}label=checkSamenum(A,d);while(label==true){cout<<"same number: "<< d<<" .please choose again"<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~12)!"<<endl;cin>>d;}while(d<0||d>12){cout<<"wrong number,please choose again"<<endl;cin>>d;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin number (1~12)!"<<endl;cin>>d;}}label=checkSamenum(A,d);}A.push_back(d);}}
// cout<<"A[2]"<<endl;
// printNum(A);single.push_back(A[0]);single.push_back(A[1]);buyNum.push_back(single);}cout<<"the number you buy:"<<endl;for(int i=0;i<totalNum;i++){printNum( buyNum[i]);}}vector<vector<int>> winSum(vector<int> &winNum,vector<vector<int>> buyNum){int size=buyNum.size();vector<vector<int>> sumCorct;for(int i=0;i<size;i++){vector<int> corct=correctNum(winNum,buyNum[i]);sumCorct.push_back(corct);}return sumCorct;
}void awards(vector<vector<int>> &winSum){int size=winSum.size();for(int i=0;i<size;i++){int a=winSum[i][0];int b=winSum[i][1];cout<<"the "<<i+1<<" lottery: 1_get: "<<a<<" 2_get: " <<b<<" won the : ";if(a==5&&b==2){cout<<"First prize"<<endl;}else if(a==5&&b==1){cout<<"Second prize"<<endl;}else if(a==5&&b==0){cout<<"Third prize"<<endl;}else if(a==4&&b==2){cout<<"Fourth prize"<<endl;}else if(a==4&&b==1){cout<<"Fifth prize"<<endl;}else if(a==3&&b==2){cout<<"Sixth prize"<<endl;}else if(a==4&&b==0){cout<<"Seventh prize"<<endl;}else if((a==3&&b==1)||(a==2&&b==2)){cout<<"Eighth prize"<<endl;}else if((a==3&&b==0)||(a==2&&b==1)||(a==1&&b==2)||(a==0&&b==2)){cout<<"Ninth prize"<<endl;}else{cout<<"nothing"<<endl;}}}int main()
{srand((unsigned)time(NULL));vector<int> producenumber;vector<vector<int>>buyNum;vector<vector<int>>winsum;string israndombuy="";cout<<"do you want to buy lottery tickets random ? (yes/no)"<<endl;cin>>israndombuy;while(cin.fail()){cin.clear();//清除cin的错误信息cin.ignore();//忽略掉缓冲区的内容,置于EOF位置cout<<"please cin (yes/no) :"<<endl;cin>>israndombuy;}if(israndombuy=="yes"){randomBuyNum(buyNum);}else if(israndombuy=="no"){chooseNum(buyNum);}else{cout<<"wrong answer"<<endl;cout<<"game over!"<<endl;return 0;}cout<<"***** winning number *****"<<endl;produceNum(producenumber);winsum=winSum(producenumber,buyNum);awards(winsum);cout<<"game over,thanks for you playing!"<<endl;system("pause");return 0;
}