首页 > 题库 > 职业考证 > 软考 > 软件设计师 > 案例题

阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
在某系统中,类Interval代表由下界(lower bound)和上界 (upper bound) 定义的区间,要求采用不同的格式显示区间范围,如[lower bound,upper bound]、[lower bound...upper bound]、[lower bound - upper bound]等。现采用策略 (Strategy) 模式实现该要求,得到如图6-1所示的类图。


【c++代码】

#include <iostream>
using namespace std;

class Interval;
class PrintStrategy {
    public:
               (1)      ;
};
class Interval {
    private:
        double lowerBound;
        double upperBound;

    public:
        Interval(double p_lower, double p_upper) {
               lowerBound = p_lower;
               upperBound = p_upper;

        }
        void printInterval(PrintStrategy*ptr){
                        (2)      ;

        }
        double getLower() {   return lowerBound;    }
        double getUpper() {   return upperBound;    }

};

class PrintIntervalsComma : public PrintStrategy {
    public:
        void doPrint(Interval *val) {
            cout << "[" << val->getLower() << "," << val->getUpper() << "]" << endl; }

};

class PrintIntervalsDots : public PrintStrategy {
    public:
         void doPrint(Interval *val) {

             cout << "[" << val->getLower() << "..." << val->getUpper() << "]" << endl; }

};

class PrintIntervalsLine:public PrintStrategy {
    public:
        void doPrint(Interval *val) {
            cout << "[" << val->getLower() << "-" << val->getUpper() << "]" << endl; }

};

enum TYPE {COMMA, DOTS, LINE};
PrintStrategy* getStrategy(int type) {
    PrintStrategy* st;
    switch(type){
        case COMMA:
                   (3)      ;
           break;
        case DOTS:
                   (4)      ;
           break;
        case LINE:
                   (5)      ;
           break;

         }
         return st;

}

int main() {
        Interval a(1.7,2.1);
        a.printInterval(getStrategy(COMMA));
        a.printInterval(getStrategy(DOTS));
        a.printInterval(getStrategy(LINE));
        return 0;

}


参考答案: 查看答案 查看解析 下载APP畅快刷题

相关知识点试题

相关试卷