Advertisement
danixan

Sessió 6: Modificar elements d'una pila (parell d'enters)

Mar 27th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include "stackIOint.hh"
  2.  
  3. void pertstack_it(stack<parint> &S, int i);
  4.  
  5.  
  6. int main()
  7. {
  8.   stack<parint> p;
  9.   llegir_stack_int(p,0);
  10.   cout << "Escriu opció -1 per modificar la pila original o -2 per fer-ne una de nova:" << endl;
  11.   int n = readint();
  12.   cout << "Escriu la component k a sumar:" << endl;
  13.   int valor=readint();
  14.   if (n == -1) {
  15.     pertstack_it(p, valor);
  16.   }
  17.   else {
  18.     stack<parint> copia(p);
  19.     pertstack_it(p, valor);
  20.   }
  21.   escriure_stack_int(p);
  22. }
  23.  
  24.  
  25. void pertstack_it(stack<parint> &s, int i)
  26. /* Pre: s = S */
  27. /* Post: La pila qeda modifica el segon component que se li suma i */
  28. {
  29.   stack<parint> n;
  30.   parint x;
  31.   while (not s.empty()){
  32.     x = s.top();
  33.     x.n2 += i;
  34.     n.push(x);
  35.     s.pop();
  36.   }
  37.   while (not n.empty()){
  38.     x = n.top();
  39.     s.push(x);
  40.     n.pop();
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement