税费计算取整题目

原创 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;
}

面向对象编程经典例题 --- 收银系统设计

SALES TAXES   Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and m...
  • fzzying3
  • fzzying3
  • 2014-12-19 11:06:31
  • 1053

分段计算居民水费

分段计算居民水费为鼓励居民节约用水,自来水公司采取按用水量阶梯式计价的办法,居民应交水费yy(元)与月用水量xx(吨)相关:当xx不超过15吨时,y=4x/3y=4x/3;超过后,y=2.5x-17....
  • yes1001
  • yes1001
  • 2016-10-31 04:19:26
  • 643

C语言小区居民查询系统

#include#include#include#include typedef struct member//成员结构体 { char name[30];//姓名  char born[10];//...
  • HQ354974212
  • HQ354974212
  • 2010-10-30 01:01:00
  • 572

(编程题)5-11分段计算居民水费

5-11 分段计算居民水费  为鼓励居民节约用水,自来水公司采取按用水量阶梯式计价的办法,居民应交水费yy(元)与月用水量xx(吨)相关:当xx不超过15吨时,y=4x/3y=4x/3;超过后,...
  • qq_22902957
  • qq_22902957
  • 2017-02-05 15:33:48
  • 361

分支-09. 分段计算居民水费

为鼓励居民节约用水,自来水公司采取按用水量阶梯式计价的办法,居民应交水费y(元)与月用水量x(吨)相关:当x不超过15吨时,y=4x/3;超过后,y=2.5x-17.5。请编写程序实现水费的计算。 ...
  • u010399331
  • u010399331
  • 2014-07-25 19:15:18
  • 2228

PAT-分段计算居民水费 (简单编程题)

为鼓励居民节约用水,自来水公司采取按用水量阶梯式计价的办法,居民应交水费y(元)与月用水量x(吨)相关:当x不超过15吨时,y=4x/3;超过后,y=2.5x−17.5。请编写程序实现水费的计算。 ...
  • qiulh125
  • qiulh125
  • 2015-12-24 10:04:15
  • 3919

Excel函数公式计算阶梯式电价

阶梯式电价是阶梯式递增电价或阶梯式累进电价的简称,也称为阶梯电价,是指把户均用电量设置为若干个阶梯分段或分档次定价计算费用。 据说,对居民用电实行阶梯式递增电价可以提高能源效率,千万不能说是变相的...
  • sinat_39566400
  • sinat_39566400
  • 2017-08-16 09:51:35
  • 331

EasyUI修改DateBox和DateTimeBox的默认日期格式

最近整理Easyui控件的时候,对Easyui的DateBox控件和DateTimeBox控件进行了梳理,而我之所以将EasyUI的DateBox控件和DateTimeBox控件放在一起,归为一类,是...
  • dandanzmc
  • dandanzmc
  • 2014-06-24 20:02:49
  • 84992

玩转flume+Kafka配置

一、FLUME介绍 Flume是一个分布式、可靠、和 高可用 的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方...
  • liu857279611
  • liu857279611
  • 2017-05-18 09:42:06
  • 2267

商铺税费计算器

  • 2014年01月13日 21:19
  • 60KB
  • 下载
收藏助手
不良信息举报
您举报文章:税费计算取整题目
举报原因:
原因补充:

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