csansoon

P6.18 P72570 P0020. El rellotge de l'Amalia

Nov 8th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void llegeix_hora(int&h, int&m, int&s) {
  6.         //hores
  7.         int auxh = h;
  8.         h = 0;
  9.         int mult = 1;
  10.         while (auxh != 0) {
  11.                 if (auxh%10 == 1) h = h + mult;
  12.                 mult = mult*2;
  13.                 auxh = auxh/10;
  14.         }
  15.         //minuts
  16.         int auxm = m;
  17.         m = 0;
  18.         mult = 1;
  19.         while (auxm != 0) {
  20.                 if (auxm%10 == 1) m = m + mult;
  21.                 mult = mult*2;
  22.                 auxm = auxm/10;
  23.         }
  24.         //segons
  25.         int auxs = s;
  26.         s = 0;
  27.         mult = 1;
  28.         while (auxs != 0) {
  29.                 if (auxs%10 == 1) s = s + mult;
  30.                 mult = mult*2;
  31.                 auxs = auxs/10;
  32.         }
  33. }
  34.  
  35. void escriu_hora(int h, int m, int s) {
  36.         cout << h << ":" << m << ":" << s << endl;
  37. }
  38.  
  39. //Pre: Llegeig una seqüčncia n de codificacions d'hora binaries on * és 1 i $ és 0
  40. //Post: Escriu l'hora en el format convencional
  41. int main() {
  42.         int n;
  43.         cin >> n;
  44.         char c;
  45.         while (n != 0) {
  46.                 int count = 3;
  47.                 int hms = 1;
  48.                 int h = 0, m = 0, s = 0;
  49.                 cin >> c;
  50.                 --count;
  51.                 while (count >= 0) {
  52.                         int mult = 1;
  53.                         while (cin >> c && c != '|') {
  54.                                 int i;
  55.                                 if (c == '*') i = 1;
  56.                                 else if (c == '$') i = 0;
  57.                                 if (hms == 1) {
  58.                                         h = (h * mult) + i;
  59.                                 }
  60.                                 else if (hms == 2) {
  61.                                         m = (m * mult) + i;
  62.                                 }
  63.                                 else if (hms == 3) {
  64.                                         s = (s * mult) + i;
  65.                                 }
  66.                                 mult = 10;
  67.                         }
  68.                         --count;
  69.                         ++hms;
  70.                 }
  71.                 llegeix_hora(h, m, s);
  72.                 escriu_hora(h, m, s);
  73.                 --n;
  74.         }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment