0

I tried taking the factorial "implementation" up and down but * (multiplication) and / (division) always comes before it. For example, if I enter 2*3!, I want the computer to read 2*(3!) not (2*3)!. The computer produces (2*3)!

I have tried switching (up and down the code) where the factorial is but that is not doing the trick.

switch (t.kind) {
    case '!': {
        double d = left;
        left = 1;
        double limit = d;
        for (int i = 0; i < limit; ++i) {
            left *= d;
            d = d - 1;
        }
        t = ts.get();
        break;
    }
    case '/': {
        double d = primary();
        if (d == 0) error("division by zero");
        left /= d;
        t = ts.get();
        break;
    }
    case '*':
        left *= primary();
        t = ts.get();
        break;
    default:
        ts.putback(t);
        return left;
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Lekan
  • 115
  • 1
  • 6
  • Also I don't fully understand your question. Switching the order of your case statements will not change the calculation. If that is what you were getting at. – drescherjm Oct 22 '19 at 00:27
  • 3
    Your code needs to account for operator precedence and not just evaluate operators in the order they appear in the expression. – Blastfurnace Oct 22 '19 at 00:58
  • 2
    If you are simply reading through the string, it is very hard to do this type of precedence. You probably need to [build a parser](https://stackoverflow.com/questions/56639607/is-returning-a-dereferenced-pointer-as-a-reference-from-a-function-undefined-beh). –  Oct 22 '19 at 01:20
  • With a limited number of available operators (and precedences), it can be solved with reading from left to right and storing intermediate results in case. (This is the way, ancient pocket calculators managed precedence.) One example: [SO: How to make C find all different symbol (+,-,*,/) combinations to have a certain output](https://stackoverflow.com/a/49921489/7478597) (The `solve()` function is the relevant part.) – Scheff's Cat Oct 22 '19 at 06:20
  • I have done some editing in your post! Hopefully, it is now a bit clearer and easier for others to read. – Adrian Mole Oct 22 '19 at 20:14
  • Welcome to Stack Overflow. Please read the [**About**](http://stackoverflow.com/tour) page soon and also visit the links describing [**How to Ask a Question**](http://stackoverflow.com/questions/how-to-ask) and [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). Providing the necessary details, including your code, compiler warnings and associated errors, if any, will allow everyone here to help you with your question. – David C. Rankin Oct 22 '19 at 20:22
  • It looks like this is a code fragment that's a part of a larger system to scan and parse things. Can you share the rest of the code with us? I suspect that the issue here is that you're editing the wrong part of the program. – templatetypedef Oct 22 '19 at 22:02
  • I tried to add the whole code but I do not know how to do that. All I am trying to do is change the order with witch the calculation goes. – Lekan Oct 26 '19 at 09:45
  • I I think the problem is that I don't know how the parser works – Lekan Oct 26 '19 at 12:00

0 Answers0