/*
Name: Assignment 4
Author: muguangde
Date: 2014-07-31-12.12
Description: This is a simple program used to generate more than one
invoices printed on separate sheets. Users are required to input
carpet length,width,discount,etc from keyboard. More than 7 functions
are used aimed to practice using reference.
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;
ofstream outfile;
const float laborCost=0.35;
const float tax=0.085;


int main()
{
    void getData(int &,int &,int &,float &);
    void calculate(int,int,int,float,float &,
                   float &,float &);

    void printResult(int,int,int,float,float,float,float,int);

    int length,width,discount;
    float pricePerFeet;
    float installPrice=0,subTotal=0,totalPrice=0;
    string endOrNot;
    int countNum=0;
//prompt the user, type yes to continue input, type end to finish
    cout<<"Please input the following info:"<<endl;
    cout<<"Input \'yes\' to begin or \'end'\ to finish"<<endl;
    cin>>endOrNot;
//Using user input to determine whether to continue
    while(endOrNot!="end") {
        countNum++;//count number of invoice, important later on
        getData(length,width,discount,pricePerFeet);
        calculate(length,width,discount,pricePerFeet,installPrice,
                  subTotal,totalPrice);
        printResult(length,width,discount,pricePerFeet,installPrice,
                    subTotal,totalPrice,countNum);

        cout<<"Please input the following info:"<<endl;
        cout<<"Input yes to begin or end to finish"<<endl;
        cin>>endOrNot;
    }
    system("PAUSE");
    return 0;
}
//Function to get data from keyboard
void getData(int &length,int &width,int &discount,
             float &pricePerFeet)
{
    cout<<"Length of room (feet)?";
    cin>>length;
    cout<<"\nWidth of room (feet)?";
    cin>>width;
    cout<<"\nCustomer discount (percent)?";
    cin>>discount;
    cout<<"\nCost per square foot (xxx.xx)?";
    cin>>pricePerFeet;
}
//Function to calculate the whole things
void calculate(int length,int width,int discount,
               float pricePerFeet,float &installPrice,float &subTotal,
               float &totalPrice)
{
    float calcInstall(int,int,int,float);
    float calcSubTotal(float,float);
    float calcTotal(float,float);
    installPrice=calcInstall(length,width,discount,pricePerFeet);
    subTotal=calcSubTotal(installPrice,discount);
    totalPrice=calcTotal(subTotal,tax);
}
//Function to calculate install price
float calcInstall(int length,int width,int discount,
                  float pricePerFeet)
{
    float install;
    install=length*width*pricePerFeet+length*width*laborCost;
    return install;
}
//Function to calculate subtotal
float calcSubTotal(float install,float discount)
{
    float subTotal;
    subTotal=install-install*discount/100.0;
    return subTotal;
}
//Function to calculate total price
float calcTotal(float subTotal,float tax)
{
    float total;
    total=subTotal+subTotal*tax;
    return total;
}
//Function to print results, special trick is used here to generate
//filename for each invoice by combining string and integer
void printResult(int length,int width,int discount,
                 float pricePerFeet,float installPrice,float subTotal,
                 float totalPrice,int countNum)
{
    //Append string to invoice number, to generate customer's invoice
    //on a separate sheet of paper
    std::ostringstream filename;
    filename << "Invoice" <<countNum<< ".txt";
    outfile.open(filename.str().c_str());
    void printMeasure(int,int);
    void printCharge(int,int,float,int,float,float,float);
    printMeasure(length,width);
    printCharge(length,width,pricePerFeet,discount,installPrice,
                subTotal,totalPrice);
}
//Function to print measure result and heading
void printMeasure(int length,int width)
{
    outfile.setf(ios::fixed,ios::floatfield);
    //setting output format
    outfile<<"     THE BROOKLYN COLLEGE CARPET STORE"<<endl;
    outfile<<"         DesperateChemist, Owner"<<endl;
    outfile<<"\n             MEASUREMENT    "<<endl;
    outfile<<setiosflags(ios::fixed)<<setiosflags(ios::right);
    outfile<<"\n      Length     "<<setw(8)<<length<<" feet";
    outfile<<"\n      Width      "<<setw(8)<<width<<" feet";
    outfile<<"\n      Area       "<<setw(8)<<width*length
           <<" square feet"<<endl;
}
//Function to print the charge
void printCharge(int length,int width,float pricePerFeet,
                 int discount,float installPrice,float subTotal,
                 float totalPrice)
{
    outfile<<"\n             CHARGES"<<endl;
    outfile<<"\nDESCRIPTION      COST/SQ.FT.     CHARGE/ROOM"<<endl;
    outfile<<setiosflags(ios::fixed)<<setiosflags(ios::right)
           <<setprecision(2);//setting output format
    outfile<<"Carpet   \t"<<setw(8)<<pricePerFeet<<"\t  $"
           <<setw(8)<<length*width*pricePerFeet;
    outfile<<"\nLabor    \t"<<setw(8)<<laborCost
           <<"\t  $"<<setw(8)<<laborCost*length*width;
    outfile<<"\n                 \t\t --------------"<<endl;
    outfile<<setiosflags(ios::fixed)<<setiosflags(ios::right)
           <<setprecision(2);
    outfile<<"INSTALLED PRICE"<<" \t\t  $"<<setw(8)<<installPrice;
    outfile<<"\nDiscount \t"<<setw(8)<<setprecision(2)<<(float)discount
           <<"%\t   "
           <<setw(8)<<discount*installPrice/100.0<<endl;
    outfile<<"                 \t\t --------------"<<endl;
    outfile<<"SUBTOTAL    \t\t\t  $"<<setw(8)<<subTotal<<endl;
    outfile<<"Tax          \t\t\t   "<<setw(8)<<subTotal*tax<<endl;
    outfile<<"TOTAL        \t\t\t  $"<<setw(8)<<totalPrice<<endl;
    outfile.close();
}



  
回到主页 回到目录