cs106b 作业1
第二题: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;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!