面向对象程序设计第三次实验课——Wuxing

这篇博客详细介绍了使用面向对象程序设计方法实现五行(金木水火土)关系的模拟,包括类的设计、单例模式的应用、相生相克关系的表示以及所属关系的管理。博主分析了设计难点,如概念抽象、单例模式实现和关系打印,并讨论了代码的优点和不足。
摘要由CSDN通过智能技术生成

这里写图片描述

请无视奇葩常量名……

// wuxing.h

#pragma once
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

const int MAX = 100; // 每个元素所存储所属的最大个数 
const int XING = 6; // 记录元素总共的种类 
enum Element {KONG, WATER, WOOD, FIRE, EARTH, GOLD};// 定义常量元素类型 

class Wuxing {  
    friend ostream& operator<< (ostream& out, const Wuxing& rhs);   
    friend bool operator< (const Wuxing& lhs, const Wuxing& rhs); // 判断相生关系 
    friend bool operator> (const Wuxing& lhs, const Wuxing& rhs); // 判断相克关系 
    public:
        // 单例模式,新建对象 
        static Wuxing* newWuxing(void);
        static Wuxing* newWuxing(const string& s);
//      Wuxing* newWuxing(const char* s);

        ~Wuxing(void) {};
        void addBelong(const string& s);//向元素中添加关系 
        void delBelong(const string& s);//向元素中删除关系 
        void clear(void) {cnt_ = 0;} ; // 清除所有包含关系 

        static void findRelation(const Wuxing& obj);//查找五行间关系 
        static void showWuxing(const Wuxing& obj);  //打印五行 
        static Wuxing* findInXing(const string& s); //查找所属在哪一个元素中 
        static Wuxing* judge(const string& s);
    private:
        // 构造函数是private的,运用了单例模式 
        Wuxing(void);
        Wuxing(const char *s);
        Wuxing(const string& s);
//      Wuxing(const Wuxing& obj);
//      Wuxing& operator= (const Wuxing& rhs);
        int findBelong(const string& s) const;
        void formattedPrint(int state) const;
        Wuxing* findLive (void) const;
        Wuxing* findDie (void) const;
        Wuxing* findBeLived (void) const;
        Wuxing* findBeDead (void) const;

        Element tag_;  // 标记当前对象的属性(元素)是什么 
        string belong_[MAX]; // 保存所属 
        int cnt_;   // 记录当前元素所属的个数 
        static Wuxing* exist[XING]; // 存储单例模式下每个元素对应的对象指针 
};

ostream& operator<< (ostream& out, const Wuxing& rhs);  
bool operator< (const Wuxing& lhs, const Wuxing& rhs);
bool operator> (const Wuxing& lhs, const Wuxing& rhs);
//wuxing.cpp

#include "wuxing.h"

Wuxing* Wuxing::exist[XING] = {
  0};

Wuxing::Wuxing(void) : Wuxing("空") {}
Wuxing::Wuxing(const char *s) : cnt_(0)
{
    if (!strcmp(s, "金")) {
        tag_ = GOLD;
    } else if (!strcmp(s, "木")) {
        tag_ = WOOD;
    } else if (!strcmp(s, "水")) {
        tag_ = WATER;
    } else if (!strcmp(s, "火")) {
        tag_ = FIRE;
    } else if (!strcmp(s, "土")) {
        tag_ = EARTH;
    } else {
        tag_ = KONG;
    }
}
Wuxing::Wuxing(const string& s) 
{
    if (s == "金") {
        tag_ = GOLD; 
    } else if (s == "木") {
        tag_ = WOOD;
    } else if (s == "水") {
        tag_ = WATER;
    } else if (s == "火") {
        tag_ = FIRE;
    } else if (s == "土") {
        tag_ = EARTH;
    } else {
        tag_ = KONG;
    }
}
Wuxing* Wuxing::newWuxing(const string& s)
{
    int tag;
    Wuxing *p;
    if (s == "金") {
        tag = GOLD; 
    } else if (s == "木") {
        tag = WOOD;
    } else if (s == "水") {
        tag = WATER;
    } else
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值