include string include stdlib include strings include iostream using n

  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
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include <iostream>
using namespace std;
String::String()
{
_length = 0;
string = new char[1];
string = '\0';
};
String::String(char ch)
{
_length = 1;
string = new char[2];
string[0] = ch;
string[1] = '\0';
};
String::String(char* str)
{
_length = strlen(str);
string = new char[strlen(str)+1];
strncpy(string, str, _length);
};
String::String(const String& str)
{
_length = str._length;
string = new char[_length+1];
strncpy(string, str.string, _length);
};
String::~String()
{
delete[] string;
}
void String::clear()
{
delete[] string;
String::String();
};
int String::length()
{
return _length;
};
ostream& operator<<(ostream& stream, String& value)
{
stream << value.string;
return stream;
};
Octal::Octal(): String('0')
{
};
Octal::Octal(char* ch): String(ch)
{
bool is_consistent = true;
for(int i=0; i < _length; i++)
{
switch (string[i])
{
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '+': case '-':
break;
default: is_consistent = false;
break;
}
}
if (!is_consistent)
{
delete[] string;
string = new char[2];
strcpy(string, "0");
_length = 1;
}
};
Octal::Octal(const Octal& number): String(number)
{
};
Octal::~Octal()
{
};
void Octal::inverse()
{
char* newstring = new char[strlen(string)];
if ( string[0] == '-' )
{
for(int i=0; i <= strlen(string)-1; i++)
newstring[i] = string[i+1];
}
else
{
sprintf(newstring, "%c%s", '-', string);
}
delete[] string;
string = newstring;
_length = strlen(newstring);
};
const Octal& Octal::operator=(const Octal& x)
{
if (&x == this) return *this;
if (string) delete[] string;
if (x.string)
{
string = new char [strlen(x.string) + 1];
strcpy(string, x.string);
}
else string = (char*)"0";
return *this;
};
Octal& Octal::operator/(Octal& x)
{
int a, b, result;
sscanf(this->string, "%o", &a);
sscanf(x.string, "%o", &b);
result = a / b;
char* tmp = new char[strlen(string)<strlen(x.string)?strlen(string):strlen(x.string)];
sprintf(tmp, "%o", abs(result));
Octal* res = new Octal(tmp);
if (result < 0) res->inverse();
return *res;
};
bool Octal::operator>=(const Octal& x)
{
int a, b;
sscanf(string, "%o", &a);
sscanf(x.string, "%o", &b);
if (a >= b) return true;
return false;
};