c++ - 如何使用 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;
}
推荐阅读
- pandas - Pandas DataFrame TypeError:“类型”对象不可下标
- ruby-on-rails - 如何用值破坏模型
- html - 使用PHP加载数据后JQuery DataTable清除数据
- javascript - onerror javascript只加载一次?
- sql - 使用 CASE 时按列值分组
- sql - 如何在同一服务器上的两个 Azure 数据库之间将一个表数据复制到另一个表数据
- angular - 获取导航链接的 id
- python - 无法使用请求从网页中获取表格内容
- bash - 如何为所有部分文件名匹配创建符号链接?
- powershell - powershell 和 windows 资源管理器(属性)有不同的计数结果