-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarL.cpp
More file actions
109 lines (82 loc) · 2.87 KB
/
Copy pathstarL.cpp
File metadata and controls
109 lines (82 loc) · 2.87 KB
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
// starL.cpp A demonstration of ASCII Art printing L characters
// Compile using the command: g++ --std=c++11 starL.cpp -o starL
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void assertEquals(string expected, string actual, string message);
string starL(int width, int height);
void runTests(void);
// starL is A C++ function that returns a string that, when printed on cout,
// renders the letter L with stars as ASCII art,
// at any width or height, provided both width and height are >= 2
// If either is less than 2, the function should return an empty string.
// When both width and height are >=2, the length of the result string
// should be (width + 1) * height (where the +1 is for the \n).
// For examples, see the test cases in runTests().
// Note that the trailing spaces on each line are REQUIRED
// for the function to be considered correct.
string starL(int width, int height)
{
string result="";
// check if parameters are valid
if ((width<2) || (height < 2))
return result; // return without printing anything
// add the first height-1 rows that are a single star
// followed by width-1 spaces, then a \n
for (int row=2; row<=height; row++) {
result += "*";
result += "\n";
}
// add the final row of width stars
for (int col=1; col<=width; col++) {
result += "*";
}
result += "\n";
return result;
}
// Test-Driven Development; check expected results against actual
void runTests(void) {
// The following line works because in C and C++ when string literals
// are separated only by whitespace (space, tab, newline), they
// automatically get concatenated into a single string literal
string starL34Expected =
"* \n"
"* \n"
"* \n"
"***\n" ;
assertEquals(starL34Expected,starL(3,4),"starL(3,4)");
string starL43Expected =
"* \n"
"* \n"
"****\n" ;
assertEquals(starL43Expected,starL(4,3),"star(4,3)");
assertEquals("",starL(1,2),"starL(1,2)");
assertEquals("",starL(2,1),"starL(2,1)");
}
// Test harness
void assertEquals(string expected, string actual, string message="") {
if (expected==actual) {
cout << "PASSED: " << message << endl;;
} else {
cout << " FAILED: " << message << endl << " Expected:[\n" << expected << "] actual = [\n" << actual << "]\n" << endl;
}
}
// Main function
int main(int argc, char *argv[])
{
if (argc!=3) {
cerr << "Usage: " << argv[0] << " width height" << endl;
exit(1);
}
int width = atoi(argv[1]);
int height = atoi(argv[2]);
// If the program is executed with parameters -1 -1 unit test
// the starL() function using our automated test framework
if (width==-1 && height==-1) {
runTests();
exit(0);
}
cout << starL(width,height);
return 0;
}