cs106b-lab1 参考代码

OnlyConnect.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* File: OnlyConnect.cpp
*
* TODO: Edit these comments to describe anything interesting or noteworthy in your implementation.
*
* TODO: Edit these comments to leave a puzzle for your section leader to solve!
*/
#include "OnlyConnect.h"
#include "GUI/SimpleTest.h"
using namespace std;

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.
*/

// 注意特判长度为0,否则后续越界错误....
if (phrase.length() == 0) return "";
int n = phrase.length() - 1;
char tmp = phrase[n];
tmp = toUpperCase(tmp);
string tt;
tt.push_back(tmp);
phrase.pop_back();
if (!isalpha(tmp)) return onlyConnectize(phrase);
if (tmp == 'A') return onlyConnectize(phrase);
if (tmp == 'E') return onlyConnectize(phrase);
if (tmp == 'O') return onlyConnectize(phrase);
if (tmp == 'U') return onlyConnectize(phrase);
if (tmp == 'I') return onlyConnectize(phrase);
return onlyConnectize(phrase) + tt;
}


/* * * * * * Provided Test Cases * * * * * */

PROVIDED_TEST("Converts lower-case to upper-case.") {
EXPECT_EQUAL(onlyConnectize("lowercase"), "LWRCS");
EXPECT_EQUAL(onlyConnectize("uppercase"), "PPRCS");
}

PROVIDED_TEST("Handles non-letter characters properly.") {
EXPECT_EQUAL(onlyConnectize("2.718281828459045"), "");
EXPECT_EQUAL(onlyConnectize("'Hi, Mom!'"), "HMM");
}

PROVIDED_TEST("Handles single-character inputs.") {
EXPECT_EQUAL(onlyConnectize("A"), "");
EXPECT_EQUAL(onlyConnectize("+"), "");
EXPECT_EQUAL(onlyConnectize("Q"), "Q");
}

/* TODO: You will need to add your own tests into this suite of test cases. Think about the sorts
* of inputs we tested here, and, importantly, what sorts of inputs we *didn't* test here. Some
* general rules of testing:
*
* 1. Try extreme cases. What are some very large cases to check? What are some very small cases?
*
* 2. Be diverse. There are a lot of possible inputs out there. Make sure you have tests that account
* for cases that aren't just variations of one another.
*
* 3. Be sneaky. Don't just try standard inputs. Try weird ones that you wouldn't expect anyone to
* actually enter, but which are still perfectly legal.
*
* Happy testing!
*/
PROVIDED_TEST("Handles none-vowel inputs.") {
EXPECT_EQUAL(onlyConnectize("YYY"), "YYY");
EXPECT_EQUAL(onlyConnectize("GGGG"), "GGGG");
EXPECT_EQUAL(onlyConnectize("QWRTYPSDFGHJKLMNBVCXZ"), "QWRTYPSDFGHJKLMNBVCXZ");
}

PlayingFair.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* File: PlayingFair.cpp
*
* TODO: Edit these comments to describe anything interesting or noteworthy in your implementation.
*/
#include "PlayingFair.h"
#include "GUI/SimpleTest.h"
#include "error.h"
using namespace std;

string aSequenceOfOrder(int n) {
/* TODO: Delete this line and the next two lines, then implement this function. */
//(void) n;
if (n < 0) error("a string containing your error message");
if (n == 0) return "A";
return aSequenceOfOrder(n - 1) + bSequenceOfOrder(n - 1);
}

string bSequenceOfOrder(int n) {
/* TODO: Delete this line and the next two lines, then implement this function. */
//(void) n;
if (n < 0) error("a string containing your error message");
if (n == 0) return "B";
return bSequenceOfOrder(n - 1) + aSequenceOfOrder(n - 1);
}


/* * * * * * Provided Test Cases * * * * * */

PROVIDED_TEST("Sequences of order 3 are correct.") {
/* Some very basic checks. */
EXPECT_EQUAL(aSequenceOfOrder(3), "ABBABAAB");
EXPECT_EQUAL(bSequenceOfOrder(3), "BAABABBA");
}

PROVIDED_TEST("Only characters should be As and Bs.") {
for (int i = 0; i < 10; i++) {
for (char ch: aSequenceOfOrder(i)) {
EXPECT(ch == 'A' || ch == 'B');
}
}
}

PROVIDED_TEST("A-sequence of positive order should have equal As and Bs.") {
/* Exclude the sequence of order 0, which is just a single character. */
for (int i = 1; i < 10; i++) {
int as = 0;
int bs = 0;
for (char ch: aSequenceOfOrder(i)) {
if (ch == 'A') as++;
else bs++;
}

EXPECT_EQUAL(as, bs);
}
}

PROVIDED_TEST("Triggers error on negative inputs.") {
/* The EXPECT_ERROR macro expects the given expression to call error(). Remember that
* you need to guard against invalid inputs.
*/
EXPECT_ERROR(aSequenceOfOrder(-137));
EXPECT_ERROR(bSequenceOfOrder(-137));
}

/* TODO: You will need to add your own tests into this suite of test cases. Think about the sorts
* of inputs we tested here, and, importantly, what sorts of inputs we *didn't* test here. Some
* general rules of testing:
*
* 1. Try extreme cases. What are some very large cases to check? What are some very small cases?
*
* 2. Be diverse. There are a lot of possible inputs out there. Make sure you have tests that account
* for cases that aren't just variations of one another.
*
* 3. Be sneaky. Don't just try standard inputs. Try weird ones that you wouldn't expect anyone to
* actually enter, but which are still perfectly legal.
*
* Happy testing!
*/
PROVIDED_TEST("extreme cases.") {
/* The EXPECT_ERROR macro expects the given expression to call error(). Remember that
* you need to guard against invalid inputs.
*/
EXPECT_EQUAL(aSequenceOfOrder(0), "A");
EXPECT_EQUAL(bSequenceOfOrder(0), "B");
// EXPECT_ERROR(aSequenceOfOrder(20));
// EXPECT_ERROR(bSequenceOfOrder(20));
}

