forked from ALX-SE-Algorithmia/Demo-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: infix notation stack based calculator
- Loading branch information
1 parent
b105380
commit a0b8d46
Showing
10 changed files
with
265 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
# Stack based Calculator. | ||
|
||
Simple stack based calculator, takes as inputs arithmetic operations described | ||
in [infix](https://en.wikipedia.org/wiki/Infix_notation) notation and output the | ||
result. | ||
|
||
## TODO: | ||
## How to use | ||
|
||
1- Compile the program | ||
```bash | ||
gcc *.c -o scp -lm | ||
``` | ||
|
||
2- Excecute the program and describe your operation. | ||
``` | ||
./scp | ||
(SCP): (12 + 4) * (1 + 8 * 6) | ||
``` | ||
|
||
- [x] Suppport simple operations (e.g: -12.2 + 5 - 6) | ||
- [ ] Support of arithmetic operation ( negative and positive number). | ||
- [ ] Support of more operator such as power. | ||
- [ ] Operator precedence. | ||
- [ ] Handle parentheses operator. | ||
3- Press ENTER to get your result. | ||
```bash | ||
(SCP): 784 | ||
``` | ||
|
||
## TODO: | ||
- [ ] Free allocated memory. | ||
- [ ] Handle errors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "num_stack.h" | ||
|
||
|
||
/** | ||
* create_num_stack - Create a new stack of operands. | ||
* Return: A pointer to the top of the stack. | ||
*/ | ||
num_stack_t *create_num_stack() | ||
{ | ||
num_stack_t *stack; | ||
|
||
stack = malloc(sizeof(*stack)); | ||
if (stack == NULL) | ||
{ | ||
printf("Malloc failed"); | ||
exit(1); | ||
} | ||
|
||
return (stack); | ||
} | ||
|
||
|
||
/** | ||
* push_num - Pushed an item to the top of the num_stack. | ||
* @top: A double poitner to the current top of the num_stack. | ||
* @n: the vlaue of the item to push on the num_stack. | ||
*/ | ||
void push_num(num_stack_t **top, int n) | ||
{ | ||
num_stack_t *item; | ||
|
||
item = malloc(sizeof(*item)); | ||
if (item == NULL) | ||
{ | ||
printf("Malloc failed"); | ||
exit(1); | ||
} | ||
|
||
item->next = *top; | ||
item->n = n; | ||
*top = item; | ||
} | ||
|
||
|
||
/** | ||
* pop_num - pop the top of the num_stack. | ||
* @top: A double pointer to the top of the num_stack. | ||
* | ||
* Return: Return A pointer to the new top of | ||
* the num_stack. | ||
*/ | ||
num_stack_t *pop_num(num_stack_t **top) | ||
{ | ||
num_stack_t *item; | ||
|
||
item = *top; | ||
if (*top && (*top)->next) | ||
*top = (*top)->next; | ||
|
||
return (item); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef NUM_STACK_H | ||
#define NUM_STACK_H | ||
|
||
|
||
/** | ||
* struct num_stack_s - operand stack. | ||
* @n: int, the value of an operand. | ||
* @next: points to the next element of the num_stack. | ||
* | ||
* Description: Stack dsa for holding the operand of | ||
* the calculator. | ||
*/ | ||
typedef struct num_stack_s | ||
{ | ||
int n; | ||
struct num_stack_s *next; | ||
} num_stack_t; | ||
|
||
num_stack_t *pop_num(num_stack_t **); | ||
void push_num(num_stack_t **, int); | ||
num_stack_t *create_num_stack(); | ||
|
||
#endif /* NUM_STACK_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "op_stack.h" | ||
|
||
|
||
/** | ||
* create_op_stack - Create a new stack of operands. | ||
* Return: A pointer to the top of the stack. | ||
*/ | ||
op_stack_t *create_op_stack() | ||
{ | ||
op_stack_t *stack; | ||
|
||
stack = malloc(sizeof(*stack)); | ||
if (stack == NULL) | ||
{ | ||
printf("Malloc failed"); | ||
exit(1); | ||
} | ||
|
||
return (stack); | ||
} | ||
|
||
|
||
/** | ||
* push_op - Pushed an item to the top of the stack. | ||
* @top: A double poitner to the current top of the stack. | ||
* @op: the value of the item to push on the stack. | ||
*/ | ||
void push_op(op_stack_t **top, char op) | ||
{ | ||
op_stack_t *item; | ||
|
||
item = malloc(sizeof(*item)); | ||
if (item == NULL) | ||
{ | ||
printf("Malloc failed"); | ||
exit(1); | ||
} | ||
|
||
item->next = *top; | ||
item->op = op; | ||
*top = item; | ||
} | ||
|
||
|
||
/** | ||
* pop_op - pop the top of the stack. | ||
* @top: A double pointer to the top of the stack. | ||
* | ||
* Return: Return A pointer to the new top of | ||
* the stack. | ||
*/ | ||
op_stack_t *pop_op(op_stack_t **top) | ||
{ | ||
op_stack_t *item; | ||
|
||
item = *top; | ||
if (*top && (*top)->next) | ||
*top = (*top)->next; | ||
|
||
return (item); | ||
} |
Oops, something went wrong.