//main.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include "Lottery.h"
#include "SuperLotto.h"
using namespace std;
int main()
{
cout<< "****************************************"<<endl;
cout<< "********** 欢迎来到超级大乐透 ********"<<endl;
cout<< "****************************************"<<endl;
SuperLotto superlotto;
int choice1;
cout<< "请选择投注方式或者退出游戏"<<endl;
cout<< "1:自选号码"<<endl;
cout<< "2:机选号码"<<endl;
cout<< "0:退出游戏"<<endl;
while(cin>>choice1)
{
if(choice1==1)
{
superlotto.setOptionalNumber();
superlotto.setWinningNumber();
superlotto.computeringWin(superlotto.getOptionalNumber(),
superlotto.getWinningNumber(),
superlotto.getSpecialOptionalNumber(),
superlotto.getSpecialMachineSelectionNumber());
cout<< "请选择投注方式或者退出游戏"<<endl;
cout<< "1:自选号码"<<endl;
cout<< "2:机选号码"<<endl;
cout<< "0:退出游戏"<<endl;
}
if(choice1==2)
{
cout<< "您选的号码:"<<endl;
superlotto.setMachineNumber();
superlotto.setWinningNumber();
superlotto.computeringWin(superlotto.getMachineSelectionNumber(),
superlotto.getWinningNumber(),
superlotto.getSpecialMachineSelectionNumber(),
superlotto.getSpecialMachineSelectionNumber());
cout<< "请选择投注方式或者退出游戏"<<endl;
cout<< "1:自选号码"<<endl;
cout<< "2:机选号码"<<endl;
cout<< "0:退出游戏"<<endl;
}
if(choice1==0)
{
break;
}
}
return 0;
}
//Lottery.h *******************************
#ifndef LOTTERY_H
#define LOTTERY_H
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
class Lottery
{
public:
vector<int> generateRandomNumbers(int minNum, int maxNum, int count);//产生不相同随机向量
vector<int> getOptionalNumber();//返回前区自选号码
vector<int> getSpecialOptionalNumber();//返回后区自选号码
vector<int> getMachineSelectionNumber();//返回前区机选号码
vector<int> getSpecialMachineSelectionNumber();//返回后区机选号码
vector<int> getWinningNumber();//返回前区中将号码
vector<int> getSpecialWinningNumber();//返回后区中奖号码
virtual void setOptionalNumbers() const=0;//设置自选号码
virtual void setMachineNumbers() const=0;//设置机选号码
virtual void setWinningNumbers() const=0;//设置中奖号码
virtual void computingWin() const=0;//计算得奖并输出
protected :
vector<int> optionalNumber;//前区自选号码
vector<int> specialOptionalNumber;//后区自选号码
vector<int> machineSelectionNumber;//前区机选号码
vector<int> specialMachineSelecttionNumber;//返回机选号码
vector<int> winningNumber;//前区中将号码
vector<int> specialWinningNumber;//后区中奖号码
};
#endif // LOTTERY_H
//Lottery.cpp********************************************
#include "Lottery.h"
using namespace std;
vector<int> Lottery::generateRandomNumbers(int minNum, int maxNum, int count)//产生不相同随机向量
{
vector<int> numbers;
srand(time(NULL)+rand());
while(numbers.size() < count)
{
int num=minNum+rand()%(maxNum-minNum+1);
bool exsit=false;
for(int n : numbers)
{
if(n==num)
{
exsit=true;
break;
}
}
if(!exsit)
{
numbers.push_back(num);
}
}
return numbers;
}
vector<int> Lottery::getOptionalNumber()//返回前区自选号码
{
return optionalNumber;
}
vector<int> Lottery::getSpecialOptionalNumber()//返回后区自选号码
{
return specialOptionalNumber;
}
vector<int> Lottery::getMachineSelectionNumber()//返回前区机选号码
{
return machineSelectionNumber;
}
vector<int> Lottery::getSpecialMachineSelectionNumber()//返回后区机选号码
{
return specialMachineSelecttionNumber;
}
vector<int> Lottery::getWinningNumber()//返回前区中将号码
{
return winningNumber;
}
vector<int> Lottery::getSpecialWinningNumber()//返回后区中奖号码
{
return specialWinningNumber;
}
//SuperLotto.h*****************************************************
#ifndef SUPERLOTTO_H
#define SUPERLOTTO_H
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include "Lottery.h"
using namespace std;
class SuperLotto :public Lottery
{
public :
void yesOrNoSecondTestBetting();//判断投注方式
void setChoice(int c);
int getChoice();
void checkRepeatAdd(vector<int>& numbers,int num);//检查重复并添加
bool checkNumber(const vector<int>& numbers,int num);//检查重复
void setOptionalNumber() const;//设置自选号码
void setMachineNumber() const;//设置机选号码
void setWinningNumber() const;//设置中奖号码
void computeringWin(const vector<int>& optionalNumber,
const vector<int>& winningNumber,
const vector<int>& specialOptionalUserNumber,
const vector<int>& specialWinningNumber) const;//计算得奖并输出
private :
int choice;
};
#endif // SUPERLOTTO_H
//SuperLotto.cpp**********************************************************************
#include "SuperLotto.h"
#include "Lottery.h"
using namespace std;
int SuperLotto::getChoice()
{
return choice;
}
void SuperLotto::setChoice(int c)
{
choice=c;
}
void SuperLotto::yesOrNoSecondTestBetting()
{
int choose;
cout<< "***************************************"<<endl;
cout<< "****************1:单式投注*************"<<endl;
cout<< "****************2:复式投注*************"<<endl;
cout<< "***************************************"<<endl;
cout<< "请选择你要投注的方式:"<<endl;
cin>>choose;
setChoice(choose);
}
void SuperLotto::checkRepeatAdd( vector<int>& numbers,int num)
{
bool exsit=false;
for(int n : numbers)
{
if(n==num)
{
cout<< "输入错误"<<endl;
exsit=true;
break;
}
}
if(!exsit)
{
numbers.push_back(num);
}
}
bool SuperLotto::checkNumber(const vector<int>& numbers,int num)
{
for(int n :numbers)
{
if(n==num)
return true;
}
return false;
}
void SuperLotto::setOptionalNumber() const
{
SuperLotto::yesOrNoSecondTestBetting();
if(getChoice()==1)
{
cout << "请输入5个普通号码(1-35之间):" << endl;
for (int i = 0; i < 5; ++i)
{
int num;
cin >> num;
checkRepeatAdd(optionalNumber,num);
}
cout << "请输入2个特别号码(1-12之间):" << endl;
for (int i = 0; i < 2; ++i)
{
int num;
cin >> num;
checkRepeatAdd(specialOptionalNumber,num);
}
}
if(SuperLotto::getChoice()==2)
{
cout<< "输入0表示投注完毕!"<<endl;
cout << "请输入普通号码(1-35之间):" << endl;
int num;
while(cin>>num)
{
checkRepeatAdd(optionalNumber,num);
if(num==0)
{
break;
}
}
cout << "请输入特别号码(1-12之间):" << endl;
int n;
while( cin >> n)
{
checkRepeatAdd(specialOptionalNumber,n);
if(n==0)
{
break;
}
}
}
}
void SuperLotto::setMachineNumber() const
{
cout << "普通号码:" << endl;
machineSelectionNumber = generateRandomNumbers(1, 35, 5);
for(int n :machineSelectionNumber)
cout<< n<< " ";
cout<<endl;
cout << "特别号码:" << endl;
specialMachineSelecttionNumber = generateRandomNumbers(1, 12, 2);
for(int n :specialMachineSelecttionNumber)
cout<< n<< " ";
cout<<endl;
}
void SuperLotto::setWinningNumber() const
{
cout << "中奖普通号码:" << endl;
winningNumber=generateRandomNumbers(1, 35, 5);
for(int n :winningNumber)
cout<< n<< " ";
cout<<endl;
cout<< "中奖特别号码:"<<endl;
specialWinningNumber=generateRandomNumbers(1,12,2);
for(int n :specialWinningNumber)
cout<< n<< " ";
cout<<endl;
}
void SuperLotto::computeringWin(const vector<int>& optionalNumber, const vector<int>& winningNumber,
const vector<int>& specialOptionalNumber, const vector<int>& specialWinningNumber) const
{
int normalMatchCount=0;
int specialMatchCount=0;
for (int num : optionalNumber)
{
if (checkNumber(winningNumber,num))
{
normalMatchCount++;
}
}
for (int num : specialOptionalNumber)
{
if (checkNumber(specialWinningNumber,num))
{
specialMatchCount++;
}
}
cout << "普通号码匹配数量:" << normalMatchCount << endl;
cout << "特别号码匹配数量:" << specialMatchCount << endl;
if (normalMatchCount == 5 && specialMatchCount == 2)
{
cout << "恭喜!你中了超级大乐透的头奖(最高!!1000万)!" << endl;
}
else if(normalMatchCount == 5 && specialMatchCount == 1)
{
cout << "恭喜!你中了二等奖(你已经是一个小富豪了)!" << endl;
}
else if(normalMatchCount == 5 && specialMatchCount == 0)
{
cout << "恭喜!你中了三等奖(10000块)!" << endl;
}
else if(normalMatchCount == 4 && specialMatchCount == 2)
{
cout << "恭喜!你中了四等奖(3000块)!" << endl;
}
else if(normalMatchCount == 4 && specialMatchCount == 1)
{
cout << "恭喜!你中了五等奖(300块)!" << endl;
}
else if(normalMatchCount == 3 && specialMatchCount == 2)
{
cout << "恭喜!你中了六等奖(200块)!" << endl;
}
else if(normalMatchCount == 4&& specialMatchCount == 0)
{
cout << "恭喜!你中了七等奖(100块)!" << endl;
}
else if(normalMatchCount == 3 && specialMatchCount == 1||normalMatchCount == 2 && specialMatchCount == 2)
{
cout << "恭喜!你中了八等奖(15块)!" << endl;
}
else if(normalMatchCount == 3 && specialMatchCount ==0||normalMatchCount == 2 && specialMatchCount ==1||normalMatchCount == 1 && specialMatchCount == 2)
{
cout << "恭喜!你中了九等奖(5块)!" << endl;
}
else
{
cout << "很遗憾,你没有中奖。再接再厉!" << endl;
}
}
以上是我完成一次练习后进行改进的代码,要求实现彩票:超级大乐透的功能,但是报错了,找不到原因
.