Last active
October 8, 2017 20:06
-
-
Save jordi-petit/9a7bd0cd3d351440d674b6e9a972afe7 to your computer and use it in GitHub Desktop.
AP1 2017-09-27
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure un rectangle amb accions | |
#include <iostream> | |
using namespace std; | |
// acció que escriu una fila amb n asteríscs | |
void escriure_fila(int n) { | |
int i = 1; | |
while (i <= m) { | |
cout << '*'; | |
i = i + 1; | |
} | |
cout << endl; | |
} | |
// acció que escriu un rectangle de m files i n columnes | |
void escriure_rectangle(int m, int n) { | |
int i = 1; | |
while (i <= m) { | |
escriure_fila(n); | |
i = i + 1; | |
} | |
} | |
// Programa principal | |
int main() { | |
int m, n; | |
cin >> m >> n; | |
escriure_rectangle(m, n); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure un rectangle amb accions (2) | |
#include <iostream> | |
using namespace std; | |
// acció que escriu una fila amb n símbols c | |
void escriure_fila(int n, char c) { | |
int i = 1; | |
while (i <= m) { | |
cout << c; | |
i = i + 1; | |
} | |
cout << endl; | |
} | |
// acció que escriu un rectangle de m files i n columnes | |
void escriure_rectangle(int m, int n, char c) { | |
int i = 1; | |
while (i <= m) { | |
escriure_fila(n, c); | |
i = i + 1; | |
} | |
} | |
// Programa principal | |
int main() { | |
int m, n; | |
char c; | |
cin >> m >> n >> c; | |
escriure_rectangle(m, n, c); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rellotge | |
#include <iostream> | |
#include <unistd.h> | |
using namespace std; | |
void escriure_num(int x) { | |
if (x < 10) cout << 0; | |
cout << x; | |
} | |
void escriure_hora(int h, int m, int s) { | |
escriure_num(h); | |
cout << ":"; | |
escriure_num(m); | |
cout << ":"; | |
escriure_num(s); | |
cout << endl; | |
} | |
// Els tres paràmetres es passen per referència! | |
// Mireu què passa si no es fa. | |
void sumar_un_segon(int& h, int& m, int&s) { | |
s = s + 1; | |
if (s == 60) { | |
s = 0; | |
m = m + 1; | |
if (m == 60) { | |
m = 0; | |
h = h + 1; | |
if (h == 24) { | |
h = 0; | |
} | |
} | |
} | |
// cout << "xivato: "; escriure_hora(h, m, s); | |
} | |
int main() { | |
int hores = 0; | |
int minuts = 0; | |
int segons = 0; | |
while (true) { // bucle infinit, atureu amb Control+C | |
escriure_hora(hores, minuts, segons); | |
sumar_un_segon(hores, minuts, segons); | |
sleep(1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Intercanvi amb una acció | |
#include <iostream> | |
using namespace std; | |
void swap(int& a, int& b) { | |
int c = a; | |
a = b; | |
b = c; | |
} | |
int main() { | |
int x = 2; | |
int y = 3; | |
swap(x, y); | |
cout << x << " " << y << endl; | |
} |
Jo si, gràcies per compartir-los, és molt útil per repassar a casa!
@OriolNarvaez: Gràcies! Algú més?
Jo també i penso el mateix que l'Oriol, gràcies!
Jo també! És molt útil per repassar i per poder autoresoldre els dubtes que ens surten quan fem els problemes del jutge!
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Algú ho mira?