/* 

  AREA4.C : A C program for calculating areas of
            various shapes.

  Barry McMullin
  2nd February 1996.

*/

#include <safe-c.h>

void put_floating_point(floating_point_type number)
{
  string_type s;

  floating_point_to_string(s, number);
  put_string(s);
}

floating_point_type get_floating_point(void)
{
  string_type s;

  get_string(s);
  return (string_to_floating_point(s));
}

floating_point_type power(floating_point_type x, integer_type n)
{
 floating_point_type c;
 integer_type a;
 boolean_type d;

 c = 1;
 a = 0;
 d = TRUE;
 while(d)
 {
  a=a+1;
  c = (c*x);

  if (a==n)
  {
   d=FALSE;
  }
  
 }
 return(c);
}



floating_point_type square(floating_point_type x)
{
  return(power(x, 2));
}

floating_point_type do_square(void)
{
  floating_point_type side;

  put_string("Please enter side of square: ");
  side = get_floating_point();
 
  return(square(side));
}

floating_point_type do_triangle(void)
{
  floating_point_type height, base;

  put_string("Please enter triangle height: ");
  height = get_floating_point();
  put_string("Please enter triangle base: ");
  base = get_floating_point();

  return(0.5 * base * height);
}

floating_point_type do_rectangle(void)
{
  floating_point_type height,length;

  put_string("Please enter rectangle height: ");
  height = get_floating_point();
  put_string("Please enter rectangle length: ");
  length = get_floating_point();

  return(length * height);
}

floating_point_type do_circle(void)
{
  floating_point_type radius;

  put_string("Please enter circle radius: ");
  radius = get_floating_point();

  return(3.142 * square(radius));
}

void main(void)
{
  string_type shape;
  floating_point_type area;
  boolean_type valid;

  put_string("Welcome to AREA4.\n");

  put_string("Please enter one of the following shapes:\n");
  put_string("  Triangle\n");
  put_string("  Square\n");
  put_string("  Rectangle\n");
  put_string("  Circle\n\n");

  put_string("Shape: ");
  get_string(shape);

  valid = TRUE;
  if (string_equal(shape, "Triangle\n"))
    area = do_triangle();
  else
    if (string_equal(shape, "Square\n"))
      area = do_square(); 
    else
      if (string_equal(shape, "Rectangle\n"))
        area = do_rectangle();
      else
       if(string_equal(shape,"Circle\n"))
         area = do_circle();
       else
         valid = FALSE;

  if (valid)
  {
    put_string("The area is: ");
    put_floating_point(area);
    put_string("\n\n");
  }
  else
    put_string("ILLEGAL SHAPE ENTERED");
 
  put_string("\n\nBye from AREA4!\n");
}
