📄 symbol.cpp
字号:
#include "StdAfx.h"
#include "Symbol.h"
CSymbol::CSymbol(void)
: m_VariableValue(0),
m_VectorLength(0),
m_VectorValue(NULL),
m_VectorSelectedSubscript(-1),
m_MatrixFirstDimensionLength(0),
m_MatrixSecondDimensionLength(0),
m_MatrixSelectedFirstDim(-1),
m_MatrixSelectedSecondDim(-1),
m_MatrixValue(NULL),
m_SymbolType(S_NONE)
{
return;
}
// Create a named symbol
CSymbol::CSymbol(string Name)
: m_VariableValue(0),
m_VectorLength(0),
m_VectorValue(NULL),
m_VectorSelectedSubscript(-1),
m_MatrixFirstDimensionLength(0),
m_MatrixSecondDimensionLength(0),
m_MatrixSelectedFirstDim(-1),
m_MatrixSelectedSecondDim(-1),
m_MatrixValue(NULL),
m_SymbolType(S_NONE)
{
this->m_Name = Name;
return;
}
// Create a named, typed symbol
CSymbol::CSymbol(string Name, symboltype_t Type)
: m_VariableValue(0),
m_VectorLength(0),
m_VectorValue(NULL),
m_VectorSelectedSubscript(-1),
m_MatrixFirstDimensionLength(0),
m_MatrixSecondDimensionLength(0),
m_MatrixSelectedFirstDim(-1),
m_MatrixSelectedSecondDim(-1),
m_MatrixValue(NULL),
m_SymbolType(S_NONE)
{
this->m_Name = Name;
this->m_SymbolType = Type;
// Create a default vector or matrix, depending
// on the given type
if(this->m_SymbolType == VECTOR)
{
this->CreateVector(10);
}
else if (this->m_SymbolType == MATRIX)
{
this->CreateMatrix(10, 10);
}
}
CSymbol::~CSymbol(void)
{
if(this->m_VectorValue != NULL)
{
free(this->m_VectorValue);
//delete[] this->m_VectorValue;
}
if(this->m_MatrixValue != NULL)
{
for(int i = 0; i < this->m_MatrixFirstDimensionLength; i++)
{
free(this->m_MatrixValue[i]);
}
free(this->m_MatrixValue);
}
return;
}
// Create a vector from an existing, uninitialized symbol
void CSymbol::CreateVector(int Dimension)
{
this->m_VectorLength = Dimension;
this->m_VectorValue = (float *)malloc(sizeof(float) * this->m_VectorLength);
//this->m_VectorValue = new float[Dimension];
// Prepopulate the array with zeros
for(int i = 0; i < this->m_VectorLength; i++)
{
this->m_VectorValue[i] = 0.0;
}
this->m_SymbolType = VECTOR;
}
// Create a matrix from an existing, uninitialized symbol
void CSymbol::CreateMatrix(int FirstDimension, int SecondDimension)
{
this->m_MatrixFirstDimensionLength = FirstDimension;
this->m_MatrixSecondDimensionLength = SecondDimension;
// Allocate the first dimension
this->m_MatrixValue = (float **)malloc(sizeof(float *) * this->m_MatrixFirstDimensionLength);
// Allocate the second dimension
for(int i = 0; i < this->m_MatrixFirstDimensionLength; i++)
{
this->m_MatrixValue[i] = (float *)malloc(sizeof(float) * this->m_MatrixSecondDimensionLength);
// Prepopulate the matrix
for(int j = 0; j < this->m_MatrixSecondDimensionLength; j++)
{
this->m_MatrixValue[i][j] = 0.0;
}
}
this->m_SymbolType = MATRIX;
}
// Set the subscript for a vector
void CSymbol::SetSubscript(int Dim)
{
this->m_VectorSelectedSubscript = Dim;
}
// Set the subscripts for a matrix
void CSymbol::SetSubscript(int FirstDim, int SecondDim)
{
this->m_MatrixSelectedFirstDim = FirstDim;
this->m_MatrixSelectedSecondDim = SecondDim;
}
// Set a value in a flat variable
void CSymbol::Set(float Value)
{
// Make sure that this is a flat variable
if(this->m_SymbolType == VARIABLE)
{
this->m_VariableValue = Value;
}
// Or a proper vector
else if(this->m_SymbolType == VECTOR && this->m_VectorSelectedSubscript >= 0)
{
// Make sure it's within bounds
if(this->m_VectorSelectedSubscript >= this->m_VectorLength)
throw SUBSCRIPT_ERROR;
this->m_VectorValue[this->m_VectorSelectedSubscript] = Value;
}
// Or a proper matrix
else if(this->m_SymbolType == MATRIX &&
this->m_MatrixSelectedFirstDim >= 0 &&
this->m_MatrixSelectedSecondDim >= 0)
{
// Stay within boundaries
if(this->m_MatrixSelectedFirstDim >= this->m_MatrixFirstDimensionLength &&
this->m_MatrixSelectedSecondDim >= this->m_MatrixSecondDimensionLength)
throw SUBSCRIPT_ERROR;
this->m_MatrixValue[this->m_MatrixSelectedFirstDim][this->m_MatrixSelectedSecondDim] = Value;
}
// Otherwise, throw an exception
else
{
throw ILLEGAL_VARIABLE;
}
}
// Set a value in a vector
void CSymbol::Set(float Value, int Subscript)
{
// Make sure the current symbol is a vector
if(this->m_SymbolType != VECTOR)
{
throw ILLEGAL_VARIABLE;
}
// Make sure the subscript is within bounds
if(Subscript >= this->m_VectorLength)
{
throw SUBSCRIPT_ERROR;
}
// Otherwise, set the value
this->m_VectorValue[Subscript] = Value;
}
// Set a value in an existing matrix
void CSymbol::Set(float Value, int FirstSubscript, int SecondSubscript)
{
// Make sure that the current symbol is a matrix
if(this->m_SymbolType != MATRIX)
{
throw ILLEGAL_VARIABLE;
}
// Make sure both subscripts are within bounds
if(FirstSubscript >= this->m_MatrixFirstDimensionLength ||
SecondSubscript >= this->m_MatrixSecondDimensionLength)
{
throw SUBSCRIPT_ERROR;
}
// Otherwise, set the value
this->m_MatrixValue[FirstSubscript][SecondSubscript] = Value;
}
// Get the identifier name of the symbol
string CSymbol::GetName(void)
{
return(this->m_Name);
}
// Get the value from any kind of variable,
// provided a selection has been made
float CSymbol::Get(void)
{
// Make sure that this is a flat variable
if(this->m_SymbolType == VARIABLE)
{
return(this->m_VariableValue);
}
// Or a proper vector
else if(this->m_SymbolType == VECTOR && this->m_VectorSelectedSubscript >= 0)
{
return(this->m_VectorValue[this->m_VectorSelectedSubscript]);
}
// Or a proper matrix
else if(this->m_SymbolType == MATRIX &&
this->m_MatrixSelectedFirstDim >= 0 &&
this->m_MatrixSelectedSecondDim >= 0)
{
return(this->m_MatrixValue[this->m_MatrixSelectedFirstDim][this->m_MatrixSelectedSecondDim]);
}
// Otherwise, throw an exception
else
{
throw ILLEGAL_VARIABLE;
}
}
// Get the value from a vector subscript
float CSymbol::Get(int Subscript)
{
// Make sure that this is a vector
if(this->m_SymbolType != VECTOR)
{
throw ILLEGAL_VARIABLE;
}
// Make sure that we're retrieving a subscript within
// bounds
if(Subscript >= this->m_VectorLength || Subscript < 0)
{
throw SUBSCRIPT_ERROR;
}
// Otherwise, return the value
return(this->m_VectorValue[Subscript]);
}
// Get a value from a matrix
float CSymbol::Get(int FirstSubscript, int SecondSubscript)
{
// Make sure that this is a matrix
if(this->m_SymbolType != MATRIX)
{
throw ILLEGAL_VARIABLE;
}
// Make sure that we're retrieving a subscript within
// bounds
if( FirstSubscript >= this->m_MatrixFirstDimensionLength ||
FirstSubscript < 0 ||
SecondSubscript >= this->m_MatrixSecondDimensionLength ||
SecondSubscript < 0)
{
throw SUBSCRIPT_ERROR;
}
// Otherwise, return the value
return(this->m_MatrixValue[FirstSubscript][SecondSubscript]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -