-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
121 lines (98 loc) · 2.03 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
#include <unistd.h>
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
int own_strlen(const char *str)
{
int size = 0;
while (str[size]) {
++size;
}
return size;
}
void own_memcpy(void *dest, const void *src, int count)
{
char *d = static_cast<char*>(dest);
const char *s = static_cast<const char*>(src);
for (int i = 0; i < count; i) {
d[i] = s[i];
}
}
template<typename T>
class Array
{
public:
Array() :
m_size(0)
{
}
void append(T n)
{
m_buffer[m_size] = n;
++m_size;
}
T at(int index) { return m_buffer[index]; }
T &operator[](int index) { return m_buffer[index]; }
int size() const { return m_size; }
int count() const { return m_size; }
void clear() { m_size = 0; }
private:
int m_size;
T m_buffer[20];
};
Array<int> splitter(long long number)
{
Array<int> array;
if (number == 0) {
array.append(0);
return array;
}
while (number) {
long reduced = number / 10;
array.append(number - reduced * 10);
number = reduced;
}
for (int i = 0; i < array.size(); ++i) {
int v = array[i];
array[i] = array[array.size() - i - 1];
array[array.size() - i - 1] = v;
}
return array;
}
class TextWriter
{
public:
TextWriter(int handle) :
m_handle(handle)
{
}
TextWriter &operator<<(char c)
{
write(m_handle, &c, 1);
return *this;
}
TextWriter &operator<<(const char *str)
{
write(m_handle, str, own_strlen(str));
return *this;
}
TextWriter &operator<<(const int n)
{
Array<int> digits = splitter(n);
for (int i = 0; i < digits.count(); ++i) {
char c = digits.at(i) + '0';
*this << c;
}
return *this;
}
private:
int m_handle;
};
TextWriter cout(STDOUT_FILENO);
const char endl = '\n';
int main(int argc, char *argv[])
{
cout << "Hello, world!" << endl;
cout << 50 << endl;
return 0;
}