/* Planets.c (c) Kari Laitinen http://www.naturalprogramming.com/ 2013-11-27 File created. 2013-11-27 Last modification. */ #include struct Planet { char planet_name[ 20 ] ; double mean_distance_from_sun ; double circulation_time_around_sun ; double rotation_time_around_own_axis ; long equatorial_radius_in_kilometers ; double mass_compared_to_earth ; int number_of_moons ; } ; // Much of the information given in the following table is // relative to the information concerning the Earth. E.g., // Pluto is 39.507 times further from the sun than the Earth. // The data concerning the number of moons may be old. struct Planet planet_table[] = { { "Mercury", 0.387, 0.241, 58.815, 2433, 0.05, 0 }, { "Venus", 0.723, 0.615, 224.588, 6053, 0.82, 0 }, { "Earth", 1.000, 1.000, 1.000, 6379, 1.00, 1 }, { "Mars", 1.523, 1.881, 1.029, 3386, 0.11, 2 }, { "Jupiter", 5.203, 11.861, 0.411, 71370, 317.93, 16 }, { "Saturn", 9.541, 29.457, 0.428, 60369, 95.07, 18 }, { "Uranus", 19.190, 84.001, 0.450, 24045, 14.52, 5 }, { "Neptune", 30.086, 164.784, 0.657, 22716, 17.18, 2 }, { "Pluto", 39.507, 248.35, 6.410, 5700, 0.18, 0 } } ; int main() { struct Planet* planet_in_table ; char planet_name_from_user[ 20 ] ; printf( "\n This program can give you information about the" "\n planets in our solar system. Give a planet name: " ) ; gets( planet_name_from_user ) ; planet_in_table = planet_table ; // Memory addresses are converted to type int in the following // complex boolean expression. That was required in order to make // this program work with the MinGW C++ compiler. // It seems that pointer arithmetics work differently in different // compilers. Again, a very good reason not to use pointers in programs. while ( ( (int) planet_in_table < (int) planet_table + sizeof( planet_table ) ) && ! ( strstr( planet_in_table -> planet_name, planet_name_from_user ) ) ) { planet_in_table ++ ; } if ( strstr( planet_in_table -> planet_name, planet_name_from_user ) ) { printf( "\n %s orbits the sun at an average distance of %.3f kilometers." "\n It takes %.3f years for %s to go around the Sun once." "\n The radius of %s is %d kilometers.\n", planet_in_table -> planet_name, ( planet_in_table -> mean_distance_from_sun ) * 149500000, planet_in_table -> circulation_time_around_sun, planet_in_table -> planet_name, planet_in_table -> planet_name, planet_in_table -> equatorial_radius_in_kilometers ) ; } else { printf( "\n Sorry, could not find information on %s.\n", planet_name_from_user ) ; } }