Skip to content

Commit bda4ccd

Browse files
committed
Ready for event
1 parent de2bcd6 commit bda4ccd

File tree

15 files changed

+159
-95
lines changed

15 files changed

+159
-95
lines changed

cpp/liquid/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ src_files = ml_add \
1919
ml_mul \
2020
ml_div \
2121
ml_mod \
22-
ml_power
22+
ml_power \
23+
ml_perms \
24+
ml_combos \
25+
ml_isPrime \
26+
ml_nextPrime \
27+
ml_factorial \
28+
ml_factorise \
29+
ml_gcd \
30+
ml_lcm
2331

2432
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
2533
# DON'T TOUCH BELOW THIS #

cpp/liquid/README.md

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#Liquid For Devs!
1+
#The `liquid`
22

3-
We don't need to add functionality in `src/`, that is the participants' job *(See the wiki* **"Liquid: How to"** *page)*.
4-
**The main file is `liquid.cpp`.**
3+
`liquid` is a simple CLI math application that can solve simple math problems.
4+
5+
#How is the code organised?
56

67
This is the directory structure:
78
```
@@ -23,37 +24,27 @@ This is the directory structure:
2324
└── ml_*****.cpp
2425
```
2526

26-
##How to `build` and `run`
2727

28-
On your terminal,
29-
```sh
30-
$ cd <path to "liquid">
31-
$ make liquid
32-
$ make test <or> ./liquid
33-
```
34-
In case you get errors in building, try
35-
```sh
36-
$ make again
37-
```
28+
We've already made the CLI which can be used to input a math expression and get the result.
29+
>Credit: @bamos for implementing the Shunting-Yard Algorithm in `cpp`.
30+
It's basically the RPN expression parser, which pushes into a `stack` to parse an expression and then evaluate based on priority rules by `popping` the `stack`.
3831

39-
`make liquid`compiles all the `src/ml_ * .cpp` files and links them to th `liquid.o` to make the executeable `liquid`.
32+
The name of the CLI file is `liquid.cpp`. *You don't have to edit `liquid.cpp`.*
4033

