题目描述
明明进了中学之后,学到了代数表达式。有一天,他碰到一个很麻烦的选择题。这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数表达式是和题干中的表达式等价的。
这个题目手算很麻烦,因为明明对计算机编程很感兴趣,所以他想是不是可以用计算机来解决这个问题。假设你是明明,能完成这个任务吗?
这个选择题中的每个表达式都满足下面的性质:
1. 表达式只可能包含一个变量‘a’。
2. 表达式中出现的数都是正整数,而且都小于10000。
3. 表达式中可以包括四种运算‘+’(加),‘-’(减),‘*’(乘),‘^’(乘幂),以及小括号‘(’,‘)’。小括号的优先级最高,其次是‘^’,然后是‘*’,最后是‘+’和‘-’。‘+’和‘-’的优先级是相同的。相同优先级的运算从左到右进行。(注意:运算符‘+’,‘-’,‘*’,‘^’以及小括号‘(’,‘)’都是英文字符)
4. 幂指数只可能是1到10之间的正整数(包括1和10)。
5. 表达式内部,头部或者尾部都可能有一些多余的空格。
下面是一些合理的表达式的例子:
((a^1) ^ 2)^3,a*a+a-a,((a+a)),9999+(a-a)*a,1 + (a -1)^3,1^10^9……
分析:表达式运算,一般都是用一个操作数栈,一个操作符栈,依据操作符的优先级逐级运算
本题的麻烦在于有一个变量,字符串操作起来就比较繁琐。
于是乎,就将多项式的每一项抽象为一个数据结构,因为每个项都可以由一个系数和指数唯一确定;那么一个多项式就可以表示为一组【系数+指数】的集合;
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
#define MAX_EXPONENT 100
-
#define MAX_POLYNOMIAL 100
-
-
typedef struct POLYNOMIAL_NODE {
-
unsigned int number;
-
short *coeff;
-
unsigned short *exp;
-
} polynomial_t;
-
-
-
polynomial_t* generate_from_list(short *exp_coeff, int len)
-
{
-
polynomial_t *ret = NULL;
-
short *coeffs;
-
unsigned short *exps;
-
-
int cnt = 0;
-
int i, j;
-
-
for (i = 0; i < len; i++)
-
{
-
if (exp_coeff[i] != 0)
-
{
-
cnt++;
-
}
-
}
-
if (cnt == 0)
-
{
-
cnt = 1;// return 0
-
}
-
-
ret = (polynomial_t *)calloc(sizeof(polynomial_t), 1);
-
if (NULL == ret)
-
{
-
return NULL;
-
}
-
-
coeffs = (short *)calloc(sizeof(short), cnt);
-
exps = (unsigned short *)calloc(sizeof(unsigned short), cnt);
-
if (NULL == coeffs || NULL == exps)
-
{
-
return NULL;
-
}
-
-
j = 0;
-
for (i = 0; i < len; i++)
-
{
-
if (exp_coeff[i] != 0)
-
{
-
coeffs[j] = exp_coeff[i];
-
exps[j++] = i;
-
}
-
}
-
ret->number = cnt;
-
ret->coeff = coeffs;
-
ret->exp = exps;
-
-
return ret;
-
}
-
-
polynomial_t* polynomial_multip(polynomial_t *p1, polynomial_t *p2)
-
{
-
short exp_coeff[MAX_EXPONENT] = { 0 };
-
int i, j;
-
-
for (i = 0; i < p2->number; i++)
-
{
-
for (j = 0; j < p1->number; j++)
-
{
-
exp_coeff[p1->exp[j] + p2->exp[i]] += p1->coeff[j] * p2->coeff[i];
-
}
-
}
-
return generate_from_list(exp_coeff, MAX_EXPONENT);
-
}
-
-
-
polynomial_t* polynomial_add(polynomial_t *p1, polynomial_t *p2)
-
{
-
short exp_coeff[MAX_EXPONENT] = { 0 };
-
int i, j;
-
-
for (i = 0; i < p1->number; i++)
-
{
-
exp_coeff[p1->exp[i]] += p1->coeff[i];
-
}
-
for (i = 0; i < p2->number; i++)
-
{
-
exp_coeff[p2->exp[i]] += p2->coeff[i];
-
}
-
return generate_from_list(exp_coeff, MAX_EXPONENT);
-
}
-
-
polynomial_t* polynomial_sub(polynomial_t *p1, polynomial_t *p2)
-
{
-
short exp_coeff[MAX_EXPONENT] = { 0 };
-
int i, j;
-
-
for (i = 0; i < p1->number; i++)
-
{
-
exp_coeff[p1->exp[i]] += p1->coeff[i];
-
}
-
for (i = 0; i < p2->number; i++)
-
{
-
exp_coeff[p2->exp[i]] -= p2->coeff[i];
-
}
-
return generate_from_list(exp_coeff, MAX_EXPONENT);
-
}
-
-
void polynomial_free(polynomial_t *p)
-
{
-
if (!p)
-
{
-
return;
-
}
-
if (p->exp)
-
{
-
free(p->exp);
-
}
-
if (p->coeff)
-
{
-
free(p->coeff);
-
}
-
free(p);
-
}
-
-
void polynomial_print(polynomial_t *p)
-
{
-
int i;
-
char sym;
-
char py[16];
-
if (!p)
-
{
-
return;
-
}
-
-
printf("polynomial<%p>, total %d:\n", p, p->number);
-
for (i = 0; i < p->number; i++)
-
{
-
memset(py, 0, 16);
-
if (p->coeff[i] < 0)
-
{
-
if (p->exp[i] == 0)
-
{
-
sprintf(py, "%d", p->coeff[i]);
-
}
-
else if (p->exp[i] == 1)
-
{
-
if (p->coeff[i] == -1)
-
{
-
sprintf(py, "-x");
-
}
-
else
-
sprintf(py, "%dx", p->coeff[i]);
-
}
-
else
-
{ if (p->coeff[i] == -1)
-
{
-
sprintf(py, "-x^%d", p->exp[i]);
-
}
-
else
-
sprintf(py, "%dx^%u", p->coeff[i], p->exp[i]);
-
}
-
}
-
else
-
{
-
char* tmp = py;
-
if (i > 0)
-
{
-
tmp[0] = '+';
-
tmp++;
-
}
-
-
if (p->exp[i] == 0)
-
{
-
sprintf(tmp, "%d", p->coeff[i]);
-
}
-
else if (p->exp[i] == 1)
-
{
-
if (p->coeff[i] == 1)
-
{
-
sprintf(tmp, "x");
-
}
-
else
-
sprintf(tmp, "%dx", p->coeff[i]);
-
}
-
else
-
{
-
if (p->coeff[i] == 1)
-
{
-
sprintf(tmp, "x^%d", p->exp[i]);
-
}
-
else
-
sprintf(tmp, "%dx^%d", p->coeff[i], p->exp[i]);
-
}
-
}
-
printf("%s", py);
-
}
-
printf("\n");
-
}
-
-
polynomial_t* polynomial_power(polynomial_t *p1, polynomial_t *p2)
-
{
-
short exp_coeff[MAX_EXPONENT] = { 0 };
-
int i, j;
-
int exp = 0;
-
polynomial_t *ret;
-
polynomial_t *tmp;
-
-
if (p2->number != 1)
-
{
-
printf("polynomial power only support number\n");
-
return NULL;
-
}
-
exp = p2->coeff[0];
-
-
if (exp == 0)
-
{
-
exp_coeff[0] = 1;
-
return generate_from_list(exp_coeff, MAX_EXPONENT);
-
}
-
else if (exp < 0)
-
{
-
printf("polynomial power only support positive exponal\n");
-
return NULL;
-
}
-
if (exp == 1)
-
{
-
return p1;
-
}
-
-
ret = p1;
-
tmp = NULL;
-
while (exp > 1)
-
{
-
ret = polynomial_multip(ret, p1);
-
if (tmp)
-
{
-
polynomial_free(tmp);
-
}
-
tmp = ret;
-
exp--;
-
}
-
return ret;
-
}
-
-
char* parse_polynomial(char *str, polynomial_t **ptr)
-
{
-
char *cur = str;
-
polynomial_t *ret = NULL;
-
short exp_coeff[2] = { 0 };
-
short coeff = 0;
-
-
if (*cur == 'x')
-
{
-
exp_coeff[1] = 1;
-
ret = generate_from_list(exp_coeff, 2);
-
*ptr = ret;
-
return ++cur;
-
}
-
-
while (*cur >= '0' && *cur <= '9')
-
{
-
coeff = coeff * 10 + (*cur - '0');
-
cur++;
-
}
-
-
exp_coeff[0] = coeff;
-
ret = generate_from_list(exp_coeff, 2);
-
*ptr = ret;
-
return cur;
-
}
-
-
polynomial_t* polynomial_comput(char *str)
-
{
-
char symstack[MAX_POLYNOMIAL] = { 0, };
-
int symhead = -1;
-
polynomial_t *pstack[MAX_POLYNOMIAL] = { NULL };
-
int phead = -1;
-
-
polynomial_t *opt1;
-
polynomial_t *opt2;
-
polynomial_t *ret;
-
char optch;
-
-
char *cur;
-
-
if (!str)
-
{
-
return NULL;
-
}
-
-
cur = str;
-
while (*cur != '\0')
-
{
-
if (*cur == 'x' || (*cur >= '0' && *cur <= '9'))
-
{
-
cur = parse_polynomial(cur, &pstack[++phead]);
-
continue;
-
}
-
else
-
{
-
switch (*cur)
-
{
-
case '(': // 左括号直接入栈
-
symstack[++symhead] = *cur;
-
break;
-
-
case '^': // 查看符号栈顶是否为'^',如果是 弹出运算,其余的直接压栈
-
if (symhead >= 0 && symstack[symhead] == '^')
-
{
-
optch = symstack[symhead--];
-
opt2 = pstack[phead--];
-
opt1 = pstack[phead--];
-
-
ret = polynomial_power(opt1, opt2);
-
pstack[++phead] = ret;
-
symstack[++symhead] = *cur;
-
polynomial_free(opt2);
-
polynomial_free(opt1);
-
}
-
else
-
{
-
symstack[++symhead] = *cur;
-
}
-
break;
-
-
case '*': // 符号栈顶为'^' 或'*'则运算,否则压栈
-
if (symhead >= 0 && (symstack[symhead] == '^' || symstack[symhead] == '*'))
-
{
-
optch = symstack[symhead--];
-
opt2 = pstack[phead--];
-
opt1 = pstack[phead--];
-
-
if (optch == '^')
-
ret = polynomial_power(opt1, opt2);
-
else
-
ret = polynomial_multip(opt1, opt2);
-
-
pstack[++phead] = ret;
-
symstack[++symhead] = *cur;
-
polynomial_free(opt2);
-
polynomial_free(opt1);
-
-
}
-
else
-
{
-
symstack[++symhead] = *cur;
-
}
-
break;
-
case '+':
-
case '-':
-
if (symhead >= 0 & symstack[symhead] != '(')
-
{
-
optch = symstack[symhead--];
-
opt2 = pstack[phead--];
-
opt1 = pstack[phead--];
-
-
if (optch == '^')
-
ret = polynomial_power(opt1, opt2);
-
else if (optch == '*')
-
{
-
ret = polynomial_multip(opt1, opt2);
-
}
-
else if (optch == '+')
-
{
-
ret = polynomial_add(opt1, opt2);
-
}
-
else if (optch == '-')
-
{
-
ret = polynomial_sub(opt1, opt2);
-
}
-
-
pstack[++phead] = ret;
-
symstack[++symhead] = *cur;
-
polynomial_free(opt2);
-
polynomial_free(opt1);
-
}
-
else
-
symstack[++symhead] = *cur;
-
-
break;
-
-
case ')': // 如果是右括号,则一直运算直到找到左括号
-
while (symhead >= 0 && symstack[symhead] != '(')
-
{
-
optch = symstack[symhead--];
-
opt2 = pstack[phead--];
-
opt1 = pstack[phead--];
-
-
if (optch == '^')
-
ret = polynomial_power(opt1, opt2);
-
else if (optch == '*')
-
{
-
ret = polynomial_multip(opt1, opt2);
-
}
-
else if (optch == '+')
-
{
-
ret = polynomial_add(opt1, opt2);
-
}
-
else if (optch == '-')
-
{
-
ret = polynomial_sub(opt1, opt2);
-
}
-
-
pstack[++phead] = ret;
-
}
-
if (symstack[symhead] != '(')
-
{
-
printf("miss match of ')'\n");
-
return NULL;
-
}
-
symhead--;
-
break;
-
default:
-
break;
-
}
-
cur++;
-
}
-
}
-
-
while (symhead >= 0)
-
{
-
optch = symstack[symhead--];
-
opt2 = pstack[phead--];
-
opt1 = pstack[phead--];
-
-
if (optch == '^')
-
{
-
ret = polynomial_power(opt1, opt2);
-
-
}
-
else if (optch == '*')
-
{
-
ret = polynomial_multip(opt1, opt2);
-
}
-
else if (optch == '+')
-
{
-
ret = polynomial_add(opt1, opt2);
-
}
-
else if (optch == '-')
-
{
-
ret = polynomial_sub(opt1, opt2);
-
}
-
-
pstack[++phead] = ret;
-
polynomial_free(opt2);
-
polynomial_free(opt1);
-
}
-
-
if (phead != 0)
-
{
-
printf("error : %d\n", phead);
-
polynomial_print(pstack[phead]);
-
return NULL;
-
}
-
return pstack[0];
-
-
}
-
-
int main(int argc, char **argv)
-
{
-
polynomial_t *pyn;
-
char src[32] = "(x+1)^2^2-(x-x)^3+((x-1)*(x-1))";
-
-
pyn = polynomial_comput(src);
-
polynomial_print(pyn);
-
return 0;
-
}
阅读(2835) | 评论(0) | 转发(0) |