⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringtable.cpp

📁 这是一个用来编译pascal的一个小程序有些还不行但也发了
💻 CPP
字号:
// StringTable.cpp: implementation of the StringTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyPascal.h"
#include "StringTable.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

StringTable::StringTable()
{
	this->i_length = 0;
	this->i_maxLength = 0;
	this->table = NULL;
	this->s_tableName = "string table";
	this->i_enlargeLength = DEFAULT_LENGTH;
	this->b_sort = true;
}

StringTable::StringTable(StringTable& table)
{
	this->i_length = table.i_length;
	this->i_maxLength = table.i_maxLength;
	this->i_enlargeLength = table.i_enlargeLength;
	this->b_sort = table.b_sort;
	this->CreateTable(i_maxLength);
	this->s_tableName = table.s_tableName;

	for(int i = 0; i < i_length; i++)
	{
		this->table[i] = table.table[i];
	}
}

StringTable::StringTable(CString tableName, bool sort/* = true*/)
{
	this->i_length = 0;
	this->i_maxLength = 0;
	this->table = NULL;
	this->s_tableName = tableName;
	this->i_enlargeLength = DEFAULT_LENGTH;
	this->b_sort = sort;
}

StringTable::~StringTable()
{

}



// set table name
void StringTable::SetName(CString name)
// set table name
{
	this->s_tableName = name;
}

bool StringTable::CreateTable(int maxLength/* = DEFAULT_LENGTH*/)
// creat table, if this table is black
{
	if(this->table != NULL)
		return false;

	this->table = new StringTableElement[maxLength];
	this->i_maxLength = maxLength;

	return true;
}

void StringTable::EnlargeTable(int enlargeLength/* = DEFAULT_LENGTH*/)
// enlarge the table
{
	if(enlargeLength <= 0)
	{
		return ;
	}

	this->i_maxLength += enlargeLength;
	StringTableElement* newtable = new StringTableElement[this->i_maxLength];

	if(this->table != NULL)
	{
		for(int i = 0; i< this->i_length; i++)
		{
			newtable[i] = this->table[i];
		}
		delete []table;
		table = NULL;
	}

	this->table = newtable;
}

void StringTable::SetEnlargeLength(int length)
// set enlarge length
{
	if(length <= 0)
	{
		length = DEFAULT_LENGTH;
	}
	this->i_enlargeLength = length;
}

void StringTable::DeleteTable()
// delete table
{
	if(this->table != NULL)
	{
		delete []table;
	}
	table = NULL;
	this->i_length = 0;
	this->i_maxLength = 0;
}

bool StringTable::InsertElement(int type, CString name)
// insert element
{
	return this->InsertElement(StringTableElement(type, name));
}

bool StringTable::InsertElement(StringTableElement te)
// insert element
{
	if(table == NULL)
	{
		this->CreateTable();
	}

	int index = this->IndexToInsert(te);

	if(index >= 0)
	{
		if(this->i_length == this->i_maxLength)
		{
			this->EnlargeTable(this->i_enlargeLength);
		}

		for(int i = this->i_length; i > index; i--)
		{
			table[i] = table[i - 1];
		}
		table[index] = te;

		this->i_length++;
	}

	return true;
}

StringTableElement StringTable::operator [](int index)
// get element
{
	StringTableElement re;

	if(index >= 0 && index < this->i_length)
	{
		re = table[index];
	}

	return re;
}

int StringTable::MatchType(CString ename)
// match string, get element type
{
	for(int i = 0; i < i_length; i++)
	{
		if(table[i].s_name == ename)
		{
			return table[i].i_type;
		}
	}

	return -1;
}

int StringTable::IndexToInsert(StringTableElement te)
// get where in the table can insert the element
{
	if(i_length == 0)
	{
		return 0;
	}
	if(!b_sort)
	{
		return i_length;
	}

	int start = 0, end = i_length;
	int i = (start + end) / 2;

	while(start <= end)
	{
		i = (start + end) / 2;
		if((*this)[i] < te)
		{
			start = i + 1;
		}
		else if((*this)[i] > te)
		{
			end = i - 1;
		}
		else
		{
			return -1;
		}
	}

	if(i < i_length && (*this)[i] < te)
	{
		i++;
	}

	return i;
}

CString StringTable::ToString()
// ToString
{
	CString sort[] = {"off", "on"};
	CString s;
	s.Format("---StringTable---\r\n<<%s>>\r\n(length: %d, max length: %d, enlarge length: %d, auto sort %s)\r\n",
		this->s_tableName, this->i_length, this->i_maxLength, this->i_enlargeLength, sort[this->b_sort]);

	for(int i = 0; i < this->i_length; i++)
	{
		CString temp;
		temp.Format("%8d:    ", i);
		s += temp + table[i].ToString();
	}

	return s;
}

int StringTable::GetLength()
// get table length
{
	return this->i_length;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -