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 assignments 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.
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 number. Errors less than 10^-2 will be ignored. 
Constraints
1<= n <= 20
Inputs will be between -1000 to 1000 
Example
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

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 

 

Example

 

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
suds13: 2017-09-09 13:49:35

@pclubiitk Is 2 - 2 a malformed input or can we take it as -2 ?

harshith_reddy: 2017-09-09 09:10:49

should 1/2 be evaluated as 0 or 0.5?

nikhilbl: 2017-09-09 08:46:02

What will be the answer of 1---2 ? Malformed expression or -1 ?

bhavy: 2017-09-09 04:25:22

Last edit: 2017-09-09 06:29:21
sshreya: 2017-09-08 22:44:13

Last edit: 2017-09-08 22:44:51
Programming Club, IITK: 2017-09-08 19:36:06

@makkarr: Same line.
@nihir16: Your code works for 50% of test cases, and you will receive 50% marks in the assignment.
@jyotshak: We cannot provide more cases
@somjain: There won't be any expression which requires division by zero. You can ignore this part.

somjain: 2017-09-08 13:59:52

Is output for 4/0 is Malformed expression

jyotshak: 2017-09-08 13:53:41

Can some more test cases be given for the unary operator? I ran my code on multiple expressions and the output is correct yet on submission the 25 points of unary - test cases are missing. ( I know that it is specifically the unary that is causing the problem because even without the unary implementation it was 75)
It would be really helpful if just a few tricky test cases were given as I haven't been able to come up with any expression that my code has evaluated incorrectly.

nihir16: 2017-09-08 13:46:36

@Programming Club, IITK : What does a score of 50 indicate?

makkarr: 2017-09-08 13:35:07

if one of the input numbers is say 25, then will 2 and 5 be in different lines or the same line?


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