Correction - Sylvain Tisserant

Transcription

Correction - Sylvain Tisserant
// POLYTECH MARSEILLE - INFO3
// Algorithmique, Programmation & Structures de données I
// Année 2016-2017
// Fonctions récursives
// ====================
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// Pour menu de choix du test
#define
#define
#define
#define
FIBO 1
EXPO 2
PGCD 3
BIZARRE 4
/* ---------------------------------------------------------------------------------------------------------- */
// Prototypes des fonctions
int choisir ( void ) ;
long int fibo ( int n ) ;
float expo ( float x , int n ) ;
int pgcd ( int a , int b ) ;
int bizarre ( int a ) ;
int bizarre_rec ( int a , int b ) ;
/* ---------------------------------------------------------------------------------------------------------- */
//===============================================//
//
//
// Dialogue pour choisir la fonction à essayer //
//
//
//===============================================//
int choisir ( void )
{
int reponse ;
do {
printf( "Choix : %d - fibo / %d - expo / %d - pgcd / %d - bizarre : " , FIBO , EXPO , PGCD , BIZARRE ) ;
scanf( "%d" , & reponse ) ;
} while ( reponse < FIBO || reponse > BIZARRE ) ;
return ( reponse ) ;
}
/* ---------------------------------------------------------------------------------------------------------- */
//========================//
//
//
// Fonction de Fibonacci //
//
//
//========================//
long int fibo ( int n )
{
if ( n == 0 || n == 1 ) return ( n ) ;
return ( fibo ( n-1 ) + fibo ( n-2 ) ) ;
}
//===========================================//
//
//
// Calcul efficace d'une puissance entière //
//
//
//===========================================//
float expo ( float x , int n )
{
float res ;
if ( n == 0 ) return( 1 ) ;
if ( n%2 == 0 ) {
res = expo( x , n/2 ) ;
return ( res * res ) ;
}
return ( x * expo( x , n-1 ) ) ;
}
//========================//
//
//
// PGCD de deux entiers //
//
//
//========================//
int pgcd (
{
if ( a
if ( b
return
}
int a , int b )
< b ) return ( pgcd( b , a ) ) ;
== 0 ) return ( a ) ;
( pgcd( b , a%b ) ) ;
//===================================//
//
//
// S'agit-il d'un nombre premier ? //
//
//
//===================================//
int bizarre ( int a )
{
assert ( a > 0 ) ;
return ( bizarre_rec( a , 2 ) ) ;
}
int bizarre_rec ( int a , int b )
{
if ( a == 1) return (0) ;
if ( a == 2 || a == b ) return( 1 ) ;
if ( bizarre( b ) && a%b == 0 ) return( 0 ) ;
return ( bizarre_rec( a , b+1 ) ) ;
}
//
//
//
//
1 n'est pas premier
2 ou divisible par lui-même => premier
divisible par b => pas premier
divisible par b+1 ?
/* ---------------------------------------------------------------------------------------------------------- */
int main ( void )
{
int choix, n, a , b, encore, i, j ;
float x ;
do {
choix = choisir() ;
switch ( choix ) {
case FIBO :
do {
printf ( "Donner une valeur entiere >= 0 : " ) ;
scanf ( "%d" , &n ) ;
} while ( n < 0 ) ;
printf( "fibo( %d ) vaut %ld\n" , n , fibo ( n ) ) ;
break ;
case EXPO :
printf ( "Donner une valeur pour la base : " ) ;
scanf ( "%f" , & x ) ;
do {
printf ( "Donner la valeur de l'exposant : " ) ;
scanf ( "%d" , & n ) ;
} while ( n < 0 ) ;
printf( "expo( %f , %d ) vaut %f\n" , x , n , expo( x , n ) ) ;
break ;
case PGCD :
do {
printf ( "PGCD :
scanf ( "%d" , &
} while ( a < 0 ) ;
do {
printf ( "PGCD :
scanf ( "%d" , &
} while ( b < 0 || (
printf ( "pgcd( %d ,
break ;
Donner la premiere valeur : " ) ;
a ) ;
Donner la seconde valeur : " ) ;
b ) ;
a == 0 && b == 0 ) ) ;
%d ) vaut %d\n" , a , b , pgcd( a , b ) ) ;
case BIZARRE :
do {
printf ( "Donner un entier non nul ou 0 pour les nombres premiers inferieurs a 100 : " ) ;
scanf ( "%d" , & n ) ;
} while ( n < 0 ) ;
if ( n > 0 ) {
if ( bizarre (n) ) printf ( "%d est un nombre premier\n", n ) ;
else printf ( "%d n\'est pas un nombre premier\n", n ) ;
} else {
printf ("\nListe des nombres premiers inferieurs a 100 : \n") ;
j = 0 ;
for ( i = 1 ; i <= 100 ; i++) {
if ( bizarre (i) ) {
printf ("%5d", i) ;
j++ ;
if (!(j%10)) printf ("\n") ;
}
}
printf ("\n") ;
}
}
printf ( "Encore ? " ) ;
scanf ( "%d" , & encore ) ;
} while ( encore ) ;
printf ( "Bye !\n" ) ;
return( 0 ) ;
}

Documents pareils