/
Main.cpp
231 lines (194 loc) · 4.13 KB
/
Main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <conio.h>
#include <fstream>
//_getch, _putch, _kbhit
namespace fio {
bool isAlphaNumeric( char c ) {
if ( c >= 48 && c <= 57 ) {
return true;
}
if ( c >= 65 && c <= 122 ) {
return true;
}
return false;
}
void print( const char* s ) {
for ( ; *s != 0; s++ ) {
_putch( *s );
}
}
void read( char* buf, int maxSize ) {
const char* const pEnd = buf + maxSize;
char c = 0;
while ( c != 13 && (buf < pEnd) ) {
c = _getch();
if ( isAlphaNumeric( c ) ) {
_putch( c );
*buf = c;
buf++;
}
}
*buf = 0;
}
void readw( char* buf, int maxSize ) {
const char* const pEnd = buf + maxSize;
char c = 0;
while ( c != 13 && (buf < pEnd + 1) ) {
c = _getch();
if ( isAlphaNumeric( c ) ) {
_putch( c );
*buf = c;
buf++;
}
}
while ( buf < pEnd ) {
*buf = ' ';
buf++;
}
buf--;
*buf = 0;
}
int str2int( const char* s ) {
const char* p = s;
for ( ; *p >= '0' && *p <= '9'; p++ ) {}
p--;
int val = 0;
int place = 1;
for ( ; p >= s; p-- ) {
val += (*p - '0') * place;
place *= 10;
}
return val;
}
void strrev( char* pl ) {
char* pr = pl;
for ( ; *pr != 0; pr++ ) {}
pr--;
for ( ; pl < pr; pl++, pr-- ) {
const char temp = *pl;
*pl = *pr;
*pr = temp;
}
}
void int2str( int val, char* buf, const int size ) {
char* const pStart = buf;
char* const pEnd = buf + size;
for ( ; val > 0 && (buf + 1 < pEnd); val /= 10, buf++ ) {
*buf = '0' + val % 10;
}
*buf = 0;
strrev( pStart );
}
int getLength( char* str ) {
int len = 0;
char* pl = str;
while ( *pl != 0 ) {
len++;
pl++;
}
return len;
}
}
char showMenu() {
fio::print( "(l)oad (s)ave (a)dd (q)uit or (p)rint?\n" );
char c = 0;
while ( c == 0 ) {
c = _getch();
if ( c != 0 ) {
return c;
}
}
}
struct nvPair {
char name[10];
char value[5];
};
void writeFile( nvPair pairs[], const int nameLength, const int valueLength, const int nvLength ) {
std::ofstream out( "nvPairs.txt" );
for ( int i = 0; i < nvLength; i++ ) {
for ( int j = 0; j < nameLength; j++ ) {
if ( pairs[i].name[j] != 0 && (fio::isAlphaNumeric( pairs[i].name[j] ) || pairs[i].name[j] == 32 )) {
out.put( pairs[i].name[j] );
}
}
out.put( ',' );
for ( int j = 0; j < valueLength; j++ ) {
if ( pairs[i].value[j] != 0 && fio::isAlphaNumeric( pairs[i].value[j] ) ) {
out.put( pairs[i].value[j] );
}
}
out.put( ',' );
}
out.close();
}
int readFile( nvPair pairs[], const int nameLength, const int valueLength ) {
std::ifstream in( "nvPairs.txt" );
bool readingName = true;
int nvIndex = 0;
int charIndex = 0;
while ( in.good() ) {
char c = in.get();
if ( c == ',' ) {
readingName = !readingName;
charIndex = 0;
if ( readingName )
nvIndex++;
} else {
if ( readingName ) {
pairs[nvIndex].name[charIndex] = c;
pairs[nvIndex].name[charIndex + 1] = 0;
charIndex++;
} else {
pairs[nvIndex].value[charIndex] = c;
pairs[nvIndex].value[charIndex + 1] = 0;
charIndex++;
}
}
}
return nvIndex;
}
void addNVPair( nvPair pairs[], const int nameLength, const int valueLength, const int nvIndex ) {
fio::print( "Enter name: " );
fio::readw( pairs[nvIndex].name, nameLength );
fio::print( "\n" );
fio::print( "Enter value: " );
fio::read( pairs[nvIndex].value, valueLength );
fio::print( "\n" );
}
void printChart( nvPair pairs[], const int nameLength, const int valueLength, int nvIndex ) {
fio::print( " Beautiful Chart Bitches!\n" );
fio::print( " ------------------------\n\n" );
for ( int j = 0; j < nvIndex; j++ ) {
fio::print( pairs[j].name );
fio::print( "|" );
const int count = fio::str2int( pairs[j].value );
for ( int i = 0; i < count; i++ ) {
fio::print( "=" );
}
fio::print( "\n" );
}
}
int main() {
nvPair pairs[100];
int nvIndex = 0;
while ( 1 ) {
switch ( showMenu() ) {
case 'l':
nvIndex = readFile( pairs, 10, 5 );
break;
case 's':
writeFile( pairs, 10, 5, nvIndex );
break;
case 'a':
addNVPair( pairs, 10, 5, nvIndex );
nvIndex++;
break;
case 'q':
return 0;
break;
case 'p':
printChart( pairs, 10, 5, nvIndex );
break;
}
}
return 0;
}