哪位高手来帮帮我啊?
要求用C语言实现并且还要求给出问题描述、数据描述、算法描述、源程序、测试过程、其它说明等内容。
一元多项式的表示和相加
[问题描述]写一程序,实现一元多项式的表示和相加
这个星期五就要交了!
小妹在此谢过了!
/* 本程序只适用于一位操作符的运算
字符型的函数和结构体名后面都加1
整型的函数和结构体名后面都加2 */
#include "Stdio.h"
#include "Conio.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
struct Stack1
{ /*字符类结构体*/
char *base;
char *top;
int stacksize;
};
struct Stack2
{ /*整形结构体*/
int *base;
int *top;
int stacksize;
};
InitStack1(struct Stack1 *S)
{ /*构造一个空栈*/
S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!S->base) printf("Application is failed!");
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
InitStack2(struct Stack2 *S)
{ /*构造一个空栈*/
S->base=(int *)malloc(STACK_INIT_SIZE * sizeof(int));
if (!S->base) printf("Application is failed!");
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
char GetTop1(struct Stack1 S)
{ /*若栈不空,则返回栈顶元素*/
if (S.top==S.base) return 0;
return(*(S.top-1));
}
int GetTop2(struct Stack2 S)
{ /*若栈不空,则返回栈顶元素*/
if (S.top==S.base) return 0;
return(*(S.top-1));
}
Push1(struct Stack1 *S,char e)
{ /*插入新的栈顶元素*/
if ( (S->top-S->base)>=(S->stacksize) )
{
S->base=(char *)realloc(S->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(char));
if (!S->base) return 0;
S->top=S->base+S->stacksize;
}
*S->top++=e;
}
Push2(struct Stack2 *S,int e)
{ /*插入新的栈顶元素*/
if ( (S->top-S->base)>=(S->stacksize) )
{
S->base=(int *)realloc(S->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(int));
if (!S->base) return 0;
S->top=S->base+S->stacksize;
}
*S->top++=e;
}
char Pop1(struct Stack1 *S)
{ /*若栈不空,则删除S栈顶元素,并返回其值*/
if (S->top==S->base) return 0;
return (*(--S->top));
}
int Pop2(struct Stack2 *S)
{ /*若栈不空,则删除S栈顶元素,并返回其值*/
if (S->top==S->base) return 0;
return (*(--S->top));
}
int Compare(char e)
{ /*比较字符是运算符还是操作数*/
if (e>='0' && e<='9')
return 0;
return 1;
}
char Precede(char e1,char e2)
{
int i=0,j=0;
char save[]={ '+', '-', '*', '/', '(', ')', '#'};
char index[][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','$'},
{'>','>','>','>','$','>','>'},
{'<','<','<','<','<','$','='}
};
for (i=0;i<7;i++)
{
if (save[i]==e1)
break;
}
for (j=0;j<7;j++)
{
if (save[j]==e2)
break;
}
return index[i][j];
}
int Operate(int operand2,char operate,int operand1)
{
switch (operate)
{
case '+':
return (operand1+operand2);
case '-':
return (operand1-operand2);
case '*':
return (operand1*operand2);
case '/':
return (operand1/operand2);
}
}
int change(int elem,int j)
{
int i=0,response=1;
for (i=0;i<=j;i++)
{
response*=elem;
}
return response;
}
int EvaluateExpression(char in[])
{ /*算术表达式求值的算符优先比较,OP为运算符集合*/
int i=1,j=1;
char temp,*ch=in;
int temp1,temp2;
struct Stack1 S1;
struct Stack2 S2;
InitStack1(&S1); /*S1存放运算符*/
InitStack2(&S2); /*S2存放一位操作数*/
Push1(&S1,*ch);
while ( *(ch+i)!='\0' && S1.base!=S1.top )
{
if (!Compare(*(ch+i)))
{
Push2(&S2,(*(ch+i)-48));
}
else
{
switch (Precede(GetTop1(S1),*(ch+i)))
{
case '<':
Push1(&S1,*(ch+i));
break;
case '=':
Pop1(&S1);
break;
case '>':
temp=Pop1(&S1);
temp1=Pop2(&S2);
temp2=Pop2(&S2);
Push2(&S2,Operate(temp1,temp,temp2));
i--;
break;
}
}
i++;
}
return GetTop2(S2);
}
int main(void)
{
int i=1;
char ch[]="#2+1-(6/3+1*2)+5*1#";
printf("\nThe expression is: ");
for (i=1;i<sizeof(ch)-2;i++)
printf("%c ",ch[i]);
printf("\n\nThe answer is: ");
printf("%d",EvaluateExpression(ch));
getch();
return 0;
}