长庚之上 2024-05-03 20:25 采纳率: 50%
浏览 4
已结题

实现彩票:超级大乐透时报错,但无法查明错误

//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;
    }
}


以上是我完成一次练习后进行改进的代码,要求实现彩票:超级大乐透的功能,但是报错了,找不到原因

.

  • 写回答

1条回答 默认 最新

  • GISer Liu 2024-05-03 21:14
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    问题描述中提到了在实现彩票超级大乐透时出现了报错,但未明确指出报错的具体信息。根据提供的代码和描述,我会假设报错可能涉及到编译错误、逻辑错误或者运行时错误。我将按照问题描述的要求给出解决思路和解决方案。

    问题分析

    1. 报错信息不明确 :首先需要获取报错信息,才能准确定位问题。没有报错信息,我们只能推测可能出现的错误。
    2. 代码逻辑检查 :需要检查代码逻辑是否符合预期,包括函数调用、参数传递等。

      解决思路

    3. 查看报错信息 :如果有编译或者运行时的报错信息,应该先根据报错信息定位问题。
    4. 检查代码逻辑 :仔细检查代码逻辑,确保函数调用、参数传递等正确。

      解决方案

    5. 查看报错信息 :如果编译时有报错信息,应该逐一解决每个报错;如果是运行时错误,查看控制台输出的信息。
    6. 检查代码逻辑 :检查代码中的函数调用和参数传递,特别注意参数类型和数量是否匹配。
      根据以上解决思路和解决方案,我建议你按照以下步骤来解决问题:
    7. 检查编译器或者运行环境的输出,获取详细的报错信息。
    8. 检查代码中函数调用和参数传递是否正确,特别是涉及到父类和子类的成员函数调用是否正确。
    9. 确保头文件的包含路径正确,类的声明和定义一致。
      如果你能提供更具体的报错信息,我可以帮助你更准确地定位问题并给出相应的解决方案。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月6日
  • 创建了问题 5月3日