Advertisement
Guest User

chestie

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <fstream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. struct nod{
  6.     short int x = 0;
  7.     nod *st, *dr;
  8. };
  9.  
  10. nod * construct(nod *& p, char s[])
  11. {
  12.     int aux = strlen(s);
  13.     nod *q = p, *r;
  14.     for (int i = 0; i < aux; i++)
  15.     {
  16.         r = new nod;
  17.         r->x = s[i] - '0';
  18.         r->st = q;
  19.         r->dr = NULL;
  20.         q->dr = r;
  21.         q = r;
  22.     }
  23.     r = p;
  24.     p = p->dr;
  25.     delete r;
  26.     p->st = NULL;
  27.     return q;
  28. }
  29.  
  30. nod * ad(nod *n, nod *m, nod *q, nod *r)
  31. {
  32.     nod *sum;
  33.     sum = new nod;
  34.     sum->st = sum->dr = NULL;
  35.     while (r)
  36.     {
  37.         q->st->x += (q->x + r->x)/10;
  38.         q->x = (q->x + r->x)%10;
  39.         q = q->st;
  40.         r = r->st;
  41.     }
  42.     while (q->st)
  43.     {
  44.         q->st->x += q->x/10;
  45.         q->x = q->x%10;
  46.         q = q->st;
  47.     }
  48. }
  49.  
  50. int main()
  51. {
  52.     ifstream fin("suma.in");
  53.     ofstream fout("suma.out");
  54.     nod *n, *m, *q, *r;
  55.     n = new nod;
  56.     n->st = n->dr = NULL;
  57.     m = new nod;
  58.     m->st = m->dr = NULL;
  59.     char s[10002];
  60.     fin >> s;
  61.     fin.get();
  62.     q = construct(n, s);
  63.     fin >> s;
  64.     r = construct(m, s);
  65.     ad(n, m, q, r);
  66.     for (q = n; q != NULL; q = q->dr)
  67.         fout << q->x;
  68.     fin.close();
  69.     fout.close();
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement