繁琐的括号消除,比1423那道复杂多了,首先要理清全部的可消除情况的逻辑,然后慢慢写。。我的处理方法是先算出来左右括号的下标对应关系,然后分括号前后的符号和括号里的符号各种情况来判断,注意,统计括号内符号的时候要跳过那些包含在子括号内的符号,比如说((a*b)+(c/d)),那么统计的时候应该只算一个加号。本题数据量超大,逻辑有一点错的话肯定过不了。。把数据弄过来,错了就慢慢改吧。。我也是一点点改对的
简化分支后的代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
static char prev(char *s, int index)
{
int i;
for (i = index - 1; i >= 0 && s[i] != '('; i--)
if (s[i] != ' ')
return s[i];
return -1;
}
static char next(char *s, int index)
{
int i;
for (i = index + 1; s[i] != '\0' && s[i] != ')'; i++)
if (s[i] != ' ')
return s[i];
return -1;
}
static void count(char *s, int begin, int end, int *par, int *map)
{
memset(map, 0, 4 * sizeof(int));
int i;
for (i = begin + 1; i < end; i++)
{
if (s[i] == '(')
i = par[i];
if (s[i] == '+')
map[0] = 1;
if (s[i] == '-')
map[1] = 1;
if (s[i] == '*')
map[2] = 1;
if (s[i] == '/')
map[3] = 1;
}
}
static void removep(char *s, int left, int right)
{
s[left] = ' ';
s[right] = ' ';
}
int main()
{
int t, par[256], stack[100], map[4], index;
scanf("%d", &t);
getchar();
char s[256];
while (t--)
{
gets(s);
memset(par, 0, sizeof(par));
index = 0;
int i;
for (i = 0; s[i] != '\0'; i++)
if (s[i] == '(')
stack[index++] = i;
else if (s[i] == ')')
par[stack[--index]] = i;
for (i = 0; i < 256; i++)
if (par[i])
{
char pp = prev(s, i);
char nn = next(s, par[i]);
count(s, i, par[i], par, map);
if (pp == -1 || pp == '+')
{
if (nn == -1 || nn == '+' || nn == '-')
removep(s, i, par[i]);
else if (!map[0] && !map[1])
removep(s, i, par[i]);
}
else if ((pp == '*' || pp == '-') && !map[0] && !map[1])
removep(s, i, par[i]);
else if (pp == '/' && !map[0] && !map[1] && !map[2] && !map[3])
removep(s, i, par[i]);
}
for (i = 0; s[i] != '\0'; i++)
if (s[i] != ' ')
putchar(s[i]);
putchar('\n');
}
return 0;
}
原始代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
static char prev(char *s, int index)
{
int i;
for (i = index - 1; i >= 0 && s[i] != '('; i--)
if (s[i] != ' ')
return s[i];
return -1;
}
static char next(char *s, int index)
{
int i;
for (i = index + 1; s[i] != '\0' && s[i] != ')'; i++)
if (s[i] != ' ')
return s[i];
return -1;
}
static void count(char *s, int begin, int end, int *par, int *map)
{
memset(map, 0, 4 * sizeof(int));
int i;
for (i = begin + 1; i < end; i++)
{
if (s[i] == '(')
i = par[i];
if (s[i] == '+')
map[0] = 1;
if (s[i] == '-')
map[1] = 1;
if (s[i] == '*')
map[2] = 1;
if (s[i] == '/')
map[3] = 1;
}
}
static void process(char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++)
if (islower(s[i]))
{
int j = i - 1;
while (j >= 0 && s[j] == '(')
if (s[2 * i - j] == ')')
{
s[j] = ' ';
s[2 * i - j] = ' ';
j--;
}
else
break;
}
}
int main()
{
int t, par[256], stack[100], map[4], index;
scanf("%d", &t);
getchar();
char s[256];
while (t--)
{
gets(s);
process(s);
memset(par, 0, sizeof(par));
index = 0;
int i;
for (i = 0; s[i] != '\0'; i++)
if (s[i] == '(')
stack[index++] = i;
else if (s[i] == ')')
par[stack[--index]] = i;
for (i = 0; i < 256; i++)
if (par[i])
{
char pp = prev(s, i);
char nn = next(s, par[i]);
count(s, i, par[i], par, map);
if (pp == -1)
{
if (nn == -1 || nn == '+' || nn == '-')
{
s[i] = ' ';
s[par[i]] = ' ';
}
else
{
if (!map[0] && !map[1])
{
s[i] = ' ';
s[par[i]] = ' ';
}
}
}
else if (pp == '*')
{
if (!map[0] && !map[1])
{
s[i] = ' ';
s[par[i]] = ' ';
}
}
else if (pp == '/')
{
if (!map[0] && !map[1] && !map[2] && !map[3])
{
s[i] = ' ';
s[par[i]] = ' ';
}
}
else if (pp == '+')
{
if (nn == -1 || nn == '+' || nn == '-')
{
s[i] = ' ';
s[par[i]] = ' ';
}
else if (!map[0] && !map[1])
{
s[i] = ' ';
s[par[i]] = ' ';
}
}
else if (pp == '-')
{
if (!map[0] && !map[1])
{
s[i] = ' ';
s[par[i]] = ' ';
}
}
}
for (i = 0; s[i] != '\0'; i++)
if (s[i] != ' ')
putchar(s[i]);
putchar('\n');
}
return 0;
}