Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- typedef struct nod
- {
- int l,c;
- int x;
- struct nod *ant,*urm;
- } NOD;
- FILE *f=fopen("date.in","r");
- FILE *g=fopen("date.out","w");
- NOD *p1,*u1;
- NOD *p2,*u2;
- NOD *p3,*u3;
- NOD* caut(NOD *p, NOD *u, int l, int c)
- {
- NOD *q;
- q=p;
- while(q!=NULL)
- {
- if(q->l==l&&q->c==c)return NULL;
- if(q->l>l) return q;
- if(q->l==l&&q->c>c) return q;
- q=q->urm;
- }
- }
- void adaug(NOD *&p,NOD *&u,int l, int c,int x)
- {
- NOD *q, *r;
- r=(NOD*)malloc(sizeof(NOD));
- r->ant=r->urm=NULL;
- r->l=l;
- r->c=c;
- r->x=x;
- if(p==NULL) p=u=r;
- else if((r->l<p->l)||(r->l==p->l&&r->c<p->c))
- {
- r->urm=p;
- p->ant=r;
- p=r;
- }
- else if((r->l>u->l)||(r->l==u->l&&r->c>u->c))
- {
- u->urm=r;
- r->ant=u;
- u=r;
- }
- else
- {
- q=caut(p,u,l,c);
- if(q==NULL) free(r);
- else
- {
- r->urm=q;
- r->ant=q->ant;
- q->ant->urm=r;
- q->ant=r;
- }
- }
- }
- void citire(NOD *&p,NOD *&u,int n)
- {
- int e,i,l,c,x;
- fscanf(f,"%d",&e);
- for(i=1;i<=e;i++)
- {
- fscanf(f,"%d%d%d",&l,&c,&x);
- adaug(p,u,l,c,x);
- }
- }
- void parc_inc(NOD *prim, NOD *ultim)
- {
- NOD *p;
- p=prim;
- if(p==NULL)
- fprintf(g,"Nu avem elemente");
- else
- {
- fprintf(g,"----------------------------------------------------------\n");
- while(p!=NULL)
- {
- fprintf(g,"%d %d %d\n",p->l,p->c,p->x);
- p=p->urm;
- }
- fprintf(g,"----------------------------------------------------------\n");
- }
- }
- void parc_fin(NOD *prim, NOD *ultim)
- {
- NOD *p;
- p=ultim;
- fprintf(g,"\n----------------------------------------------------------\n");
- while(p!=NULL)
- {
- fprintf(g,"%d %d %d\n",p->l,p->c,p->x);
- p=p->ant;
- }
- fprintf(g,"----------------------------------------------------------\n");
- }
- void afis_mat(NOD *p,NOD *u,int n)
- {
- NOD *q;
- q=p;
- int i,j;
- fprintf(g,"----------------------------------------------------------\n");
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=n;j++)
- {
- if((q!=NULL)&&(i==q->l&&j==q->c))
- {fprintf(g,"%d ",q->x); q=q->urm;}
- else fprintf(g,"0 ");
- }
- fprintf(g,"\n");
- }
- fprintf(g,"----------------------------------------------------------\n");
- }
- NOD* cauta(NOD *p, NOD *u, int l, int c)
- {
- NOD *q1;
- q=p;
- while(q!=NULL)
- {
- if(q->l==l&&q->c==c)return q;
- q=q->urm;
- }
- }
- void sum_mat(NOD *&p3,NOD *&u3,NOD *p1, NOD *u1,NOD *p2,NOD *u2)
- {
- NOD *q,*r;
- q=p1;
- while(q!=NULL)
- {
- adaug(p3,u3,q->l,q->c,q->x);
- q=q->urm;
- }
- q=p2;
- while(q!=NULL)
- {
- if(caut(p3,u3,q->l,q->c)==NULL)
- {
- r=cauta(p3,u3,q->l,q->c);
- r->x=r->x+q->x;
- }
- else
- adaug(p3,u3,q->l,q->c,q->x);
- q=q->urm;
- }
- }
- int main()
- {
- int n;
- fscanf(f,"%d",&n);
- citire(p1,u1,n);
- citire(p2,u2,n);
- parc_inc(p1,u1); afis_mat(p1,u1,n);
- parc_inc(p2,u2); afis_mat(p2,u2,n);
- sum_mat(p3,u3,p1,u1,p2,u2);
- afis_mat(p3,u3,n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement