下面是字符串数据类型的实现
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; }