# Calculate income tax

(1) Problem description
A country's rules for calculating income tax consist of the following indeterminate rules:
M1, R1
M2, R2
...
Mn, Rn
Among them, n is the number of rules, input by the user. For any 1<=i<=n, Ri represents the tax rate for the portion of personal income in the interval [Mi, Mi+1), which is an integer representing a few percent. M1 is the tax threshold, M0=0, R0=0.
(2) Question requirements
① Please define a structure to store a rule.
② According to the number of rules entered by the user, use the array of structures to store all the rules.
③ Define a sub-function inputRules(struct Rule rules[], int n), according to the number of rules entered by the user, get all the rules from the keyboard and store them in an array.
④ Define a sub-function to calculate income tax.
// parameter income represents personal income
double computeTax(struct Rule rules[], int n, int income);
⑤ In the main function, after the rule is entered once, the income tax of any number of individuals can be calculated cyclically until the entered income is -1.
⑥ Please test at least the following

• After entering the rules, when entering income, directly enter -1;
• income is 0;
• Income is less than M1;
• Income equals Mi in various situations;
• Income is greater than Mn;
• The case where the income is between Mn−1 and Mn.

(3) Program running requirements
The result of one run of the program is as follows:

Please enter the number of rules: 3
Please enter rule 1: 800 3
Please enter rule 2: 2000 4
Please enter rule 3: 5000 3

The tax rules are as follows:
tax line tax rate
800 3
2000 4
5000 36

Your income is: 0, income tax payable: 0.00 yuan.

Your income is: 800, income tax payable: 0.00 yuan.

Your income is: 801, income tax payable: 0.03 yuan.

Your income is: 2000. Income tax payable: \$36.00.
(2000 – 800) × 3 / 100

Your income is: 1999, income tax payable: \$35.97.

Your income is: 5000, income tax payable: \$156.00.
[(2000 – 800) × 3 + (5000 – 2000) × 4] / 100
Your income is: 10000, income tax payable: 306.00 yuan.
[(2000 – 800) × 3 + (5000 – 2000) × 4 + (10000 – 5000) × 3] / 100

goodbye
Please press any key to continue...

```#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Rule {          // A structure representing a rule
double taxLine;    // tax line
double taxRate;    // tax rate
};

// According to the number of rules entered by the user, all rules are obtained from the keyboard and stored in an array.
void inputRules(struct Rule rules[], int num);

// A function to calculate income tax.
double computeTax(struct Rule rules[], int num, double income);

// According to the number of rules entered by the user, print all the rules to the screen.
void printRules(struct Rule rules[], int num);

int main() {
int ruleNumber;
double income;
double tax;
struct Rule myrules[50];

while (true) {
cout << "Please enter the number of rules:";
cin >> ruleNumber;
if (ruleNumber > 0) {
break;
} else {
cout << "Invalid input, please enter a positive integer!" << endl;
}
}
inputRules(myrules, ruleNumber);
printRules(myrules, ruleNumber);
while (true) {
while (true) {
cin >> income;
if (income > 0 || income == -1) {
break;
} else {
cout << "Invalid input, please enter a positive number!" << endl;
}
}
if (income == -1) {
break;
}
tax = computeTax(myrules, ruleNumber, income);
cout << "Your income is:" << setprecision(2) << fixed << income << "Yuan, income tax payable:"
<< setprecision(2) << fixed << tax << "Yuan." << endl;
}
cout << "Thanks for using, bye!";
return 0;
}

void inputRules(struct Rule rules[], int num) {
for (int i = 0; i < num; i++) {
cout << "Please enter the " << i + 1 << " Rule (tax line tax rate):";
cin >> (rules + i)->taxLine >> (rules + i)->taxRate;
}
}

double computeTax(struct Rule rules[], int num, double income) {
double sum = 0;
int i;
if (income > rules->taxLine) {
for (i = 1; i < num; i++) {
if (income < (rules + i)->taxLine) {
sum += (income - (rules + i - 1)->taxLine) * (rules + i - 1)->taxRate / 100;
break;
} else {
sum += ((rules + i)->taxLine - (rules + i - 1)->taxLine) * (rules + i - 1)->taxRate / 100;
}
}
if (i == num) {
sum += (income - (rules + i - 1)->taxLine) * (rules + i - 1)->taxRate / 100;
}
}
return sum;
}

void printRules(struct Rule rules[], int num) {
cout << "The tax rules are as follows:" << endl;
cout << "   tax line     tax rate   " << endl;
for (int i = 0; i < num; i++) {
cout << setw(8) << (rules + i)->taxLine << setw(8) << (rules + i)->taxRate << endl;
}
}
```

Posted by cjones on Tue, 30 Aug 2022 22:20:50 +0530