U E D R , A S I H C RSS

데블스캠프2011/셋째날/String만들기/김준석

소스 코드

#include<iostream>

using namespace std;

class String{
private :
	char *value;
	int offset;
	int count;

public :
	String(){
		value = NULL;
		offset = 0;
		count = 0;
	}

	String(const char *original){
		offset = 0;
		count = strlen(original);
		count++;
		value = new char[count];
		strcpy(value,original);
	}

	String(const String& str, const int offset,const int count){
		//예외처리점...
		if(offset >= str.count){
			this->value = '\0';
		}
		else{
			this->value = str.value + offset;
		}

		this->offset = 0;
		this->count = count;

	}

	void print(){
		cout << "Value : " ;
		for(int i =0;i<count;i++){
			cout << *(value+i);
		}
	}

	char charAt(const int at){
		if(at>count) return '\0';
		//길이에 따른 예외처리 필요함.
		return value[at];
	}

	int indexOf(String& str){

		for(int i =0; i < this->count; i++){
		int select = -1;
		if(this->charAt(i) == str.charAt(0)){
			select = i;
			for(int j = 0; j < str.count-1;j++){
				//크기 예외처리
				if(this->charAt(select + j) != str.charAt(j)) select = -1;
				}
				if(select != -1) return select;
			}
		}
		return -1;
	} 

	int lastIndexOf(String& str){

		int choice = -1;
		for(int i =0; i < this->count; i++){
			int select = -1;
			if(this->charAt(i) == str.charAt(0)){
					select = i;
					for(int j = 0; j < str.count-1;j++){
						//크기 예외처리
						if(this->charAt(select + j) != str.charAt(j)) select = -1;
					}
					if(select != -1) choice = select;
				}
		}
		if(choice != -1) return choice;
		return -1;
	} 

	bool isEmpty(){
		if(count == 0) return true;
		else return false;
	}

	int compareTo(const char * compare){
		String * temp = new String(compare);
		if(this->equals(*temp)) return 0;
		else return -1;

		//ㅋㅋ 야매임.
	}

	void concat(String& str){
		int tempCount = this->count +  str.count -2 + 1;
		char * temp = new char[tempCount];
		strcpy(temp,this->value);
		strcat(temp,str.value);
		this->value = temp;
		this->count = tempCount;

		//완료?? 아마도?
	}

	int length(){
		return this->count;
	}

	String * subString(int offset, int count){
		char * temp = new char[count+1];
	
		//크기 예외처리
		char * offvalue = this->value+ offset;
		for(int i =0;i< count;i++)
			*(temp + i) = *(offvalue+i); 

		temp[count] = '\0';
		
		return new String(temp);		
	}

	bool equals(String& str){

		if(this->count != str.count) return false;
		
		char * temp = this->value + this->offset;
		char * temp2 = str.value +  str.offset;
		
		for(;*temp != '\0'; temp++,temp2++){
			if(*temp != *temp2) return false;
		}

		return true;
	}

};

int main(){
	
	String * s = new String("abcdefgh");
	String * s2 = new String("de");
	if(s->subString(3,2)->equals(*s2)) cout << "꾸엑" <<endl;
	
	//cout << s->lastIndexOf(*s2) << endl;
	
	return 0;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:14
Processing time 0.0165 sec