C++ Online Compiler
Example: Regional Taxation with Std::Map in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Regional Taxation with Std::Map #include <iostream> #include <string> #include <map> #include <iomanip> // For std::fixed and std::setprecision // Define a map to store region tax rates std::map<std::string, double> regionalTaxRates; void initializeTaxRates() { regionalTaxRates["North"] = 0.05; // 5% regionalTaxRates["South"] = 0.07; // 7% regionalTaxRates["East"] = 0.06; // 6% regionalTaxRates["West"] = 0.08; // 8% regionalTaxRates["Central"] = 0.04; // 4% } double calculateTaxMap(double amount, const std::string& region) { double taxRate = 0.0; // Check if the region exists in the map if (regionalTaxRates.count(region)) { taxRate = regionalTaxRates[region]; } else { std::cout << "Warning: Region '" << region << "' not found in tax policy. Applying default tax (0%)." << std::endl; taxRate = 0.0; } return amount * taxRate; } int main() { // Step 1: Initialize the tax rates map initializeTaxRates(); double itemAmount = 250.0; // Step 2: Calculate tax for East region std::string region1 = "East"; double tax1 = calculateTaxMap(itemAmount, region1); double totalCost1 = itemAmount + tax1; std::cout << std::fixed << std::setprecision(2); std::cout << "Item: $" << itemAmount << ", Region: " << region1 << ", Tax: $" << tax1 << ", Total: $" << totalCost1 << std::endl; // Step 3: Calculate tax for Central region std::string region2 = "Central"; double tax2 = calculateTaxMap(itemAmount, region2); double totalCost2 = itemAmount + tax2; std::cout << "Item: $" << itemAmount << ", Region: " << region2 << ", Tax: $" << tax2 << ", Total: $" << totalCost2 << std::endl; // Step 4: Calculate tax for an unknown region std::string region3 = "UnknownLand"; double tax3 = calculateTaxMap(itemAmount, region3); double totalCost3 = itemAmount + tax3; std::cout << "Item: $" << itemAmount << ", Region: " << region3 << ", Tax: $" << tax3 << ", Total: $" << totalCost3 << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS