C++ Online Compiler
Example: Regional Taxation with Struct/Class in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Regional Taxation with Struct/Class #include <iostream> #include <string> #include <map> #include <iomanip> // For std::fixed and std::setprecision // Define a struct to hold detailed tax policy for a region struct TaxPolicy { std::string name; double baseRate; double luxuryTaxRate; // Example of a more complex policy element double minimumTaxableAmount; // Constructor for convenience TaxPolicy(std::string n, double br, double ltr, double mta) : name(n), baseRate(br), luxuryTaxRate(ltr), minimumTaxableAmount(mta) {} // Method to calculate total tax based on this policy double calculateTotalTax(double amount) const { double tax = 0.0; if (amount > minimumTaxableAmount) { tax += (amount - minimumTaxableAmount) * baseRate; if (amount > 500.0) { // Example: additional luxury tax for high amounts tax += amount * luxuryTaxRate; } } return tax; } }; // Map to store TaxPolicy objects by region name std::map<std::string, TaxPolicy> regionalPolicies; void initializeComplexPolicies() { regionalPolicies.emplace("StateA", TaxPolicy("StateA", 0.06, 0.02, 10.0)); // 6% base, 2% luxury, $10 min regionalPolicies.emplace("StateB", TaxPolicy("StateB", 0.075, 0.01, 0.0)); // 7.5% base, 1% luxury, $0 min regionalPolicies.emplace("StateC", TaxPolicy("StateC", 0.05, 0.00, 50.0)); // 5% base, no luxury, $50 min } double calculateComplexTax(double amount, const std::string& region) { if (regionalPolicies.count(region)) { return regionalPolicies.at(region).calculateTotalTax(amount); } else { std::cout << "Warning: Complex policy for region '" << region << "' not found. Applying default (0%) tax." << std::endl; return 0.0; } } int main() { // Step 1: Initialize the complex tax policies initializeComplexPolicies(); double itemAmount1 = 150.0; double itemAmount2 = 600.0; double itemAmount3 = 5.0; // Below minimum taxable amount for StateA // Step 2: Calculate tax for StateA std::string region1 = "StateA"; double tax1 = calculateComplexTax(itemAmount1, region1); double totalCost1 = itemAmount1 + tax1; std::cout << std::fixed << std::setprecision(2); std::cout << "Item: $" << itemAmount1 << ", Region: " << region1 << ", Tax: $" << tax1 << ", Total: $" << totalCost1 << std::endl; // Step 3: Calculate tax for StateB (high amount triggering luxury tax) std::string region2 = "StateB"; double tax2 = calculateComplexTax(itemAmount2, region2); double totalCost2 = itemAmount2 + tax2; std::cout << "Item: $" << itemAmount2 << ", Region: " << region2 << ", Tax: $" << tax2 << ", Total: $" << totalCost2 << std::endl; // Step 4: Calculate tax for StateA (below minimum taxable amount) std::string region3 = "StateA"; double tax3 = calculateComplexTax(itemAmount3, region3); double totalCost3 = itemAmount3 + tax3; std::cout << "Item: $" << itemAmount3 << ", Region: " << region3 << ", Tax: $" << tax3 << ", Total: $" << totalCost3 << std::endl; // Step 5: Calculate tax for an unknown region std::string region4 = "NonExistent"; double tax4 = calculateComplexTax(itemAmount1, region4); double totalCost4 = itemAmount1 + tax4; std::cout << "Item: $" << itemAmount1 << ", Region: " << region4 << ", Tax: $" << tax4 << ", Total: $" << totalCost4 << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS