繁体   English   中英

如何使用c ++中的给定信息计算单身或已婚的税收

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

我一直在试图找出此问题的正确计算方法,但似乎无法使用以下信息弄清楚如何正确计算。 我也在下面发布了我的代码。 我不明白如何获得任何给定收入的实际税额。 这可能更像是一个数学问题,但任何帮助将不胜感激!

使用此信息计算税收。

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

该问题已在评论中指出,因为这只是由于对表格的误解。 目前,您已经直接在规则中对大多数数字进行了硬编码,因此如果将来阈值 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;
}

另请注意,已婚夫妇的门槛是单身人士门槛的两倍。 因此,如果我们愿意,我们可以使代码更紧凑,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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