📄 coord.cpp
字号:
#include <fstream.h>
#include <limits.h>
#include "coord.h"
// error handling
int mgcCoordinate::verbose = 0;
unsigned mgcCoordinate::error = 0;
const unsigned mgcCoordinate::allocation_failed = 0x00000001;
const unsigned mgcCoordinate::zero_dimension = 0x00000002;
const unsigned mgcCoordinate::invalid_dimension = 0x00000004;
const unsigned mgcCoordinate::incompatible_dimensions = 0x00000008;
const char* mgcCoordinate::message[4] = {
"failed to allocate memory",
"zero dimension in variable argument list",
"invalid dimension requested for operator[]",
"incompatible dimensions in comparison or update"
};
//===========================================================================
void mgcCoordinate::
Create (int _dimensions, const int* initial)
{
if ( (dimensions = _dimensions) == 0 )
component = 0;
else {
if ( (component = new int[dimensions]) == 0 ) {
Report(allocation_failed);
return;
}
int d;
if ( initial )
for (d = 0; d < dimensions; d++)
component[d] = initial[d];
else
for (d = 0; d < dimensions; d++)
component[d] = 0;
}
}
//---------------------------------------------------------------------------
mgcCoordinate::
mgcCoordinate (int _dimensions, const int* initial)
{
Create(_dimensions,initial);
}
//---------------------------------------------------------------------------
mgcCoordinate::
mgcCoordinate (int _dimensions, int initial_all)
{
if ( _dimensions == 0 ) {
Create(0,0);
Report(zero_dimension);
return;
}
int* initial = new int[_dimensions];
for (int d = 0; d < _dimensions; d++)
initial[d] = initial_all;
Create(_dimensions,initial);
delete[] initial;
}
//---------------------------------------------------------------------------
mgcCoordinate::
mgcCoordinate (int _dimensions, int initial0, int initial1, ...)
{
if ( _dimensions < 2 ) {
Create(0,0);
Report(zero_dimension);
return;
}
int* initial = new int[_dimensions];
initial[0] = initial0;
initial[1] = initial1;
va_list ap;
va_start(ap,initial1);
for (int d = 2; d < _dimensions; d++)
initial[d] = va_arg(ap,int);
va_end(ap);
Create(_dimensions,initial);
delete[] initial;
}
//---------------------------------------------------------------------------
mgcCoordinate::
mgcCoordinate (const mgcCoordinate& coordinate)
{
Create(coordinate.dimensions,coordinate.component);
}
//---------------------------------------------------------------------------
int& mgcCoordinate::
operator[] (int d) const
{
if ( 0 <= d && d < dimensions )
return component[d];
Report(invalid_dimension);
static int dummy;
return (dummy = INT_MAX);
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator= (const mgcCoordinate &coordinate)
{
if ( dimensions != coordinate.dimensions ) {
delete[] component;
Create(coordinate.dimensions,coordinate.component);
}
for (int d = 0; d < dimensions; d++)
component[d] = coordinate.component[d];
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator= (int value)
{
for (int d = 0; d < dimensions; d++)
component[d] = value;
return *this;
}
//---------------------------------------------------------------------------
int mgcCoordinate::
operator== (const mgcCoordinate& coordinate) const
{
if ( dimensions != coordinate.dimensions )
return 0;
for (int d = 0; d < dimensions; d++)
if ( component[d] != coordinate.component[d] )
return 0;
return 1;
}
//---------------------------------------------------------------------------
int mgcCoordinate::
operator< (const mgcCoordinate& coordinate) const
{
if ( dimensions != coordinate.dimensions )
return 0;
for (int d = 0; d < dimensions; d++)
if ( component[d] >= coordinate.component[d] )
return 0;
return 1;
}
//---------------------------------------------------------------------------
int mgcCoordinate::
operator<= (const mgcCoordinate& coordinate) const
{
if ( dimensions != coordinate.dimensions )
return 0;
for (int d = 0; d < dimensions; d++)
if ( component[d] > coordinate.component[d] )
return 0;
return 1;
}
//---------------------------------------------------------------------------
int mgcCoordinate::
operator> (const mgcCoordinate& coordinate) const
{
if ( dimensions != coordinate.dimensions )
return 0;
for (int d = 0; d < dimensions; d++)
if ( component[d] <= coordinate.component[d] )
return 0;
return 1;
}
//---------------------------------------------------------------------------
int mgcCoordinate::
operator>= (const mgcCoordinate& coordinate) const
{
if ( dimensions != coordinate.dimensions )
return 0;
for (int d = 0; d < dimensions; d++)
if ( component[d] < coordinate.component[d] )
return 0;
return 1;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator+= (const mgcCoordinate& coordinate)
{
if ( dimensions != coordinate.dimensions )
Report(incompatible_dimensions);
else
for (int d = 0; d < dimensions; d++)
component[d] += coordinate.component[d];
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator-= (const mgcCoordinate& coordinate)
{
if ( dimensions != coordinate.dimensions )
Report(incompatible_dimensions);
else
for (int d = 0; d < dimensions; d++)
component[d] -= coordinate.component[d];
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator+= (int value)
{
for (int d = 0; d < dimensions; d++)
component[d] += value;
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator-= (int value)
{
for (int d = 0; d < dimensions; d++)
component[d] -= value;
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator*= (int value)
{
for (int d = 0; d < dimensions; d++)
component[d] *= value;
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate& mgcCoordinate::
operator/= (int value)
{
for (int d = 0; d < dimensions; d++)
component[d] /= value;
return *this;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator+ (const mgcCoordinate& coordinate)
{
mgcCoordinate result = *this;
result += coordinate;
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator- (const mgcCoordinate& coordinate)
{
mgcCoordinate result = *this;
result -= coordinate;
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator+ (int value)
{
mgcCoordinate result = *this;
result += value;
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator- (int value)
{
mgcCoordinate result = *this;
result -= value;
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator* (int value)
{
mgcCoordinate result = *this;
result *= value;
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator/ (int value)
{
mgcCoordinate result = *this;
result /= value;
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate mgcCoordinate::
operator- ()
{
mgcCoordinate result = *this;
for (int d = 0; d < dimensions; d++)
result.component[d] = -result.component[d];
return result;
}
//---------------------------------------------------------------------------
mgcCoordinate operator* (int value, mgcCoordinate& coordinate)
{
return coordinate*value;
}
//---------------------------------------------------------------------------
ostream& operator<< (ostream& ostr, const mgcCoordinate& coordinate)
{
ostr << "(";
if ( coordinate.dimensions > 0 ) {
ostr << coordinate.component[0];
for (int d = 1; d < coordinate.dimensions; d++)
ostr << ',' << coordinate.component[d];
}
ostr << ")";
return ostr;
}
//---------------------------------------------------------------------------
int mgcCoordinate::
Number (unsigned single_error)
{
int result;
for (result = -1; single_error; single_error >>= 1)
result++;
return result;
}
//---------------------------------------------------------------------------
void mgcCoordinate::
Report (unsigned single_error)
{
if ( verbose )
cout << "mgcCoordinate: " << message[Number(single_error)] << endl;
else
ofstream("coord.err",ios::out|ios::app)
<< "mgcCoordinate: " << message[Number(single_error)] << endl;
error |= single_error;
}
//---------------------------------------------------------------------------
void mgcCoordinate::
Report (ostream& ostr)
{
for (unsigned single_error = 1; single_error; single_error <<= 1) {
if ( error & single_error )
ostr << "mgcCoordinate: " << message[Number(single_error)]
<< endl;
}
error = 0;
}
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -