Connection Status:
Competition Arena > SimpleCalculator
SRM 178 · 2004-01-07 · by brett1479 · String Parsing
Class Name: SimpleCalculator
Return Type: int
Method Name: calculate
Arg Types: (string)
Problem Statement

Problem Statement

A simple calculator accepts the following kinds of strings as input:
  • 1) NUM+NUM
  • 2) NUM-NUM
  • 3) NUM*NUM
  • 4) NUM/NUM
where NUM is a positive integer, between 1 and 10000 inclusive that can contain leading zeros. Return the value produced by the given expression. Here +,-,*, and / denote addition, subtraction, multiplication and division respectively. All operations are done on integers, so "5/3" returns 1.

Constraints

  • input will contain between 3 and 50 characters inclusive.
  • input will have the form where is a positive integer between 1 and 10000 inclusive, that may contain leading zeros and is one of (quotes for clarity) '+','*','-', or '/'.
  • input will not contain any spaces.
Examples
0)
"5/3"
Returns: 1

Remember integer division is used, so results are truncated.

1)
"15*3"
Returns: 45
2)
"1-10000"
Returns: -9999

Negative results are allowed.

3)
"17+18"
Returns: 35
4)
"0000000000000018/00000000000000000009"
Returns: 2

The long way of writing 18/9.

Submissions are judged against all 24 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SimpleCalculator with a public method int calculate(string input) · 24 test cases · 2 s / 256 MB per case

Submitting as anonymous