Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct nod
  7. {
  8. int l,c;
  9. int x;
  10. struct nod *ant,*urm;
  11. } NOD;
  12.  
  13. FILE *f=fopen("date.in","r");
  14. FILE *g=fopen("date.out","w");
  15.  
  16. NOD *p1,*u1;
  17. NOD *p2,*u2;
  18. NOD *p3,*u3;
  19.  
  20. NOD* caut(NOD *p, NOD *u, int l, int c)
  21. {
  22. NOD *q;
  23. q=p;
  24. while(q!=NULL)
  25. {
  26. if(q->l==l&&q->c==c)return NULL;
  27. if(q->l>l) return q;
  28. if(q->l==l&&q->c>c) return q;
  29. q=q->urm;
  30. }
  31. }
  32.  
  33. void adaug(NOD *&p,NOD *&u,int l, int c,int x)
  34. {
  35. NOD *q, *r;
  36. r=(NOD*)malloc(sizeof(NOD));
  37. r->ant=r->urm=NULL;
  38. r->l=l;
  39. r->c=c;
  40. r->x=x;
  41. if(p==NULL) p=u=r;
  42. else if((r->l<p->l)||(r->l==p->l&&r->c<p->c))
  43. {
  44. r->urm=p;
  45. p->ant=r;
  46. p=r;
  47. }
  48. else if((r->l>u->l)||(r->l==u->l&&r->c>u->c))
  49. {
  50. u->urm=r;
  51. r->ant=u;
  52. u=r;
  53. }
  54. else
  55. {
  56. q=caut(p,u,l,c);
  57. if(q==NULL) free(r);
  58. else
  59. {
  60. r->urm=q;
  61. r->ant=q->ant;
  62. q->ant->urm=r;
  63. q->ant=r;
  64. }
  65. }
  66. }
  67.  
  68. void citire(NOD *&p,NOD *&u,int n)
  69. {
  70. int e,i,l,c,x;
  71. fscanf(f,"%d",&e);
  72. for(i=1;i<=e;i++)
  73. {
  74. fscanf(f,"%d%d%d",&l,&c,&x);
  75. adaug(p,u,l,c,x);
  76. }
  77. }
  78.  
  79. void parc_inc(NOD *prim, NOD *ultim)
  80. {
  81. NOD *p;
  82. p=prim;
  83. if(p==NULL)
  84. fprintf(g,"Nu avem elemente");
  85. else
  86. {
  87. fprintf(g,"----------------------------------------------------------\n");
  88. while(p!=NULL)
  89. {
  90. fprintf(g,"%d %d %d\n",p->l,p->c,p->x);
  91. p=p->urm;
  92. }
  93. fprintf(g,"----------------------------------------------------------\n");
  94. }
  95. }
  96.  
  97. void parc_fin(NOD *prim, NOD *ultim)
  98. {
  99. NOD *p;
  100. p=ultim;
  101. fprintf(g,"\n----------------------------------------------------------\n");
  102. while(p!=NULL)
  103. {
  104. fprintf(g,"%d %d %d\n",p->l,p->c,p->x);
  105. p=p->ant;
  106. }
  107. fprintf(g,"----------------------------------------------------------\n");
  108. }
  109.  
  110. void afis_mat(NOD *p,NOD *u,int n)
  111. {
  112. NOD *q;
  113. q=p;
  114. int i,j;
  115. fprintf(g,"----------------------------------------------------------\n");
  116. for(i=1;i<=n;i++)
  117. {
  118. for(j=1;j<=n;j++)
  119. {
  120. if((q!=NULL)&&(i==q->l&&j==q->c))
  121. {fprintf(g,"%d ",q->x); q=q->urm;}
  122. else fprintf(g,"0 ");
  123. }
  124. fprintf(g,"\n");
  125. }
  126. fprintf(g,"----------------------------------------------------------\n");
  127. }
  128.  
  129. NOD* cauta(NOD *p, NOD *u, int l, int c)
  130. {
  131. NOD *q1;
  132. q=p;
  133. while(q!=NULL)
  134. {
  135. if(q->l==l&&q->c==c)return q;
  136. q=q->urm;
  137. }
  138. }
  139.  
  140.  
  141. void sum_mat(NOD *&p3,NOD *&u3,NOD *p1, NOD *u1,NOD *p2,NOD *u2)
  142. {
  143. NOD *q,*r;
  144. q=p1;
  145. while(q!=NULL)
  146. {
  147. adaug(p3,u3,q->l,q->c,q->x);
  148. q=q->urm;
  149. }
  150. q=p2;
  151. while(q!=NULL)
  152. {
  153. if(caut(p3,u3,q->l,q->c)==NULL)
  154. {
  155. r=cauta(p3,u3,q->l,q->c);
  156. r->x=r->x+q->x;
  157. }
  158. else
  159. adaug(p3,u3,q->l,q->c,q->x);
  160. q=q->urm;
  161. }
  162. }
  163.  
  164. int main()
  165. {
  166. int n;
  167. fscanf(f,"%d",&n);
  168. citire(p1,u1,n);
  169. citire(p2,u2,n);
  170. parc_inc(p1,u1); afis_mat(p1,u1,n);
  171. parc_inc(p2,u2); afis_mat(p2,u2,n);
  172. sum_mat(p3,u3,p1,u1,p2,u2);
  173. afis_mat(p3,u3,n);
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement