Comments | Online Safety | Help
     
 

Don't ask what the program does as it was for what I did at my previous employer. It's just a kind of calculator, read the code and you'll see what it does.

/*****************************************************************
Mr L Meakin - Borland C++ 4.52 - DOS based project.
*** Will cause a general protection error with win project.
    
Panel Shot Times Calculator for ---------------------------.
   Date - 27.04.1996
   Date - 27.01.2001 - Changes made -
     Local variables that were not needed have been removed.
     Graphic intro screen has been removed.
     Formal parameter names changed to make code readable.
     Global variables removed.
     Pointers are now used for some function calls.
   Date - 28.01.01 - Changes made -
     scanf() replaced with a secure input function get_panel().
     ***get_panel causes a general protection error if program
     is not compiled as a dos project.
   Date - 07.02.01 - Changes made -
     Structure replaces all those nasty float variables.
     All functions updated to use structure pointer.
     New Shot 2 times calculation and new lance length
     and new Shot 1 times to keep with new lance lengths.

   This code is copyright to Mr L Meakin.
   It may be freely distributed as long as this message remains
   with any part of the code. http://www.incubus.co.uk
******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h> /* atof() */
#include <conio.h> /* DOS only header used for getch()*/
#include <ctype.h> /* toupper() */

#define TRUE 1
#define FALSE 0
#define MAX_INPUT 6 /* Needed for get_panel() */

struct panel {
    float size ,
           shot2 ,
           shot1 ,
           lance ;
};

int continue_function (void);
void calc_lance (struct panel *panel_STR);
void calc_shot_1 (struct panel *panel_STR);
void calc_shot_2 (struct panel *panel_STR);
void display_times (struct panel *panel_STR);
void get_panel (struct panel *panel_STR);

int main (void)
{  struct panel new_panel,
                    *panel_ptr;

  panel_ptr = &new_panel;

  clrscr(); /*DOS only function */
  printf("%17c Welcome to the Panel Shot Time Calculator.\n",' ');
  printf("%17c __________________________________________\n",' ');

  while (continue_function())
  {
    get_panel (panel_ptr);
    calc_lance (panel_ptr);
    calc_shot_1 (panel_ptr);
    calc_shot_2 (panel_ptr);
    display_times (panel_ptr);
  }
  return EXIT_SUCCESS;
}

void get_panel (struct panel *panel_STR)
{
  int counter = 0;
  char input[MAX_INPUT + 1] = "00000";

  printf("\n\nPlease enter panel length below -\n");

  do
  {
    panel_STR->size = 0.0;
    printf("\r < Input between must be between 0.600 & 7.800\r");
    printf("\r\b\b\b\b\b\r"); /* Clear line on incorrect input */

    for (counter = 0 ; (counter < MAX_INPUT && counter >= 0) ; counter++ )
    {
      input[counter] = getche(); /* DOS only function */

      switch (input[counter])
      {
        case '\b': //Overwrites previous char if backspace pressed.
                printf(" \b");
                counter -= 2;
                break;

        case '\r': //On 'Enter Key' exit for loop.
                counter = MAX_INPUT;
                break;

        default: //Get input while incorrect is keyed in.
                while( input[counter] < '0' || input[counter] > '9')
                {
                        if (input[counter] == '.')
                                //Accept decimal point.
                                break;
                        else
                        {
                                //Overwrites incorrect input on screen.
                                printf("\b \b");
                                counter--;
                                break;
                        }
                }
        }
    }
    input[counter+1] = '\0'; /*Append null to end of string*/
    panel_STR->size = (float)(atof(input));

  } while ( panel_STR->size < 0.600 || panel_STR->size > 7.800 );
}

int continue_function (void)
{
  printf("\n%24c Do you wish to continue (Y/N)? ",' ');

  if(toupper(getch()) == 'Y')
    return (TRUE);
  else
    return (FALSE);
}

void display_times (struct panel *panel_STR)
{
  printf("\n\n");
  printf("%12c =============================================\n",' ');
  printf("%12c | Lance (m) | Shot 1 (sec) | Shot 2 (sec) |\n",' ');
  printf("%12c =============================================\n",' ');
  printf("%14c %.3f | %.2f | %.2f \n",' ',
  panel_STR->lance, panel_STR->shot1, panel_STR->shot2);
  printf("%12c =============================================\n",' ');
}

void calc_shot_1 (struct panel *panel_STR)
{
  /* Lance lengths = 1.5m & 2.0m & 3.0m */
  /* Shot 1 times = 0.50 & 1.20 & 1.60 */

  if(panel_STR->lance <= 1.500)
    panel_STR->shot1 = 00.50;
  else if(panel_STR->lance <= 2.000)
    panel_STR->shot1 = 01.20;
  else
    panel_STR->shot1 = 01.60;
}

void calc_shot_2 (struct panel *panel_STR)
{
  /* New Shot 2 times as of 07.02.2001 */
  if ( panel_STR->size < 3.000 )
    panel_STR->shot2 = (panel_STR->size * 01.35 );
  else if ( panel_STR->size < 3.500 )
    panel_STR->shot2 = (panel_STR->size * 01.40 );
  else if ( panel_STR->size < 4.000 )
    panel_STR->shot2 = (panel_STR->size * 01.43 );
  else if ( panel_STR->size < 5.000 )
    panel_STR->shot2 = (panel_STR->size * 01.45 );
  else if ( panel_STR->size < 6.000 )
    panel_STR->shot2 = (panel_STR->size * 01.55 );
  else if ( panel_STR->size < 7.000 )
    panel_STR->shot2 = (panel_STR->size * 01.65 );
  else if ( panel_STR->size < 7.801 )
    panel_STR->shot2 = (panel_STR->size * 01.75 );
}

void calc_lance (struct panel *panel_STR)
{
  if (panel_STR->size < .800)
    panel_STR->lance = 0.400;
  else if (panel_STR->size <= 1.000)
    panel_STR->lance = 0.500;
  else if (panel_STR->size <= 2.000)
    panel_STR->lance = 0.700;
  else if (panel_STR->size <= 2.700)
    panel_STR->lance = 0.800;
  else if (panel_STR->size <= 3.000)
    panel_STR->lance = 0.900;
  else if (panel_STR->size <= 3.500)
    panel_STR->lance = 0.900;
  else if (panel_STR->size <= 4.000)
    panel_STR->lance = 1.200;
  else if (panel_STR->size <= 4.500)
    panel_STR->lance = 1.300;
  else if (panel_STR->size <= 4.750)
    panel_STR->lance = 1.400;
  else if (panel_STR->size <= 5.000)
    panel_STR->lance = 1.800;
  else if (panel_STR->size <= 5.500)
    panel_STR->lance = 2.000;
  else if (panel_STR->size <= 6.000)
    panel_STR->lance = 2.300;
  else if (panel_STR->size <= 7.000)
    panel_STR->lance = 2.600;
  else if (panel_STR->size > 7.000 )
    panel_STR->lance = 2.800;
}