/*
 * Conversion between Celsius and Fahrenheit
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "routines.c"
float get_data(void);
float CtoF(float);
float FtoC(float);
void wait(void);

int exit_status;  /* 0: normal; 1: back to menu */

int main(void)

{
  char ch = 0;
  float temp;

  while (ch != 27)
  {
    clrscr();
    printf("Celsius <-> Fahrenheit Convertor\n\n");
    printf("Pick an option:\n\n");
    printf("[a] From C to F\n");
    printf("[b] From F to C\n");
    printf("<Esc> to Quit\n\n");
    ch = toupper(getch());
    exit_status = 0;

    switch (ch)
    {
      case 'A':
      {
	printf("Enter a Celsius temperature: ");
	temp = get_data();
	if (exit_status == 0)
	{
	  printf("\n\n%.1f degree C equals %.1f degree F", temp, CtoF(temp));
	  wait();
	}
	break;
      }
      case 'B':
      {
	printf("Enter a Fahrenheit temperature: ");
	temp = get_data();
	if (exit_status == 0)
	{
	  printf("\n\n%.1f degree F equals %.1f degree C", temp, FtoC(temp));
	  wait();
	}
	break;
      }
      case 27:     /* ESC entered */
	printf("Programming by Allen Lam, April 1999\n");
    }  /* switch */
  }    /* while  */
  return 0;
}

/***************************************************************
 *                         functions                           *
 ***************************************************************/



  float CtoF(float input_C)
  {
    float output;

    output = input_C * 9.0 / 5.0 + 32.0;
    return(output);
  }

  float FtoC(float input_F)
  {
    float output;

    output = (input_F - 32.0) * 5.0 / 9.0;
    return(output);
  }

  void wait(void)       /* wait for a keypress */
  {
    getch();
  }

back