税费计算取整题目

原创 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)。也就是说在...

double取整,计算时的精度问题

这是一题求多点路线问题,每两个点之间都可能存在关系,可以用dfs,不过如果有环的话就会爆栈,也可以有floy直接暴搜。关键在于浮点型计算的时候回丢精度,可以每次都不做浮点型的乘除法,最后在运算。不过这...

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

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

matlab取整指令

  • 2009年07月30日 22:44
  • 26KB
  • 下载

java取整和java四舍五入方法

import java.math.BigDecimal;  import java.text.DecimalFormat; public class TestGetInt{  public ...

JAVA取整计算器源码

  • 2007年08月14日 14:16
  • 8KB
  • 下载

java数值取整及保留两位小数

  • 2012年11月30日 10:25
  • 2KB
  • 下载

关于二分法中取中间值时向下和向上取整的问题(由大白LA3971想到的)

最近在做刘汝佳的大白,有一道题目LA_3971,也是UVA_12124,是用二分法做的。 题目地址:http://uva.onlinejudge.org/index.php?option=com_o...
  • dr5459
  • dr5459
  • 2013年03月27日 14:41
  • 1479

excel取整公式

  • 2014年11月04日 10:17
  • 15KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:税费计算取整题目
举报原因:
原因补充:

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