For example, the following row:
"a+12.3456-b-0.456789+1.3-45678"
will be transformed into
"a+12.34-b-0.45+1.3-45678"
That is, we need to cut out the figures after the point from the third figure. The signal of the row's end is the sign '&'.
With the getchar
and putchar
functions:
char c = 0, c_ = 0,flag=0;
while ((c = getchar()) != '&')
{
if (isdigit(c) && !(isdigit(c_)))
{
flag = 1;
}
putchar(c);
if (flag)
{
while ((c = getchar()) != '.') putchar(c);
putchar(c);
c = getchar();
putchar(c);
if (!isdigit(c)){ flag = 0; continue; }
c = getchar();
putchar(c);
if (!isdigit(c)){ flag = 0; continue; }
while (isdigit(c=getchar()));
putchar(c);
flag = 0;
}
c_ = c;
}
Using strings:
char s[100] = { 0 }, s1[100] = { 0 };
inti = 0, j = 0;
gets(s);
while (s[i] != '&')
{
if (isdigit(s[i])
{
while (s[i] != '.')
{
s1[j++] = s[i++];
}
s1[j++] = s[i];
s1[j++] = s[++i];
if (!isdigit(s[++i])) continue;
s1[j++] = s[i];
if (!isdigit(s[++i])) continue;
while (isdigit(s[++i]));
continue;
}
s1[j++] = s[i++];
}
puts(s1);
The programs are confusing and ugly, but they're the best ones I ended up with. Please suggest a better algorithm or change mine to help it make more sense.