简体   繁体   中英

How to calculate taxes for single or married with given information in c++

I have been trying to figure out the correct calculations for the this problem and can't seem to figure out how to get it right using the following information. I have posted my code below as well. I don't understand what to do to get the actual tax amount for any given income. This is probably more of a math issue, but any help would be appreciated!

使用此信息计算税收。

'''
//Write a program that computes taxes.

#include <iostream>
#include<string>
using namespace std;

int main() 
{

double tax1 = 0;
double tax2 = 0;
double tax3 = 0;

double income = 0;
string marital_status;

cout << "Please enter your income: ";
  cin >> income;

cout << "Please enter s for single, or m for married: ";
  cin >> marital_status;

if (marital_status == "s")
  {
    if (income <= 8000)
      {
        tax1 = income * .10;
      }
      else if (income <= 32000)
      {
         tax1 = income * .10;
         tax2 = income * .15 + 800;
      }
      else
      {
        tax1 = income * .10; 
        tax2 = income * .15 + 800;
        tax3 = income * .25 + 4400;
      }
  }
else
{
  if (income <= 16000)
      {
        tax1 = income * .10;
      }
      else if (income <= 64000)
      {
         tax1 = income * .10;
         tax2 = income * .15 + 1600;
      }
      else
      {
        tax1 = income * .10;
        tax2 = income * .15 + 1600;
        tax3 = income * .25 + 8800; 
      }
}

   double total_tax = tax1 + tax2 + tax3;

   cout << "The tax is $" << total_tax << endl;

return 0;
}
'''

The problem has been pointed out in the comment as it is just due to misinterpretation of the table. Currently, you have hard coded most numbers in the rule directly and hence if the threshold 8000 change in the future, you have to change the corresponding value in multiple places. You might like to define a variable to store those values.

#include <iostream>
#include<string>
using namespace std;

int main() 
{

double tax = 0;
double first_threshold = 8000;
double second_threshold = 32000;
double first_percent = .1;
double second_percent = .15;
double third_percent = .25;

double income = 0;
string marital_status;

cout << "Please enter your income: ";
  cin >> income;

cout << "Please enter s for single, or m for married: ";
  cin >> marital_status;

if (marital_status == "s")
  {
    if (income <= first_threshold)
      {
        tax = income * first_percent;
      }
      else if (income <= second_threshold)
      {
         tax = (income - first_threshold) * second_percent + first_threshold * first_percent;
      }
      else
      {
        tax = (income - second_threshold) * third_percent + first_threshold * first_percent + (second_threshold - first_threshold) * second_percent;
      }
  }
else
{
  if (income <= 2 * first_threshold)
      {
        tax = income * first_percent;
      }
      else if (income <= 2 * first_threshold)
      {
         tax = (income - 2*first_threshold) * second_threshold + 2 * first_threshold * first_percent;
      }
      else
      {
        tax = (income - 2 *second_threshold) * third_percent + 2* first_threshold * first_percent + 2*(second_threshold - first_threshold) * second_threshold;
      }
}

   cout << "The tax is $" << tax << endl;

return 0;
}

Also, notice that the threshold for married couple is twice the threshold for the singles. Hence, if we wish, we can make the code to be more compact as follows:

#include <iostream>
#include<string>
using namespace std;

int main() 
{

double tax = 0;
double first_threshold = 8000;
double second_threshold = 32000;
double first_percent = .1;
double second_percent = .15;
double third_percent = .25;

double income = 0;
string marital_status;

cout << "Please enter your income: ";
  cin >> income;

cout << "Please enter s for single, or m for married: ";
  cin >> marital_status;

if (marital_status == "m"){
    first_threshold *= 2;
    second_threshold *= 2;
}

if (income <= first_threshold)
      tax = income * first_percent;
else if (income <= second_threshold)
      tax = (income - first_threshold) * second_percent + first_threshold * first_percent;
else{
        tax = (income - second_threshold) * third_percent + first_threshold * first_percent + (second_threshold - first_threshold) * second_percent;
}

   cout << "The tax is $" << tax << endl;

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM