cs106b 作业1

第二题:Only Connect

第二题注意的点:

  1. 不可以使用循环
  2. 可以使用isalpha一个字符是否为一个英文字母,包含在cctype头文件中
  3. 可以使用toUpperCase返回一个大写的字符,在strlib.h头文件中
  4. 可以使用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;
     }
   }
}

posted @   yuzuki_n  阅读(92)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示