5
Forget BIDMAS! Write a program that takes an equation and an operator precedence order, and prints the result.
Example input format:
1.2+3.4*5.6/7.8-9.0 */+-
Rules & guidelines:
- The only operators that are defined are addition (+), subtraction (-), multiplication (*), and division (/). No parentheses, no exponentiation.
- Associativity is always left-to-right. For example,
10/4/2is to be interpreted as(10/4)/2with a result of 1.25, rather than10/(4/2). - The input format is as follows:
- An equation, followed by a space, followed by the operator precedence specification (or two string arguments, one for the equation and the other for the precedence).
- The equation comprises base-10 decimal numbers separated by operators, with no spaces. Integer values do not have to contain a period character, i.e. both
5and5.0are to be accepted values. - For simplicity, negative numbers may not be included in the input, e.g.
6/3is valid but6/-3is not. Input also may not contain a leading or trailing operator, so-6/3isn't considered valid, nor is6-3+. - The precedence specification string is always 4 characters long and always contains the characters
+,-,/, and*once each. Precedence is read as left-to-right, e.g.*/+-specifies multiplication with the highest precedence, division next, then addition, and finally subtraction. EDIT: It is acceptable to take the precedence string in reverse order (lowest to highest) as long as you specify this in your answer.
- Input is a string to be taken via command line arguments, STDIN, or the default input format in programming languages that do not support these input methods.
- You are free to assume that the given input will be in the correct format.
- Output is via STDOUT or your language's normal output method.
- The printed result should be in base-10 decimal.
- Results must be computed to at least 4 decimal points of accuracy when compared to a correct implementation that uses double precision (64-bit) floating point arithmetic. This degree of freedom is designed to allow for the use of fixed-point arithmetic in languages that have no floating-point support.
- Divide by zero, overflow, and underflow are undefined behaviour. Your code is free to assume that no inputs will be given that will trigger these cases.
- You may not call out to any external services (e.g. Wolfram Alpha)
- You may not call out to any programs whose primary function is to solve these types of problems.
Test cases:
6.3*7.8followed by any operator precedence specification prints 49.142.2*3.3+9.9/8.8-1.1 */+-is parsed as((2.2*3.3)+(9.9/8.8))-1.1and should print 7.2852.2*3.3+9.9/8.8-1.1 +*/-is parsed as((2.2*(3.3+9.9))/8.8)-1.1and should print 2.210/2+5-1 +-/*is parsed as10/((2+5)-1)and the printed result should be 1.6666666...2147480/90+10*5 +/-*is parsed as(2147480/(90+10))*5and the printed result should be 1073743*55-5/8/4+1 -/+*is parsed as3*((((55-5)/8)/4)+1)should print 7.6875- An input containing one thousand instances of the number
1.015separated by multiplier operators (i.e. the expanded multiplicative form of1.015^1000), followed by any operated precedence specification, should print a number within 0.0001 of 2924436.8604.
Code golf, so shortest code wins.
"BIDMAS" is "PEMDAS" in the US because why wouldn't it be? – Giuseppe – 2017-11-27T15:06:32.323
It's also taught as "BODMAS" in some UK schools (Brackets Orders). – Polynomial – 2017-11-27T15:08:00.850
1Related. – user202729 – 2017-11-27T15:14:59.490
3Also related. – Arnauld – 2017-11-27T15:17:39.527
The fact that you can't have two operators have the same precedence (like
*/and+-in normal math) saddens me :( oh well :P – HyperNeutrino – 2017-11-27T15:22:40.633Can I take the operator precedence in reverse order? – HyperNeutrino – 2017-11-27T15:23:14.440
Can I take 5 arguments to a function as the expression and then the 4 operators in their order of precedence? – HyperNeutrino – 2017-11-27T15:26:10.640
@HyperNeutrino Input is a string in the specified format only. – Polynomial – 2017-11-27T15:27:04.093
3
@Polynomial cough
– NieDzejkob – 2017-11-27T15:34:03.2832@NieDzejkob In this case I think the input format is flexible enough, and HyperNeutrino's request to allow 5 arguments to a function as the expression removes a lot of the parsing work that makes this challenge interesting. Operator precedence in reverse order is acceptable, though, now that I think about it more. – Polynomial – 2017-11-27T15:47:36.827
1@Polynomial I also think that five arguments is too much, but allowing two strings, one for the formula and one for the operators, seems reasonable. Splitting at white space is probably not the interesting part of parsing. – Laikoni – 2017-11-27T15:56:25.383
1@Laikoni Also acceptable. Edited to reflect. – Polynomial – 2017-11-27T16:03:14.903
Can intermediate results be negative? For example, is something like
5-8+4 -+/*(=-3+4=1) a possible input to the program? – Jonathan S. – 2017-11-27T20:11:26.957@Polynomial The parsing of I/O is NOT the interesting part of challenges. – mbomb007 – 2017-11-27T22:59:12.820
@JonathanS. Yes – Polynomial – 2017-11-27T23:33:47.203
The parsing work does not make the challenge interesting. The parsing methods should be completely answerer-defined IMO. It's the actual challenge, not the I/O, that makes the challenge interesting. – MD XF – 2017-11-28T05:27:25.933
I think your profile picture perfectly relates how most of us feel about the I/O :P (no offense meant, just a jest) – MD XF – 2017-11-28T05:35:38.053