税费计算取整题目

来源:互联网 发布:基于密度的聚类算法ppt 编辑:程序博客网 时间:2024/04/29 00:59

题目:

CODING PROBLEM: SALES TAXES


Basic sales tax is applicable at a rate of 10% on all goods, except books,
food, and medical products that are exempt. Import duty is an additional
sales tax applicable on all imported goods at a rate of 5%, with no
exemptions.


When I purchase items I receive a receipt which lists the name of all the
items and their price (including tax), finishing with the total cost of the
items, and the total amounts of sales taxes paid.  The rounding rules for
sales tax are that for a tax rate of n%, a shelf price of p contains (np/100
rounded up to the nearest 0.05) amount of sales tax.


Write an application that prints out the receipt details for these shopping
baskets...

INPUT:


Input 1:

1 book at 12.49

1 music CD at 14.99

1 chocolate bar at 0.85


Input 2:

1 imported box of chocolates at 10.00

1 imported bottle of perfume at 47.50


Input 3:

1 imported bottle of perfume at 27.99

1 bottle of perfume at 18..99

1 packet of headache pills at 9.75

1 box of imported chocolates at 11.25


OUTPUT


Output 1:

1 book : 12.49

1 music CD: 16.49

1 chocolate bar: 0.85

Sales Taxes: 1.50

Total: 29.83


Output 2:

1 imported box of chocolates: 10.50

1 imported bottle of perfume: 54.65

Sales Taxes: 7.65

Total: 65.15


Output 3:

1 imported bottle of perfume: 32.19

1 bottle of perfume: 20.89

1 packet of headache pills: 9.75

1 imported box of chocolates: 11.85

Sales Taxes: 6.70

Total: 74.68

 

解答(难度主要在取整部分):

sales.h

 

#ifndef SALES_H
#define SALES_H
#include 
<iostream>
#include 
<math.h>
#include 
<string>
#include 
<vector>

class Product
{
    std::
string name;
    
int number;
    
bool imported;
    
bool exempt;
    
double value;
    
double price;
public:
    friend 
class Receipt;
    Product()
    
{
        name 
= "";
        number 
= 0;
        imported 
= false;
        exempt 
= false;
        value 
= 0.00;
        price 
= 0.00;
    }

