//关于申请空间的释放问题 ,还没有解决……
表达式的加减乘都有。这是我的课程设计,还没有最后改进好,但是已经可以编译和正确运行并输出结果了。输入的时候,先输系数值按回车,再输指数值按回车,一直到输入结束。不用按指数大小顺序输入,程序会自动按指数大小降序排列多项式。如果输入同一个多项式时候,输入了两个相同的指数,程序则会将他们对应的系数相加……
自己看吧!
第一次课程设计啊!!
#include"iostream"
#include<stdlib.h>
using namespace std;
#define NULL 0
typedef struct node{
int zhishu,xishu;
struct node *next;
}Nodetype,*PNodetype;
//定义结点类型
PNodetype Creat_(void)
//创建表达式
{
PNodetype Head,p,pre,pree;
int x,z;
Head=(PNodetype)malloc(sizeof(Nodetype));
Head->next=NULL;
printf("When you input xishu==0,the programe will finish!\n");
printf("Please input the first xishu:");
scanf("%d",&x);
if(x==0)
{
p=(PNodetype)malloc(sizeof(PNodetype));
p->xishu=0;
p->zhishu=0;
Head->next=p;
p->next=NULL;
}
while(x!=0)
{
printf("Please input the zhishu:");
scanf("%d",&z);
p=(PNodetype)malloc(sizeof(Nodetype));
p->xishu=x;
p->zhishu=z;
pre=Head;
while(pre->next&&pre->next->zhishu>=z)
{
pree=pre;
pre=pre->next;
}
p->next=pre->next;
pre->next=p;
if(pre->zhishu==p->zhishu)
{
pre->xishu+=p->xishu;
pre->next=p->next;
free(p);
}
if(pre->xishu==0)
{
pree->next=pre->next;
free(pre);
}
printf("Please input next xishu:");
scanf("%d",&x);
}
if(Head->next==NULL)
{
pre=(PNodetype)malloc(sizeof(PNodetype));
pre->xishu=0;
pre->zhishu=0;
pre->next=Head->next;
Head->next=pre;
}
return Head;
}
void OutPut_(PNodetype Head)
//输出表达式
{
PNodetype pre;
pre=Head->next;
if(pre->zhishu==0)
printf("%d",pre->xishu);
else
printf("%d*X(%d)",pre->xishu,pre->zhishu);
pre=pre->next;
while(pre)
{
if(pre->zhishu==0)
{
if(pre->xishu>0)
printf("+%d",pre->xishu);
else if(pre->xishu<0)
printf("%d",pre->xishu);
}
else
{
if(pre->xishu>0)
printf("+%d*X(%d)",pre->xishu,pre->zhishu);
else if(pre->xishu<0)
printf("%d*X(%d)",pre->xishu,pre->zhishu);
}
pre=pre->next;
}
printf("\n");
}
PNodetype Plus_(PNodetype H1,PNodetype H2)
//表达式相加
{
PNodetype H3,p1,p2,p3,pre;
H3=(PNodetype)malloc(sizeof(PNodetype));
H3->next=NULL;
p1=H1->next;
p2=H2->next;
pre=H3;
while(p1&&p2)
{
if(p1->zhishu>p2->zhishu)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->zhishu=p1->zhishu;
p3->xishu=p1->xishu;
p3->next=pre->next;
pre->next=p3;
pre=p3;
p1=p1->next;
}
else if(p1->zhishu<p2->zhishu)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->zhishu=p2->zhishu;
p3->xishu=p2->xishu;
p3->next=pre->next;
pre->next=p3;
pre=p3;
p2=p2->next;
}
else if(p1->xishu+p2->xishu!=0)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->zhishu=p1->zhishu;
p3->xishu=p1->xishu+p2->xishu;
p3->next=pre->next;
pre->next=p3;
pre=p3;
p1=p1->next;
p2=p2->next;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
if(p2)
{
while(p2)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->zhishu=p2->zhishu;
p3->xishu=p2->xishu;
p3->next=pre->next;
pre->next=p3;
pre=p3;
p2=p2->next;
}
}
else if(p1)
{
while(p1)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->zhishu=p1->zhishu;
p3->xishu=p1->xishu;
p3->next=pre->next;
pre->next=p3;
pre=p3;
p1=p1->next;
}
}
if(H3->next==NULL)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->xishu=0;
p3->zhishu=0;
p3->next=NULL;
H3->next=p3;
}
return H3;
}
PNodetype Minus_(PNodetype H1,PNodetype H2)
//表达式相减
{
PNodetype H3,pre;
pre=H2->next;
while(pre)
{
pre->xishu=-pre->xishu;
pre=pre->next;
}
H3=Plus_(H1,H2);
pre=H2->next;
while(pre)
{
pre->xishu=-pre->xishu;
pre=pre->next;
}
return H3;
}
PNodetype Multiply_(PNodetype H1,PNodetype H2)
//表达式相乘
{
PNodetype H3,H4,p1,p2,p3,pre;
p2=H2->next;
H4=(PNodetype)malloc(sizeof(PNodetype));
H4->next=NULL;
while(p2)
{
H3=(PNodetype)malloc(sizeof(PNodetype));
pre=H3;
H3->next=NULL;
p1=H1->next;
while(p1)
{
p3=(PNodetype)malloc(sizeof(PNodetype));
p3->xishu=p1->xishu*p2->xishu;
p3->zhishu=p1->zhishu+p2->zhishu;
p3->next=pre->next;
pre->next=p3;
pre=p3;
p1=p1->next;
}
H4=Plus_(H4,H3);
p2=p2->next;
}
return H4;
}
void main()
{
PNodetype Head1,Head2,Head3,Head4,Head5;
Head1=Creat_();
printf("表达式Head1为:");
OutPut_(Head1);
Head2=Creat_();
printf("表达式Head2为:");
OutPut_(Head2);
Head3=Plus_(Head1,Head2);
printf("Head3=Head1+Head2\n");
printf("表达式Head3为:");
OutPut_(Head3);
Head4=Minus_(Head1,Head2);
printf("Head4=Head1-Head2\n");
printf("表达式Head4为:");
OutPut_(Head4);
Head5=Multiply_(Head1,Head2);
printf("Head5=Head1*Head2\n");
printf("表达式Head5为:");
OutPut_(Head5);
}
[[it] 本帖最后由 tang5188 于 2008-6-25 15:25 编辑 [/it]]