Record – Ejemplo 2

Enunciado

Se quiere escribir un procedimiento que lea características de distintas figuras geométricas calcule, almacene en memoria y devuelva la superficie correspondiente. Suponer las siguientes posibilidades :

Figura

Círculo

Paralelogramo

Cuadrado

Rectángulo

Triángulo

Rombo

Trapecio

Datos de Entrada

radio r

base b , altura h

lado l

base b , altura h

base b , altura h

diagonales d1 y d2

bases b1 y b2 , altura h

Superficie

π r 2

b * h

l * l

b * h

b * h / 2

d1 * d2 / 2

( b1 + b2 ) / 2

Código

Python

def MuestraMenu():
    print( " 1 << Circulo\n" ) ;
    print( " 2 << Paralelogramo\n" ) ;
    print( " 3 << Cuadrado\n" ) ;
    print( " 4 << Rectangulo\n" ) ;
    print( " 5 << Triangulo\n" ) ;
    print( " 6 << Rombo\n" ) ;
    print( " 7 << Trapecio\n" ) ;
   
    opcion = int(input( "Ingresar opcion: " ))
    while opcion < 1 or opcion > 7:
        opcion = int(input("Error-Ingresar opcion: "))         
    return opcion


def MuestraResultados( datos ):
    print( "nDatos de la figura:n" )
    
    if datos[0]=="circulo":
        print( "Circulo de radio:", datos[1] )
        print( "Superficie:", datos[2] )

    elif datos[0]=="paralelogramo":
        print( "Paralelogramo de base:", datos[1] )
        print( "y altura:", datos[2] )
        print( "Superficie:", datos[3] )

    elif datos[0]=="cuadrado":
        print( "Cuadrado de lado:", datos[1] )
        print( "Superficie:", datos[2] )

    elif datos[0]=="rectangulo":
        print( "Rectangulo de base:", datos[1] )
        print( "y altura:", datos[2] )
        print( "Superficie:", datos[3] )    

    elif datos[0]=="triangulo":
        print( "Triangulo de base:", datos[1] )
        print( "y altura:", datos[2] )
        print( "Superficie:", datos[3] )
                              
    elif datos[0]=="rombo":
        print( "Rombo de diagonales:", datos[1] )
        print( "y", datos[2] )
        print( "Superficie:", datos[3] ) 
        
    elif datos[0]=="trapecio":
        print( "Trapecio de bases:", datos[1] )
        print( "y", datos[2], "y altura:", datos[3] )
        print( "Superficie:", datos[4] )

def Calcula( opcion ):
    if opcion==1:
        forma = "circulo"
        r = int( input( "Ingresar radio " ) )
        superficie = 3.1416 * r ** 2
        
        datos = [ forma, r, superficie ]

    elif opcion==2:
        forma = "paralelogramo"
        bp = int( input( "Ingresar base: " ) )
        hp = int( input( "Ingresar altura: " ) )
        superficie = bp * hp
        
        datos = [ forma, bp, hp, superficie ]

    elif opcion==3:
        forma = "cuadrado"
        l = int( input( "Ingresar lado " ) )
        superficie = l ** 2
        
        datos = [ forma, l, superficie ]

    elif opcion==4:
        forma = "rectangulo"
        br = int( input( "Ingresar base: " ) )
        hr = int( input( "Ingresar altura: " ) )
        superficie = br * hr
        
        datos = [ forma, br, hr, superficie ]

    elif opcion==5:
        forma = "triangulo"
        bt = int( input( "Ingresar base: " ) )
        ht = int( input( "Ingresar altura: " ) )
        superficie = ( bt * ht ) / 2
        
        datos = [ forma, bt, ht, superficie ]

    elif opcion==6:
        forma = "rombo"
        d1 = int( input( "Ingresar diagonal d1 : " ) )
        d2 = int( input( "Ingresar diagonal d2 : " ) )
        superficie = ( d1 * d2 ) / 2
        
        datos = [ forma, d1, d2, superficie ]

    elif opcion==7:
        forma = "trapecio"
        b1 = int( input( "Ingresar base 1: " ) )
        b2 = int( input( "Ingresar base 2: " ) )
        h = int( input( "Ingresar altura: " ) )
        superficie = h * ( b1 + b2 ) / 2
        
        datos = [ forma, b1, b2, h, superficie ]
        
    return datos
   
def main():
    while True:
        opcion = MuestraMenu()
        datos = Calcula( opcion )
        MuestraResultados( datos )
         
        print( "\nPresionar S para salir", end="" )
        print( " o cualquier tecla para continuar: " )
        resp = input( ).upper()
        if resp == "S": break
main()

C++

#include <iostream>
#include <iomanip>
using namespace std ;
#include <conio.h>
#include <math.h>
#include <stdio.h>

enum format { circulo , paralelogramo , cuadrado , 
	      rectangulo , triangulo , 
	      rombo , trapecio } ;
format forma;

struct DatosFigura
{
   float superficie ;
   float r ;
   float bp , hp ;
   float l ;
   float br , hr ;
   float bt , ht ;
   float d1, d2 ;
   float b1 , b2 , h ;
};

