税费计算取整题目

原创 2007年09月16日 12:49:00

题目:

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

PHP 浮点数计算比较及取整不准确的解决方法

浮点数计算结果比较 一则浮点数计算例子如下: $a = 0.2+0.7; $b = 0.9; var_dump($a == $b); 打印出的结果是:bool(false)。也就是说在...

PHP 浮点数计算比较及取整不准确的解决方法

PHP 浮点数计算比较及取整不准确的解决方法 浮点数计算结果比较 一则浮点数计算例子如下: $a = 0.2+0.7; $b = 0.9; var_dump($a == $b); ...

第九周项目2-用switch语句计算应缴税费及税后收入

/* *程序的版权和版本声明部分: *Copyright(c)2013,烟台大学计算机学院学生 *All rights reserved. ...

matlab取整指令

  • 2009-07-30 22:44
  • 26KB
  • 下载

Java基础知识点笔记(一):java中的取整与四舍五入

一.java种取整数的方式 1.直接使用强制转换 public static void roundOne(){ System.out.println("正数:(int)10.1...

JAVA取整计算器源码

js只保留整数,取整,四舍五入等函数

1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) ...

excel取整公式

  • 2014-11-04 10:17
  • 15KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)