(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
③
Please enter your income: 0
Your income is: 0, income tax payable: 0.00 yuan.
④
Please enter your income: 800
Your income is: 800, income tax payable: 0.00 yuan.
⑤
Please enter your income: 801
Your income is: 801, income tax payable: 0.03 yuan.
⑥
Please enter your income: 2000
Your income is: 2000. Income tax payable: $36.00.
(2000 – 800) × 3 / 100
⑦
Please enter your income: 1999
Your income is: 1999, income tax payable: $35.97.
⑧
Please enter your income: 5000
Your income is: 5000, income tax payable: $156.00.
[(2000 – 800) × 3 + (5000 – 2000) × 4] / 100
Please enter your income: 10000
Your income is: 10000, income tax payable: 306.00 yuan.
[(2000 – 800) × 3 + (5000 – 2000) × 4 + (10000 – 5000) × 3] / 100
⑨
Please enter your income: -1
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) { cout << "Please enter your income (enter-1 quit):"; 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; } }