CS136-Projects/PA02-0914/Project_2.c
cdez 978fe59a26 Modified some project files, added gitignore
+- Modified Project_2.c to reflect more towards the assignment
++ Added a gitignore to stop exe files from being uploaded.
2022-09-14 09:53:28 -07:00

399 lines
13 KiB
C

// 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");
// Then print a blank line for formatting.
printEndline();
// Print a dividing line, use printCharacters().
// Literal used since no constants can be created, and no standard found.
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: ");
// Then print a blank line for formatting.
printEndline();
// Calculate the house width(2/3 rounded of the area)
//(formula: sqrt((desiredArea/SIDE_WALL_RATIO)))
// then divide by trimmed value, cast to int,
// and then multiply by trimmed value to get result.
houseWidth = desiredArea/SIDE_WALL_RATIO;
houseWidth = ((findSquareRoot(houseWidth)) + BOOSTING_VALUE);
houseWidth = houseWidth / TRIMMING_VALUE;
houseWidth = (int) houseWidth * TRIMMING_VALUE;
// Calculate the house depth(1/3 rounded of the area)
// (formula: desiredArea/(1/SIDE_WALL_RATIO))
houseDepth = desiredArea/(1/SIDE_WALL_RATIO);
houseDepth = ((findSquareRoot(houseDepth)) + BOOSTING_VALUE);
houseDepth = houseDepth / TRIMMING_VALUE;
houseDepth = (int) houseDepth * TRIMMING_VALUE;
// 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().
// Then print a blank line for formatting.
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
// Uses functions printCharacter, printStringJustified,
// printIntegerJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("Desired Area", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printIntegerJustified(desiredArea, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("sq ft", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the actual area, using the houseWidth * houseDepth
// Uses functions printCharacter, printStringJustified,
// printIntegerJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("Actual Area", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printIntegerJustified(actualArea, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("sq ft", UNIT_WIDTH -1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the house's width
// Uses functions printCharacter, printStringJustified,
// printIntegerJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("House Width", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printIntegerJustified(houseWidth, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("feet", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the house's depth
// Uses functions printCharacter, printStringJustified,
// printIntegerJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("House Depth", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printIntegerJustified(houseDepth, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("feet", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the roofing cost
// Uses functions printCharacter, printStringJustified,
// printIntegerJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("Roofing Cost", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printDoubleJustified(roofingCost, PRECISION, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("dollars", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the plywood cost
// Uses functions printCharacter, printStringJustified,
// printDoubleJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("Plywood Cost", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printDoubleJustified(plywoodCost, PRECISION, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("dollars", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the 2x4 cost
// Uses functions printCharacter, printStringJustified,
// printDoubleJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("2 X 4 Cost", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printDoubleJustified(twoByFourCost, PRECISION, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("dollars", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the drywall cost
// Uses functions printCharacter, printStringJustified,
// printDoubleJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("Drywall Cost", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printDoubleJustified(drywallCost, PRECISION, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("dollars", UNIT_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printEndline();
printLightDividedLine();
printEndline();
// Printed output of the Total Estimate
// Uses functions printCharacter, printStringJustified,
// printDoubleJustified, printLightDividedLine, and printEndline
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("Total Estimate", DESCRIPTION_WIDTH - 1, "LEFT");
printCharacter(PIPE);
printDoubleJustified(totalEstimate, PRECISION, VALUE_WIDTH - 1, "RIGHT");
printCharacter(SPACE);
printCharacter(PIPE);
printCharacter(SPACE);
printStringJustified("dollars", UNIT_WIDTH - 1, "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 );
}