C++ 购买彩票游戏(类和对象)
最编程
2024-06-28 19:01:07
...
题目描述
假设福利彩票共有七个号码,每个号码的区间为0-30,请你利用C++语言设计一个福利彩票机器,可以根据给定的一组摇奖号码,判断用户所买彩票的获奖等级。
在此基础上,你已有了一个n组的福利彩票号码,并假定给出摇奖后的一组号码为:6,13,1,24,28,8,10,把你所买的每组号码依次匹配,当从左至右各位置上的号码一一相同,则表示你中了一等奖,如果有5、6个相同则为二等奖,余下大于两个以上相同的号码均为三等奖,编程实现该游戏规则!
输入
第一行输入所买彩票的组数;
第二行输入买入彩票的客户名称;
第三行开始每行输入每组福利彩票的号码;
最后行输入摇奖中奖号码。输出根据摇奖号码输出客户所中的奖金等级,比如:
恭喜你 中了(多少注)N等奖!或者:
加油!继续!
样例输入
2
Tom
2 5 3 24 10 8 9
20 23 30 1 5 9 2
6 13 1 24 28 8 10
样例输出
恭喜Tom中了1注三等奖!
#include<iostream>
using namespace std;
class lottery{//定义一个彩票的类
public:
void Setname(char *name);
void Setlotterynum(int j,int i,int num);
void Setwin_num(int i,int w_num);
void compare(int t);//对比两个彩票的相同号码
private:
char *lottery_name;//买家名字
int lotterynum[50][7]; //买入的彩票号码,不一定买一注彩票,所以定义二维数组
int win_num[7];//中奖号码
};
void lottery::Setlotterynum(int j,int i,int num){
lotterynum[j][i]=num;
}
void lottery::Setname(char *name ){
lottery_name=name;
}
void lottery::Setwin_num(int i,int w_num){
win_num[i]=w_num;
}
void lottery::compare(int t){
int same=0;
for(int i=0;i<7;i++){//依次对比彩票的每个号码
if(lotterynum[t][i]==win_num[i])
same++;
}
if(same==7) cout<<"恭喜"<<lottery_name<<"中了1注一等奖!"<<endl;
else if(same>=5) cout<<"恭喜"<<lottery_name<<"中了1注二等奖!"<<endl;
else if(same>=2) cout<<"恭喜"<<lottery_name<<"中了1注三等奖!"<<endl;
}
int main(){
int t,i,w_num,num,j=0;
lottery customer;
char name[20];
cin>>t>>name;
customer.Setname(name);
for(i=0;i<7*t;i++){//依次输入每注彩票里的每个号码
cin>>num;
customer.Setlotterynum(j,i,num);
if(i%7==0&&i!=0) j++;//每满七个号码就表示到下一注彩票
}
for(i=0;i<7;i++){
cin>>w_num;
customer.Setwin_num(i,w_num);
}
for(i=0;i<t;i++)
customer.compare(i);
}
遇到类和对象的题目,
1.把类定义出来,把题目私有的数据成员列出来,列出基本的公有的成员函数,void Setxxxxx(){…}
2.写出主函数的题目流程,逐步补充会使用到的成员函数或者数据成员
推荐阅读
-
C++ 类和对象 - 通过 4 级考试
-
C++ 学习笔记 ----8, 掌握类和对象 (VII) ---- 构建稳固的接口
-
C++ 类和对象 - 3. 内部类
-
C++ 类和对象初步介绍-4.类访问限定符和封装
-
C++ 学习笔记 ----8, 掌握类和对象 (II) ---- 更多关于成员函数的知识 (2)
-
[c++] 类和对象 (2)
-
玩转C++:理解类与对象 - 这些关键知识点:对象模型、this指针的秘密、成员变量和成员函数独立存放、深入解析this指针、以及如何安全地使用空指针调用成员函数,还有const修饰的成员函数的作用
-
C++] 类和对象的基本概念和用法
-
C++] 类和对象(中) - 上一页 - II.构造函数
-
C++ - 类和对象(5):静态成员、好友、内部类、匿名对象