Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed May 5, 2024
1 parent cbf5363 commit 7a35f76
Show file tree
Hide file tree
Showing 11 changed files with 922 additions and 2 deletions.
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
77 changes: 77 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '43 7 * * 4'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'cpp' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Use only 'java' to analyze code written in Java, Kotlin or both
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:

- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
56 changes: 56 additions & 0 deletions .github/workflows/windows-mingw.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: MinGW

on:
workflow_dispatch:
push:
branches:
- 'master'
pull_request:
branches:
- 'master'

env:
# Path to the solution file relative to the root of the project.
SOLUTION_FILE_PATH: .

# Configuration type to build.
# You can convert this to a build matrix if you need coverage of multiple configuration types.
# https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
BUILD_CONFIGURATION: Debug

permissions:
contents: read

jobs:
build:
runs-on: windows-latest

strategy:
matrix:
include:
- { sys: mingw64, env: x86_64 }

defaults:
run:
shell: msys2 {0}

steps:
- uses: msys2/setup-msys2@v2
with:
msystem: ${{matrix.sys}}
update: true
install: >-
mingw-w64-${{matrix.env}}-openssl
base-devel
mingw-w64-${{matrix.env}}-cmake
mingw-w64-${{matrix.env}}-gcc
- name: Checkout repository
uses: actions/checkout@v3

- name: Build Code
run: |
mkdir build
cd build
cmake ..
cmake --build .
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app

# CMake
build/
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.20)
project(calculator VERSION 1.0.0 LANGUAGES CXX)

option(BUILD_TESTS "Build tests" ON)
option(BUILD_EXAMPLES "Build examples" ON)
option(BUILD_BENCHMARKS "Build benchmarks" ON)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

add_library(calculator INTERFACE)
target_include_directories(calculator INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.0.0")
message(FATAL_ERROR "GCC/Clang version must be at least 8.0.0")
endif()
elseif(MSVC)
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0.0")
message(FATAL_ERROR "MSVC version must be at least 19.0.0")
endif()
endif()

install(FILES calculator.hpp DESTINATION calculator)

if (BUILD_EXAMPLES)
add_executable(example example/main.cpp)
target_link_libraries(example PRIVATE calculator)
endif()

if (BUILD_TESTS)
enable_testing()
find_package(GTest REQUIRED)
add_executable(calc_test test/main.cpp)
target_link_libraries(calc_test PRIVATE calculator GTest::gtest)
add_test(NAME CalcTest COMMAND calc_test)
endif()

if (BUILD_BENCHMARKS)
find_package(benchmark REQUIRED)
add_executable(benchmark benchmark/main.cpp)
target_link_libraries(benchmark PRIVATE calculator benchmark::benchmark)
endif()
112 changes: 110 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,110 @@
# calculator
Calculator for Modern C++
# Modern C++ Calculator

This is a modern C++ implementation of a calculator that can evaluate mathematical expressions. It supports various arithmetic operations, bitwise operations, and follows the standard order of operations.

> [!IMPORTANT]
> This library is still under development and may contain bugs.
## Features

- Supports basic arithmetic operations: addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), and modulo (`%`)
- Supports bitwise operations: OR (`|`), XOR (`^`), AND (`&`), left shift (`<<`), and right shift (`>>`)
- Supports exponentiation: power (`**`) and scientific notation (`e` or `E`)
- Handles unary operators: unary plus (`+`), unary minus (`-`), and bitwise NOT (`~`)
- Follows the standard order of operations and operator precedence
- Supports parentheses for grouping subexpressions
- Provides error handling and informative error messages
- Supports different numeric types: `int`, `float`, and `double`
- Lightweight and header-only implementation

## Usage

1. Include the `calculator.hpp` header file in your C++ project.

2. Use the `calculator::eval` function to evaluate mathematical expressions:

```cpp
#include "calculator.hpp"
#include <iostream>

int main() {
std::string expr = "12.34 + 56.78";
double result = calculator::eval(expr);
std::cout << "Result: " << result << std::endl;

try {
std::string expr2 = "12.34 + 56.78";
int result2 = calculator::eval<int>(expr2);
std::cout << "Result: " << result2 << std::endl;
} catch (const calculator::error& e) {
std::cerr << e.what() << std::endl;
}

}
```

Output:

```txt
Result: 14
Integer Result: 12
```

## Supported Operations

The calculator supports the following operations:

- Arithmetic Operations:
- Addition: `+`
- Subtraction: `-`
- Multiplication: `*`
- Division: `/`
- Modulo: `%`
- Bitwise Operations:
- Bitwise OR: `|`
- Bitwise XOR: `^`
- Bitwise AND: `&`
- Left Shift: `<<`
- Right Shift: `>>`
- Exponentiation:
- Power: `**`
- Scientific Notation: `e` or `E`
- Unary Operators:
- Unary Plus: `+`
- Unary Minus: `-`
- Bitwise NOT: `~` (only supported for `int` type)

## Error Handling

The calculator provides error handling and throws exceptions of type `calculator::error` in case of syntax errors or invalid expressions. The error message includes information about the unexpected token and its position in the expression.

## Numeric Types

The calculator supports different numeric types:

- `int`: Integers
- `float`: Single-precision floating-point numbers
- `double`: Double-precision floating-point numbers

By default, the `calculator::eval` function uses `double` as the result type. To use a different numeric type, you can explicitly specify the type as a template argument:

```cpp
int result = calculator::eval<int>("1 + 2");
float result = calculator::eval<float>("3.14 * 2");
```

Note that bitwise operations and modulo are only supported for integer types.

## Limitations

- The calculator assumes that the input expression is well-formed and does not contain any invalid characters or unsupported operations.
- The calculator does not support variables or functions.
- The maximum length of numbers is limited by the precision of the chosen numeric type.

## License

This calculator implementation is released under the [MIT License](LICENSE).

## Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
Loading

0 comments on commit 7a35f76

Please sign in to comment.