Advertisement
JosepRivaille

X18958: Cerca en piles de parints

Oct 16th, 2015
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. /* ---------------------------- PilaIOParInt.hh -------------------------------- */
  2.  
  3. #include "ParInt.hh"
  4. #include <iostream>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. void llegirPilaParInt(stack<ParInt>& p);
  9.  
  10. void escriurePilaParInt(stack<ParInt> p);
  11.  
  12. /* ---------------------------- PilaIOParInt.cc -------------------------------- */
  13.  
  14. #include "PilaIOParInt.hh"
  15.  
  16. void llegirPilaParInt(stack<ParInt>& p) {
  17.     int a, b;
  18.     while (cin >> a >> b && (a != 0 && b != 0)) {
  19.       ParInt par(a, b);
  20.       p.push(par);
  21.     }
  22. }
  23.  
  24. void escriurePilaParInt(stack<ParInt> p) {
  25.   int m;
  26.   cin >> m;
  27.   bool found = false;
  28.   int aux;
  29.   while (!p.empty()) {
  30.     ParInt par = p.top();
  31.     par.escriure();
  32.     if (!found && par.primer() == m) {
  33.       found = true;
  34.       aux = par.segon();
  35.     }
  36.     p.pop();
  37.   }
  38.   if (found) cout << aux << endl;
  39.   else cout << "No trobat" << endl;
  40. }
  41.  
  42. /* ------------------------------- program.cc ---------------------------------- */
  43.  
  44. #include "PilaIOParInt.hh"
  45.  
  46. int main() {
  47.   stack<ParInt> p;
  48.   llegirPilaParInt(p);
  49.   escriurePilaParInt(p);
  50. }
  51.  
  52. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement