Code:
/* Creates customer's bills given the length and the width of the carpet in feet,
the carpet price per square foot and the percent of discount for each customer.
Written by:
Date: 10/9/14
*/
#include <stdio.h>
#include <stdlib.h>
//Constants
#define TAX 8.5
#define LABOR 0.35
//Function Declarations
int readInput (int* length, int* width,
float* percent_discount, float* cost);
void installedPrice(int length, int width, float cost,
int* area, float* carpet_cost, float* labor_cost);
void calc (int length, int width, float cost,
int* area, float* carpet_cost,
float* labor_cost,
float percent_discount, float* subtotal,
float* discount, float* carpetandlabor,
float* total_price, float* total_tax);
void calcSubtotal(float carpet_cost, float labor_cost, float percent_discount,
float* subtotal, float* discount, float* carpetandlabor);
void calcTotal(float subtotal, float discount,
float* total_price, float* total_tax);
void printResult (int length, int width, int area, float cost, float percent_discount,
float carpet_cost, float labor_cost, float subtotal, float discount,
float total_price, float total_tax, float carpetandlabor);
void printMeasurements(int length, int width, int area);
void printCharges(float cost, float percent_discount, float carpet_cost, float labor_cost,
float subtotal, float discount, float total_price, float total_tax, float carpetandlabor);
int main()
{
//Variables
int length;
int width;
float percent_discount;
float cost;
//Calculated variables
int area;
float carpet_cost;
float labor_cost;
float subtotal;
float discount;
float total_price;
float total_tax;
float carpetandlabor;
//Statements
readInput(&length, &width, &percent_discount, &cost);
installedPrice(length, width, cost, &area,
&carpet_cost, &labor_cost);
calcSubtotal(carpet_cost, labor_cost, percent_discount,
&subtotal, &discount, &carpetandlabor);
calcTotal(subtotal, discount, &total_price, &total_tax);
printMeasurements(length, width, area);
printCharges(cost, percent_discount, carpet_cost, labor_cost,
subtotal, discount, total_price, total_tax, carpetandlabor);
return 0;
}//main
/*=========================readInput========================================
This function prompts and reads input entered at the keyboard and stores the input
in the proper memory address.
*/
int readInput (int* length, int* width, float* percent_discount, float* cost)
{
printf("\nEnter length in feet (integer): ");
scanf("%d", length);
printf("\nEnter width in feet (integer): ");
scanf("%d", width);
printf("\nEnter discount rate (float): ");
scanf("%f", percent_discount);
printf("\nEnter cost per sq. ft. (float): ");
scanf("%f", cost);
return 0;
}//readInput
/*============================calc======================================
This function encompasses all sub functions for the calculations of
user input data to produce measurements and costs.
*/
void calc (int length, int width, float cost,
int* area, float* carpet_cost, float* labor_cost,
float percent_discount, float* subtotal,
float* discount, float* carpetandlabor,
float* total_price, float* total_tax)
{
//Statements
calc installedPrice(length, width, cost, area, carpet_cost, labor_cost);
calc calcSubtotal(carpet_cost, labor_cost, percent_discount,
subtotal, discount, carpetandlabor);
calc calcTotal(subtotal, discount, total_price, total_tax);
return;
}//calc
/*==========================installedPrice=======================
This function calculates area and costs of carpet installation per square foot
and labor per square foot.
*/
void installedPrice(int length, int width, float cost, int* area,
float* carpet_cost, float* labor_cost)
{
//Statements
*area = length * width;
*carpet_cost = *area * cost;
*labor_cost = *area * LABOR;
return;
}//installedPrice
/*===========================calcSubtotal=====================================
This function calculates by calculating variables and user input into a price
that can be output.
*/
void calcSubtotal(float carpet_cost, float labor_cost, float percent_discount,
float* subtotal, float* discount, float* carpetandlabor)
{
//Statements
*carpetandlabor = carpet_cost + labor_cost;
*discount = ((percent_discount/100) * *carpetandlabor);
*subtotal = *carpetandlabor - *discount;
return;
}//calcSubtotal
/*==========================calcTotal==============================================
This function calculates the total price of installation including labor, material
costs, applied dousounts, and tax.
*/
void calcTotal(float subtotal, float discount, float* total_price, float* total_tax)
{
//Statements
*total_tax = ((TAX/100) * subtotal);
*total_price = *total_tax + subtotal;
return;
}//calcTotal
/*====================printResult=================================
This function encompasses the sub functions to print user input
and print calculated output.
*/
void printResult (int length, int width, int area, float cost, float percent_discount,
float carpet_cost, float labor_cost, float subtotal, float discount,
float total_price, float total_tax, float carpetandlabor)
{
//Statements
printMeasurements (length, width, area);
printCharges (cost, percent_discount, carpet_cost,
labor_cost, subtotal, discount,
total_price, total_tax, carpetandlabor);
return;
}//printResult
/*=============printMeasurements======================
This function prints the length, width, and area
entered by the user.
*/
void printMeasurements(int length, int width, int area)
{
//Statements
printf("\n\t\tMEASUREMENTS\t\t");
printf("\nLength\t%d ft.",length);
printf("\nWidth\t%d ft.",width);
return;
}//printMeasurements
/*=============================printCharges=============================
This function prints the output of the calculations input by the user
through the use implementation of calculation functions.
*/
void printCharges(float cost, float percent_discount, float carpet_cost,
float labor_cost, float subtotal, float discount,
float total_price, float total_tax, float carpetandlabor)
{
//Statements
printf("\n\t\tCHARGES\t\t");
printf("\nDESCRIPTION\tCOST PER SQ.FT.\t\tCHARGE");
printf("\n-----------\t---------------\t\t------");
printf("\nCarpet\t\t\t%.2f\t\t$%.2f",cost, carpet_cost);
printf("\nLabor\t\t\t%.2f\t\t$%.2f",LABOR, labor_cost);
printf("\n\t\t\t\t\t------");
printf("\nInstalled Price\t\t\t\t$%.2f",carpetandlabor);
printf("\nDiscount\t\t%.2f%\t\t$%.2f",percent_discount, discount);
printf("\n\t\t\t\t\t------");
printf("\nSubtotal\t\t\t\t$%.2f", subtotal);
printf("\nTax\t\t\t\t\t$%.2f",total_tax);
printf("\nTotal\t\t\t\t\t$%.2f",total_price);
return;
}//printCharges