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
| #include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
/*На входе массив строк и его длина.
Каждая строка имеет вид:
Фамилия Имя Отчество
(разделяются одним пробелом).
Функция должна возвращать новый массив, состоящий из строк вида И.О. Фамилия.
*/
void del_mem(char** text, int n) //освобождение памяти
{
for (int i = 0; i < n; i++)
{
delete[] text[i];
}
delete[] text;
}
char** new_mem(int n) //выделяем память под массив строк
{
char** text = new char*[n];
int i = 0;
try
{
for(; i < n; i++)
{
text[i] = new char[255];
}
}
catch(bad_alloc e)
{
del_mem(text,i);
throw;
}
return text;
}
void cin_text(char** text, int n) //заполнение массива
{
for (int i = 0; i < n; i++)
{
cin.getline(text[i],255);
}
}
int copysubstr(char* str, char* rez, int x1,int x2, int i)
{
for(int j = x1; j < x2; i++,j++)
{
rez[i] = str[j];
}
return i;
}
char* swap(char* str)
{
int fe = 0;
for(; str[fe] != ' ' && str[fe] != 0; fe++);
cout << "fe = " << fe << endl;
char* rez = new char[fe+6];
int sb = fe+2;
cout << "sb = " << sb << endl;
int se = sb;
for(; str[se] != ' '; se++);
cout << "se = " << se << endl;
int kb = se + 2;
cout << "kb = " << kb << endl;
int ke = 0;
for(ke = kb; str[ke] != 0; ke++);
cout << "ke = " << ke << endl;
int i = 0;
i = copysubstr(str,rez,sb-1,sb,i);
rez[i] = '.';
i++;
i = copysubstr(str,rez,kb-1,kb,i);
rez[i] = '.';
i++;
rez[i] = ' ';
i++;
i = copysubstr(str,rez,0,fe,i);
rez[i] = 0;
return rez;
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(0,"rus");
int n = 0;
cout << "Введите количество строк" << endl;
cin >> n;
char**text;
new_mem(n);
cin_text(text,n);
char ins[255];
cin.getline(ins,255);
system ("pause");
return 0;
} |