Added Homework 2 - May need some further changes
Might have to make some further changes later.
This commit is contained in:
parent
2fabde4bea
commit
7c8246c576
5 changed files with 1266 additions and 0 deletions
333
PA02-0914/Project_2.c
Normal file
333
PA02-0914/Project_2.c
Normal file
|
|
@ -0,0 +1,333 @@
|
|||
// header files
|
||||
#include "Console_IO_Utility.h"
|
||||
|
||||
// global constants
|
||||
const double ABSOLUTE_BASE = 0.0000001;
|
||||
const double BOOSTING_VALUE = 5.55;
|
||||
const double DRYWALL_PRICE = 3.00;
|
||||
const double PLYWOOD_PRICE = 3.125; // per square foot
|
||||
const double ROOFING_PRICE = 3.535525; // per square foot
|
||||
const double SIDE_WALL_RATIO = 0.67;
|
||||
const double SQUARE_ROOT_PRECISION = 0.000001;
|
||||
const double TWO_BY_FOUR_PRICE = 7.50; // per board
|
||||
|
||||
const int HOUSE_HEIGHT = 10;
|
||||
const int NUM_2x4_PER_FOOT = 6;
|
||||
const int TRIMMING_VALUE = 10;
|
||||
const int TWO_ENDLINES = 2;
|
||||
|
||||
// constants for table
|
||||
const int DESCRIPTION_WIDTH = 16;
|
||||
const int TABLE_WIDTH = 39;
|
||||
const int UNIT_WIDTH = 9;
|
||||
const int VALUE_WIDTH = 10;
|
||||
|
||||
const char THICK_LINE = '=';
|
||||
const char THIN_LINE = '-';
|
||||
|
||||
// prototypes
|
||||
double findAbsolute( int value );
|
||||
double findSquareRoot( double value );
|
||||
void printDividedLine( char thickness );
|
||||
void printHeavyDividedLine();
|
||||
void printLightDividedLine();
|
||||
void printTopLine();
|
||||
|
||||
// main program
|
||||
int main()
|
||||
{
|
||||
// Define Variables Needed: desiredArea (int) actualArea (int) houseWidth (int) houseDepth (int)
|
||||
// roofingCost (double) plywoodCost (double) twoByFourCost (double) drywallCost (double)
|
||||
// totalEstimate (double)
|
||||
int desiredArea, actualArea, houseWidth, houseDepth;
|
||||
double roofingCost, plywoodCost, twoByFourCost, drywallCost, totalEstimate;
|
||||
|
||||
// Print the house cost estimator headline
|
||||
printString("House Cost Estimator");
|
||||
printEndline();
|
||||
|
||||
// Print a dividing line, use printCharacters().
|
||||
printCharacters(20, '=');
|
||||
|
||||
// Print 2 blank lines for formatting nicely.
|
||||
printEndlines(2);
|
||||
|
||||
// Ask user for input about the desired floor sq.ft.-age.
|
||||
desiredArea = promptForInteger("Enter the desired floor square footage: ");
|
||||
|
||||
// Calculate the house width(2/3 rounded of the area) (formula: sqrt((desiredSQFT/0.67)))
|
||||
// then divide by trimmed value, cast to int, and then multiply by trimmed value to get result.
|
||||
houseWidth = ((findSquareRoot(desiredArea/SIDE_WALL_RATIO)) + BOOSTING_VALUE);
|
||||
houseWidth = houseWidth / TRIMMING_VALUE;
|
||||
houseWidth = (int) houseWidth * TRIMMING_VALUE;
|
||||
printEndline();
|
||||
// Calculate the house depth(1/3 rounded of the area) (formula: desiredArea/houseWidth)
|
||||
houseDepth = desiredArea / houseWidth;
|
||||
// Calculate the actual area (houseWidth * houseDepth)
|
||||
actualArea = houseWidth * houseDepth;
|
||||
|
||||
// Calculate the roofing cost(using the equation provided on BBLearn)
|
||||
roofingCost = actualArea * ROOFING_PRICE;
|
||||
// Calculate the plywood cost(sums up all the walls and floors of the house.)
|
||||
plywoodCost = ((houseWidth * HOUSE_HEIGHT) + (houseWidth * HOUSE_HEIGHT)) * PLYWOOD_PRICE;
|
||||
plywoodCost = plywoodCost + ((houseDepth * HOUSE_HEIGHT) + (houseDepth * HOUSE_HEIGHT)) * PLYWOOD_PRICE;
|
||||
plywoodCost = plywoodCost + (houseWidth * houseDepth) * PLYWOOD_PRICE;
|
||||
// Calculate the 2x4 cost(using the equation provided on BBLearn)
|
||||
twoByFourCost = (NUM_2x4_PER_FOOT * houseWidth);
|
||||
twoByFourCost = twoByFourCost + (NUM_2x4_PER_FOOT * houseDepth);
|
||||
twoByFourCost = twoByFourCost * TWO_BY_FOUR_PRICE;
|
||||
// Calculate the drywall cost(using the equation provided on BBLearn)
|
||||
drywallCost = (((houseWidth * HOUSE_HEIGHT)*2) + ((houseDepth * HOUSE_HEIGHT)*2)) * DRYWALL_PRICE;
|
||||
// Calculate the Total Estimate cost(add up using the previous values)
|
||||
totalEstimate = roofingCost + plywoodCost + twoByFourCost + drywallCost;
|
||||
|
||||
// Print a blank line for formatting.
|
||||
printEndline();
|
||||
|
||||
// Print a dividing line, use the printTopLine().
|
||||
printTopLine();
|
||||
printEndline();
|
||||
|
||||
// Print the table header. (Description, centered; Value, Centered; Unit, centered.)
|
||||
printCharacter(PIPE);
|
||||
printStringJustified("Description", DESCRIPTION_WIDTH, "CENTER");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified("Value", VALUE_WIDTH, "CENTER");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified("Unit", UNIT_WIDTH, "CENTER");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
|
||||
// Divider for the table header and data, using '='. uses printHeavyDividedLine()
|
||||
printHeavyDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the desired area
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" Desired Area", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printIntegerJustified(desiredArea, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" sq ft", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the actual area, using the houseWidth * houseDepth
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" Actual Area", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printIntegerJustified(actualArea, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" sq ft", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the house's width
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" House Width", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printIntegerJustified(houseWidth, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" feet", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the house's depth
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" House Depth", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printIntegerJustified(houseDepth, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" feet", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the roofing cost
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" Roofing Cost", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printDoubleJustified(roofingCost, PRECISION, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" dollars", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the plywood cost
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" Plywood Cost", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printDoubleJustified(plywoodCost, PRECISION, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" dollars", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the 2x4 cost
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" 2 X 4 Cost", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printDoubleJustified(twoByFourCost, PRECISION, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" dollars", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the drywall cost
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" Drywall Cost", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printDoubleJustified(drywallCost, PRECISION, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" dollars", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// Printed output of the Total Estimate
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" Total Estimate", DESCRIPTION_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printDoubleJustified(totalEstimate, PRECISION, VALUE_WIDTH, "RIGHT");
|
||||
printCharacter(PIPE);
|
||||
printStringJustified(" dollars", UNIT_WIDTH, "LEFT");
|
||||
printCharacter(PIPE);
|
||||
printEndline();
|
||||
printLightDividedLine();
|
||||
printEndline();
|
||||
|
||||
// End the table
|
||||
|
||||
// Print 2 blank lines for formatting.
|
||||
printEndlines(2);
|
||||
// Print end program
|
||||
printString("End Program");
|
||||
// Print a blank line for formatting.
|
||||
printEndline();
|
||||
|
||||
// return program success
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// supporting functions
|
||||
|
||||
double findAbsolute( int value )
|
||||
{
|
||||
// test for value less than negative precision
|
||||
if( value < -ABSOLUTE_BASE )
|
||||
{
|
||||
// return negated value
|
||||
return -value;
|
||||
}
|
||||
|
||||
// otherwise, assume positive value, return unchanged
|
||||
return value;
|
||||
}
|
||||
|
||||
double findSquareRoot( double value )
|
||||
{
|
||||
// initialize function/variables
|
||||
double testSquare, mid, high = value, low = 0;
|
||||
|
||||
// set initial mid value
|
||||
mid = ( high + low ) / 2.0;
|
||||
|
||||
// set initial test square value
|
||||
testSquare = mid * mid;
|
||||
|
||||
// loop while difference between test square and value
|
||||
// is greater than desired precision
|
||||
// function: findAbsolute
|
||||
while( findAbsolute( testSquare - value ) > SQUARE_ROOT_PRECISION )
|
||||
{
|
||||
// test for calculated value too high
|
||||
if( testSquare > value )
|
||||
{
|
||||
// change low to the mid value
|
||||
high = mid;
|
||||
}
|
||||
|
||||
// otherwise, assume value was too low
|
||||
else
|
||||
{
|
||||
low = mid;
|
||||
}
|
||||
|
||||
// calculate new mid
|
||||
mid = ( high + low ) / 2.0;
|
||||
|
||||
// calculate new test square
|
||||
testSquare = mid * mid;
|
||||
}
|
||||
|
||||
// return estimated square root
|
||||
return mid;
|
||||
}
|
||||
|
||||
void printDividedLine( char thickness )
|
||||
{
|
||||
// print first pipe
|
||||
// function: printCharacter
|
||||
printCharacter( PIPE );
|
||||
|
||||
// print description line
|
||||
// function: printCharacters
|
||||
printCharacters( DESCRIPTION_WIDTH, thickness );
|
||||
|
||||
// print divider pipe
|
||||
// function: printCharacter
|
||||
printCharacter( PIPE );
|
||||
|
||||
// print description line
|
||||
// function: printCharacters
|
||||
printCharacters( VALUE_WIDTH, thickness );
|
||||
|
||||
// print divider pipe
|
||||
// function: printCharacter
|
||||
printCharacter( PIPE );
|
||||
|
||||
// print description line
|
||||
// function: printCharacters
|
||||
printCharacters( UNIT_WIDTH, thickness );
|
||||
|
||||
// print last pipe
|
||||
// function: printCharacter
|
||||
printCharacter( PIPE );
|
||||
}
|
||||
|
||||
void printHeavyDividedLine()
|
||||
{
|
||||
// call print divided line with heavy character
|
||||
// function: printDividedLine
|
||||
printDividedLine( THICK_LINE );
|
||||
}
|
||||
|
||||
void printLightDividedLine()
|
||||
{
|
||||
// call print divided line with heavy character
|
||||
// function: printDividedLine
|
||||
printDividedLine( THIN_LINE );
|
||||
}
|
||||
|
||||
void printTopLine()
|
||||
{
|
||||
// print top line
|
||||
// function: printCharacters
|
||||
printCharacters( TABLE_WIDTH, THIN_LINE );
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue