Le héros a 50 PV et 2 potions. drink(h) en utilise une : une potion de moins et 30 PV de plus, mais jamais au-dessus de 100. Sans potion, la fonction ne fait rien (c’est déjà écrit). main boit trois fois.
HP: 100, potions: 0
#include <stdio.h>
struct Hero {
int hp;
int potions;
};
void drink(struct Hero *h) {
if (h->potions == 0) {
return;
}
// ton code ici: une potion de moins, 30 hp de plus, et retour à 100 si au-dessus
}
int main(void) {
struct Hero hero = {50, 2};
drink(&hero);
drink(&hero);
drink(&hero);
printf("HP: %d, potions: %d\n", hero.hp, hero.potions);
return 0;
}
Exécuter teste les exemples. Quand ils sont bons, appuie sur Envoyer.