30.05.2012, 18:20. Просмотров 323. Ответов 0
помогите Разобраться в функции. Она форматирует строку по ширине. удаляет лишние пробелы.
C |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
| #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
//using namespace std;
void printAligned(char* str, int width);
int main()
{
int width;
cout << "vvedite dlinu viravnivania stroki : ";
cin >> width;
cout << "vvedite text:" << endl;
char** lines = (char**)malloc(sizeof(char*));
int k = 0;
char* newLine;
do {
lines = (char**) realloc(lines, sizeof(char*) * (k + 1));
newLine = (char*) malloc(sizeof(char) * 255);
gets(newLine);
lines[k++] = newLine;
} while ( newLine[strlen(newLine) - 1] != '.');
cout << "Format text:" << endl;
for (int i = 0; i < k; ++i) {
printAligned(lines[i], width);
free(lines[i]);
}
free(lines);
cout<<k;
getch();
return 0;
}
void printAligned(char* str, int width) {
if (strlen(str) == 0) {
cout << endl;
return;
}
int wordBegin = 0;
char** words = (char**) malloc(sizeof(char*));
int wordsSize = 0;
while (wordBegin < strlen(str)) {
if (str[wordBegin] == ' ') {
wordBegin++;
continue;
}
int wordLen = 1;
while (wordBegin + wordLen < strlen(str)
&& str[wordBegin + wordLen] != ' ')
{
wordLen++;
}
words = (char**) realloc(words, sizeof(char*) * (wordsSize + 1)); //[COLOR="Red"]не понимаю начиная с этого момента [/COLOR]
words[wordsSize] = strncpy((char*)malloc(sizeof(char)*(wordLen + 1)),
str + wordBegin, wordLen); // str + wordBegin как это?
words[wordsSize++][wordLen] = '\0';
wordBegin += wordLen;
}
if (wordsSize == 0)
{
cout << endl;
return;
}
for (int front = 0; front < wordsSize; )
{
while (strlen(words[front]) > width)
{
char * toOut = strncpy((char*) malloc(sizeof(char) * (width + 1)),
words[front], width);
char * rest = strcpy((char*) malloc(sizeof(char) * (strlen(words[front]) - width + 1)),
words[front] + width); //непонятно что и куда мы тут копируем
toOut[width] = '\0';
rest[strlen(words[front]) - width + 1] = '\0';
cout << toOut << endl;
free(words[front]);
free(toOut);
words[front] = rest;
}
int toPrint = 0; // за что отвечают эти переменные?
int toPrintW = 0;
for (int i = front; i != wordsSize; ++i)
{
if (toPrintW + strlen(words[i]) + toPrint <= width)
{
toPrintW += strlen(words[i]);
toPrint++;
} else
{
break;
}
}
if (toPrint > 1)
{
int spaces = (width - toPrintW) / (toPrint - 1); //spaces between words каак?
int spaces2 = (width - toPrintW) % (toPrint - 1);//count of words with +1 space after
for (int j = 0; j < toPrint; ++j)
{
cout << words[front++];
if (j != toPrint - 1)
{
for (int s = 0; s < spaces; ++s) cout << ' ';
if (j < spaces2) cout << ' ';
}
}
}
else
{
cout << words[front++];
}
cout << endl;
}
} |
|
если можно объясните пожалуйста, как это работает