Jump to content

Programming Challenge: Text-Based Calculator (with History) (Jan 8, 2025)


Recommended Posts

Posted

Challenge:

Create a command-line calculator that performs basic arithmetic operations and keeps a history of calculations during the session.

Basic Requirements:

  1. Allow the user to perform addition, subtraction, multiplication, and division.
  2. Accept input in the format: number operator number (e.g., 5 + 3).
  3. Display the result of each calculation.
  4. Store the history of calculations in a list and display it upon request.

Bonus Features:

  • Add support for more complex operations like square roots, exponents, and percentages.
  • Implement input validation to handle errors (e.g., dividing by zero).
  • Allow the user to save the history to a file for future reference.

Example Output:

Welcome to the Text-Based Calculator!
Enter your calculation (or type 'history' to view past calculations, 'help' for help, 'exit' to quit):
> 5 + 3
Result: 8

> 10 / 2
Result: 5

> sqrt(144)
Result: 12

> history
1. 5 + 3 = 8
2. 10 / 2 = 5
3. sqrt(144) = 12

> help
Basic Operations:
  num + num (e.g., 5 + 3) - Addition
  num - num (e.g., 10 - 4) - Subtraction
  num * num (e.g., 6 * 7) - Multiplication
  num / num (e.g., 20 / 4) - Division
  num % num (e.g., 10 % 3) - Modulus
  num ^ num (e.g., 5 ^ 5) - Power and Exponents

Advanced Functions:
  sqrt(num) (e.g., sqrt(16)) - Square Root
  cubert(num) (e.g., cubert(27)) - Cube Root
  log(num) (e.g., log(1000)) - Logarithm
  ln(num) (e.g., ln(2.71828)) - Natural Logarithm
  sin(num), cos(num), tan(num) (e.g., sin(30)) - Trigonometric Functions (Degrees or Radians)
  asin(num), acos(num), atan(num) (e.g., asin(0.5)) - Inverse Trigonometric Functions
  abs(num) (e.g., abs(-10)) - Absolute Value
  fact(num) (e.g., fact(5)) - Factorial
  gcd(num, num) (e.g., gcd(48, 18)) - Greatest Common Divisor
  lcm(num, num) (e.g., lcm(4, 5)) - Least Common Multiple
  rand(min, max) (e.g., rand(1, 100)) - Random Number Generator
  percent(num, num) (e.g., percent(20, 200)) - Percentage
  isprime(num) (e.g., isprime(17)) - Prime Check
  fib(num) (e.g., fib(10)) - Fibonacci Sequence

> exit

 

CodeName: Jessica

💻 Linux Enthusiast | 🌍 Adventurer | 🦄 Unicorn 
🌐 My Site | 📢 Join the Forum

spacer.png

 

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use Privacy Policy Guidelines We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.