由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这个Strategy design pattern的例子为什么人为得弄得这么复杂?
相关主题
请问这样写程序错了吗?弱弱的问一个问题
问两个题An example of strategy pattern
问一个memory allocate/release的问题新鲜 interveiw questions
请教大家一道关于c++的面试题大家谈谈看???
G家电面,这回肯定挂了。附面经。问一个coding的skill
放c code求老师傅指教寻找子序列/子段落
砸了面试,发面题问几个unix/c++工作面试题
c++ 程序一问bloomberg电面,攒rp求bless
相关话题的讨论汇总
话题: width话题: strategy话题: line话题: int话题: buf
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
这个Strategy design pattern的例子为什么人为得弄得这么复杂?
#include
#include
#include
using namespace std;
class Strategy;
class TestBed
{
public:
enum StrategyType
{
Dummy, Left, Right, Center
};
TestBed()
{
strategy_ = NULL;
}
void setStrategy(int type, int width);
void doIt();
private:
Strategy *strategy_;
};
class Strategy
{
public:
Strategy(int width): width_(width){}
void format()
{
char line[80], word[30];
ifstream inFile("quote.txt", ios::in);
line[0] = '\0';
inFile >> word;
strcat(line, word);
while (inFile >> word)
{
if (strlen(line) + strlen(word) + 1 > width_)
justify(line);
else
strcat(line, " ");
strcat(line, word);
}
justify(line);
}
protected:
int width_;
private:
virtual void justify(char *line) = 0;
};
class LeftStrategy: public Strategy
{
public:
LeftStrategy(int width): Strategy(width){}
private:
/* virtual */void justify(char *line)
{
cout << line << endl;
line[0] = '\0';
}
};
class RightStrategy: public Strategy
{
public:
RightStrategy(int width): Strategy(width){}
private:
/* virtual */void justify(char *line)
{
char buf[80];
int offset = width_ - strlen(line);
memset(buf, ' ', 80);
strcpy(&(buf[offset]), line);
cout << buf << endl;
line[0] = '\0';
}
};
class CenterStrategy: public Strategy
{
public:
CenterStrategy(int width): Strategy(width){}
private:
/* virtual */void justify(char *line)
{
char buf[80];
int offset = (width_ - strlen(line)) / 2;
memset(buf, ' ', 80);
strcpy(&(buf[offset]), line);
cout << buf << endl;
line[0] = '\0';
}
};
void TestBed::setStrategy(int type, int width)
{
delete strategy_;
if (type == Left)
strategy_ = new LeftStrategy(width);
else if (type == Right)
strategy_ = new RightStrategy(width);
else if (type == Center)
strategy_ = new CenterStrategy(width);
}
void TestBed::doIt()
{
strategy_->format();
}
int main()
{
TestBed test;
int answer, width;
cout << "Exit(0) Left(1) Right(2) Center(3): ";
cin >> answer;
while (answer)
{
cout << "Width: ";
cin >> width;
test.setStrategy(answer, width);
test.doIt();
cout << "Exit(0) Left(1) Right(2) Center(3): ";
cin >> answer;
}
return 0;
}
/* Input: 2 \ 75 \ 3 \ 75 \ 0
output:
Exit(0) Left(1) Right(2) Center(3): 2
Width: 75
Exit(0) Left(1) Right(2) Center(3): 3
Width: 75 */
1 (共1页)
进入JobHunting版参与讨论
相关主题
bloomberg电面,攒rp求blessG家电面,这回肯定挂了。附面经。
请教一个bloomberg题目放c code求老师傅指教
written test from Interactive Brokers砸了面试,发面题
C++问题3c++ 程序一问
请问这样写程序错了吗?弱弱的问一个问题
问两个题An example of strategy pattern
问一个memory allocate/release的问题新鲜 interveiw questions
请教大家一道关于c++的面试题大家谈谈看???
相关话题的讨论汇总
话题: width话题: strategy话题: line话题: int话题: buf