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一下吧,学完慢慢续上。