税费计算取整题目

2024-04-18 13:48
文章标签 计算 题目 取整 税费

本文主要是介绍税费计算取整题目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

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
< string int >  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;
}

这篇关于税费计算取整题目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/914959

相关文章

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

Python中经纬度距离计算的实现方式

《Python中经纬度距离计算的实现方式》文章介绍Python中计算经纬度距离的方法及中国加密坐标系转换工具,主要方法包括geopy(Vincenty/Karney)、Haversine、pyproj... 目录一、基本方法1. 使用geopy库(推荐)2. 手动实现 Haversine 公式3. 使用py

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

python中的整除向下取整的操作方法

《python中的整除向下取整的操作方法》Python中的//是整数除法运算符,用于执行向下取整的除法,返回商的整数部分,不会四舍五入,它在分治法、索引计算和整数运算中非常有用,本文给大家介绍pyth... 目录1. // 的基本用法2. // vs /(普通除法)3. // 在 mid = len(lis

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如