void MuestraMenu ( int &opcion  )
{
   cout << " 1 << Circulo" << endl ;
   cout << " 2 << Paralelogramo" << endl ;
   cout << " 3 << Cuadrado" << endl ;
   cout << " 4 << Rectangulo" << endl ;
   cout << " 5 << Triangulo" << endl ;
   cout << " 6 << Rombo" << endl ;
   cout << " 7 << Trapecio" << endl ;
   do
   {
      cout << "n Ingresar opcion:  " ;
      cin >> opcion ;
   }
   while ( opcion<1 || opcion>7 ) ;
}

void MuestraResultados( DatosFigura &f )
{
   cout << endl << "Datos de la figura: " << endl ;
   switch ( forma )
   {
      case circulo : cout << "Circulo de radio: " 
                     << f.r  << endl << " Superficie: " 
                     <<  f.superficie << endl;
   		     break ;
      case paralelogramo : 
                     cout << "Paralelogramo de base: " 
                     << f.bp << " y altura: "<< f.hp 
                     << endl << "   Superficie: "
                     << f.superficie << endl ;  
		     break ;
      case cuadrado : 
                     cout << "Cuadrado de lado: " 
                     << f.l << endl << " Superficie: " 
                     << f.superficie << endl ;
		     break ;
      case rectangulo : 
                     cout << "Rectangulo de base: " 
                     << f.br << " y altura : "
                     << f.hr << endl << " Superficie: "
                     << f.superficie << endl ; 	
		     break ;
      case triangulo : 
                     cout << "Triangulo de base: " 
                     << f.bt << " y altura : "
                     << f.ht << endl << " Superficie: " 
                     << f.superficie << endl ;
		     break ;
      case rombo :   cout << "Rombo de diagonales: " 
                     << f.d1 << " y " << f.d2 << endl 
                     << " Superficie: " 
                     << f.superficie << endl ;  
		     break ;
      case trapecio : 
                     cout << "Trapecio bases: " 
                     << f.b1 << " y " << f.b2 
                     << " y altura: " << f.h << endl 
                     << " Superficie: " << f.superficie 
                     << endl ;  
		     break ;
   }
}

void Calcula( int opcion , DatosFigura &f )
{
   switch (opcion)
   {
      case 1 : forma = circulo ;
	       cout << "Ingresar radio: " ; 
               cin >> f.r ;
	       f.superficie = M_PI * pow( f.r , 2 ) ;
	       break ;
      case 2 : forma = paralelogramo ;
	       cout << "Ingresar base: " ; 
               cin >> f.bp ;
	       cout << "Ingresar altura: " ; 
               cin >> f.hp ;
	       f.superficie = f.bp * f.hp ;
	       break ;
      case 3 : forma = cuadrado ;
	       cout << "Ingresar lado " ; 
               cin >> f.l ;
	       f.superficie = pow( f.l , 2 ) ;
	       break ;
      case 4 : forma = rectangulo ;
	       cout << "Ingresar base: " ; 
               cin >> f.br ;
               cout << "Ingresar altura: " ; 
               cin >> f.hr ;
	       f.superficie = f.br * f.hr ;
	       break ;
      case 5 : forma = triangulo ;
	       cout << "Ingresar base: "  ; 
               cin >> f.bt ;
	       cout << "Ingresar altura: " ; 
               cin >> f.ht ;
	       f.superficie = ( f.bt * f.ht ) / 2 ;
	       break ;
      case 6 : forma = rombo ;
	       cout << "Ingresar diagonal d1 : " ; 
               cin >> f.d1 ;
	       cout << "Ingresar diagonal d2 : " ; 
               cin >> f.d2 ;
	       f.superficie = ( f.d1 * f.d2 ) / 2 ;
	       break ;
      case 7 : forma = trapecio ;
	       cout << "Ingresar base 1: " ; 
               cin >> f.b1 ;
	       cout << "Ingresar base 2: " ; 
               cin >> f.b2 ;
	       cout << "Ingresar altura: " ; 
               cin >> f.h ;
	       f.superficie = f.h*( f.b1+f.b2 )/2  ;
	       break ;
   }
}

int main()
{
   DatosFigura f ;
   int opcion ;

   do
   {
       MuestraMenu( opcion ) ;
       Calcula( opcion , f ) ;
       MuestraResultados( f ) ;
       cout << endl << endl ;
       cout << " Presionar ESC para salir" << endl ;
    }
    while (getch() != char (27));
    return 0 ;
}

Pascal

Program Problema19_5 ;
uses CRT ;
const pi = 3.141592 ;
type
   Figura = ( circulo , paralelogramo , cuadrado , 
	      rectangulo , triangulo , rombo , 
              trapecio ) ;
   DatosFigura = 
	record
		superficie : real ;
		case forma : figura of
		circulo  : ( r : real ) ;
		paralelogramo : ( bp , hp : real ) ;
		cuadrado : ( l : real ) ;
		rectangulo : ( br , hr : real ) ;
		triangulo : ( bt , ht : real ) ;
		rombo : ( d1, d2 : real ) ;
		trapecio : ( b1 , b2 , h : real )
	end ;
