本文主要是介绍100个囚犯和灯泡C语言,求助大神:用C++ 面向对象方法解决100个囚犯1盏灯问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
class Prisoner {
protected:
static bool s_bInitialized;
static bool s_bLight;
static Prisoner *m_arlpPrisoners[100];
bool m_bHasTurnedOnTheLight;
protected:
Prisoner() : m_bHasTurnedOnTheLight(false) { }
virtual ~Prisoner() { };
public:
static void Initialize();
virtual void Release();
};
class Counter : public Prisoner {
protected:
static Counter *s_lpcntrSingleton;
unsigned int m_uCount;
bool m_bHaveIBeenReleasedYet;
protected:
Counter() : m_uCount(0), m_bHaveIBeenReleasedYet(false) { }
virtual ~Counter() { };
public:
static Counter* CreateSingleton();
virtual void Release();
};
bool Prisoner::s_bInitialized = false;
bool Prisoner::s_bLight = false;
Prisoner *Prisoner::m_arlpPrisoners[100] = {};
Counter *Counter::s_lpcntrSingleton = nullptr;
void Prisoner::Initialize(){
s_bLight = false;
m_arlpPrisoners[0] = Counter::CreateSingleton();
for(unsigned int i = 1;i < 100;++i){
m_arlpPrisoners[i] = new Prisoner();
}
}
void Prisoner::Release(){
if(!this->m_bHasTurnedOnTheLight && !s_bLight){
s_bLight = true;
this->m_bHasTurnedOnTheLight = true;
}
}
Counter* Counter::CreateSingleton(){
if(s_lpcntrSingleton != nullptr){
return nullptr;
}
s_lpcntrSingleton = new Counter();
return s_lpcntrSingleton;
}
void Counter::Release(){
if(!this->m_bHaveIBeenReleasedYet){
this->m_bHaveIBeenReleasedYet = true;
++m_uCount;
}
if(Prisoner::s_bLight){
Prisoner::s_bLight = false;
++m_uCount;
}
}
int main(){
Prisoner::Initialize();
/* Add your code here... */
return 0;
}
完全没跑过
这篇关于100个囚犯和灯泡C语言,求助大神:用C++ 面向对象方法解决100个囚犯1盏灯问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!