-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarZ.cpp
More file actions
104 lines (81 loc) · 2.32 KB
/
Copy pathstarZ.cpp
File metadata and controls
104 lines (81 loc) · 2.32 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
// starZ.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 starZ(int width);
void runTests(void);
// Write starZ per specifictions at
// https://foo.cs.ucsb.edu/16wiki/index.php/F14:Labs:lab04
// and so that internal tests pass, and submit.cs system tests pass
string starZ(int width)
{
string result="";
if(width>=3) {
for (int i = 0; i < width; i++) {
result+="*";
}
result+="\n";
for(int i=2; i<width; i++) {
int num_Spaces = width-i;
for(int k=0; k<num_Spaces; k++) {
result+=" ";
}
result+="*";
for(int k=0; k<(i-1); k++) {
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 starZ3Expected =
"***\n"
" * \n"
"***\n";
assertEquals(starZ3Expected,starZ(3),"starZ(3)");
string starZ4Expected =
"****\n"
" * \n"
" * \n"
"****\n";
assertEquals(starZ4Expected,starZ(4),"starZ(4)");
assertEquals("",starZ(0),"starZ(0)");
assertEquals("",starZ(2),"starZ(2)");
}
// 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!=2) {
cerr << "Usage: " << argv[0] << " width" << endl;
exit(1);
}
int width = atoi(argv[1]);
// If the program is executed with parameters -1 -1 unit test
// the starL() function using our automated test framework
if (width==-1) {
runTests();
exit(0);
}
cout << starZ(width);
return 0;
}