-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarC.cpp
More file actions
106 lines (82 loc) · 2.41 KB
/
Copy pathstarC.cpp
File metadata and controls
106 lines (82 loc) · 2.41 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
// starC.cpp, 4/21/18, Mason Corey, A demonstration of ASCII Art printing C characters
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void assertEquals(string expected, string actual, string message);
string starC(int width, int height);
void runTests(void);
// Write starC per specifictions in the lab writeup
// so that internal tests pass, and submit.cs system tests pass
string starC(int width, int height)
{
string result="";
if(width>=2 && height>=3) {
for(int i=0; i<width; i++) {
result+="*";
}
result+="\n";
for(int k=2; k<height; k++) {
result+="*";
for(int j=1; j<width; j++) {
result+=" ";
}
result+="\n";
}
for(int i=0; i<width; i++) {
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 starC34Expected =
"***\n"
"* \n"
"* \n"
"***\n";
assertEquals(starC34Expected,starC(3,4),"starC(3,4)");
string starC53Expected =
"*****\n"
"* \n"
"*****\n";
assertEquals(starC53Expected,starC(5,3),"starC(5,3)");
assertEquals("",starC(2,1),"starC(2,1)");
assertEquals("",starC(2,2),"starC(2,2)");
string starC23Expected =
"**\n"
"* \n"
"**\n";
assertEquals(starC23Expected,starC(2,3),"starC(2,3)");
}
// 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 << starC(width,height);
return 0;
}