第二题:Only Connect
第二题注意的点:
- 不可以使用循环
- 可以使用isalpha一个字符是否为一个英文字母,包含在cctype头文件中
- 可以使用toUpperCase返回一个大写的字符,在strlib.h头文件中
- 可以使用charToString将单个字符转为string,在头文件strlib.h中
- string onlyConnectize(string phrase) {
- /* TODO: The next few lines just exist to make sure you don't get compiler warning messages
- * when this function isn't implemented. Delete these lines, then implement this function.
- */
- if (phrase == "") return phrase;
- char first = phrase[0];
- // 判断是否是字母
- if (isalpha(first))
- {
- // 判断是否为元音
- if (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u' ||
- first == 'A' || first == 'E' || first == 'I' || first == 'O' || first == 'U')
- {
- return onlyConnectize(phrase.substr(1));
- }
- // 不是元音变大写
- first = toUpperCase(first);
- return charToString(first) + onlyConnectize(phrase.substr(1));
- }
- // 不是字母直接下一个
- return onlyConnectize(phrase.substr(1));
- }
复制代码 第三题
- string aSequenceOfOrder(int n) {
- if (n < 0) error("n should not less than zero");
- if (n == 0) return "A";
- else
- return aSequenceOfOrder(n - 1) + bSequenceOfOrder(n - 1);
- }
- string bSequenceOfOrder(int n) {
- if (n < 0) error("n should not less than zero");
- if (n == 0) return "B";
- else
- return bSequenceOfOrder(n - 1) + aSequenceOfOrder(n - 1);
- }
复制代码 第四题
- void dropSandOn(Grid<int>& world, int row, int col) {
- world[row][col] += 1;
- if (world[row][col] > 3)
- {
- world[row][col] = 0;
- if (world.inBounds(row - 1, col))
- dropSandOn(world, row - 1, col);
- if (world.inBounds(row + 1, col))
- dropSandOn(world, row + 1, col);
- if (world.inBounds(row, col - 1))
- dropSandOn(world, row, col - 1);
- if (world.inBounds(row, col + 1))
- dropSandOn(world, row, col + 1);
- }
- }
复制代码 第五题
- void runPlotterScript(istream& input) {
- bool ud=0;string op;
- double x1=0,y1=0,x2,y2;
- PenStyle pen={1,"black"};
- while(!input.eof())
- {
- input>>op;op=toLowerCase(op);
- if(op=="penup")
- {
- ud=0;
- }
- else if(op=="pendown")
- {
- ud=1;
- }
- else if(op=="moverel")
- { double a,b;
- input>>a>>b;
- x2=x2+a;y2=y2+b;
- if(ud)drawLine(x1,y1,x2,y2,pen);
- x1=x2;y1=y2;
- }
- else if(op=="moveabs")
- {
- input>>x2>>y2;
- if(ud)drawLine(x1,y1,x2,y2,pen);
- x1=x2;y1=y2;
- }
- else if(op=="penwidth")
- {
- input>>pen.width;
- }
- else if(op=="pencolor")
- {
- input>>pen.color;
- }
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |