@Minniat, post #1
@szuler, post #2
@MDW, post #3
@Minniat, post #5
@Minniat, post #8
@Minniat, post #9
class line
{
private:
struct Node line_node; // węzeł
char *text; // tekst w linijce
int linelen; // długość tekstu
int lineused; // ilość używanych znaków
public:
line(int len); // konstruktor pobiera długość linijki i alokuje pamięć
~line(); // destruktor zwalnia pamięć
void ClearLine(); // kasuje linię
void SetText(char *newtext); // wstawia tekst do linii
void Print(); // wyświetla tekst
void Backspace(); // usuwa znak przed kursorem
void Delete(); // usuwa znak w miejscu kursora
void AddChar(char c); // dodaje nowy znak w miejscu kursora
void SplitLine(); // dzieli linię na dwie (póki co brak implementacji)
}
*cursorline;
int cursorpos; // pozycja x kursora
line :: line(int len)
{
printf("Constructing line (len=%d)...n",len);
text = (char *)malloc(len);
linelen = len;
}
line :: ~line()
{
printf("Deconstructing line...n" );
free(text);
}
void line :: ClearLine()
{
int i;
printf("Clearing line...n" );
text[0] = 'n';
text[1] = ' ';
for (i = 2; i < linelen; i++)
text[ i ] = ' ';
lineused = strlen(text);
}
void line :: SetText(char *newtext)
{
printf("Setting text to %s", newtext );
strcpy(text, newtext);
lineused = strlen(text);
}
void line :: Print()
{
printf("Printing line...n" );
printf("CHARS: %d LINE: %s", lineused, text);
}
void line :: Backspace()
{
int i;
printf("Backspacing...n" );
if (cursorpos > 0)
{
cursorpos--;
for (i = cursorpos; i < lineused; i++)
text[ i ] = text[i + 1];
text[ i ] = ' ';
}
lineused--;
cursorpos--;
}
void line :: Delete()
{
int i;
printf("Deleting...n" );
for (i = cursorpos; i < lineused; i++)
text[ i ] = text[i + 1];
text[ i ] = ' ';
lineused--;
}
void line::AddChar(char c)
{
int i;
printf("Adding char -%c-...n", c);
lineused++;
for (i = lineused; i > cursorpos; i--)
text[ i ] = text[i - 1];
text[cursorpos] = c;
cursorpos++;
}
void line::SplitLine()
{
} A oto przykład wykorzystania tej klasy. Trzeba dodać niezbędne inkludy. Program kompiluje się bez problemów w GCC. int main()
{
line testline(100);
char test[]="Hello!n ";
cursorline = &testline;
testline.ClearLine();
testline.Print();
testline.SetText(test);
testline.Print();
cursorpos = 5;
printf("Setting cursor at %d...n", cursorpos);
testline.AddChar(' ');
testline.AddChar('W');
testline.AddChar('o');
testline.AddChar('r');
testline.AddChar('l');
testline.AddChar('d');
testline.Print();
return 0;
} @Minniat, post #10
line new_line(2); new_line.SetText("Dupa zbita");@MinisterQ, post #12
@Minniat, post #13
@Minniat, post #15
clear ();
string& erase ( size_t pos = 0, size_t n = npos ); iterator erase ( iterator position ); iterator erase ( iterator first, iterator last );
string& erase ( size_t pos = 0, size_t n = npos ); iterator erase ( iterator position ); iterator erase ( iterator first, iterator last );
string& insert ( size_t pos1, const string& str );
string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
string& insert ( size_t pos1, const char* s, size_t n);
string& insert ( size_t pos1, const char* s );
string& insert ( size_t pos1, size_t n, char c );
iterator insert ( iterator p, char c );
void insert ( iterator p, size_t n, char c );
template
void insert ( iterator p, InputIterator first, InputIterator last ); string substr ( size_t pos = 0, size_t n = npos ) const;
std::string string_1; std::string string_2; std::string string_3; string_1 = "Hello "; string_2 = "World"; string_3 = string_1 + string_2; std::cout << string_3 << std::endl;
@szuler, post #16
@Minniat, post #10
@smith, post #20