Calculator

How to setup a c++ debug environment for vscode (Optional)

Gist

REST API Calculator

This is a simple REST API calculator built using the Crow C++ microframework. It supports basic arithmetic operations, trigonometric functions, and compound interest calculations.

Base URL

http://localhost:18080

Endpoints

1. Basic Math Operations

Endpoint: /basic_math Method: GET Description: Perform basic arithmetic operations (addition, subtraction, multiplication, division).

Request Body:

{
    "operation": "add" | "subtract" | "multiply" | "divide",
    "a": <number>,
    "b": <number>
}

Example Request:

curl -X GET -d '{"operation": "add", "a": 5, "b": 3}' http://localhost:18080/basic_math

Response:

{
    "result": <number>
}

Possible Arguments:

2. Trigonometric Functions

Endpoint: /trig_functions Method: GET Description: Perform trigonometric functions (sine, cosine, tangent).

Request Body:

{
    "operation": "sin" | "cos" | "tan",
    "angle": <number>
}

Example Request:

curl -X GET -d '{"operation": "sin", "angle": 1.5708}' http://localhost:18080/trig_functions

Response:

{
    "result": <number>
}

Possible Arguments:

3. Compound Interest Calculation

Endpoint: /compound_interest Method: GET Description: Calculate compound interest.

Request Body:

{
    "principal": <number>,
    "rate": <number>,
    "times": <integer>,
    "years": <integer>
}

Example Request:

curl -X GET -d '{"principal": 1000, "rate": 0.05, "times": 4, "years": 5}' http://localhost:18080/compound_interest

Response:

{
    "result": <number>
}

Possible Arguments:

Example Requests

  1. Basic Math Operations (/basic_math):
    curl -X GET -d '{"operation": "add", "a": 5, "b": 3}' http://localhost:18080/basic_math
    
  2. Trigonometric Functions (/trig_functions):
    curl -X GET -d '{"operation": "sin", "angle": 1.5708}' http://localhost:18080/trig_functions
    
  3. Compound Interest Calculation (/compound_interest):
    curl -X GET -d '{"principal": 1000, "rate": 0.05, "times": 4, "years": 5}' http://localhost:18080/compound_interest
    

Compilation and Running

Make sure you have the Asio library included. Here is an example Makefile:

CXX = g++
CXXFLAGS = -std=c++11 -pthread -I/path/to/asio/include

TARGET = calculator
SRCS = main.cpp

all: $(TARGET)

$(TARGET): $(SRCS)
	$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRCS)

clean:
	rm -f $(TARGET)

Replace /path/to/asio/include with the actual path to the Asio include directory.

Compile and run the project:

make
./calculator

This will start the Crow web server on port 18080, and you can send GET requests with a body to the respective endpoints for different types of calculations.