Skip to content

Instantly share code, notes, and snippets.

@doyonghoon
Last active November 12, 2015 21:06
Show Gist options
  • Save doyonghoon/f1d67525e51aec557cff to your computer and use it in GitHub Desktop.
Save doyonghoon/f1d67525e51aec557cff to your computer and use it in GitHub Desktop.
Uguly Carpet
/*
Write a C++ program that creates a customer bill for a carpet company. The following information is input:
The length of width of the carpet in feet.
The carpet price per square foot
The percent of discount for the customer
The labor cost is fixed at $ 0.35 per square foot. The tax rate is 8.5%. Both of these constants are to be set as globally preprocessor constants.
All input is to be done in a separate function called input().
All calculations are performed in a function that calls three subfunctions as follows:
A subfunction calcInstall() to calculate carpet cost, labor cost, and installed cost (carpet cost plus labor cost).
A subfunction calcSubtotal() to calculate the amoun of the discount and the subtotal
A subfunction calcTotal() to calculate the amount of tax and the total of the order.
All output is to be done in a separate function called output()
Test data: Length = 20; Width = 10; Rate of discount = 5%; Cost per sq ft = $30.00.
Programmers:
1) Spencer Do
2) Yongjun YU
*/
#include <iostream>
#include <iomanip>
using namespace std;
#define _DEBUG false
#define TAX_RATE 0.085f
#define LABOR_RATE 0.35f
//Function Prototypes
void inputData(int& l, int& w, int& dis, double& unitCost);
int calc(int l,int w, int d, double unitC, int area, double& carpetCost,
double& labour, double& install, double& discount, double& taxes, double& subtotal, double& total);
void output(int l, int w, int dis, double unitCost, int area, double carpetCost,
double labour, double install, double discount, double taxes, double subtotal, double total);
int calcInstall(int area, double unitC, double& carpet, double& labor);
void calcSubtotal(double installCost, int discountRate, double& discount, double& subtotal);
void calcTotal(const double subtotal, double& tax, double& total);
int main()
{
//declare variables
int length, width, discountRate, area;
double priceSqFt, carpetCost, labour, install, discount;
double subtotal, taxes, total;
// input dimensions of room, discount and price per sq ft of carpet
inputData(length, width, discountRate, priceSqFt);
// compute cost
area = calc(length, width, discountRate, priceSqFt, area, carpetCost,
labour, install, discount, taxes, subtotal, total);
//output
output(length, width, discountRate, priceSqFt, area, carpetCost,
labour, install, discount, taxes, subtotal, total);
system("pause");
return 0;
}
/*====================input==============================
Purpose: Inputs the room dimensions, percent of discount, and
the cost of the carpet per square foot. */
void inputData(int& l, int& w, int& dis, double& unitCost) {
#if _DEBUG
l = 20;
w = 10;
dis = 5;
unitCost = 30;
#else
/*
Pre: Addresses for room dimensions, rate of discount, price per square foot of carpet
Post: Nothin
*/
cout << "please enter the length of room: ";
cin >> l;
cout << "please enter the width of room: ";
cin >> w;
cout << "please enter the discount rate: ";
cin >> dis;
cout << "please enter the cost per square feet of carpet: ";
cin >> unitCost;
#endif
return;
}
/* ===================== calculations====================================
Purpose: Computes the costs for labor and carpet. Computes discount, subtotal, taxes and total.
Pre: length, width, discount rate, and unit price along with references to cost of carpet,
cost of labour, installation cost, amount of discount, tax, subtotal and total for order.
Post: The area of the room is returned.*/
int calc(int l,int w, int d, double unitC, int area, double& carpetCost,
double& labour, double& install, double& discount, double& taxes, double& subtotal, double& total) {
area = l * w;
install = calcInstall(area, unitC, carpetCost, labour);
calcSubtotal(install, d, discount, subtotal);
calcTotal(subtotal, taxes, total);
return area;
}
/* =====================calcInstall=================================
Purpose: Computes costs of labor and carpet and their sum.
Pre: area and unit price along with the references to carpet cost, labor cost, and
installed cost.
Post: returns install cost*/
int calcInstall(int area, double unitC, double& carpet, double& labor) {
carpet = unitC * area;
labor = LABOR_RATE * area;
return carpet + labor;
}
/* =======================calcSubtotal===============================
Purpose: Computes the amount of discount and subtotal.
Pre: The installed cost and discount rate along with addresses for the
amount of the discount and the subtotal.
Post: Nothing*/
void calcSubtotal(double installCost, int discountRate, double& discount, double& subtotal) {
discount = installCost * static_cast<double>(discountRate) / 100;
subtotal = installCost - discount;
}
/*========================calcTotal=================================
Computes the tax and total.
Pre: Subtotal along with reference pointers to tax and total
Post: Nothing */
void calcTotal(const double subtotal, double& tax, double& total) {
tax = subtotal * TAX_RATE;
total = tax + subtotal;
}
void output(int l, int w, int dis, double unitCost, int area, double carpetCost,
double labor, double install, double discount, double taxes, double subtotal, double total) {
cout << setprecision(2) << fixed;
cout<< "\t\t\tMEASUREMENT" << endl;
cout<<"Length: " << setw(3) << l <<" ft" << endl;
cout<<"Width: " << setw(3) << w <<" ft" << endl;
cout<<"Area: " << setw(3) << area <<" ft" << endl;
cout<<"\n\n\t\t\tCHARGES" <<endl;
cout<<"\nDESCRIPTION COST/SQ.FT. CHARGE" <<endl;
cout<<"----------- ----------- ----------";
cout<<left << setw(17) << "\nCarpet " << right << setw(7) << unitCost << " " << setw(10) << carpetCost;
cout<<left << setw(17) <<"\nLabor " << right << setw(7) << LABOR_RATE<< " " << setw(10) << labor;
cout<< setw(25) <<"\n-----------";
cout<<left << setw(20) <<"\nINSTALLED PRICE $ " << right << setw(15) << install;
cout<<left << setw(20) <<"\nDiscount " << right << setw(7)<< dis << "%" <<setw(7) << discount;
cout << setw(25) <<"\n-----------";
cout<<left << setw(28) <<"\nSUBTOTAL $ " << right << setw(7) << subtotal;
cout<<left << setw(28) <<"\nTax " << right << setw(7) << taxes;
cout<<left << setw(28) <<"\nTOTAL $ " << right << setw(7) << total;cout<< endl;
cout << endl << endl << "Programmed by: 1) Spencer Do" << endl;
cout << right << setw(28) << "2) Yongjun YU" << endl << endl;
}
/*
please enter the length of room: 20
please enter the width of room: 10
please enter the discount rate: 5
please enter the cost per square feet of carpet: 30
MEASUREMENT
Length: 20 ft
Width: 10 ft
Area: 200 ft
CHARGES
DESCRIPTION COST/SQ.FT. CHARGE
----------- ----------- ----------
Carpet 30.00 6000.00
Labor 0.35 70.00
-----------
INSTALLED PRICE $ 6070.00
Discount 5% 303.50
-----------
SUBTOTAL $ 5766.50
Tax 490.15
TOTAL $ 6256.65
Programmed by: 1) Spencer Do
2) Yongjun YU
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment