Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string.h>
- using namespace std;
- struct nod{
- short int x = 0;
- nod *st, *dr;
- };
- nod * construct(nod *& p, char s[])
- {
- int aux = strlen(s);
- nod *q = p, *r;
- for (int i = 0; i < aux; i++)
- {
- r = new nod;
- r->x = s[i] - '0';
- r->st = q;
- r->dr = NULL;
- q->dr = r;
- q = r;
- }
- r = p;
- p = p->dr;
- delete r;
- p->st = NULL;
- return q;
- }
- nod * ad(nod *n, nod *m, nod *q, nod *r)
- {
- nod *sum;
- sum = new nod;
- sum->st = sum->dr = NULL;
- while (r)
- {
- q->st->x += (q->x + r->x)/10;
- q->x = (q->x + r->x)%10;
- q = q->st;
- r = r->st;
- }
- while (q->st)
- {
- q->st->x += q->x/10;
- q->x = q->x%10;
- q = q->st;
- }
- }
- int main()
- {
- ifstream fin("suma.in");
- ofstream fout("suma.out");
- nod *n, *m, *q, *r;
- n = new nod;
- n->st = n->dr = NULL;
- m = new nod;
- m->st = m->dr = NULL;
- char s[10002];
- fin >> s;
- fin.get();
- q = construct(n, s);
- fin >> s;
- r = construct(m, s);
- ad(n, m, q, r);
- for (q = n; q != NULL; q = q->dr)
- fout << q->x;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement