#include <iostream>
#include <cmath> // synonyme de <math.h>
using namespace std;

void boite(int l, int c, char e) {
    for (int i=0; i<l; i++) {
        for (int j=0; j<c; j++)
            cout << e;
        cout << endl;
    }
}

void boite_bordure(int l, int c, char e) {
    for (int i=0; i<l; i++) {
        for (int j=0; j<c; j++)
            if (i > 0 && i < l-1 && j > 0 && j < c-1)
                cout << ' ';
            else
                cout << e;
        cout << endl;
    }
}

void boite_cadre(int l, int c, char coin, char bvert, char bhoriz) {
    for (int i=0; i<l; i++) {
        for (int j=0; j<c; j++)
            if ((i == 0 || i == l-1) && j > 0 && j < c-1)
                cout << bhoriz;
            else if ((i == 0 && (j == 0 || j == c-1))
                    || (i == l-1 && (j == 0 || j == c-1)))
                cout << coin;
            else if ((i > 0 && i < l-1) && (j == 0 || j == c-1))
                cout << bvert;
            else
                cout << ' ';
        cout << endl;
    }
}

void diagonales(int l, int c, char e) {
    /* avec min(l,c) on sélectionne le plus grand carré inclu dans
     * le rectangle */
    for (int i=0; i < min(l,c); i++) {
        for (int j=0; j < min(l,c); j++)
            if (i == j || min(l,c)-1-i == j)
                cout << e;
            else 
                cout << ' ';
        cout << endl;
    }
}

double discriminant(double a, double b, double c) {
    return b*b - 4.0 * a *c;
}

void resoudre_eq2(double a, double b, double c) {
    double delta = discriminant(a,b,c);
    cout << "Résolution de l'équation "
        << a << "x2 + " << b << "x + " << c << endl;
    if (delta == 0)
        cout << "Solution double: " << -b/(2*a) << endl;
    else if (delta > 0)
        /* petite astuce, si s vaut 0 ou 1 alors
         * s*2 - 1 vaut 1 ou -1 et on peut donc faire une boucle
         * suivant un signe */
        for (int s = 0; s < 2; s++)
            cout << "Solution " << s+1 << " : " 
                << (-b+sqrt(delta)*(s*2.0-1.0))/(2*a) << endl;
    else
        for (int s = 0; s < 2; s++)
            cout << "Solution " << s+1 << " : " 
                << -b/(2*a) << '+' 
                << sqrt(-delta)*(s*2.0-1.0)/(2*a) << 'i' << endl;
}

int celsius(int t) {
    return ((t-32) * 5)/9;
}

int fahrenheit(int t) {
    return (t*9)/5 + 32;
}

void tables_temperatures() {
    cout << "  °C | °F" << endl;
    for (int i = 0; i <= 100; i++) {
        if (i < 10)
            cout << "   ";
        else if (i == 100)
            cout << " ";
        else
            cout << "  ";

        cout << i << " | " << fahrenheit(i) << endl;
    }

    cout << endl;

    cout << "  °F | °C" << endl;
    for (int i = 32; i <= 212; i++) {
        if (i >= 100)
            cout << " ";
        else
            cout << "  ";

        cout << i << " | " << celsius(i) << endl;
    }
}

int main(int argc, char **argv) {
    // Exercice 2.1
    cout << "Test de la fonction boite" << endl;
    boite(3,3,'*');
    cout << "Test de la fonction boite_bordure" << endl;
    boite_bordure(3,3,'*');
    cout << "Test de la fonction boite_cadre" << endl;
    boite_cadre(3,4,'*','|','-');
    cout << "Test de la fonction diagonales" << endl;
    diagonales(4,10,'*');

    // Exercice 2.2
    resoudre_eq2(5,3,0);
    resoudre_eq2(1,0,0);
    resoudre_eq2(0.5,0,1);

    // Exercice 2.3
    tables_temperatures();
}
