Difference between r1.2 and the current
@@ -30,7 +30,7 @@
* String.cpp 소스파일
{{{
#include <stdio.h>
{{{#include <stdio.h>
#include <malloc.h>#include <stdlib.h>
#include "String.h"
@@ -79,6 +79,7 @@
String::~String()
{
///////////////////////////////////////////////////////////////////////
@@ -97,7 +98,7 @@
return false;
}
void String::concat(const char* str)
int i, newLength;
for(i=0; ; i++)
}
String String::concat(const char* str)
{int i, newLength;
for(i=0; ; i++)
@@ -116,8 +117,9 @@
newValues[i] = str[i-this->length];
}
values = newValues;
bool String::equals(String str)
}
length = newLength;
String temp(newValues);
temp.length = newLength;
return temp;
}bool String::equals(String str)
@@ -153,6 +155,7 @@
return true;
}
const char* String::toLower()
{
if(valuesForCase != NULL)
}
{
if(valuesForCase != NULL)
@@ -199,6 +202,7 @@
}
printf("\n");
}
}}}
* 메인함수
printf("\n");
}
* 메인함수
@@ -206,6 +210,7 @@
#include <stdio.h>
#include "String.h"
int main()
{
String str("dddd");
#include "String.h"
{
String str("dddd");
@@ -215,11 +220,11 @@
printf("oo \n");
// concat
// equals
String str2("ddddccc");
// toUpper
// concat
str.concat("ccc");
String strCon = str.concat("ccc");
// equals
String str2("ddddccc");
if(str.equals(str2)) // OK
if(str2.equals(strCon)) // OK
printf("oo2 \n");// toUpper
@@ -242,3 +247,5 @@
return 0;
}
}}}
}
}}}
----
* 짱재밌당!! 올해 데블스에도 이런거하자 - [서지혜]
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; } }}} ---- * 짱재밌당!! 올해 데블스에도 이런거하자 - [서지혜]