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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
| #include <iostream>
#include <string>
#include <locale.h>
#include <iomanip>
#include <fstream>
using namespace std;
char* R(char* str,int width_colon)
{
char* str2=new char[width_colon+1];
str2[0]='\0';
int n, length, i, c, j, bcount, rem, onen=1;
if ((length=strlen(str)) <width_colon )
{
for( i=0, bcount=0; i<length; ++i )
if( str[i] ==' ' )
++bcount;
if( bcount >0)
{
n=width_colon-length;
if( bcount <=n )
onen=n /bcount;
if( (rem=(n-(onen*bcount))) >0 );
for( i=0, c=0; i <length ; ++i, ++c )
{
if( str[i]==' ' && n >0 )
{
for( j=0; j <onen; ++j, ++c )
str2[c]=' ';
if( rem >0 )
{
for( j=0; j <rem; ++j, ++c )
str2[c]=' ';
rem=0;
}
n-=onen;
}
str2[c]=str[i];
}
str2[c]='\0';
}
else
{
n=width_colon-length;
for( i=0; i <n ; ++i )
str2[i]=' ';
for( c=0; i <width_colon ; ++i, ++c )
str2[i]=str[c];
str2[i]='\0';
strcpy( str2, str );
}
}
return str2;
}
void main()
{
char filename[100];
char text[10000];
setlocale(LC_ALL,"Russian");
ofstream in("a.txt,IOS::in);
cout <<endl;
if (!(in))
{
cout<<"Ошибка открытия файла!"<<endl;
return;
}
text[0]='\0';
while (in)
{
char addstr[500];
in.getline(addstr,500);
strcat(text,addstr);
}
char* text2=new char [strlen(text)+1];
for(int i = 0 ; i <= strlen(text) ; i++)
{
static int j=0;
if(text[i] != '\n')
{
text2[j]=text[i];
j++;
}
}
char* text1=new char[strlen(text)+1];
strcpy(text1,text);
strcat(text1," ");
char*tok=strtok(text1," ");
int m=1;
int length=0;
while (tok!=NULL)
{
tok[strlen(tok)]='\0';
length+=strlen(tok);
tok=strtok(NULL," ");
m++;
}
strcpy(text1,text);
cout<<"Введите ширину колонки:";
int width_colon;
cin>>width_colon;
cout<<endl;
if (width_colon<=0)
{
cout<<"Ведено недопустимое значение!"<<endl;
return;
}
cout<<"Введите количество колонок: ";
int count_colon;
cin>>count_colon;
cout<<endl;
if (count_colon<=0)
{
cout<<"Ведено недопустимое значение!"<<endl;
return;
}
while (strlen(text)&count_colon!=0)
strcat(text," ");
int n=strlen(text)/count_colon;
while (n%width_colon!=0)
n++;
int strok=m;
char*** colon=new char**[count_colon];
for (int i=0;i<count_colon;i++)
{
colon[i]=new char*[strok];
}
for (int i=0;i<count_colon;i++)
for (int j=0;j<strok;j++)
{
colon[i][j]=new char [width_colon];
colon[i][j][0]='\0';
}
int l=0;
int* count_strok=new int[count_colon];
tok=strtok(text1," ");
for (int i=0;i<count_colon;i++)
{
int len=0;
int j=0;
int count=width_colon;
while ((len<n)&&(tok!=NULL)&&(j<strok))
{
if (strlen(tok)<=count)
{
strcat(colon[i][j],tok);
if (count-strlen(tok)!=0)
{
strcat(colon[i][j]," ");
count-=strlen(tok)+1;
}
else
count-=strlen(tok);
len+=strlen(tok)+1;
tok=strtok(NULL," ");
while (colon[i][j][0]==' ')
{
static int c=0;
colon[i][j][c]=colon[i][j][c+1];
c++;
}
}
else
{
int c=1;
while (colon[i][j][0]==' ')
{
static int c=0;
colon[i][j][c]=colon[i][j][c+1];
c++;
}
c=1;
while (colon[i][j][strlen(colon[i][j])-c]==' ')
{
colon[i][j][strlen(colon[i][j])-c]='\0';
c++;
}
if (width_colon-strlen(colon[i][j])!=0)
strcpy(colon[i][j],Rovno(colon[i][j],width_colon));
j++;
count=width_colon;
}
}
count_strok[i]=j;
}
cout<<"Введите название файла для вывода текста: ";
cin>>filename;
cout<<endl;
ofstream out;
out.open(filename);
int k=0;
for (int i=0;i<strok;i++)
{
while (k!=count_colon)
{
out<<setw(width_colon)<<colon[k][i]<<"\t";
k++;
}
out<<endl;
k=0;
}
in.close();
out.close();
system("pause");
return;
} |