【CS106B】作业1 Assignment 1. Welcome to C++!

Part Two: Only Connect

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.
     */
    int len = phrase.length();
    if(len == 1){
        if(isalpha(stringToChar(phrase))){
            phrase = toUpperCase(phrase);
            if(phrase == "A" || phrase == "E" || phrase == "I" || phrase == "O" || phrase == "U"){
                return "";
            }else return phrase;
        }else return "";
    }else{
        string sub_str1 = phrase.substr(0, 1);
        string sub_str2 = phrase.substr(1, len-1);
        string ans = onlyConnectize(sub_str1)+onlyConnectize(sub_str2);
        return ans;
    }
}

Part Three: Playing Fair

string aSequenceOfOrder(int n) {
    if(n<0){
        error("n must not be negative");
    }
    else if(n == 0){
        return "A";
    }
    else {
        return aSequenceOfOrder(n-1)+bSequenceOfOrder(n-1);
    }
}

string bSequenceOfOrder(int n) {
    if(n<0){
        error("n must not be negative");
    }
    else if(n == 0){
        return "B";
    }
    else {
        return bSequenceOfOrder(n-1)+aSequenceOfOrder(n-1);
    }
}

Part Four: Sandpiles

void dropSandOn(Grid<int>& world, int row, int col) {
    if(world.inBounds(row, col)){
        world[row][col] += 1;
        if(world[row][col]>3){
            world[row][col] = 0;
            dropSandOn(world, row+1, col);
            dropSandOn(world, row-1, col);
            dropSandOn(world, row, col+1);
            dropSandOn(world, row, col-1);
        }else return;
    }else return;
}

Part Five: Plotter

void runPlotterScript(istream& input) {
    PenStyle style = {1, "black"};
    int penCondition = 0;// 0 means up, and 1 means down
    double x = 0, y = 0;

    for (string line; getline(input, line); ) {
        Vector<string> command = stringSplit(line, " ");
        if(command[0] == "PenDown"){
            penCondition = 1;
        }else if(command[0] == "PenUp"){
            penCondition = 0;
        }else if(command[0] == "MoveAbs"){
            if(penCondition == 0){
                x = stringToDouble(command[1]);
                y = stringToDouble(command[2]);
            }else{
                drawLine(x, y, stringToDouble(command[1]), stringToDouble(command[2]), style);
                x = stringToDouble(command[1]);
                y = stringToDouble(command[2]);
            }
        }else if(command[0] == "MoveRel"){
            if(penCondition == 0){
                x += stringToDouble(command[1]);
                y += stringToDouble(command[2]);
            }else{
                drawLine(x, y, x+stringToDouble(command[1]), y+stringToDouble(command[2]), style);
                x += stringToDouble(command[1]);
                y += stringToDouble(command[2]);
            }
        }else if(command[0] == "PenColor"){
            style.color = command[1];
        }else if(command[0] == "PenWidth"){
            style.width = stringToDouble(command[1]);
        }
    }
}

之前学的,后面中断了,先把中断前做的作业po一下吧,学完慢慢续上。

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于 CS106B 的环境搭建,您可以按照以下步骤进行操作: 1. 首先,确保您的计算机上已经安装了 C++ 的编译器。推荐使用 g++ 或者 clang++,您可以在终端中输入 `g++ --version` 或者 `clang++ --version` 来检查是否已经安装。 2. 接下来,您需要下载并安装 Stanford 的 C++ 标准库。您可以在 Stanford 的官方网站上找到这个库的下载链接。下载完成后,将其解压到您自定义的目录中。 3. 现在,您可以下载 CS106B 的课程代码。您可以在 Stanford 的官方网站上找到这些代码的下载链接。下载完成后,将其解压到您自定义的目录中。 4. 打开您喜欢的集成开发环境(IDE),例如 Visual Studio Code、Xcode 或者 Eclipse。创建一个新的项目,并将 CS106B 的课程代码添加到该项目中。 5. 配置项目的编译选项,确保编译器能够找到 Stanford 的 C++ 标准库。具体操作方式取决于您使用的 IDE。通常情况下,您需要添加标准库的路径和头文件的路径。 6. 现在,您可以开始编写和运行 CS106B 的代码了!根据课程要求,您可以选择运行命令行程序或者图形化界面程序。 请注意,这些步骤可能会因为您使用的操作系统和开发工具而略有不同。如果您遇到任何问题,建议您查阅课程文档或者向您的教师或同学寻求帮助。祝您在 CS106B 中取得好成绩!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值