IITKESO207A_2P - Calculator

no tags 

The input is an arithmetic expression in infix notation, for example, 24 * (3 + 5)/7%3. The problem is to write a program that reads the expression given over multiple lines, parses it, evaluates it and gives the output value as a floating point integer or prints the string “Malformed expression” if the input is malformed. The numbers in the expression are integers but the ouput can be floating point. The operators come from the set {+, −, *, /, %, (, )}. The operators act as per their usual definitions. We assume the usual precedence among operators, that is, from lowest to highest, {+, −}, then, *, followed by {/, %} and parenthesis, where, + and binary − are at the same level of precedence and have left to right associativity, so do {/, %}.

Note

The three problems given in the assignment have been merged. 75% of the test cases will check if your code works for problem 1 and 2 and 25% of the test cases will check for problem 3. The result that you will get will be between 0 and 100, denoting the number of testcases that your code passed. We will consider your maximum score while grading.

Update 1: (7 Sep, 2017)

For negative numbers, the '-' and the number will be in separate lines. It is your job to figure out whether it is a unary minus or binary.

Update 2: (13 Sep, 2017 3:15PM)

Earlier you were supposed to print string "Malformed expression" without new line at the end. Sorry, for not being explicit about this fact. But from now onwards, new line at the end of "Malformed expression" won't matter.

Input

The first line contains 'n', the number of tokens in the expression. 'n' lines follow each containing a either a number or an operator.

Output

This should be a single real number. Errors less than 10^-2 will be ignored.

Constraints

1<= n <= 20

Inputs will be between -1000 to 1000.

Examples

Input 1:
4
4
-
-
3

Output 1:
7
Input 2:
7
3
/
(
2
+
1
)

output 2:
1
Input 3:
3
1
+
(

Output 3:
Malformed expression

hide comments
Programming Club, IITK: 2017-09-08 06:47:44

@bansalm : % won't work. But do you really think that there is no mod function for floating point values?

baapsinghcoder: 2017-09-08 00:22:19

'(' & ')' are the types of parenthesis and not curly or square brackets correct?

poopybutthole: 2017-09-07 20:17:20

-8/-(9*-4/2)+20
There is something wrong with your solution.
I don't know whether your code is correct or not, but the test cases are allowing erroneous codes to get 100.
Check the expression I have given. The correct answer is 19.5556 and a solution giving 0.210526 as answer is getting 100.
My code is showing 75, whereas some erroneous ones are 100 in submission.

Last edit: 2017-09-07 21:24:57
bansalm: 2017-09-07 16:00:22

How can we get floating point output, as 4/3 is a floating number and now % won't work with floating numbers
e.g 4/3%10

Programming Club, IITK: 2017-09-07 14:19:24

@jyotshak: There will be no unnecessary operators or brackets in the testcases.

jyotshak: 2017-09-07 13:44:25

Can some more test cases be given for the unary operator part? Just for debugging of our own code.
for example, is this a correct expression
2+3------(5/---6)
because this is mathmetically correct, as it can be simplified to 2+3+(5/-6) = 4.16667

sarthmit: 2017-09-07 13:26:52

@Programming Club, IITK : can you please mention that the question requires the output "Malformed expression" without a new line?

Last edit: 2017-09-07 13:33:05
Programming Club, IITK: 2017-09-07 10:22:49

@sarthmit : nice question. Please check the update portion.

sarthmit: 2017-09-07 10:08:37

Is any input line allowed to have a number like -100,
Or will it always be in two lines:
-
100


Added by:Programming Club, IITK
Date:2017-09-07
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All