请无视奇葩常量名……
// 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