var
    f : DatosFigura ;
    opcion : shortint ;

Procedure MuestraMenu ( var opcion : shortint ) ;
begin
    ClrScr ;
    writeln( ' 1 << Circulo ' ) ;
    writeln( ' 2 << Paralelogramo ' ) ;
    writeln( ' 3 << Cuadrado ' ) ;
    writeln( ' 4 << Rectangulo ' ) ;
    writeln( ' 5 << Triangulo ' ) ;
    writeln( ' 6 << Rombo ' ) ;
    writeln( ' 7 << Trapecio ' ) ;
    repeat
	write( 'Ingresar opcion :' ) ; 
	readln( opcion ) ;
    until ( opcion>=1) and ( opcion<=7 )
end ;

Procedure MuestraResultados( f : DatosFigura ) ;
begin
    writeln( ' Datos de la figura ' ) ;
    with f do
    begin
	case forma of
	    circulo :
		writeln( 'Circulo de radio : ' , 
		          r:0:2 , ' Superficie : ' ,  
                          superficie:0:2 ) ;
	    paralelogramo :
		writeln( 'Paralelogramo de base : ' , 
			  bp:0:2 , ' y altura : ' , 
                          hp:0:2 , ' Superficie : ' , 
                          superficie:0:2 ) ;
	    cuadrado :
		writeln( 'Cuadrado de lado : ' 
                          , l:0:2 , ' Superficie : ' ,
                          superficie:0:2 ) ;
	    rectangulo :
		writeln( 'Rectangulo de base : ' , 
			  br:0:2 , ' y altura : ' , 
                          hr:0:2 , ' Superficie : ' , 
                          superficie:0:2 ) ;
	    triangulo :
		writeln( 'Triangulo de base : ' ,
			  bt:0:2 , ' y altura : ' , 
                          ht:0:2 , ' Superficie : ' , 
                          superficie:0:2 ) ;
	    rombo :
		writeln( 'Rombo de diagonales : ' , 
			  d1:0:2 , ' y ' , d2:0:2 ,
			  ' Superficie : ' , 
                          superficie:0:2 ) ;
	    trapecio :
		writeln( 'Trapecio de bases : ' , 
			  b1:0:2 , ' y ' , b2:0:2 , 
                          ' y altura : ' , h:0:2 , ' 
                          Superficie : ' , 
                          superficie:0:2 ) ;
	end { case }
    end { with }
end ;

Procedure Calcula( opcion : shortint ; 
                   var f : DatosFigura ) ;
begin
    case opcion of
	1 : begin
		f.forma := circulo ;
		write( 'Ingresar radio : ' ) ; 
		readln( f.r ) ;
		f.superficie := pi * sqr( f.r )
	     end ;
	 2 : begin
		f.forma := paralelogramo ;
		write( 'Ingresar base : ' ) ; 
		readln( f.bp ) ;
		write( 'Ingresar altura : ' ) ; 
		readln( f.hp ) ;
		f.superficie := f.bp * f.hp
	     end ;
	 3 : begin
		f.forma := cuadrado ;
		write( 'Ingresar lado : ' ) ; 
		readln( f.l ) ;
		f.superficie := sqr( f.l )
	     end ;
	 4 : begin
		f.forma := rectangulo ;
		write( 'Ingresar base : ' ) ; 
		readln( f.br ) ;
		write( 'Ingresar altura : ' ) ; 
		readln( f.hr ) ;
			.superficie := f.br * f.hr
	     end ;
	 5 : begin
		f.forma := triangulo ;
		write( 'Ingresar base : ' ) ; 
		readln( f.bt ) ;
		write( 'Ingresar altura : ' ) ; 
		readln( f.ht ) ;
		f.superficie := ( f.bt * f.ht ) / 2
	     end ;
	 6 : begin
		f.forma := rombo ;
		write( 'Ingresar diagonal d1 : ' ) ; 
		readln( f.d1 ) ;
		write( 'Ingresar diagonal d2 : ' ) ; 
		readln( f.d2 ) ;
		f.superficie := ( f.d1 * f.d2 ) / 2
	     end ;
	 7 : begin
		f.forma := trapecio ;
		write( 'Ingresar base 1 : ' ) ; 
		readln( f.b1 ) ;
		write( 'Ingresar base 2 : ' ) ; 
		readln( f.b2 ) ;
		write( 'Ingresar altura : ' ) ; 
		readln( f.h ) ;
		f.superficie := f.h * ( f.b1 + f.b2 )/2
	      end
    end
end ;

begin
    repeat
	 ClrScr ;
	 MuestraMenu( opcion ) ;
	 Calcula( opcion , f ) ;
	 MuestraResultados( f ) ;
	 write( 'Presione ESC para salir ' ) ;
	 write( 'o cualquier tecla para ' ) ;     
         writeln( 'continuar ' ) ;
    until ReadKey = #27
end.

Diagramas

Subprogramas

Diagrama Principal