U E D R , A S I H C RSS

데블스캠프2011/셋째날/String만들기/서지혜

C++로 Java의 String 클래스 구현하기
  • 주의 : 매우 구림

  • String.h 헤더파일

class String {
private:
	char* values;
	char* valuesForCase;
	int length;
	int offset;
	int lengthForSubs;

public:
	String();
	String(const char* origin);
	String(const String str, const int offset, const int length);
	~String();
public:
	const char charAt(const int offset);
	void concat(const char* str); 
	bool equals(String str);
	bool equals(const char* str);
	bool isEmpty();
	const char* toLower();
	const char* toUpper();
	void print();
};

  • String.cpp 소스파일

{{{
{{{#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include "String.h"


String::String()
{	
	values = NULL;
	valuesForCase = NULL;
	offset = -1;
	length = 0;
}

String::String(const char* origin)
{	
	int i;
 	for(i=0; ; i++) 
	{
		if(origin[i] == '\0') break;
	}

	offset = 0;
	length = i;
	values = (char*)malloc(sizeof(char) * length);
	valuesForCase = NULL;
	
	for(i=0 ; i<length; i++)
	{
		values[i] = origin[i];
	}
}

String::String(const String str, const int offset, const int length)
{
	this->offset = 0;
	this->length = length;
	values = (char*)malloc(sizeof(char) * length);
	valuesForCase = NULL;

	int i;
	for(i=0; i<length; i++)
	{
		values[i] = str.values[offset + i];
	}
}

String::~String() 
{

}

///////////////////////////////////////////////////////////////////////
////

const char String::charAt(int offset)
{
	return values[offset];
}

bool String::isEmpty()
{
	if(length <= 0) 
		return true;

	return false;
}

String String::concat(const char* str)
{
	int i, newLength;
	for(i=0; ; i++) 
	{
		if(str[i] == '\0') break;
	}
	newLength = this->length+i;

	char* newValues = (char*)malloc(sizeof(char) * newLength);

	for(i=0; i<newLength; i++)
	{
		if(i < this->length)
			newValues[i] = values[i];
		else if(i >= this->length)
			newValues[i] = str[i-this->length];
	}

	String temp(newValues);
	temp.length = newLength;
	return temp;
}

bool String::equals(String str)
{
	if(this->length != str.length)
		return false;

	for(int i=0; i<this->length; i++)
	{
		if(values[i] != str.values[i])
			return false;
	}

	return true;
}

bool String::equals(const char* str)
{
	int i;
	for(i=0; ; i++)
	{
		if(str[i] == '\0') break;
	}
	if(i != length) 
		return false;

	for(i=0; i<this->length; i++)
	{
		if(values[i] != str[i])
			return false;
	}

	return true;
}


const char* String::toLower()
{
	if(valuesForCase != NULL)
		free(valuesForCase);
	valuesForCase = (char*)malloc(sizeof(char) * (length+1));
	
	int i;
	for(i=0; i<length; i++)
	{
		if(values[i] >= 65 && values[i] <= 90)
		{
			valuesForCase[i] = values[i]+32;
		}
	}
	valuesForCase[i] = '\0';

	return valuesForCase;
}

const char* String::toUpper()
{
	if(valuesForCase != NULL)
		free(valuesForCase);
	valuesForCase = (char*)malloc(sizeof(char) * (length+1));
	
	int i;
	for(i=0; i<length; i++)
	{
		if(values[i] >= 90 && values[i] <= 122)
		{
			valuesForCase[i] = values[i]-32;
		}
	}
	valuesForCase[i] = '\0';

	return valuesForCase;
}

void String::print()
{
	for(int i=0; i<length; i++)
	{
		putchar(values[i]);
	}
	printf("\n");
}

}}}

 * 메인함수
{{{
#include <stdio.h>
#include "String.h"


int main()
{
	String str("dddd");
	
	// charAt
	if(str.charAt(3) == 'd')	// OK
		printf("oo \n");
	
	// concat
	String strCon = str.concat("ccc");

	// equals
	String str2("ddddccc");
	if(str2.equals(strCon))	// OK
		printf("oo2 \n");

	// toUpper
	String temp(str2.toUpper());
	if(temp.equals("DDDDCCC"))
		printf("DDDDCCCC is same\n");

	// toLower
	String temp2(temp.toLower());
	if(temp2.equals("ddddccc"));
		printf("ddddccc is same\n");

	// isEmpty
	if(!str2.isEmpty())
		printf("str2 is not empty\n");
	String str3;
	if(str3.isEmpty())
		printf("str2 is empty\n");

	return 0;
}
}}}
----
 * 짱재밌당!! 올해 데블스에도 이런거하자 - [서지혜]
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:13
Processing time 0.0136 sec