C++ 知识复习--一个简易围棋比赛系统

2024-05-09 12:48

本文主要是介绍C++ 知识复习--一个简易围棋比赛系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include <fstream>
using namespace std;
#define LongOfChar 100  // 定义了一个常量为100的宏LongOfChar/**
* The class of Player
* 2017/2/19
*/
class Player{private:int dan;      //棋手段位public :char  name[LongOfChar]; //棋手姓名int mikeScore;public:  Player(){dan=0;mikeScore=0;}void setDan(int da){dan=da;}int getDan(){return dan;}
};
/*Player降序排列*/
class CompGreater
{
public:bool operator ()(const Player& pstItem1, const Player& pstItem2){return pstItem1.mikeScore> pstItem2.mikeScore;}};  /*
* The Class Of PlayerPair
* 2017/2/23
*/class PlayerPair{public :Player* A_Player;  //对手中A的指针Player* B_Player;  //对手中B的指针Player* winer_Player; //获胜者的指针
};/**
* The class of Complete
* 2017/2/19
*/
class WeiQiComplete{public :int numberOfPlayer;     //参赛人数int roundOfCompelte;    //比赛轮数int currentCompelte;    //当前第几轮vector<Player>  mPlayerVetor;   //存放参赛选手的Vectorvector<PlayerPair>  mPlayerPairVetor; //存放配对实体的Vectorpublic :WeiQiComplete(){numberOfPlayer=0;roundOfCompelte=0;currentCompelte=0;}//Print the info of playervoid printPlayer(){   //循环打印所有参赛选手信息vector<Player>::iterator it;    cout << "The In of Player:"<< endl;for(it=mPlayerVetor.begin();it!=mPlayerVetor.end();it++){cout<<"name:"<<(*it).name<<"  dan:"<<(*it).getDan()<<"  MikeScore:"<<(*it).mikeScore<<endl;}}//input the info of completevoid inputPlayer(){ //输入比赛信息(根据当前第几轮判断是否已经开始比赛,开始则不可以输入)if(currentCompelte>0){cout<<"the complete has begin,you cannot enter again!";}else{cout << "Please input the number of player :"<<endl;cin>>numberOfPlayer;cout << "Please input the round of Compelte :"<<endl;cin>>roundOfCompelte;cout << "Please input the Info of all Player"<<endl;for(int i=0;i < numberOfPlayer; i++){Player mPlayer;int dan;cout << "Please input name of "<< i+1 <<" player :"<<endl;cin>>mPlayer.name;cout << "Please input dan of "<< i+1 <<" player :"<<endl;cin>>dan;mPlayer.setDan(dan);mPlayer.mikeScore=dan;mPlayerVetor.push_back(mPlayer);}}}//sort the Playervoid sortPlayer(){  //根据选手的麦克马洪分数对选手进行排序sort(mPlayerVetor.begin(), mPlayerVetor.end(), CompGreater());}//make Pair of Player //根据选手的麦克马洪分数从高到低依次配对void makePair(){int sizeOfArray=mPlayerVetor.size() / 2;for(int i=0;i<sizeOfArray;i++){PlayerPair mPlayerPair;mPlayerPair.A_Player=&mPlayerVetor[2*i];mPlayerPair.B_Player=&mPlayerVetor[2*i+1];mPlayerPairVetor.push_back(mPlayerPair);}if(sizeOfArray*2 < mPlayerVetor.size()){mPlayerVetor[mPlayerVetor.size()-1].mikeScore+=0.5;}}//print the pair of Playervoid printpair(){  //打印出所有的分组信息cout << "*The Result of the pair*"<< endl;for(int i=0;i<mPlayerPairVetor.size();i++){cout<< (*(mPlayerPairVetor[i].A_Player)).name<<"   :   "<<(*(mPlayerPairVetor[i].B_Player)).name<<endl;}cout << "you can enter the result "<< endl;}//print the Result of roundvoid printTheResult(){ //打印此轮的比赛结果for(int i=0;i<mPlayerPairVetor.size();i++){cout<< (*(mPlayerPairVetor[i].A_Player)).name<<"  :  "<<(*(mPlayerPairVetor[i].B_Player)).name<< "  Winer is *" <<(*(mPlayerPairVetor[i].winer_Player)).name<<"*"<<endl;}if(roundOfCompelte==currentCompelte){cout<<"The Complete End ! The Sort Of all Payer is --"<<endl;sortPlayer();printPlayer();}}//input the result of roundvoid inputResult(){  //输入此轮的比赛每组的结果for(int i=0;i<mPlayerPairVetor.size();i++){int playerid;cout<<"please input the result of "<<(*(mPlayerPairVetor[i].A_Player)).name<<" win is 1 ; "<<(*(mPlayerPairVetor[i].B_Player)).name<<" win is 2 "<<endl;cin>>playerid;if(playerid==1){mPlayerPairVetor[i].winer_Player=mPlayerPairVetor[i].A_Player;(*(mPlayerPairVetor[i].A_Player)).mikeScore+=1;}else{if(playerid==2){mPlayerPairVetor[i].winer_Player=mPlayerPairVetor[i].B_Player;(*(mPlayerPairVetor[i].B_Player)).mikeScore+=1;}  else{(*(mPlayerPairVetor[i].A_Player)).mikeScore+=0.5;(*(mPlayerPairVetor[i].B_Player)).mikeScore+=0.5;}}}}
};/**
* The class 0f Login
* 2017/2/26
*/
class Login{private :string  name;string  password;public:void login(){  //登录验证的入口cout<< "*************************************"<<endl;cout<< "           WeiQi System              "<<endl;cout<< "*************************************"<<endl;cout<< "Please input your name and password :"<<endl;input();if(login_Verification(name,password)){login_success();    }else{login_error();}}//input info of loginvoid input(){  //输入登录信息cout<< "Your name:";cin>>name;cout<< "Your password:";cin>>password;}//Verificationbool login_Verification(string  name,string  password){ //比对登录信息bool resp=false;if(name.compare("li")==0 && password.compare("123")==0){resp=true;}return resp;}//login successvoid login_success(){  //比对成功后cout<< "Login Success !"<<endl;}//login errorvoid login_error(){   //比对失败后cout<< "Your name Or Password is Error!"<<endl;login();}
};class fileUtil{public:static void outPutToFile(WeiQiComplete weiQiComplete){   //将类对象打印保存到文件中ofstream file;   file.open("WeiQiComplete.txt", ios::out | ios::binary | ios::app);   file.write((char *)&weiQiComplete,sizeof(WeiQiComplete));  file.close();  }static WeiQiComplete readFromFile(){ //从文件中将之前所保存的对象读取出来WeiQiComplete weiQiComplete;ifstream infile;   infile.open("WeiQiComplete.txt", ios::in | ios::binary);if(infile.is_open()){infile.read((char *)&weiQiComplete,sizeof(weiQiComplete));     //(char *)&weiQiComplete 指读存放读取到的数据的对象的指针infile.close();                                               //sizeof(weiQiComplete) 存放读取数据所需要的空间大小cout << "finish read file " << endl;}print(weiQiComplete);        return  weiQiComplete;    }static void print(WeiQiComplete weiQiComplete){cout <<" have read a  WeiQiComplete Info "<< ":" << &weiQiComplete << endl;}
};/**
* The class 0f Menu
* 2017/2/27
*/
class Menu{   //菜单类WeiQiComplete mWeiQiComplete;public:void menu(){int select;cout<< "******************************************"<<endl;cout<< "              WelCome You                 "<<endl;cout<< "This is Menu ,you can input the number of list"<<endl;cout<< "* 1: Enter The Number of Player And Round "<<endl;cout<< "* 2: Start a Round "<<endl;cout<< "* 3: Enter The Result Of Round"<<endl;cout<< "* 4: Print The Sort Of Player"<<endl;cout<< "* 5: Save to the file"<<endl;cout<< "* 6: read  the file"<<endl;cout<< "* 7: Back "<<endl;cout<< "******************************************"<<endl;cin>>select;switch(select){case 1:enterInfo();break;case 2:startRound();break;case 3:inputResultOfRound();break;case 4:printSort();break;case 5:SaveToFile();break;case 6:ReadFromFile();break;case 7:exit(0);break;}}void enterInfo(){ //输入比赛信息mWeiQiComplete.inputPlayer();mWeiQiComplete.printPlayer();mWeiQiComplete.sortPlayer();menu();}void startRound(){  //开始新的一轮比赛if(mWeiQiComplete.numberOfPlayer <= 0 || mWeiQiComplete.roundOfCompelte<=0){cout <<"Please Enter the Info About Player and Compelte!"<<endl;menu();}else{if(mWeiQiComplete.roundOfCompelte <= mWeiQiComplete.currentCompelte){cout <<"The Compelte Has Finish!"<<endl;menu();}else{mWeiQiComplete.currentCompelte = mWeiQiComplete.currentCompelte + 1;cout <<"you will begin the  * "<<mWeiQiComplete.currentCompelte<<"  * round "<<endl;mWeiQiComplete.makePair();mWeiQiComplete.printpair();menu();                }}}void printSort(){ //输出排序后的选手信息mWeiQiComplete.printPlayer();menu();}void inputResultOfRound(){ //输入每轮比赛每组结果mWeiQiComplete.inputResult();mWeiQiComplete.printTheResult();menu();}void SaveToFile(){ //将比赛对象整体保存到文件中fileUtil::outPutToFile(mWeiQiComplete);menu();}void ReadFromFile(){ //从已存在的文件读取比赛对象数据mWeiQiComplete=fileUtil::readFromFile();menu();}
};
/**
* main method
* 2017/2/19
*/
int main(){Menu  mMenu;  Login mLogin;mLogin.login();mMenu.menu();return 0;}
其中在保存类对象时,如果对象中的域是string,a将会报错(在读取时报错)

这篇关于C++ 知识复习--一个简易围棋比赛系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/973461

相关文章

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文