This is a quick practice program I did this morning.
You are loading a ferry and need to keep it in balance. This program determines if the number of cars are even or odd and balances the boat for you.
My question is: How is the style? Is it logical in it's structure? Is there any advice you have to improve the program?
Here is the code.
Code:
/* Ferry boat loading program
*
* File: main.c
* Author: James Tuvell
*
* Created on May 29, 2010, 9:24 AM
* This program is practice.
*
* In it the user will input a number up to 9
* and the program will determine whether it is
* even or odd number and print out a boat loading
* layout that keeps the boat balanced
*/
#include <stdio.h>
#include <stdlib.h>
/*
*Define Constants
*/
#define MinCars '0'
#define MaxCars '9'
#define EVEN 0
#define ODD 1
#define CURSOR ">>"
#define ASCII 48
// Define the boat
#define BowTip " ^"
#define Bow1 " / \\"
#define Bow2 " / \\"
#define Bow3 " / \\"
#define Bow_WCar " / x \\"
#define Bow_WOCar " / \\"
#define Bow4 " / \\"
#define Middle_First " | *** |"
#define Middle_Wcar " | x | | x |"
#define Middle_WOCar " | | | |"
#define Middle_Last " | *** |"
#define Aft1 " \\ /"
#define Aft2 " \\_______/"
#define Ramp " | | "
void Print_Instructions (void);
int EvenOdd ( int number );
int UserInput (void);
void ClearBuff (void);
void PrintBoat (int numb, int odd);
void PrintBow ( int cars , int odd);
void PrintMid ( int cars);
void PrintAft (void);
void PrintLine (void);
/*------------------------------------------------------------
* main
* Parameters: NONE
* Gets : UserInput
* Prints : Boat balancing layout
* Returns : Nothing
*------------------------------------------------------------*/
int main(void) {
int rvalue;
int numb;
Print_Instructions ();
numb = UserInput();
rvalue = EvenOdd (numb);
PrintBoat (numb,rvalue);
return 0;
}
/*------------------------------------------------------------
* Print Instructions
* Parameters: NONE
* Prints : The Instructions
* Returns : Nothing
*------------------------------------------------------------*/
void Print_Instructions (void){
PrintLine();
printf ("This is the automatic boat loader");
PrintLine();
printf ("Just input the number of cars to load");
PrintLine();
printf ("and I will print out a loading chart");
PrintLine();
printf ("for you to use to keep the boat in balance");
PrintLine();
}
/*------------------------------------------------------------
* EvenOdd
* Parameters:
* number = then number to check wether even or odd
* Prints : Nothing
* Returns : EVEN or ODD ( 0 or 1)
*------------------------------------------------------------*/
int EvenOdd ( int number ){
int even;
even = number % 2;
if ( even == EVEN ) return EVEN;
return ODD;
}
/*------------------------------------------------------------
* UserInput
* Parameters: NONE
* Prints : Nothing
* Returns : A number between 0 and 9
*------------------------------------------------------------*/
int UserInput (void){
int input;
input = MinCars;
printf ("Enter the number of cars to load (0-9)\n");
do{
printf ( CURSOR );
input = getchar();
ClearBuff ();
}
while ( input < MinCars || input > MaxCars);
return input-ASCII;
}
/*------------------------------------------------------------
* ClearBuff
* clears the stdin of extra stuff
* Parameters: NONE
* Prints : Nothing
* Returns : Nothing
*------------------------------------------------------------*/
void ClearBuff (void){
while (getchar() != '\n');
}
/*------------------------------------------------------------
* Print Boat
* Parameters:
* numb = number of cars to load
* odd = is the number of cars odd
* Prints : The boat using calls to Printbow,PrintMid and PrintAft
* Returns : Nothing
*------------------------------------------------------------*/
void PrintBoat (int numb, int odd){
PrintBow (numb , odd);
if ( odd == ODD ) PrintMid (numb-1);
else PrintMid (numb);
PrintAft ();
}
/*------------------------------------------------------------
* PrintBow
* Parameters:
* cars = number of cars to load
* odd = is that value odd
* Prints : The bow of the boat checking on if a car should go upfront
* Returns : Nothing
*------------------------------------------------------------*/
void PrintBow ( int cars, int odd){
int load;
if (odd == ODD){
load = cars -1;
printf (BowTip);
PrintLine();
printf (Bow1);
PrintLine();
printf (Bow2);
PrintLine();
printf (Bow3);
PrintLine();
printf (Bow_WCar);
PrintLine();
printf (Bow4);
PrintLine();
}
else{
printf (BowTip);
PrintLine();
printf (Bow1);
PrintLine();
printf (Bow2);
PrintLine();
printf (Bow3);
PrintLine();
printf (Bow_WOCar);
PrintLine();
printf (Bow4);
PrintLine();
}
}
/*------------------------------------------------------------
* PrintMid
* Parameters:
* cars = the even number of cars to load (2,4,6,8)
* Prints : The middle of the boat
* Returns : Nothing
*------------------------------------------------------------*/
void PrintMid ( int cars){
printf (Middle_First);
PrintLine();
if ( cars < 6 ) printf (Middle_WOCar);
else printf (Middle_Wcar);
PrintLine();
if ( cars > 0) printf (Middle_Wcar);
else printf (Middle_WOCar);
PrintLine();
if ( cars > 2 && cars != 6) printf (Middle_Wcar);
else printf (Middle_WOCar);
PrintLine();
if ( cars == 6 || cars == 8) printf (Middle_Wcar);
else printf (Middle_WOCar);
PrintLine();
printf (Middle_Last);
PrintLine();
}
/*------------------------------------------------------------
* PrintLine
* Parameters: NONE
* Prints : A line feed to make things more readable
* Returns : Nothing
*------------------------------------------------------------*/
void PrintLine (void){
printf("\n");
}
/*------------------------------------------------------------
* PrintAft
* Parameters: NONE
* Prints : The aft of the boat
* Returns : Nothing
*------------------------------------------------------------*/
void PrintAft (void){
printf (Aft1);
PrintLine();
printf (Aft2);
PrintLine();
printf (Ramp);
PrintLine();
}