-1

just a quick question. I have a school project which I have to solve the problem again as a new separate problem. Provide the tax calculation using only one line of code and without using any if/else or switch statements or using the ternary operator. I may be able to do the calculations i one line of code but I have no idea what to write if I am supposed to get rid of the conditional statements. Below is my code that I am supposed to change. My teacher is kind of picky about what we can use and what not. Also this project was given to us when we hadn't learned functions in all that. I am looking for an answer that is not too higher level, since I am new to c++. /***************************************************************

Write a program for a state that computes tax according to the rate schedule:• No tax on first $15,000 of income• 5% tax on each dollar from $15,001 to $25,000• 10% tax on each dollar over $25,000 ***************************************************************/

   #include <iostream>
        #include <string>
        #include <sstream>
        #include <iomanip>

        using namespace std;
        int main()
        {
            string income2;
            double income;
            const double FIRSTTAX = 500;
            double secondDifference;
            const double FTAX = 0.05;
            const double TTAX = 0.1;
            cout << "Please enter the annual income: $ ";
            getline (cin, income2);
            stringstream (income2)>> income;


                if (income == 0 || income <= 15000 )
                {
                    cout << "No income on first 15000 " << endl;
                }
            else if (income > 15000 && income <= 25000)
                {
                    income = (income - 15000)* FTAX;
                    cout << " Your net income " << income <<endl;

                }
             else if (income > 25000)
                {
                    secondDifference = (income - 25000) * TTAX +(income - 15000- (income - 25000)) * FTAX;



                    cout << " Your net income " << income <<fixed<<setprecision(2) <<endl;

                    cout << " Your tax " << secondDifference <<fixed<<setprecision(2) << endl;
}




        return 0;
    }
Mooshak
  • 1
  • 2

2 Answers2

1

In one single line:

tax = max(income-15000,0)*FTAX + max(income-25000,0)*(TTAX-FTAX)
shrike
  • 4,449
  • 2
  • 22
  • 38
0

To account for the two different tax rates, and to allow either tax rate to vary individually:

double lo_thresh = 15000;
double hi_thresh = 25000;

double lo_rate = 0.05;
double hi_rate = 0.10;

double tax = lo_rate * min(hi_thresh - lo_thresh, max(income - lo_thresh, 0)) +
             hi_rate * max(income - hi_thresh, 0);

The first half looks at income in excess of 15k, but caps the amount above that at which that amount applies to 10k (i.e. the difference between the two thresholds).

The second half calculates the higher tax rate due on income above 25k.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • imho, `min()` is useless – shrike Apr 30 '16 at 23:33
  • @Alnitka; yet, my proposal using 2 max, no min, seams to work... or maybe I missed something in the OP's question... – shrike Apr 30 '16 at 23:41
  • 1
    It's not useless, it's just a different formulation. This one (IMHO) more accurately describes the rule, i.e "you pay X% tax between A and B, and Y% tax above B". Yours is equivalent, but semantically is closer to "you pay X% tax on _everything_ above A, and (Y-X)% tax _on top of that_ for everything above B" – Alnitak Apr 30 '16 at 23:46
  • and FWIW, in the UK where I am the HMRC tax calculations are always shown like this - you see how much tax you're paying in the lower band at that rate, and then a separate line for how much you're paying on income in excess of the upper threshold. They never treat the higher rate as an "difference" on top of the lower rate. – Alnitak Apr 30 '16 at 23:52
  • not exactly, develop the formula and you get: X% between A and B, and Y% above B ; ie: `(max(income-15000,0)-max(income-25000,0))*FTAX + max(income-25000,0)*TTAX` . Anyway, have a good night, bye. – shrike Apr 30 '16 at 23:58
  • @shrike that's what I said - "X% on _everything_ above A" (thus X% + Y% - X%, i.e. Y% net on everything above B) – Alnitak May 01 '16 at 08:41