//////////////////////////////////////////////// // // // Sylvain MARECHAL // // Exemple d'implementation pour la table de // // pythagore. // // // //////////////////////////////////////////////// #include #include // Constante de gestion de largeur/hauteur // de la table de pythagore const int Largeur = 6; const int Hauteur = 2; ///////////////////////////////////////// // // Ecriture d'un caractere c en (x,y) // ///////////////////////////////////////// void tracechar( int x, int y, char c ) { gotoxy(x,y); putchar(c); } ///////////////////////////////////////// // // Trace d'une colone de hauteur Hauteur // a partir de (xOrg,yOrg) // ///////////////////////////////////////// void tracecolone( int xOrg, int yOrg, int Hauteur ) { for( int y = yOrg; y < yOrg + Hauteur; y ++ ) { tracechar(xOrg,y,'º' ); } } ///////////////////////////////////////// // // Trace d'une ligne de longueur Longueur // a partir de (xOrg,yOrg) // ///////////////////////////////////////// void traceligne( int xOrg, int yOrg, int Longueur ) { for( int x = xOrg; x < xOrg + Longueur; x ++ ) { tracechar(x,yOrg,'Í'); } } ///////////////////////////////////////// // // Trace d'une ligne de longueur Longueur // a partir de (xOrg,yOrg) // cDeb : 1ø car // cRemplis : car de remplissage // cInter : gestion des colones // cFin : dernier car // ///////////////////////////////////////// void traceligne2( int xOrg, int yOrg, int Longueur, int xLarg, char cDeb, char cRemplis, char cInter, char cFin ) { for( int x = xOrg; x < xOrg + Longueur; x ++ ) { char cToPrint; if( xOrg == x ) { // On est sur la premiere colone cToPrint = cDeb; } else if( x - xOrg == Longueur-1 ) { // on est sur la derniere colone cToPrint = cFin; } else if( (x - xOrg) % xLarg == 0 ) { // On est sur une colone intermediaire cToPrint = cInter; } else { cToPrint = cRemplis; } tracechar(x,yOrg, cToPrint); } } ///////////////////////////////////////// // // Trace d'un cadre // a partir de (xOrg,yOrg) jusqu'a // (xFin, yFin) // ///////////////////////////////////////// void cadre( int xOrg, int yOrg, int xFin, int yFin ) { tracechar( xOrg, yOrg, 'É' ); tracechar( xFin, yOrg, '»' ); tracechar( xOrg, yFin, 'È' ); tracechar( xFin, yFin, '¼' ); traceligne( xOrg + 1, yOrg, (xFin-xOrg) - 1 ); traceligne( xOrg + 1, yFin, (xFin-xOrg) - 1 ); tracecolone( xOrg, yOrg + 1, (yFin-yOrg) - 1 ); tracecolone( xFin, yOrg + 1, (yFin-yOrg) - 1 ); } ///////////////////////////////////////// // // Trace d'une grille // a partir de (xOrg,yOrg) jusqu'a // (xFin, yFin) // Les lignes intermediaires sont espacees // de yHaut // Les hauteurs intermediaires sont espacees // de xLarg // ///////////////////////////////////////// void grille( int xOrg, int yOrg, int xFin, int yFin, int xLarg, int yHaut ) { for( int y = yOrg; y <= yFin; y ++ ) { char cDeb, cRemplis, cInter, cFin; if( y == yOrg ) { // Caracteres pour la premiere ligne cDeb = 'É'; cRemplis = 'Í'; cInter = 'Ë'; cFin = '»'; } else if( y == yFin ) { // Caracteres pour la derniere ligne cDeb = 'È'; cRemplis = 'Í'; cInter = 'Ê'; cFin = '¼'; } else if( (y - yOrg) % yHaut == 0 ) { // Caracteres pour une ligne intermediaire cDeb = 'Ì'; cRemplis = 'Í'; cInter = 'Î'; cFin = '¹'; } else { // Caracteres pour une ligne 'vide' cDeb = 'º'; cRemplis = ' '; cInter = 'º'; cFin = 'º'; } traceligne2( xOrg, y, xFin - xOrg + 1, xLarg, cDeb, cRemplis, cInter, cFin ); } } ///////////////////////////////////////// // // Affichage des valeurs la table // ///////////////////////////////////////// void affichetable( int xCol, int yLig, int xLarg, int yHaut, int nbValues ) { int x, y; for( y=1; y < nbValues; y ++ ) { for( x = 1; x < nbValues; x ++ ) { gotoxy( ((x-1) * xLarg) + xCol + 1, (y * yHaut) + yLig - 1 ); cprintf( "%2d", x * y ); } } } ///////////////////////////////////////// // // cree une fenetre vide // ///////////////////////////////////////// void creefenetre( int xOrg, int yOrg, int xFin, int yFin, int textC, int backC ) { textcolor(textC); textbackground(backC); window( xOrg,yOrg,xFin,yFin); clrscr(); } ///////////////////////////////////////// // // main() // ///////////////////////////////////////// void main() { int xCol, yLig; int xOrg, yOrg; int xFin, yFin; int xLarg, yHaut; int touche; do { // Efface ecran creefenetre( 1, 1, 80, 25, WHITE, BLACK ); // Init xLarg = Largeur; yHaut = Hauteur; // Saisie cprintf( "Origine en X: " ); scanf( "%d", &xOrg ); cprintf( "\nOrigine en Y:" ); scanf( "%d", &yOrg ); cprintf( "\n" ); // Deduction xFin et yFin xFin = xOrg + 9*xLarg; yFin = yOrg + 9*yHaut; // Verifie coherence if( xOrg < 1 || yOrg < 1 || xFin > 80 || yFin > 25 ) { cprintf( "\nSaisie incorrecte !" ); continue; } // Fenetre creefenetre(xOrg,yOrg,xFin,yFin, YELLOW, BLUE); // Attention, les coordonnees sont relatives // a la fenetre (window(...) ), pas a // l'origine de l'ecran // cadre( 1, 1, xFin - xOrg + 1, yFin - yOrg + 1 ); grille( 1, 1, xFin - xOrg + 1, yFin - yOrg + 1, xLarg, yHaut ); affichetable( 1, 1, xLarg, yHaut, 10 ); window( 1, 1, 80, 25 ); gotoxy( 40, 25); cprintf( "Quitter (Q/q) ? "); touche = getch(); } while( touche != 'q' && touche != 'Q' ); }