A client-server system built in C for evaluating mathematical infix expressions using the two-stack algorithm. It uses epoll for concurrent client handling and supports negative numbers, unary minus, and parentheses.
math-eval-epoll/
├── client.c // Connects to the server and sends expressions
├── server.c // Handles multiple clients using epoll, processes expressions
├── eval.c // Validates and evaluates infix expressions
├── eval.h // Declarations for eval.c functions
├── stack.c // Generic stack implementation using arrays
├── stack.h // Header for stack operations
└── README.md // Project documentation
gcc stack.c eval.c server.c -o server
gcc client.c -o client
./server
./client
- Connects to server using TCP.
- Displays welcome message and rules.
- Sends expressions and receives evaluated results.
- Supports quitting with inputs like
exit
,n
, orNo
.
- Accepts new connections using
epoll
(non-blocking I/O). - Sends rules and prompt to each new client.
- For each expression received:
- Checks for ambiguous spacing (e.g.,
34 5
). - Validates the input as a correct infix expression.
- Evaluates using the two-stack algorithm.
- Sends the result or an appropriate error.
- Checks for ambiguous spacing (e.g.,
- Gracefully closes connection if the client chooses to quit.
- Uses a two-stack approach:
- One stack for operands (integers).
- One stack for operators (
+
,-
,*
,/
,%
,(
,)
).
- Supports unary minus, including:
- Negative numbers like
-5
- Nested negations like
-(-(5))
- Negative numbers like
- Handles operator precedence and parenthesis evaluation.
- C Socket Programming (TCP)
- epoll for efficient I/O multiplexing
- Dynamic, generic stack implementation
- Mathematical expression parsing and evaluation
- Unary operator handling in infix expressions
Expression | Result |
---|---|
23 + 4 - 12 * (4 * 6) |
-261 |
34 + 56 - 7 + -(11 * -2) |
98 |
32 + (-(-5) + -12 * 6) |
-35 |
-5 * (2 + 3) |
-25 |
- ❌
23 45
→ Rejected due to ambiguous spacing - ❌
5 + * 4
→ Rejected due to invalid infix - ✅
-5 + (3 * 2)
→ Valid expression
- Works on Linux natively.
- Windows users can run it via WSL (Windows Subsystem for Linux).
- Beej's Guide to Network Programming
- Linux epoll man page
- The Linux Programming Interface by Michael Kerrisk
This project is licensed under the MIT License.