    Product(std::
string& name, 
        
int number, 
        
bool imported, 
        
bool exempt,
        
double value)
    
{
        
this->name = name;
        
this->number = number;
        
this->imported = imported;
        
this->exempt = exempt;
        
if(value >= 0.00)
            
this->value = value;
        
else
            
this->value = 0.00;
        price 
= 0.00;
    }

    
void calPrice() //calculate price(including tax)
    {
        price 
= value;
        
if(imported)
        
{
            price 
+= value * 0.05;

            price 
= price * 10;
            
double temp1 = floor(price + 0.5);
            
double temp2 = floor(price);
            
if(temp1 == temp2)
            
{
                price 
= (price * 10/ 5;
                price 
= (ceil(price) * 5/ 100;
            }

            
else
            
{
                price 
= price * 10;
                price 
= ceil(price) / 100;
            }

        }

        
if(!exempt)
        
{
            price 
+= value * 0.1;

            price 
= price * 10;
            
double temp1 = floor(price + 0.5);
            
double temp2 = floor(price);
            
if(temp1 == temp2)
            
{
                price 
= (price * 10/ 5;
                price 
= (ceil(price) * 5/ 100;
            }

            
else
            
{
                price 
= price * 10;
                price 
= ceil(price) / 100;
            }

        }

    }

}
;

class Receipt
{
    std::vector
<Product> products;
    
double salesTax;
    
double total;
public:
    Receipt()
    
{
        salesTax 
= 0.00;
        total 
= 0.00;
    }

    
void reset()
    
{
        
if(!products.empty())
        
{
            products.clear();
        }

        salesTax 
= 0.00;
        total 
= 0.00;
    }

    
void addProduct(Product& product)
    
{
        products.push_back(product);
    }

    
double getTotalPrice()
    
{
        
for(std::vector<Product>::iterator it = products.begin();
        it 
!= products.end(); ++it)
        
{
            total 
+= it->number * it->price;
        }

        
return total;
    }

    
double getSalesTaxes()
    
{
        
for(std::vector<Product>::iterator it = products.begin();
        it 
!= products.end(); ++it)
        
{
            salesTax 
+= it->price - it->value;
        }

        
return salesTax;
    }

    
void print()
    
{
        
if(products.size() == 0)
            
return;
        
for(std::vector<Product>::iterator it = products.begin();
        it 
!= products.end(); ++it)
        
{
            std::cout 
<< it->number << " ";
            
if(it->imported)
                std::cout 
<< "imported" << " ";
            std::cout 
<< it->name << "" << it->price << std::endl;
        }

        std::cout 
<< "Sales Taxes: " << salesTax << std::endl;
        std::cout 
<< "Total: " << total << std::endl;
    }

}
;

#endif //SALES_H

 

main.cpp

 

#pragma   warning   (disable:4786)
#include 
"sales.h"

#include 
<map>
#include 
<fstream>
using namespace std;
#define TEST_FILE "test.txt"
map
<stringint> exemptionPrduct;

//record exemption product
void init() 
{
    exemptionPrduct.insert(map
<string,int>::value_type("book"1));
    exemptionPrduct.insert(map
<string,int>::value_type("chocolate bar"1));
    exemptionPrduct.insert(map
<string,int>::value_type("box of chocolates"1));
    exemptionPrduct.insert(map
<string,int>::value_type("packet of headache pills"1));
}


//remove space character ' ' and ' '
string& trim(string& temp)
{
    
if (temp.empty()) 
    
{
        
return temp;
    }


    temp.erase(
0, temp.find_first_not_of("  "));
    temp.erase(temp.find_last_not_of(
"  "+ 1);
    
return temp;
}


int main()
{
    cout 
<< "usage: input message as following" << endl;
    cout 
<< "number [space] import/no product at [space] value" << endl;
    cout 
<< "example : 1 imported book at 12.49" << endl;
    init();
    cout 
<< "starting..." << endl;

    fstream fs(TEST_FILE, ios::
in);
    
if(!fs)
    
{
        cout 
<< "test open failed!" << endl;
        getchar();
        
return -1;
    }

    Receipt list;
    
while(!fs.eof())
    
{
        
char buffer[1024];
        fs.getline(buffer, 
1024);
        
string input(buffer);
        
if(input.empty())
        
{
            list.getSalesTaxes();
            list.getTotalPrice();
            list.print();
            cout 
<< endl;
            list.reset();
            
continue;
        }

        
else
        
{
            
if(input.find("input"!= string::npos)
            
{
                
continue;
            }

        }

        
        
int number;
        
bool imported = false;
        
bool exempt = false;
        
string name;
        
double value;

        
string::value_type begin = 0;
        
string::value_type end = 0;
        
string::value_type pos = 0;
        
string temp;

        
//get number
        end = input.find_first_of("  "0);
        temp 
= input.substr(begin, end - begin);
        temp 
= trim(temp);
        number 
= atoi(temp.c_str());
        begin 
= end;

        
//get name, check imported and exempt.
        pos = input.find(" imported ");
        
if(pos != string::npos)
        
{
            imported 
= true;
            input.erase(pos, 
9);
        }


        end 
= input.rfind("at");
        
if(end == string::npos)
        
{
            cout 
<< "input error!(check the format of input string)" << endl;
            
break;
        }

        temp 
= input.substr(begin, end - begin);
        name 
= trim(temp);
        
if(exemptionPrduct.find(name) != exemptionPrduct.end())
        
{
            exempt 
= true;
        }


        
//get value
        begin = end + 2;
        temp 
= input.substr(begin, input.length() - begin);
        temp 
= trim(temp);
        value 
= atof(temp.c_str());

        Product newone(name, number, imported, exempt, value);
        newone.calPrice();
        list.addProduct(newone);
    }

    
    fs.close();
    
    getchar();
    
return 0;
}