41-
##What needs to be done?
42-
Currently, `liquid.cpp` is has a non-intuitive way of interaction. Since we are making a math expression solver, we should be able to input `infix` expressions.
43-
@bamos built a nice implementation of the [Shunting-Yard Algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm) [here](/bamos/https://github.com/bamos/cpp-expression-parser/)
44-
We need to change our `liquid.cpp` to utilise the power of `shunting-yard.cpp`.
34+
#What do I edit?
4535

46-
* Get user expression from `stdin`.
47-
* Compile the expression (using `calculator::compile()`)
48-
* Evaluate the expression
49-
- by over-riding / modifying the `calculator::calculate(TokenQueue_t)` method.
50-
- the functions implemented by `src/ml_ * .cpp` would be invoked.
51-
* Show output, wait for more expressions.
36+
See the `src/` directory? Read `math-lib.h`. This is the list of functions that provide the computational functionality to `liquid`.
37+
```
38+
double ml_add(double, double);
39+
double ml_mul(double, double);
40+
double ml_div(double, double);
41+
double ml_sub(double, double);
42+
43+
```
44+
But, except the `add`, `mul`, `div`, and `sub`, files, the rest are empty.
45+
And there's also a template file (`ml_template.cpp`).
5246

53-
**That's not all!**
54-
Checkout the [issues](/arrow-/explore-git/issues) for details on adding function calls, exotic operators.
47+
#Appendix
5548

56-
***
57-
***Comment on the issue to let everyone know you are working on this. Why should 14 people develop just 1 feature which can be developed by a far less number of people and there are numerous other things to work on.***
58-
***
59-
To work on this, you must understand the "Shuning Yard Algorithm" pretty well. The [wikipedia page has a lot of references](https://en.wikipedia.org/wiki/Shunting-yard_algorithm#External_links).
49+
**CLI**
50+
*Command Line Interface. It means a* **terminal** *is opened for the user, not a GUI.*

cpp/liquid/liquid.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
22
#include <stdio.h>
33
#include <string.h>
44

5+
/*
6+
* DONT EDIT THIS FILE!
7+
* See the wiki, for a list of keywords that are understood by Liquid.
8+
*/
9+
10+
void strip_wspace(char *ipstr, char* stripped){
11+
int i, j = 0;
12+
for (i=0; ipstr[i] != '\0' && i<99; i++){
13+
if (ipstr[i] != ' ')
14+
stripped[j++] = ipstr[i];
15+
}
16+
stripped[j] = '\0';
17+
//printf("%s\n", stripped);
18+
}
19+
520
int main(int agrc, char *argv[]){
621
double result;
7-
char input[100];
22+
char input[100], stripped[100];
823
int i,j;
24+
25+
printf("Liquid is a smooth CLI math expression evaluator.\nEnter expressions, you need to match all parens.\n\nSee the wiki to know the keywords understood by Liquid\nTo quit just type 'Q'\n");
926
while(1)
10-
{
11-
scanf("%s", input);
27+
{
28+
printf("<< ");
29+
gets(input);
1230
if(strcmp(input,"Q")==0)
1331
break;
14-
15-
for(i=0;input[i]!='\0';i++)
16-
if(input[i]==' ')
17-
for(j=i;input[j]!='\0';j++)
18-
input[j]=input[j+1];
19-
20-
Status response = shunting_yard(input, &result);
32+
strip_wspace(input, stripped);
33+
Status response = shunting_yard(stripped, &result);
2134
printf(">> %f\n", result);
2235
}
2336
return 0;

cpp/liquid/shunting-yard.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@ static const Operator OPERATORS[] = {
4747
{'^', 2, OPERATOR_BINARY, OPERATOR_RIGHT},
4848
{'+', 3, OPERATOR_UNARY, OPERATOR_RIGHT},
4949
{'-', 3, OPERATOR_UNARY, OPERATOR_RIGHT},
50-
{'*', 4, OPERATOR_BINARY, OPERATOR_LEFT},
51-
{'/', 4, OPERATOR_BINARY, OPERATOR_LEFT},
52-
{'%', 4, OPERATOR_BINARY, OPERATOR_LEFT},
53-
{'+', 5, OPERATOR_BINARY, OPERATOR_LEFT},
54-
{'-', 5, OPERATOR_BINARY, OPERATOR_LEFT},
55-
{'(', 6, OPERATOR_OTHER, OPERATOR_NONE}
50+
{'P', 4, OPERATOR_BINARY, OPERATOR_LEFT},
51+
{'C', 4, OPERATOR_BINARY, OPERATOR_LEFT},
52+
{'#', 4, OPERATOR_BINARY, OPERATOR_LEFT},
53+
{'$', 4, OPERATOR_BINARY, OPERATOR_LEFT},
54+
{'*', 5, OPERATOR_BINARY, OPERATOR_LEFT},
55+
{'/', 5, OPERATOR_BINARY, OPERATOR_LEFT},
56+
{'%', 5, OPERATOR_BINARY, OPERATOR_LEFT},
57+
{'+', 6, OPERATOR_BINARY, OPERATOR_LEFT},
58+
{'-', 6, OPERATOR_BINARY, OPERATOR_LEFT},
59+
{'(', 7, OPERATOR_OTHER, OPERATOR_NONE}
5660
};
5761

5862
// Returns an array of tokens extracted from the expression. The array is
@@ -130,7 +134,7 @@ Token *tokenize(const char *expression) {
130134
token.type = TOKEN_OPEN_PARENTHESIS;
131135
else if (*c == ')')
132136
token.type = TOKEN_CLOSE_PARENTHESIS;
133-
else if (strchr("!^*/%+-", *c)) {
137+
else if (strchr("!#$PC^*/%+-", *c)) {
134138
token.type = TOKEN_OPERATOR;
135139
token.value = strndup(c, 1);
136140
} else if (sscanf(c, "%m[0-9.]", &token.value))
@@ -318,14 +322,26 @@ Status apply_operator(const Operator *operator, Stack **operands) {
318322
x = ml_div(x,y);
319323
break;
320324
case '%':
321-
x = ml_mod(x,y);
325+
x = ml_mod((int)x,(int)y);
322326
break;
323327
case '+':
324328
x = ml_add(x,y);
325329
break;
326330
case '-':
327331
x = ml_sub(x,y);
328332
break;
333+
case '#':
334+
x = ml_gcd((int)x,(int)y);
335+
break;
336+
case '$':
337+
x = ml_lcm((int)x,(int)y);
338+
break;
339+
case 'C':
340+
x = ml_combos((int)x,(int)y);
341+
break;
342+
case 'P':
343+
x = ml_perms((int)x,(int)y);
344+
break;
329345
default:
330346
return ERROR_UNRECOGNIZED;
331347
}
@@ -342,7 +358,7 @@ Status apply_unary_operator(const Operator *operator, Stack **operands) {
342358
x = -x;
343359
break;
344360
case '!':
345-
x = tgamma(x + 1);
361+
x = ml_factorial((int)x);
346362
break;
347363
default:
348364
return ERROR_UNRECOGNIZED;
@@ -358,8 +374,6 @@ Status apply_function(const char *function, Stack **operands) {
358374
double x = pop_double(operands);
359375
if (strcasecmp(function, "abs") == 0)
360376
x = fabs(x);
361-
else if (strcasecmp(function, "sqrt") == 0)
362-
x = sqrt(x);
363377
else if (strcasecmp(function, "ln") == 0)
364378
x = log(x);
365379
else if (strcasecmp(function, "lb") == 0)
@@ -373,6 +387,21 @@ Status apply_function(const char *function, Stack **operands) {
373387
x = sin(x);
374388
else if (strcasecmp(function, "tan") == 0)
375389
x = tan(x);
390+
else if (strcasecmp(function, "isPrime") == 0)
391+
x = ml_isPrime((int)x);
392+
else if (strcasecmp(function, "nextPrime") == 0)
393+
x = ml_nextPrime((int)x);
394+
else if (strcasecmp(function, "factorise") == 0){
395+
int factors[120] = {0}, exponents[120] = {0}, i;
396+
x = ml_factorise(abs((int)x), factors, exponents);
397+
if (x > 0.1){
398+
for (i=0; i<120; i++){
399+
if (factors[i] == 0) break;
400+
printf("%d^%d\n", factors[i], exponents[i]);
401+
}
402+
}
403+
else printf("factorise Failed.\n");
404+
}
376405
else
377406
return ERROR_UNDEFINED_FUNCTION;
378407
push_double(x, operands);

cpp/liquid/src/math-lib.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ double ml_mul(double, double);
99
double ml_div(double, double);
1010
double ml_sub(double, double);
1111
double ml_power(double, double);
12-
double ml_mod(double,double);
12+
int ml_mod(int, int);
13+
int ml_gcd(int,int);
14+
int ml_lcm(int,int);
15+
int ml_isPrime(int);
16+
int ml_nextPrime(int);
17+
int ml_factorial(int);
18+
int ml_factorise(int, int*, int*);
19+
int ml_combos(int,int);
20+
int ml_perms(int,int);
1321

1422
#endif

cpp/liquid/src/ml_combos.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "math-lib.h"
22

3-
double ml_power(double n1, double n2){
4-
// convert these to ints!
5-
// returning a double isn't really wrong...
3+
int ml_combos(int n1, int n2){
4+
// Compute and return "n1 Choose n2"
65
return 0;
76
}

cpp/liquid/src/ml_factorial.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "math-lib.h"
2+
3+
int ml_factorial(int n1)
4+
// n1 >= 0. No need of error checking!
5+
// Compute the n1!
6+
{
7+
return 0;
8+
}

cpp/liquid/src/ml_factorise.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
#include "math-lib.h"
22

3-
double ml_factorise(double n1)
4-
{
5-
return 0;
3+
int ml_factorise(int n1, int *factors, int *exponent){
4+
/*
5+
n1 > 0
6+
factors is an array of ints, initiallised to ALL ZERO {size = 120}
7+
exponent is an array of ints, initiallised to ALL ZERO {size = 120}
8+
The arrays must look like:
9+
{2, 3, 5, 19}
10+
{1, 1, 1, 2}
11+
Order is not important. Your primes might be in any jumbled order, exponent and facor must match.
12+
Feel free to contact a volunteer if you have doubts!
13+
14+
NOT LIKE
15+
{1, 2, 3, 5, 7, 9, ... , 19, 23, ...., 113, ...}
16+
{1, 1, 1, 1, 0, 0, ... , 1, 0, ...., 0, ...}
17+
18+
Return 0 if operation failed, else 1
19+
*/
20+
factors[0] = 3;
21+
exponent[0] = 1;
22+
factors[1] = 78;
23+
exponent[1] = 1;
24+
factors[2] = 31;
25+
exponent[2] = 1;
26+
return 1;
627
}

cpp/liquid/src/ml_gcd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "math-lib.h"
22

3-
double ml_gcd(double n1,double n2)
3+
int ml_gcd(int n1, int n2)
4+
// Compute the GCD(n1, n2)
45
{
56
return 0;
67
}

cpp/liquid/src/ml_isPrime.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "math-lib.h"
22

3-
bool ml_isPrime(double n1)
3+
int ml_isPrime(int n1)
4+
// return 1 if n1 is prime.
45
{
56
return 0;
67
}

0 commit comments

Comments
 (0)