/* 
 * Example of function with a structured output argument
 */ 
 
#include <stdio.h> 
#define STRSIZE 20 

typedef struct{
    char name[STRSIZE]; 
    double diameter;      // equatorial diameter in km 
    int moons;            // number of moons 
    double orbit_time,    // years to orbit sun once 
           rotation_time; // hours to complete one revolution on axis 
} planet_t;  

int scan_planet(planet_t *plnp); 
void print_planet(planet_t pl); 
planet_t  get_planet();
                           
int
main()
{
    planet_t planet1; 
    int status; 

    status = scan_planet(&planet1);  

    if (status == 1)
        print_planet(planet1); 
    else 
        printf("error encountered in scan_plane"); 

    planet1 = get_planet();
    print_planet(planet1);
    return 0; 
}  

/*
 * Fills a type planet_t structure with input data. 
 * Integer returned as function result is success/failure/EOF indicator.
 *     1 => successful input of one planet
 *     0 => error encountered
 *     EOF => insufficient data before end of file
 * In case of error or EOF, value of type planet_t output argument is 
 * undefined.
 */

int
scan_planet(planet_t *plnp) /* output-address of planet_t structure to fill*/
{
    int result;
 
    result = scanf("%s%lf%d%lf%lf", (*plnp).name,
                                    &(*plnp).diameter,
                                    &(*plnp).moons,
                                    &(*plnp).orbit_time,
                                    &(*plnp).rotation_time);
    if (result == 5)
       result = 1;
    else if (result != EOF)
       result = 0;
 
    return (result);
}

/*
 * Example of function with a structured input parameter
 * 
 * Displays with labels all components of a planet_t structure
 */

void
print_planet(planet_t pl) /* input - one planet structure */
{
    printf("Planet name: %s\n", pl.name);
    printf("Equatorial diameter: %.0f km\n", pl.diameter);
    printf("Number of moons: %d\n", pl.moons);
    printf("Time to complete one orbit of the sun: %.2f years\n",
            pl.orbit_time);
    printf("Time to complete one rotation on axis: %.4f hours\n",
            pl.rotation_time);
}

planet_t
get_planet() /* input - one planet structure */
{
  planet_t result;
 
  scanf("%s%lf%d%lf%lf", result.name,
		 &result.diameter,
		 &result.moons,
		 &result.orbit_time,
		 &result.rotation_time);
    
  return (result);
  
}
