All pastes #205708 Raw Edit

Something

public text v1 · immutable
#205708 ·published 2006-10-17 00:24 UTC
rendered paste body
#include "Includes.h"

int CComputer_Entry::_CompareSpeed(CComputer_Entry * left,const CComputer_Entry * right)
{
	double leftSpeed=atof(left->cpuSpeed.c_str());
	double rightSpeed=atof(right->cpuSpeed.c_str());

	if(leftSpeed>rightSpeed)
	{
		return 1;
	}

	if(leftSpeed<rightSpeed)
	{
		return -1;
	}

	return 0;
}

void CComputer_Entry::Print()
{
	//	Prints out a summary of itself

	int x=0;

	this->Print_Word(this->manufacturer,15,true,&x);
	cout << "   ";
	this->Print_Word(this->modelNumber,10,false,&x);
	cout << "   ";
	this->Print_Word(this->cpuSpeed,5,false,&x);
	cout << "   ";
	this->Print_Word(this->ramAmount,15,false,&x);
	cout << "   ";
	this->Print_Word(this->hdSize,15,false,&x);
}

void CComputer_Entry::Print_Word(string word,size_t columnWidth,bool rightJustify,int * cursorX)
{
	columnWidth-=word.size();

	if(!rightJustify){
		cout << word.c_str();
	}

	while(columnWidth>0)
	{
		cout << " ";
		columnWidth--;
	}

	if(rightJustify){
		cout << word.c_str();
	}
}

void CComputer_Entry::Print_Proper()
{
	//	Prints out a summary of itself
	cout
		<< this->manufacturer << ": " << this->modelNumber
		<< " @ " << this->cpuSpeed << "Ghz"
		<< ", " << this->ramAmount << " ram"
		<< ", " << this->hdSize << " hard drive";
}

bool CComputer_Entry::operator>(const CComputer_Entry & right)
{
	int result=this->_CompareSpeed(this,&right);
	return (result==1);
}

bool CComputer_Entry::operator>=(const CComputer_Entry & right)
{
	int result=this->_CompareSpeed(this,&right);
	return (result==1 || result==0);
}

bool CComputer_Entry::operator<(const CComputer_Entry & right)
{
	int result=this->_CompareSpeed(this,&right);
	return (result==-1);
}

bool CComputer_Entry::operator<=(const CComputer_Entry & right)
{
	int result=this->_CompareSpeed(this,&right);
	return (result==-1 || result==0);
}

bool CComputer_Entry::operator==(const CComputer_Entry & right)
{
	int result=this->_CompareSpeed(this,&right);
	return (result==0);
}

bool CComputer_Entry::operator!=(const CComputer_Entry & right)
{
	int result=this->_CompareSpeed(this,&right);
	return (result!=0);
}

void CComputer::Clear()
{
	//	Clear out vector
	this->entries.clear();
}

bool CComputer::GrabFromFile(string filePath)
{
    ifstream file;
	CComputer_Entry entry;
	bool done;
	bool result;

	//	Clear myself of data
	this->Clear();

	//	Open the file
	file.open(filePath.c_str(),ifstream::in);
	if(!file){
		this->_Log("Couldn't open file: ",filePath);
		return false;
	}
	this->_Log("File opened: ",filePath);

    //	Read lines until done
	done=false;
	this->_Log("CComputer::GrabFromFile - will read in the file...");
	while(!done)
	{
		result=this->_ReadLine(&file,&entry,&done);
		if(!result){
			this->_Log("CComputer::GrabFromFile has failed.");
			done=true;
		}
		else{
			this->entries.push_back(entry);
		}

		//	For dramatic effect
		Sleep(10);
	}

	this->_Log("CComputer::GrabFromFile has completed successfully.");
    
	return true;
}

void CComputer::Pager()
{
	int key;
	size_t index;
	bool done;

	//	Print our first frame
	index=0;
	this->PrintPage("Showing data... use a/z for up/down, escape to quit",index,10);

	//	Clear buffer
	while(kbhit()){getch();}

	done=false;
	while(!done)
	{
		key=getch();

		if(key=='a'){
			if(index>0){
				index--;
			}
		}
		else if(key=='z'){
			index++;
		}
		else if(key==27){
			done=true;
		}
		else{
			continue;
		}

		this->PrintPage("Showing data... use a/z for up/down, escape to quit",index,10);
	}
}

void CComputer::PrintPage(string heading,size_t startRecord,size_t pageSize)
{
	size_t a;

	system("cls");
	cout << "\n" << heading.c_str() << "\n";

	//	Iterate through each entry and print it
	for(a=startRecord;a<this->entries.size() && (a-startRecord<pageSize);a++){
		cout << "\n " << (int)a << ". ";
		this->entries[a].Print();
	}
}

CComputer_Entry * CComputer::FindSpeed(double speed)
{
	size_t a;
	double dTempSpeed;

	for(a=0;a<this->entries.size();a++){
		dTempSpeed=atof(this->entries[a].cpuSpeed.c_str());
		if(dTempSpeed==speed){
			return &this->entries[a];
		}
	}

	return NULL;
}

vector<CComputer_Entry *> CComputer::GetSpeedSortedList()
{
	vector<CComputer_Entry*> list;
	CComputer_Entry *left, *right, *temp;
	size_t a;
	bool didAnything;

	//	Fill list with unsorted pointers
	for(a=0;a<this->entries.size();a++){
		list.push_back(&this->entries[a]);
	}

	///////////////
	//	Bubble sort
	didAnything=true;

	//	Keep going until we've run through the list
	//	without having to switch anything
	while(didAnything)
	{
		didAnything=false;

		//	Run through the entire list (from first item to second to last item)
		//	switching any pair if needed
		for(a=0;a<list.size()-1;a++){
			left=list[a];
			right=list[a+1];

			//	Switch them?
			if(*left<*right){
				temp=list[a];
				list[a]=list[a+1];
				list[a+1]=temp;
				didAnything=true;
			}
		}
	}
	//	Bubble sort
	///////////////

	return list;
}

void CComputer::PrintSpeedSortedList()
{
	vector<CComputer_Entry*> list;
	size_t a;

	list=this->GetSpeedSortedList();

	cout << "\n\n-------Speed sorted list\n";
	for(a=0;a<list.size();a++){
		cout << "\n";
		list[a]->Print();
	}
}

void CComputer::_Log(string message1,string message2,string message3)
{
	cout 
		<< "\n"
		<< message1.c_str()
		<< message2.c_str()
		<< message3.c_str();
}
void CComputer::_Log(string message1,string message2)
{
	this->_Log(message1,message2,"");
}
void CComputer::_Log(string message1)
{
	this->_Log(message1,"");
}

bool CComputer::_ReadLine(/*IN*/ifstream * file,/*IN/OUT*/CComputer_Entry * entry,/*OUT*/bool * done)
{
	string temp;

	if(!*file){
		return false;
	}

	/*
		File should have this order per line:
			Computer Manufacturer,
			Model Number,
			Amount of RAM,
			Harddrive Size,
			Processor Speed
	*/

	*file >> entry->manufacturer;
	if(!*file){
		//	If first item won't read we're just done
		*done=true;
		return true;
	}

	*file >> entry->modelNumber;
	if(!*file){
		return false;
	}

	*file >> entry->ramAmount;
	if(!*file){
		return false;
	}

	*file >> entry->hdSize;
	if(!*file){
		return false;
	}

	//	This last one just sets to done if the file is now over
	*file >> entry->cpuSpeed;
	if(!*file){
		*done=true;
	}

	cout << ".";

	return true;
}