In: Computer Science
I wrote this code and just realized I need to put it into at least 6 different functions and I don't know how. No specific ones but recommended is: Read Data, Calculate Installation Price, Calculate Subtotal, Calculate Total, Print -> 1) Print Measurements & 2) Print Charges. Can somebody help?
#include <stdio.h>
// Function Declarations
int length, width, area, discount;
int main ()
{
// Local Declarations
double price, cost, charge, laborCharge, installed, amtDiscount,
subtotal, amtTax, total;
const double tax = 8.5;
const double labor = .35;
printf("Enter length in feet: ");
scanf("%d", &length);
printf("Enter width in feet: ");
scanf("%d", &width);
printf("Enter the percentage discount: ");
scanf("%d", &discount);
printf("Enter carpet price per foot: ");
scanf("%lf", &price);
area = length * width;
charge = price * area;
laborCharge = labor * area;
installed = charge + laborCharge;
amtDiscount = installed * (discount / 100.);
subtotal = installed - amtDiscount;
amtTax = tax / 100. * subtotal;
total = subtotal + amtTax;
printf("\n\t\tMEASUREMENT\n\n");
printf("Length \t%4d feet\n", length);
printf("Width \t%4d feet\n", width);
printf("Area \t%4d sq. ft\n", area);
printf("\n\t\tCHARGES\n\n");
printf("DESCRIPTION COST/SQ.FT. CHARGE/ROOM\n");
printf("----------- ----------- -----------\n");
printf("Carpet %5.2f $%7.2f\n", price, charge);
printf("Labor %.2f $%7.2f\n", labor, laborCharge);
printf("\t\t\t-----------\n");
printf("INSTALLED PRICE \t$%7.2f\n", installed);
printf("Discount %2d%% \t\t$%7.2f\n", discount, amtDiscount);
printf("\t\t\t-----------\n");
printf("SUBTOTAL \t\t$%7.2f\n", subtotal);
printf("Tax \t\t\t$%7.2f\n", amtTax);
printf("TOTAL \t\t\t$%7.2f\n", total);
return 0;
}
#include <stdio.h>
double GetArea(double length, double width)
{
return length * width;
}
double GetCharge(double price, double area)
{
return price * area;
}
double GetLaborCharge(double labor, double area)
{
return labor * area;
}
double GetInstalled(double charge, double laborCharge)
{
return charge + laborCharge;
}
double GetDiscountAmount(double installed, double
discount)
{
return installed * (discount / 100.0);
}
double GetSubtotal(double installed, double
discountAmount)
{
return installed - discountAmount;
}
double GetTaxAmount(double tax, double subtotal)
{
return tax / 100.0 * subtotal;
}
double ReadData()
{
double data;
scanf("%lf", &data);
return data;
}
void PrintMeasurment(double length, double width, double
area)
{
printf("\n\tMEASUREMENT\n\n");
printf("Length \t\t%4d feet\n", (int)length);
printf("Width \t\t%4d feet\n", (int)width);
printf("Area \t\t%3.2lf sq. ft\n", area);
}
void PrintCharges(double price, double charge, double labor,
double laborCharge)
{
printf("\n\tCHARGES\n\n");
printf("DESCRIPTION COST/SQ.FT. CHARGE/ROOM\n");
printf("----------- ----------- -----------\n");
printf("Carpet
%5.2f $%7.2f\n", price,
charge);
printf("Labor
%.2f $%7.2f\n", labor,
laborCharge);
printf("\n");
}
double CalculateInstallationPrice(double area, double price,
double labor)
{
return GetInstalled(GetCharge(price, area), GetLaborCharge(labor,
area));
}
double CalculateDiscount(double area, double price, double
labor, double discount)
{
return GetDiscountAmount(CalculateInstallationPrice(area, price,
labor), discount);
}
int main ()
{
int length, width, discount;
double price;
double area, charge, laborCharge;
double installed, amtDiscount, subtotal, amtTax, total;
const double tax = 8.5;
const double labor = .35;
printf("Enter length in feet: ");
length = (int)ReadData();
printf("Enter width in feet: ");
width = (int)ReadData();
printf("Enter the percentage discount: ");
discount = (int)ReadData();
printf("Enter carpet price per foot: ");
price = ReadData();
area = GetArea(length, width);
charge = GetCharge(price, area);
laborCharge = GetLaborCharge(labor, area);
PrintMeasurment(length, width, area);
PrintCharges(price, charge, labor, laborCharge);
installed = CalculateInstallationPrice(area, price,
labor);
amtDiscount = CalculateDiscount(area, price, labor,
discount);
subtotal = GetSubtotal(installed, amtDiscount);
amtTax = GetTaxAmount(tax, subtotal);
total = subtotal + amtTax;
printf("\t\t\t-----------\n");
printf("INSTALLED PRICE \t$%7.2f\n", installed);
printf("Discount %2d%% \t\t$%7.2f\n", discount, amtDiscount);
printf("\t\t\t-----------\n");
printf("SUBTOTAL \t\t$%7.2f\n", subtotal);
printf("Tax \t\t\t$%7.2f\n", amtTax);
printf("TOTAL \t\t\t$%7.2f\n", total);
return 0;
}
/*
output:-
Enter length in feet: 12
Enter width in feet: 78
Enter the percentage discount: 34
Enter carpet price per foot: 75
MEASUREMENT
Length
12 feet
Width
78 feet
Area
936.00 sq. ft
CHARGES
DESCRIPTION COST/SQ.FT. CHARGE/ROOM
----------- ----------- -----------
Carpet
75.00 $70200.00
Labor
0.35 $ 327.60
-----------
INSTALLED PRICE
$70527.60
Discount
34%
$23979.38
-----------
SUBTOTAL
$46548.22
Tax
$3956.60
TOTAL
$50504.81
*/