This C program revolutionizes mathematical computations by interpreting strings, offering a seamless and intuitive experience for complex number operations. Ideal for both educational and professional use, it's demonstrate C programming concepts such as pointers, structs, and dynamic memory allocation.
This program was created as a homework assignment for the System programming Laboratory course (20465) at the Open University of Israel, and earned a score of 98
.
- Features ✨
- Usage 🚀
- New GUI! 🖥️
- Commands ✍️
- Testing 🧪
- New: Expression Parser and Evaluator 🧮
- New: Reversed Polish Calculator 🧮
- Contributing 🤝
- License 📜
Update: This repository now includes a new expression parser and evaluator written in C: calc.c. This program parses mathematical expressions and evaluates them using an expression tree. It can be used as a standalone program or as a library for other projects. See the Expression Parser and Evaluator section for more details.
Update2: A new reversed polish calculator features dynamic stack allocation was added to the repository PRN.c. See the Reversed Polish Calculator section for more details.
- Advanced Pipeline & Multi-Threading 🚀: Seamlessly manage inputs/outputs with ComplexCalculatorC's cutting-edge pipeline. Perfect as a standalone calculator or as a part of other projects, it's boosted by multi-threading for speedy calculations. Enjoy easy inputs and calculations with a straightforward command-line interface, designed for all user levels and adaptable to any other project by the STDIN/STDOUT pipeline: the functions have no idea that the input is redirected!
- Complex Number Operations 🔢: Tackle everything from simple additions to absolute value calculations with ease, thanks to a wide range of supported complex number operations. Includes Multiple Variable Support: Handle up to six complex variables (A-F) simultaneously, ideal for intricate, multi-variable mathematical tasks.
-
Building the Program:
To compile the program, use the following command:
make
ComplexCalculatorC now includes a graphical user interface (GUI) for a more intuitive user experience. The GUI is built using the Gtk+
library and allows you to perform complex number calculations and explore mathematical concepts in a visual environment.
ComplexCalculatorC supports a variety of commands for complex number operations, enhancing mathematical exploration:
Command | Syntax | Description |
---|---|---|
print_comp |
print_comp <variable> |
Prints the specified complex variable. |
abs_comp |
abs_comp <variable> |
Calculates and displays the absolute value of a complex variable. |
mult_comp_real |
mult_comp_real <variable>, <real_number> |
Multiplies a complex variable by a real number and displays the result. |
mult_comp_img |
mult_comp_img <variable>, <imaginary_number> |
Multiplies a complex variable by an imaginary number. |
read_comp |
read_comp <variable>, <real_part>, <imaginary_part> |
Assigns real and imaginary parts to a complex variable. |
add_comp |
add_comp <variable1>, <variable2> |
Adds two complex variables and prints the outcome. |
sub_comp |
sub_comp <variable1>, <variable2> |
Subtracts the second complex variable from the first and displays the result. |
mult_comp_comp |
mult_comp_comp <variable1>, <variable2> |
Multiplies two complex variables and presents the result. |
stop |
stop |
Exits the program. |
Each command is designed to be intuitive and user-friendly, catering to both beginners and experienced users interested in complex number computations.
To facilitate testing of ComplexCalculatorC, two text files are provided:
-
input.txt: Contains a series of valid inputs for testing various functionalities of the program. You can use these inputs to see how the program handles typical use cases.
Example of valid inputs ininput.txt
-
invalid_input.txt: Includes examples of invalid inputs to test the program's error handling and robustness. This can help ensure that the program gracefully handles unexpected or incorrect user input.
Example of invalid inputs ininvalid_input.txt
Feel free to use these files to experiment with the program and understand its capabilities and limitations.
This program is a C-based expression parser and evaluator. It constructs an expression tree from mathematical expressions and then evaluates them. Perfect for learning and understanding how parsing works in compilers!
The parser was originaly designed for the leetcode problem Basic Calculator, but it can be used as a standalone program or as a library for other projects.
- Expression Parsing: Converts a string representation of a mathematical expression into an expression tree.
- Unary and Binary Operations: Handles both unary (e.g.,
-5
) and binary (e.g.,5 + 3
) operations. - Nested Expressions: Supports expressions within parentheses (e.g.,
(3 + (2 - 1))
). - Inline Functions: Utilizes inline functions for efficient calculation.
- Readable Tree Output: Prints the structure of the expression tree for easy visualization.
To compile the program, use a C compiler such as gcc
or clang
:
gcc -std=c99 -O2 calc.c -o calc -lm -Wall -Wextra -Werror -pedantic-errors -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-but-set-parameter -Wno-unused-value -Wno-unused-label -Wno-unused-result -Wno-unused-local-typedefs -Wno-unused && ./expression_parser
This command compiles the calc.c file using the C99 standard with level 2 optimizations, which includes function inlining.
Run the compiled program from the command line. You can input mathematical expressions, and the program will parse these expressions, construct an expression tree, and then evaluate them.
Simply execute ./expression_parser
and enter your expression at the prompt. The program will output the evaluated result along with a visual representation of the expression tree.
The program will parse the expression, construct the tree, and output the result along with the tree structure:
Output of calc_tests.txt:
File PRN.c is a C-based reversed polish calculator designed for the leetcode problem Evaluate Reverse Polish Notation. It evaluates mathematical expressions written in reversed polish notation (RPN) and supports dynamic stack allocation.
This solution is 99% faster and 99% less memory-intensive than other submissions on leetcode, by utilizing dynamic stack allocation combined with function inlining.
/* The structure includes pointer to functions to demonstrate basic OOP concepts */
typedef struct Stack {
int *arr;
int sp;
int max_size;
int (*pop)(struct Stack *);
void (*push)(struct Stack *s, int);
} Stack;
Contributions to ComplexCalculatorC are welcome! If you have suggestions for improvement or have identified issues, please feel free to open an issue or submit a pull request on the GitHub repository.
This project is licensed under the MIT License - see the LICENSE file for details.
🚀 Happy Exploring in the World of Complex Numbers! 🧮