Как рассчитать налоги для одиноких или женатых с учетом информации в C ++ - PullRequest
0 голосов
/ 02 апреля 2020

Я пытался выяснить правильные расчеты для этой проблемы и, похоже, не могу понять, как сделать это правильно, используя следующую информацию. Я также разместил свой код ниже. Я не понимаю, что делать, чтобы получить фактическую сумму налога для любого данного дохода. Вероятно, это скорее математический вопрос, но любая помощь будет признательна!

Calculate taxes using this information.

'''
//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;
}
'''

1 Ответ

0 голосов
/ 02 апреля 2020

Проблема была указана в комментарии, так как это просто из-за неправильной интерпретации таблицы. В настоящее время вы жестко закодировали большинство чисел в правиле напрямую, и, следовательно, если в будущем порог 8000 изменится, вам придется изменить соответствующее значение в нескольких местах. Возможно, вы захотите определить переменную для хранения этих значений.

#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;
}

Также обратите внимание, что порог для супружеской пары вдвое превышает порог для одиноких. Следовательно, если мы будем sh, мы можем сделать код более компактным, как показано ниже:

#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;
}
...