Advertisement
rihardmarius

final 2c - numeros astronomicos

Nov 24th, 2013
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. // numeros astronomicos
  2. #include <fstream>
  3. #include "pilas_con_listas.h"
  4.  
  5. using namespace std;
  6.  
  7. stack& leer_numero (string nombre)
  8. {
  9.     stack s;
  10.     int a;
  11.     ifstream input (nombre);
  12.     if (not input.is_open())
  13.     {
  14.         cout << "No se encontro el archivo." << endl;
  15.         return s;
  16.     }
  17.     while (input >> a)
  18.         push(s,a);
  19.     return s;
  20.  
  21. }
  22.  
  23. stack sumar_numeros (stack& a, stack& b)
  24. {
  25.     int x, y, suma=0, resto=0; stack s;
  26.     while (not is_stack_empty(a) and not is_stack_empty(b))
  27.     {
  28.  
  29.         pop(a,x);
  30.         pop(b,y);
  31.         suma = x+y+resto;
  32.         resto=0;
  33.         if (suma>9)
  34.         {
  35.             suma = suma%10;
  36.             resto++;
  37.         }
  38.         push(s, suma);
  39.         suma=0;
  40.     }
  41.  
  42.     while (not is_stack_empty(a))
  43.     {
  44.         pop(a,x);
  45.         suma = x + resto;
  46.         resto = 0;
  47.         if (suma>9)
  48.         {
  49.             suma = suma%10;
  50.             resto++;
  51.         }
  52.         push(s, suma);
  53.         suma=0;
  54.     }
  55.     while (not is_stack_empty(b))
  56.     {
  57.         pop(b,x);
  58.         suma = x + resto;
  59.         resto = 0;
  60.         if (suma>9)
  61.         {
  62.             suma = suma%10;
  63.             resto++;
  64.         }
  65.         push(s, suma);
  66.         suma=0;
  67.     }
  68.  
  69.     if (resto != 0)
  70.         push (s,1);
  71.  
  72.     return s;
  73.  
  74. }
  75.  
  76. int main()
  77. {
  78.     stack a, b, c; string nombre; int d;
  79.  
  80.     nombre = "archivo1.txt";
  81.     a = leer_numero(nombre);
  82.  
  83.     nombre = "archivo2.txt";
  84.     b = leer_numero(nombre);
  85.  
  86.     c = sumar_numeros(a,b);
  87.  
  88.     vaciar_stack(c);
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement