code-snippets/cpp/progressive_taxes.cpp

50 lines
1.1 KiB
C++
Raw Normal View History

2024-04-08 18:30:18 -04:00
#include <iostream>
#include <map>
//{
//0 - 10: 0.5,
//11 - 50: 0.10,
// 51 - 100: 0.20,
//}
// NET: 75
// 10 * 0.5 + 40 * 0.10 + 25 * 0.20 = answer
constexpr int NET_INFINITY = -1;
double calculateTax(int net_sum, const std::map<double, int>& tax_table)
{
double overall_tax = .0;
int lower_bound = 0;
for (const auto& tax_entry : tax_table)
{
const auto& upper_bound = tax_entry.second;
const auto& percentage = tax_entry.first;
if (net_sum < upper_bound || upper_bound == NET_INFINITY)
{
overall_tax += (net_sum - lower_bound) * percentage;
break;
}
overall_tax += (upper_bound - lower_bound) * percentage;
lower_bound = upper_bound;
}
return overall_tax;
}
int main()
{
const auto net_sum = 58000;
const std::map<double, int> tax_table =
{
{ 0.10, 11000 },
{ 0.12, 44725 },
{ 0.22, NET_INFINITY }
};
std::cout << "net: " << net_sum << std::endl;
std::cout << calculateTax(net_sum, tax_table) << std::endl;
}