#include <iostream> #include <ctime> #include <cstdlib> #include <string> using namespace std; class CSportLot; class CWelfareLot; class CCustomer { private: int *pSport; int *pWelfare; public: CCustomer() { pSport=pWelfare=NULL; } void SetSport(int *p) { pSport=p; } void SetWelfare(int *p) { pWelfare=p; } int Compare(CSportLot &); int Compare(CWelfareLot &); }; class CSportLot { private: int LotNum[7]; public: CSportLot(){} void SetLot(); void PrintLot(); friend int CCustomer::Compare(CSportLot &); }; class CWelfareLot { private: int LotNum[6]; public: CWelfareLot(){} void SetLot(); void PrintLot(); friend int CCustomer::Compare(CWelfareLot &); }; int CCustomer::Compare(CSportLot &sportlot) { int t=0; for(int i=0;i<7;i++) { if(sportlot.LotNum==pSport) t++; } return 7-t; } int CCustomer::Compare(CWelfareLot &welfarelot) { int t=0; for(int i=0;i<6;i++) { if(welfarelot.LotNum==pWelfare) t++; } return 6-t; } void CSportLot::SetLot() { int nRnd,nNum; for(int i=0;i<7;i++) { nRnd=rand(); for(int j=0;j<nRnd;j++) { nNum=rand()%10; cout<<nNum<<'\b'; } cout<<nNum; LotNum=nNum; } } void CSportLot:rintLot() { for(int i=0;i<7;i++) cout<<LotNum<<'\t'; cout<<endl; } void CWelfareLot::SetLot() { int nRnd,nNum; for(int i=0;i<6;i++) { nRnd=rand(); for(int j=0;j<nRnd;j++) { nNum=rand()%21; if(nNum<10) cout<<nNum<<'\b'; else cout<<nNum<<'\b'<<'\b'; } cout<<nNum<<" "; LotNum=nNum; } } void CWelfareLot:rintLot() { for(int i=0;i<6;i++) cout<<LotNum<<'\t'; cout<<endl; } void SportLottery() { cout<<"\n\t\t体育彩票,机会多多!\n\n"; cout<<"\t请输入7位(0~9)数字:\n"; int userport[7]; for(int i=0;i<7;i++) { cout<<"第"<<i+1<<"位数字:"; cin>>userport; if(userport>=10||userport<0) { cout<<"输入的位数应该在0~9之间,请重新输入!\n"; i--; } }//caimin CCustomer customer; CSportLot sport; customer.SetSport(userport); cout<<"\t现在开始开奖,按任意键开始\n";//ticaikaijiang cin.get(); cout<<"\n\n激动人心一刻,体育彩票开奖!\n\n"; sport.SetLot(); cout<<endl; cout<<"\t大奖号码为:\n"; sport.PrintLot(); int nRank; nRank=customer.Compare(sport);//caiminjiaodui if(nRank==7) cout<<"\t很遗憾,这次你没有中奖,请以后继续努力!\n\n\n"; else cout<<"\t恭喜你,您中的奖是"<<nRank<<"等奖,该请客了!\n\n\n"; } void WelfareLottery() { cout<<"\n\t\t福利彩票,惊喜无限!\n\n"; cout<<"\t请输入6位(0~20)数字:\n"; int userwelfare[6]; for(int i=0;i<6;i++) { cout<<"第"<<i+1<<"位数字:"; cin>>userwelfare; if(userwelfare>20||userwelfare<0) { cout<<"输入的数字应在0~20之间,请重新输入!\n"; i--; } } CCustomer customer; CWelfareLot welfare; customer.SetWelfare(userwelfare); cout<<"\t现在开始开奖,按任意键开始\n"; cin.get(); cout<<"\n\t\t激动人心的一刻,福利彩票开奖!\n\n"; welfare.SetLot(); cout<<endl; cout<<"\t大奖号码为:\n"; welfare.PrintLot(); int nRank; nRank=customer.Compare(welfare); if(nRank==6) cout<<"\t很遗憾,这次您没有中奖,请以后继续努力!\n\n\n"; else cout<<"\t恭喜你,您中的奖是"<<nRank<<"等奖,该请客了!\n\n\n"; } int main(void) { srand(time(NULL)); string strChoice; do { cout<<"\t\t欢迎进入彩票游戏,祝你好运!\n\n\n"; cout<<"\t\t1. 体育彩票\n"; cout<<"\t\t2. 福利彩票\n"; cout<<"\t\t3. 退出\n"; cout<<"\n\n\t\t请选择下注福彩还是体彩:"; cin>>strChoice; if(strChoice=="1") SportLottery(); else if(strChoice=="2") WelfareLottery(); else if(strChoice!="3") cout<<"\t\t输入错误,请重新选择: \n"; }while(strChoice!="3"); cout<<"\n\n\t\t谢谢使用,下次再见!\n"; return 0; } |