More actions
imported>rabierre No edit summary |
(Repair batch-0005 pages from live compare) |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 47: | Line 47: | ||
for(i=0; ; i++) | for(i=0; ; i++) | ||
{ | { | ||
if(origin | if(origin[i] == '\0') break; | ||
} | } | ||
| Line 57: | Line 57: | ||
for(i=0 ; i<length; i++) | for(i=0 ; i<length; i++) | ||
{ | { | ||
values | values[i] = origin[i]; | ||
} | } | ||
} | } | ||
| Line 71: | Line 71: | ||
for(i=0; i<length; i++) | for(i=0; i<length; i++) | ||
{ | { | ||
values | values[i] = str.values[offset + i]; | ||
} | } | ||
} | } | ||
| Line 85: | Line 85: | ||
const char String::charAt(int offset) | const char String::charAt(int offset) | ||
{ | { | ||
return values | return values[offset]; | ||
} | } | ||
| Line 101: | Line 101: | ||
for(i=0; ; i++) | for(i=0; ; i++) | ||
{ | { | ||
if(str | if(str[i] == '\0') break; | ||
} | } | ||
newLength = this->length+i; | newLength = this->length+i; | ||
| Line 110: | Line 110: | ||
{ | { | ||
if(i < this->length) | if(i < this->length) | ||
newValues | newValues[i] = values[i]; | ||
else if(i >= this->length) | else if(i >= this->length) | ||
newValues | newValues[i] = str[i-this->length]; | ||
} | } | ||
| Line 127: | Line 127: | ||
for(int i=0; i<this->length; i++) | for(int i=0; i<this->length; i++) | ||
{ | { | ||
if(values | if(values[i] != str.values[i]) | ||
return false; | return false; | ||
} | } | ||
| Line 139: | Line 139: | ||
for(i=0; ; i++) | for(i=0; ; i++) | ||
{ | { | ||
if(str | if(str[i] == '\0') break; | ||
} | } | ||
if(i != length) | if(i != length) | ||
| Line 146: | Line 146: | ||
for(i=0; i<this->length; i++) | for(i=0; i<this->length; i++) | ||
{ | { | ||
if(values | if(values[i] != str[i]) | ||
return false; | return false; | ||
} | } | ||
| Line 163: | Line 163: | ||
for(i=0; i<length; i++) | for(i=0; i<length; i++) | ||
{ | { | ||
if(values | if(values[i] >= 65 && values[i] <= 90) | ||
{ | { | ||
valuesForCase | valuesForCase[i] = values[i]+32; | ||
} | } | ||
} | } | ||
valuesForCase | valuesForCase[i] = '\0'; | ||
return valuesForCase; | return valuesForCase; | ||
| Line 182: | Line 182: | ||
for(i=0; i<length; i++) | for(i=0; i<length; i++) | ||
{ | { | ||
if(values | if(values[i] >= 90 && values[i] <= 122) | ||
{ | { | ||
valuesForCase | valuesForCase[i] = values[i]-32; | ||
} | } | ||
} | } | ||
valuesForCase | valuesForCase[i] = '\0'; | ||
return valuesForCase; | return valuesForCase; | ||
| Line 196: | Line 196: | ||
for(int i=0; i<length; i++) | for(int i=0; i<length; i++) | ||
{ | { | ||
putchar(values | putchar(values[i]); | ||
} | } | ||
printf("\n"); | printf("\n"); | ||
| Line 204: | Line 204: | ||
#include <stdio.h> | #include <stdio.h> | ||
#include "String.h" | #include "String.h" | ||
int main() | int main() | ||
| Line 214: | Line 215: | ||
// concat | // concat | ||
str.concat("ccc"); | String strCon = str.concat("ccc"); | ||
// equals | // equals | ||
String str2("ddddccc"); | String str2("ddddccc"); | ||
if( | if(str2.equals(strCon)) // OK | ||
printf("oo2 \n"); | printf("oo2 \n"); | ||
| Line 240: | Line 241: | ||
return 0; | return 0; | ||
} | } | ||
---- | |||
* 짱재밌당!! 올해 데블스에도 이런거하자 - [[서지혜]] | |||
Latest revision as of 00:44, 27 March 2026
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;
}
- 짱재밌당!! 올해 데블스에도 이런거하자 - 서지혜