include stdio include string include malloc int getStr char ôóíêöèÿ ââ

  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
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int getStr(char**);//ôóíêöèÿ ââîäà ñòðîê
int Amount(char* s);
char* reorg(char *, int q); //ôóíêöèÿ ïðåîáðàçîâàíèÿ ñòðîê
int len(char* s); //îïðåäåëåíèå äëèíû ñëîâà
char *skipSpaces(char *); //ïðîïóñê ïðîáåëîâ
int check(char* s); //ïðîâåðêà ñòðîêè
char* copyNumber(char** res, char* s, int q); //ôóíêöèÿ, ïåðåâîðà÷èâàþùàÿ è çàïèñûâàþùàÿ ÷èñëî
//îñíîâíàÿ ôóíêöèÿ ïðîãðàììû
int main()
{
char* s = NULL; //ñòðîêà èñòî÷íèê
char* p = NULL; //ñòðîêà ïðè¸ìíèê
int flag;
int amount;
//ïîêà íå ââåëè êîíåö ôàéëà ðàáîòàåò öèêë
while (1)
{
puts("Enter string to make new or CTRL+Z to exit");
flag = getStr(&s); //flag - êîä îøèáêè
if (flag == -1) //åñëè îí ðàâåí -1 - òî ââåëè êîíåö ôàéëà
return 0;
if (flag == -2) //åñëè ðàâåí -2 - íåõâàòêà ïàìÿòè
{
puts("Memory is out");
return 0;
}
amount = Amount(s);
printf("Source amount: %d\n", amount);
printf("Source string: %s\n", s); //âûâîäèì òî, ÷òî ââåëè
if (!check(s))
{
puts("Incorrect string. String could contain inly digits.");
free(s);
}
else
{
p = reorg(s, amount); //ïîëó÷àåì íåîâóþ ñòðîêó
if (p)
printf("Result string: \"%s\"\n", p); //âûâîäèì íîâóþ ñòðîêó
else
puts("Result string is empty");
free(s); //î÷èùàåì ïàìÿòü
free(p); //î÷èùàåì ïàìÿòü
}
}
puts("That's all. Bye!");
return 0;
}
//ïðîïóñê ïðîáåëîâ
char* skipSpaces(char* s)
{
int k = strspn(s, " \t");
return s + k;
}
//ïðîâåðêà, ÷òî ñòðîêà ñîñòîèò èç ÷èñåë
int check(char* s)
{
int k = strspn(s, "01 \t"); //ïîäñ÷èòàëè ñêîëüêî ñèìâîëîâ â õîäèò â ïåðå÷èñëåííûå
char c = *(s + k); //ïîëó÷èëè ñèìâîë ïîñëå íèõ, åñëè ýòî íóëü-áàéò, òî ñòðîêà îê, èíà÷å - íåò
return c == '\0';
}
int getStr(char** s) //ïîëó÷àåì ñòðîêó èç âõîäíîãî ïîòîêà
{
char buf[21]; //ñ÷èòûâàåì èç âõîäíîãî ïîòîêà ñòðîêó ñ ïîìîùüþ ýòîãî áóôåðà, êóñêàìè ïî 20 ñèìâîëîâ
int n; //ñþäà áóäåò çàïèñûâàòüñÿ ðåçóëüòàò scanf
int len = 0; //ñþäà äëèíà ðåçóëüòèðóþùåé ñòðîêè
*s = (char *)malloc(1); //óêàçàòåëü íà ðåçóëüòèðóþùóþ ñòêðîó
**s = '\0'; //íîëü áàéò, ïîêà ñòðîêà èìååò òîëüêî êîíåö ñòðîêè
do {
n = scanf("%20[^\n]", buf); //ñ÷èòûâàåì áóôåð
if (n < 0)
{ //åñëè ââåëè êîíåö ôàéëà (ctrl+Z), òî áóäåò -1
free(*s); //î÷èùàåì ïàìÿòü, âîçâðàùàåì ïóñòîé óêàçàòåëü
return -1;
}
if (n > 0) { //åñëè áóôåð íå ïóñòîé
len += strlen(buf); //óâåëè÷èâàåì ðåçóëüòèðóþùóþ äëèíó
*s = (char *)realloc(*s, len + 1); //äîáàâëÿåì ïàìÿòü
if (*s) //åñëè ïàìÿòü âûäåëèëàñü
strcat(*s, buf); //êîïèðóåì ñòðîêó èç áóôåðà â êîíåö íàøåé ñòðîêè
else
{ //åñëè ïàìÿòü íå âûäåëèëàñü
free(*s); //î÷èùàåì ïàìÿòü
return -2;
}
}
else
scanf("%*c"); //åñëè ïåðåíîñ ñòðîêè, òî î÷èùàåì âõîäíîé ïîòîê
} while (n > 0); //ïîêà âî âõîäíîì ïîòîêå åñòü õîòü îäèí ñèìâîë
return 0;
}
//îïðåäåëåíèå äëèíû ïåðâîãî ñëîâà
int len(char* s)
{
s = skipSpaces(s); //ïðîïóñòèëè ïðîáåëû
if (s == NULL)
return 0; //åñëè êîíåö ñòðîêè - òî ñëîâ íåò, äëèíà = 0
return strcspn(s, " \t"); //èíà÷å - ñëîâà åñòü, âîçâðàùàåì äëèíó ïåðâîãî ñëîâà (÷èñëî ñèìâîëîâ îò íà÷àëà äî ïðîáåëà/òàáà/êîíöà ñòðîêè)
}
//ôóíêöèÿ ïðåîáðàçîâàíèÿ ñòðîêè
char* reorg(char* s, int q)
{
char* p; //óêàçàòåëü íà êîíåö ðåçóëüòàòà
char* res; //ðåçóëüòàò
if (*s == 0)
return NULL; //åñëè ñòðîêà ïóñòàÿ, òî âîçâðàùàåì ïóñòîé óêàçàòåëü
p = (char *)calloc(strlen(s) + 1, sizeof(char)); // Âûäåëÿåì ïàìÿòü ïîä ðåçóëüòèðóþùóþ ñòðîêó
res = p;
while (*(s = skipSpaces(s)))
{
if (p != res)
*p++ = ' '; //åñëè ÷èñëî íå ïåðâîå - äîïèøåì ïðáåë-ðàçäåëèòåëü
s = copyNumber(&p, s, q);
}
return (char *)realloc(res, p - res + 1); //âåðíóëè ñòðîêó res, íî ñ îòðóáëåííûìè ëèøíèìè ñèìâîëàìè
}
int Amount(char* s)
{
int count = 0;
while (*s != '\0' && *s != '\t' && *s != ' ')
{
//èä¸ì äî êîíöà ÷èñëà
if (*s == '1')
++count; //è ñ÷èòàåì ÷èñëî öèôð
++s;
}
return count;
}
//ôóíêöèÿ, ïåðåâîðà÷èâàþùàÿ è çàïèñûâàþùàÿ ÷èñëî
char* copyNumber(char** res, char* s, int q)
{
char* next;
int count = 0, i = 0, t;
while (*s=='0' || *s==' ')
++s;
while (*s != '\0' && *s != '\t' && *s != ' ')
{
if (*s == '1')
++i;
++s; //èä¸ì äî êîíöà ÷èñëà
++count; //è ñ÷èòàåì ÷èñëî öèôð
}
next = s; //óêàçàòåëü íà ìåñòî ïîñëå ÷èñëà
t = count;
if (i == q) {
while (t)
{
--s; //èä¸ì äî êîíöà ÷èñëà
--t; //è ñ÷èòàåì ÷èñëî öèôð
}
while (count) //ïåðåïèøåì öèôðû
{
**res = *s;
++*res;
++s;
--count;
}
}
return next;
}