return(-1);
elseif(x.expn==y.expn)
return(0);
elsereturn
(1);
}
/************************************************************/
/*两多项式多项式相加*/
/************************************************************/
PolynAddPolyn(Polynh1,Polynh2)
{
Item*head,*pa=h1->next,*pb=h2->next,*s,*s0;
CreateItem(head);
head->next=NULL;
while(pa!
=NULL&&pb!
=NULL)
{
if(ItemComp(*pa,*pb)==-1)
{
CreateItem(s);
s->coef=pa->coef;
s->expn=pa->expn;
insert(head,s);
pa=pa->next;
}
elseif(ItemComp(*pa,*pb)==1)
{
CreateItem(s);
s->coef=pb->coef;
s->expn=pb->expn;
insert(head,s);
pb=pb->next;
}
else
{
CreateItem(s);
s->coef=(pa->coef+pb->coef);
s->expn=pa->expn;
insert(head,s);
pa=pa->next;
pb=pb->next;
}
}
while(pa!
=NULL)
{
CreateItem(s0);
s0->coef=pa->coef;
s0->expn=pa->expn;
insert(head,s0);
pa=pa->next;
}
while(pb!
=NULL)
{
CreateItem(s0);
s0->coef=pb->coef;
s0->expn=pb->expn;
insert(head,s0);
pb=pb->next;
}
returnhead;
}
/************************************************************/
/*两多项式多项式相减*/
/************************************************************/
PolynSubtractPolyn(Polynh1,Polynh2)
{
Item*head,*pa=h1->next,*pb=h2->next,*s,*s0;
CreateItem(head);
head->next=NULL;
while(pa!
=NULL&&pb!
=NULL)
{
if(ItemComp(*pa,*pb)==-1)
{
CreateItem(s);
s->coef=pa->coef;
s->expn=pa->expn;
insert(head,s);
pa=pa->next;
}
elseif(ItemComp(*pa,*pb)==1)
{
CreateItem(s);
s->coef=(-1)*pb->coef;
s->expn=pb->expn;
insert(head,s);
pb=pb->next;
}
else
{
CreateItem(s);
s->coef=(pa->coef-pb->coef);
s->expn=pa->expn;
insert(head,s);
pa=pa->next;
pb=pb->next;
}
}
while(pa!
=NULL)
{
CreateItem(s0);
s0->coef=pa->coef;
s0->expn=pa->expn;
insert(head,s0);
pa=pa->next;
}
while(pb!
=NULL)
{
CreateItem(s0);
s0->coef=(-1)*pb->coef;
s0->expn=pb->expn;
insert(head,s0);
pb=pb->next;
}
returnhead;
}
/************************************************************/
/*两多项式多项式相乘*/
/************************************************************/
PolynMultPolyn(Polynh1,Polynh2)//两个多项式相乘
{
Item*head,*pa=h1->next,*pb=h2->next,*s,*pp;
intflag;
CreateItem(head);
head->next=NULL;
while(pa!
=NULL)
{
while(pb!
=NULL)
{
pp=head;
flag=0;
while(pp!
=NULL)
{
if(pa->expn+pb->expn==pp->expn)
{
pp->coef+=(pa->coef*pb->coef);
flag=1;
break;
}
pp=pp->next;
}
if(!
flag)
{
CreateItem(s);
s->coef=pa->coef*pb->coef;
s->expn=pa->expn+pb->expn;
insert(head,s);
}
pb=pb->next;
}
pa=pa->next;
}
returnhead;
}
/************************************************************/
/*菜单选择*/
/************************************************************/
intmenu(void)
{intnum;
system("cls");
printf("%20c1--createP(x)\n",'');
printf("%20c2--createQ(x)\n",'');
printf("%20c3--p(x)+Q(x)\n",'');
printf("%20c4--P(x)-Q(x)\n",'');
printf("%20c5--p(x)*Q(x)\n",'');
printf("%20c6--printP(x)\n",'');
printf("%20c7--printQ(x)\n",'');
printf("%20c8--printP(x)+Q(x)\n",'');
printf("%20c9--printP(x)-Q(x)\n",'');
printf("%20c10--printP(x)*Q(x)\n",'');
printf("%20c11--Quit\n",'');
printf("pleaseselect1,2,3,4,5,6,7,8,9,10,11:
");
do{
scanf("%d",&num);
}while(num<1||num>11);
return(num);
}
/************************************************************/
/*判断多项式是否存在*/
/************************************************************/
intPolynNotEmpty(Polynh,char*p)
{if(h==NULL)
{printf("%sisnotexist!
\n",p);
getchar();
}
elsereturn
(1);
return0;
}
/************************************************************/
/*主函数*/
/************************************************************/
voidmain()
{intnum;
Polynh1=NULL;//p(x)
Polynh2=NULL;//Q(x)
Polynh3=NULL;//P(x)+Q(x)
Polynh4=NULL;//P(x)-Q(x)
Polynh5=NULL;//P(x)*Q(x)
while
(1)
{num=menu();
getchar();
switch(num)
{
case1:
//输入第一个多项式,若多项式存在,首先撤消然后再输入
if(h1!
=NULL)
{if(Select("P(x)isnotEmpty,CreateP(x)again?
"))
{Destroy(h1);
h1=Input();
}
}
elseh1=Input();
break;
case2:
//输入第二个多项式,若多项式存在,首先撤消然后再输入
if(h2!
=NULL)
{if(Select("Q(x)isnotEmpty,CreateQ(x)again?
"))
{Destroy(h2);
h2=Input();
}
}
elseh2=Input();
break;
case3:
//两多项式相加
if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))
{h3=AddPolyn(h1,h2);
Output(h3,"P(x)+Q(X)");
printf("P(x)+Q(x)hasfinished!
\n");
getchar();
}
break;
case4:
//两多项式相减
if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))
{h4=SubtractPolyn(h1,h2);
Output(h4,"P(x)-Q(x)");
printf("P(x)-Q(x)hasfinished!
\n");
getchar();
}
break;
case5:
//两多项式相乘
if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))
{h5=MultPolyn(h1,h2);
Output(h5,"P(x)*Q(x)");
printf("P(x)*Q(x)hasfinished!
\n");
getchar();
}
break;
case6:
//显示第一个多项式
if(PolynNotEmpty(h1,"p(x)"))
{Output(h1,"P(x)");
getchar();
}
break;
case7:
//显示第二个多项式
if(PolynNotEmpty(h2,"Q(x)"))
{Output(h2,"Q(x)");
getchar();
}
break;
case8:
//显示相加结果多项式
if(PolynNotEmpty(h3,"P(x)+Q(x)"))
{Output(h3,"P(x)+Q(x)");
getchar();
}
break;
case9:
//显示相减结果多项式
if(PolynNotEmpty(h4,"P(x)-Q(x)"))
{Output(h4,"P(x)-Q(x)");
getchar();
}
break;
case10:
//显示相乘结果多项式
if(PolynNotEmpty(h5,"P(x)*Q(x)"))
{Output(h5,"P(x)*Q(x)");
getchar();
}
break;
case11:
//结束程序运行。
结束前应先撤消已存在的多项式
if(h1!
=NULL)Destroy(h1);
if(h2!
=NULL)Destroy(h2);
if(h3!
=NULL)Destroy(h3);
if(h4!
=NULL)Destroy(h4);
if(h5!
=NULL)Destroy(h5);
}
if(num==11)break;
}
getch();
}
实验四算术表达式求值
#include
#include
#include
#include
#definePLUS0
#defineMINUS1
#definePOWER2
#defineDIVIDE3
#defineLEFTP4
#defineRIGHP5
#defineSTARTEND6
#defineDIGIT7
#definePOINT8
#defineNUM7
#defineNO32767
#defineSTACKSIZE20
//运算符
chara[]={'+','-','*','/','(',')','#'};
//优先关系矩阵,规定:
=为0,>为1,<为-1,空为NO
intPriorityTable[7][7]={{1,1,-1,-1,-1,1,1},
{1,1,-1,-1,-1,1,1},
{1,1,1,1,-1,1,1},
{1,1,1,1,-1,1,1},
{-1,-1,-1,-1,-1,0,NO},
{1,1,1,1,NO,1,1},
{-1,-1,-1,-1,-1,NO,0}
};
intmenu(void);
/***************************/
/*输入表达式函数*/
/***************************/
voidInputExpression(charstr[])
{intlen;
printf("Inputexpressionstring:
\n");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';
}
/*************************************************/
/*确定当前字符类型*/
/*************************************************/
/*若为运算符,则返回运算符在运算符数组中的序号*/
/*若为数字字符,则返回DIGIT*/
/*若为小数点字