簡體   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