forked from avigael-cs/snowman-b
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowman.cpp
150 lines (127 loc) · 4.14 KB
/
snowman.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdexcept>
#include <vector>
#include <iostream>
using namespace std;
namespace ariel{
//the num
const int zero = 0; //hat
const int one = 1; //nose
const int two = 2; //left eye
const int three = 3; //right eye
const int four = 4; //left arm
const int five = 5; //right arm
const int six = 6; //torso
const int seven = 7; //base
const int eight = 8; //stam
const vector<string> hat = {"_===_",
" ___ \n.....",
" _ \n /_\\",
" ___ \n(_*_)"};
const vector<string> noses = {",",
".",
"_",
" "};
const vector<string> EyesLandR = {".",
"o",
"O",
"-"};
const vector<string> LArms = {"<",
"\\",
"/",
""};
const vector<string> RArms = {">",
"/",
"\\",
""};
const vector<string> torsos = {":",
"] [",
"> <",
" "};
const vector<string> body = {":",
"\" \"",
"___",""};
//level 1 of the snowman
string printHead(char head){
return hat[(head-'0')-1]+'\n' ;
}
string printNose(char nose){
return noses[(nose -'0')-1];
}
//level 2 of the snowman
string printFace(char nose, char l_Eye,char r_Eye){
string left = EyesLandR[(l_Eye-'0')-1];
string right = EyesLandR[(r_Eye-'0')-1];
return "("+left+printNose(nose)+right+")"+'\n';
}
//level 3 of the snowman
string makeTorso(char t){
string torso = torsos[(t-'0')-1];
string space = " ";
if(torso == ":" || torso.empty()){
return "("+space+torso+space+")"+'\n';
}
return "("+torso+")"+'\n';
}
//level 4 of the snowman
string makeBase(char num){
int base_i = (num-'0')-1;
string space = " ";
if (body[base_i] == ":"){
return "("+space+body[base_i]+space+")";
}
if(body[base_i].empty()){
return "("+space+body[base_i]+space+space+")";
}
return "("+body[base_i]+")";
}
string snowman(int num){
//convert the num to array
string numArr = to_string(num);
//error msg
string errorMsg= "Invalid code ";
//check that it positive number
if (num < 0) {
throw out_of_range("the number must be positive!");
}
//check the length of the input - should be eight numbers
if(numArr.length() < eight || numArr.length() > eight){
throw invalid_argument(errorMsg + "'" + to_string(num) + "'");
}
//check that all the eight numbers between 1-4
for (int i = 0 ; i< numArr.length() ; i++){
int a = numArr[i]-'0';
if(a <1 || a > 4){
throw invalid_argument(errorMsg + "'" + to_string(num) + "'");
}
}
//first level of the snowman
string head = printHead(numArr[zero]);
//second level of the snowman
string EyeAndNose = printFace(numArr[one],numArr[two],numArr[three]);
//third level of the snowman
string torso = makeTorso(numArr[six]);
//fourth level of the snowman
string base = makeBase(numArr[seven]);
//check left up arm:
if (numArr[four] == '2'){
EyeAndNose = LArms[(numArr[four]-'0')-1] + EyeAndNose;
}
//check right up arm:
if (numArr[five] == '2'){
EyeAndNose.erase(EyeAndNose.size() - 1);
EyeAndNose += RArms[(numArr[five]-'0')-1] + '\n';
}
//check torso down left arm:
if (numArr[four] == '1' || numArr[four] == '3' || numArr[four] == '4'){
torso = LArms[(numArr[four]-'0')-1] + torso;
}
//check torso down right arm:
if (numArr[five] == '1' || numArr[five] == '3' || numArr[five] == '4'){
torso.erase(torso.size() - 1);
torso += RArms[(numArr[five]-'0')-1]+'\n';
}
//final snowman
string snowman = head+EyeAndNose+torso+base;
return snowman;
}
}