📄 functiontable.cpp
字号:
#include "StdAfx.h"
#include "FunctionTable.h"
CFunctionTable::CFunctionTable(void)
{
this->m_FunctionTable = new functionlist_t();
// Array of builtin functions
struct builtins_t {
string Name;
float (*coderef)(float Value);
} Builtin_Functions[] = {
"SIN", &CBuiltins::SIN,
"COS", &CBuiltins::COS,
"TAN", &CBuiltins::TAN,
"ATN", &CBuiltins::ATN,
"EXP", &CBuiltins::EXP,
"ABS", &CBuiltins::ABS,
"LOG", &CBuiltins::LOG,
"SQR", &CBuiltins::SQR,
"RND", &CBuiltins::RND,
"INT", &CBuiltins::INT,
"", NULL
};
// Loop through the array of built in functions and bind them
int i = 0;
do
{
struct builtins_t Builtin = Builtin_Functions[i];
CFunction *Func = new CFunction(Builtin.Name, Builtin.coderef);
this->Add(Func);
i++;
} while(Builtin_Functions[i].Name != "");
// Seed the random number generator
srand((unsigned int)time(NULL));
}
CFunctionTable::~CFunctionTable(void)
{
functionlist_t::iterator functionIterator = this->m_FunctionTable->begin();
while(functionIterator != this->m_FunctionTable->end())
{
delete(*functionIterator);
functionIterator++;
}
delete this->m_FunctionTable;
}
// Add a function to the function table
void CFunctionTable::Add(CFunction * Function)
{
// First, make sure there are no existing functions
// with this name
CFunction *thisFunction = this->Find(Function->GetName());
if(thisFunction != NULL)
{
// If there is an existing function, redefine it
// (Does not work on builtin functions)
if(thisFunction->GetBuiltin() == true)
{
throw ILLEGAL_INSTRUCTION;
}
thisFunction->SetExpression(Function->GetExpression());
return;
}
// Otherwise, then push the symbol onto the list
this->m_FunctionTable->push_back(Function);
}
// Find a function by name
CFunction * CFunctionTable::Find(string Name)
{
// Iterate over the function table, find the function that
// matches and return
functionlist_t::iterator func = this->m_FunctionTable->begin();
while(func != this->m_FunctionTable->end())
{
CFunction *thisFunction = *func;
if(thisFunction->GetName() == Name)
{
return thisFunction;
}
func++;
}
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -