#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<iostream>
using namespace std;
typedef struct node *link;
typedef struct node
{
double a1;
int b1;
link next;
}Node;
link NewNode()
{
return (link)malloc(sizeof(Node));
}
typedef struct llist *List;
typedef struct llist
{
link first;
}Llist;
List ListInit()
{
List L=(List)malloc(sizeof *L);
L->first=0;
return L;
}
//函数模板,检查完了
List add(List L1,List L2)
{
link p,q;p=L1->first;q=L2->first;
List L;L=ListInit();
int flag=0;
while(p&&q)
{
if(p->b1>q->b1)//将p插入表
{
link p1;p1=NewNode();p1->a1=p->a1;p1->b1=p->b1;
if(!flag)//表中没有任何东西
{
L->first=p1;p1->next=0; flag=1;
}
else//表中有东西
{
//不是插在头结点处,直接插在队尾
link haha;haha=L->first;
while(haha->next!=0)
{
haha=haha->next;
}
haha->next=p1;p1->next=0;
}
p=p->next;
continue;//p向后移动
}
else if(p->b1<q->b1)//按照刚刚的方式将 q插入表格
{
link q1;q1=NewNode();q1->a1=q->a1;q1->b1=q->b1;
if(!flag)//表中没有任何东西
{
L->first=q1;q1->next=0; flag=1;
}
else//表中有东西
{
//不是插在头结点处,直接插在队尾
link haha;haha=L->first;
while(haha->next!=0)
{
haha=haha->next;
}
haha->next=q1;q1->next=0;
}
q=q->next;continue;//q向后移动
}
else if(p->b1==q->b1)
{
if(p->a1+q->a1==0)
{
p=p->next;q=q->next;continue;
}
else if(p->a1+q->a1!=0)
{
link p1;p1=NewNode();
p1->a1=p->a1+q->a1;//系数相加,并把p插入表中
p1->b1=p->b1;
if(!flag)//表中没有任何东西
{
L->first=p1;p1->next=0; flag=1;
}
else//表中有东西
{
//不是插在头结点处,直接插在队尾
link haha;haha=L->first;
while(haha->next!=0)
{
haha=haha->next;
}
haha->next=p1;p1->next=0;
}
p=p->next;q=q->next;continue;
}
}
}
if(p)//q为NULL的情况
{
link haha;haha=L->first;
while(haha->next!=0) haha=haha->next;
haha->next=p;
}
else if(q)
{
link haha;haha=L->first;
while(haha->next!=0) haha=haha->next;
haha->next=q;
}
return L;
}
List sub(List L1,List L2)
{
link x;x=L2->first;
while(x)
{
x->a1=-x->a1;x=x->next;
}
List L;L=add(L1,L2);
return L;
}
List mul(List L1,List L2)
{
List L;L=ListInit();
int flag=0;
link p=L1->first;link q;link p1;
while(p)
{
q=L2->first;
while(q)
{
p1=NewNode();
p1->a1=p->a1*q->a1;
p1->b1=p->b1+q->b1;
if(!flag)
{
L->first=p1;p1->next=0;flag=1;
}
else if(flag)
{
if(p1->b1>L->first->b1)
{
p1->next=L->first;L->first=p1;
}
else
{
int flag1=0;
link x=L->first;
while(x)
{
if(x->b1>p1->b1&&x->next!=0&&p1->b1>x->next->b1)
break;
else if(x->b1==p1->b1)
{
flag1=1; break;
}
else if(x->next!=0)
x=x->next;
else break;
}
if(flag1)
{
x->a1+=p1->a1;
}
else if(!flag1)
{
p1->next=x->next;
x->next=p1;
}
}
}
q=q->next;
}
p=p->next;
}
return L;
}
void oper(List L1)
{
int i;
link p=L1->first;
int flag=0,flag1=0;
while(p)
{
if(p->a1==0) continue;
else if(p->a1>0&&p->a1!=1)
{
if(flag==0)
{
cout<<setprecision(6)<<p->a1;flag=1;
}
else
cout<<"+"<<setprecision(6)<<p->a1;
}
else if(p->a1==1)
{
if(flag==0) flag++;
else printf("+");
}
else if(p->a1<0&&p->a1!=-1)
{
if(flag==0)
{
cout<<setprecision(6)<<p->a1;flag=1;
}
else
cout<<setprecision(6)<<p->a1;
}
else if(p->a1==-1)
{
if(flag==0) flag++;
printf("-");
}
if(p->b1>1)
{
printf("x^%d",p->b1);flag1=1;
}
else if(p->b1==1)
{
printf("x");flag1=1;
}
p=p->next;
}
if(!flag1)
{
printf("0");
}
}
char fuhao[21];
int main()
{
link p1,p;
int i,j;
List L[21];
int k,k1;
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d",&k1);
L[i]=ListInit();
for(j=0;j<k1;j++)
{
p1=NewNode();
scanf("%lf %d",&p1->a1,&p1->b1);
if(j==0)
{
L[i]->first=p1;p1->next=0;
}
else
{
if(p1->b1>L[i]->first->b1)
{
p1->next=L[i]->first;
L[i]->first=p1;
}
else
{
int flag1=0;
link p2;p2=L[i]->first;
while(1)
{
if(p2->b1>p1->b1&&p2->next!=0&&p2->next->b1<p1->b1)
{
break;
}
else if(p2->b1==p1->b1)
{
flag1=1;break;
}
else if(p2->next!=0)
{
p2=p2->next;
}
else break;
}
if(!flag1)//只会出现这两种情况
{
p1->next=p2->next;
p2->next=p1;
}
else if(flag1)
{
p2->a1+=p1->a1;
}
}
}
}
}getchar();
for(i=0;i<k-1;i++)
{
scanf("%c",&fuhao[i]);
}
List Lr;Lr=L[0];
for(i=0;i<k-1;i++)
{
if(fuhao[i]=='+')
{
Lr=add(Lr,L[i+1]);
}
else if(fuhao[i]=='-')
{
Lr=sub(Lr,L[i+1]);
}
else if(fuhao[i]=='*')
{
Lr=mul(Lr,L[i+1]);
}
}
oper(Lr);
return 0;
}
有史以来最长的代码 第二周第一题
最新推荐文章于 2024-10-29 16:39:01 发布