第七次课程作业
第七次课程作业
截图
Random* RandomFactory::Create(std::string& model,int& n)
{
if (model == "Number")
{
return new RandomNumber(n);
}
else if (model == "Operation")
{
return new RandomOperation(n);
}
else
{
assert(false);
}
return NULL;
}
void equation::initequation()
{
result_0 = 9.9;
while (result_0 != (int)result_0)
{
int* in;
char * ch;
string modeName;
Random * random = NULL;
digit_num = rand() % 3 + 5; //5-7位运算
memset(m, 0, 30);
memset(n, '\0', 50);
modeName = "Number";
random = RandomFactory::Create(modeName, digit_num);
in = random->geti();
for (int i = 0;i < 30;i++) m[i] = in[i];
modeName = "Operation";
random = RandomFactory::Create(modeName, digit_num);
ch = random->getc();
for (int i = 0;i < 50;i++) n[i] = ch[i];
delete random;
random = NULL;
try
{
clean();
toget();
result_0 = result(); //进行计算
}
catch (const string &e)
{
}
}
print();
}
感想
关于父类子类的应用就是上课老师讲的,正常做就可以,关于简单工厂模式,我认为可能在很大的工程里会很有用,加强代码的后期可维护性和封装性,使代码设计得更人性化。
学习链接:
1.http://blog.chinaunix.net/uid-25958655-id-4243289.html
2.http://blog.csdn.net/chollima/article/details/3985570