Statistics

Problem Statement for "SimpleCalculator"

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.

Definition

Class:
SimpleCalculator
Method:
calculate
Parameters:
String
Returns:
int
Method signature:
int calculate(String input)
(be sure your method is public)

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

  1. "5/3"

    Returns: 1

    Remember integer division is used, so results are truncated.

  2. "15*3"

    Returns: 45

  3. "1-10000"

    Returns: -9999

    Negative results are allowed.

  4. "17+18"

    Returns: 35

  5. "0000000000000018/00000000000000000009"

    Returns: 2

    The long way of writing 18/9.

  6. "0000010000*10000"

    Returns: 100000000

  7. "10000/1"

    Returns: 10000

  8. "000000000000000000000000000000000000000000000001+3"

    Returns: 4

  9. "2334-002340"

    Returns: -6

  10. "9998*9997"

    Returns: 99950006

  11. "1231/002044"

    Returns: 0

  12. "19/000000000000000000000000000000000002"

    Returns: 9

  13. "000000010000+00000000010000"

    Returns: 20000

  14. "99/11"

    Returns: 9

  15. "99/12"

    Returns: 8

  16. "094-00094"

    Returns: 0

  17. "000000238+00000002142"

    Returns: 2380

  18. "23-000000000002234"

    Returns: -2211

  19. "09876+01234"

    Returns: 11110

  20. "09876*01234"

    Returns: 12186984

  21. "09876/01234"

    Returns: 8

  22. "09876-01234"

    Returns: 8642

  23. "0000000000000018/00000000000000000009"

    Returns: 2

  24. "1/1"

    Returns: 1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: