二叉ルックアップツリーのベースクラス実装(三)
1307 ワード
次に、文字列データ型の実装を示します.
StringClass.hファイル
StringClass.cppファイル
StringClass.hファイル
#ifndef STRING_CLASS_H
#define STRING_CLASS_H
#include "objectclass.h"
class StringClass:public ObjectClass
{
private:
char *data;
int length;
public:
StringClass();
StringClass(char *data,int length);
public:
int Compare(ObjectClass *objcls);
virtual void OutPut();
float getValue();
int getLength();
};
#endif
StringClass.cppファイル
#include "StringClass.h"
StringClass::StringClass()
{
data=NULL;
length=0;
}
StringClass::StringClass(char *data,int length)
{
this->data=data;
this->length=length;
}
int StringClass::Compare(ObjectClass *objcls)
{
if(this->getValue() < objcls->getValue())
return -1;
else if(this->getValue() > objcls->getValue())
return 1;
else
return 0;
}
// ,
//
float StringClass::getValue()
{
if(data==NULL)return 0;
float returnValue = 0;
for(int i=0;i<length;i++)
{
float mul=1;
for(int k=0;k<i;k++)
mul=mul*10;
returnValue = returnValue+((int)(*(data+i))-(int)'A')/mul;
}
return returnValue;
}
void StringClass::OutPut()
{
for(int i=0;i<length;i++)
cout<<*(data+i);
}
int StringClass::getLength()
{
return length;
}