The Smart schrieb:
Ihr könnt das Thema jetzt beenden.
Ich mach noch kurz C++ draus
#include "tools.hpp"
#include "circle.hpp"
#include "sphere.hpp"
enum do_t {
do_exit,
do_circle,
do_sphere
};
int main( ) {
int choice( do_exit );
double radius( 0 );
do {
std::cout << "Welche Figur moechten Sie berechnen?\n\n\t1:\tKreis\n\t2:\tKugel\n\n\t0:\tEnde\n" << std::endl;
choice = read_value< int >( std::cin, true, do_exit, do_sphere );
if( choice != do_exit ) {
std::cout << "\nBitte geben Sie einen Radius ein:" << std::endl;
radius = read_value< double >( std::cin );
}
std::cout.put( '\n' );
switch( choice ) {
case do_circle:
{
std::cout << circle_t( radius ) << std::endl;
break;
}
case do_sphere:
{
std::cout << sphere_t( radius ) << std::endl;
break;
}
case do_exit:
{
std::cout << "Das Programm wird beendet." << std::endl;
}
}
std::cout.put( '\n' );
} while( choice != do_exit );
}
tools.hpp
#ifndef TOOLS_INCLUDED
#define TOOLS_INCLUDED TOOLS_INCLUDED
#include <iostream>
template< typename T >
T read_value( std::istream &input_stream, bool range_check = false, T lower = T( ), T upper = T( ) ) {
T value = T( );
while( !( input_stream >> value ) || ( range_check && ( value < lower || upper < value ) ) ) {
std::cout << "Fehlerhafte Eingabe.\n\nBitte wiederholen:" << std::endl;
std::cin.clear( );
std::cin.ignore( std::cin.rdbuf( )->in_avail( ), '\n' );
}
return value;
}
#endif /* TOOLS_INCLUDED */
circle.hpp
#ifndef CIRCLE_INCLUDED
#define CIRCLE_INCLUDED KREIS_HPP_INCLUDED
#include <iostream>
class circle_t {
private:
double m_radius;
double m_area;
double m_outline;
public:
circle_t( double radius );
double get_radius( ) const;
double get_area( ) const;
double get_outline( ) const;
};
std::ostream& operator<<( std::ostream &output_stream, const circle_t &circle );
#endif /* CIRCLE_HPP_INCLUDED */
circle.cpp
#define _USE_MATH_DEFINES /* M_PI is a nonstandard extension */
#include <cmath>
#include "circle.hpp"
circle_t::circle_t( double radius ):
m_radius( radius ),
m_area( radius * radius * M_PI ),
m_outline( 2 * radius * M_PI )
{
}
double circle_t::get_radius( ) const
{
return m_radius;
}
double circle_t::get_area( ) const
{
return m_area;
}
double circle_t::get_outline( ) const
{
return m_outline;
}
std::ostream& operator<<( std::ostream &output_stream, const circle_t &circle ) {
return output_stream << "Der Umfang des Kreises betraegt " << circle.get_outline( ) << ",\ndie Flaeche " << circle.get_area( );
}
sphere.hpp
#ifndef SPHERE_INCLUDED
#define SPHERE_INCLUDED SPHERE_INCLUDED
#include <iostream>
class sphere_t {
private:
double m_radius;
double m_surface_area;
double m_volume;
public:
sphere_t( double radius );
double get_radius( ) const;
double get_surface_area( ) const;
double get_volume( ) const;
};
std::ostream& operator<<( std::ostream &output_stream, const sphere_t &sphere );
#endif /* SPHERE_INCLUDED */
sphere.cpp
#define _USE_MATH_DEFINES /* M_PI is a nonstandard extension */
#include <cmath>
#include "sphere.hpp"
sphere_t::sphere_t( double radius ):
m_radius( radius ),
m_surface_area( 4. * M_PI * radius * radius ),
m_volume( ( 4. / 3. ) * M_PI * radius * radius * radius )
{
}
double sphere_t::get_radius( ) const
{
return m_radius;
}
double sphere_t::get_surface_area( ) const
{
return m_surface_area;
}
double sphere_t::get_volume( ) const
{
return m_volume;
}
std::ostream& operator<<( std::ostream &output_stream, const sphere_t &sphere ) {
return output_stream << "Die Oberflaeche der Kugel betraegt " << sphere.get_surface_area( ) << ",\ndas Volumen " << sphere.get_volume( );
}
greetz, Swordfish