Sandpiles.cpp

Grid类型是该校自编库中的类型,可自行前往下面cs106b库中查阅

cs106b库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* File: Sandpiles.cpp
*
* TODO: Edit these comments to describe anything interesting or noteworthy in your implementation.
*/
#include "Sandpiles.h"
#include "GUI/SimpleTest.h"
using namespace std;

void dropSandOn(Grid<int>& world, int row, int col) {
/* TODO: Delete this line and the three after it, then implement this function. */
// (void) world;
// (void) row;
// (void) col;
if (!world.inBounds(row, col)) {
return ;
}
if (world[row][col] <= 2) {
world[row][col]++;
}
else {
world[row][col] = 0;
dropSandOn(world, row + 1, col);
dropSandOn(world, row - 1, col);
dropSandOn(world, row, col + 1);
dropSandOn(world, row, col - 1);
}
}


/* * * * * * Provided Test Cases * * * * * */

PROVIDED_TEST("Dropping into an empty cell only changes that cell.") {
/* Create a simple source grid. */
Grid<int> before = {
{ 3, 3, 3 },
{ 3, 0, 3 },
{ 3, 3, 3 }
};
Grid<int> after = {
{ 3, 3, 3 },
{ 3, 1, 3 },
{ 3, 3, 3 }
};

dropSandOn(before, 1, 1);
EXPECT_EQUAL(before, after); // The above call changes 'before.'
}

PROVIDED_TEST("Non-chaining topples work.") {
/* Create a simple source grid. */
Grid<int> before = {
{ 0, 0, 0 },
{ 1, 3, 1 },
{ 0, 2, 0 }
};
Grid<int> after = {
{ 0, 1, 0 },
{ 2, 0, 2 },
{ 0, 3, 0 }
};

dropSandOn(before, 1, 1);
EXPECT_EQUAL(before, after); // The above call changes 'before.'
}

PROVIDED_TEST("Two topples chain.") {
/* Create a simple source grid. */
Grid<int> before = {
{ 0, 0, 0, 0 },
{ 0, 3, 3, 0 },
{ 0, 0, 0, 0 }
};
Grid<int> after = {
{ 0, 1, 1, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 1, 0 }
};

dropSandOn(before, 1, 1);
EXPECT_EQUAL(before, after); // The above call changes 'before.'
}

/* TODO: You will need to add your own tests into this suite of test cases. Think about the sorts
* of inputs we tested here, and, importantly, what sorts of inputs we *didn't* test here. Some
* general rules of testing:
*
* 1. Try extreme cases. What are some very large cases to check? What are some very small cases?
*
* 2. Be diverse. There are a lot of possible inputs out there. Make sure you have tests that account
* for cases that aren't just variations of one another.
*
* 3. Be sneaky. Don't just try standard inputs. Try weird ones that you wouldn't expect anyone to
* actually enter, but which are still perfectly legal.
*
* Happy testing!
*/

PROVIDED_TEST("out bound.") {
/* Create a simple source grid. */
Grid<int> before = {
{ 0, 0, 0 },
{ 1, 3, 1 },
{ 0, 2, 0 }
};
Grid<int> after = {
{ 0, 0, 0 },
{ 1, 3, 1 },
{ 0, 2, 0 }
};

dropSandOn(before, 3, 3);
EXPECT_EQUAL(before, after); // The above call changes 'before.'
}

Plotter.cpp

  • 本文件运行后,请确认所有提供的图像都可以画出来,否则,你的代码可能错误,需要更改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Plotter.h"
#include "strlib.h"
#include <vector.h>
using namespace std;

void PenDown(bool& f) {
if (!f) {
//f == 1 down.
f = 1;
}
}

void PenUp(bool& f) {
if (f) {
f = 0;
}
}

void MoveAbs(double& x, double& y, double x_now, double y_now) {
x = x_now;
y = y_now;
}

void MoveRel(double& x, double& y, double dx, double dy) {
x += dx;
y += dy;
}

void PenColor_color(PenStyle& syl, const string& s_now) {
syl.color = s_now;
}

void PenWidth_width(PenStyle& syl, const double& wid) {
syl.width = wid;
}

void runPlotterScript(istream& input) {
/* TODO: Delete this line and the one after it, then implement this function. */
//(void) input;
PenStyle style = {1, "black"};
bool f = 0;
double x = 0, y = 0;
for (string line; getline(input, line); ) {
Vector<string> str = stringSplit(line, " ");
str[0] = toLowerCase(str[0]);
if (str[0] == "pendown") {
PenDown(f);
}
else if (str[0] == "penup") {
PenUp(f);
}
else if (str[0] == "moveabs") {
double x_now = stringToReal(str[1]);
double y_now = stringToReal(str[2]);
if (f) {
drawLine(x, y, x_now, y_now, style);
}
MoveAbs(x, y, x_now, y_now);
}
else if (str[0] == "moverel") {
double dx = stringToReal(str[1]);
double dy = stringToReal(str[2]);
if (f) {
drawLine(x, y, x + dx, y + dy, style);
}
MoveRel(x, y, dx, dy);
}
else if (str[0] == "pencolor") {
PenColor_color(style, str[1]);
}
else if (str[0] == "penwidth") {
PenWidth_width(style, stringToReal(str[1]));
}
}
}