如何使用 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;
}
'''
问题已在评论中指出,因为它只是由于对 table 的误解。目前,您已经直接在规则中硬编码了大部分数字,因此如果将来阈值 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;
}
我一直在尝试找出此问题的正确计算方法,但似乎无法使用以下信息弄清楚如何正确计算。我也在下面发布了我的代码。我不明白如何获得任何给定收入的实际税额。这可能更像是一个数学问题,但我们将不胜感激!
'''
//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;
}
'''
问题已在评论中指出,因为它只是由于对 table 的误解。目前,您已经直接在规则中硬编码了大部分数字,因此如果将来阈值 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;
}