Created
May 1, 2020 15:12
-
-
Save solarshado/75955fc24dbc0e88bac421a35316ce2c to your computer and use it in GitHub Desktop.
Silly code for a reddit thread
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
# Modified from: https://www.reddit.com/r/ProgrammerHumor/comments/gawu9i/and_using_light_theme/fp3xd19/ | |
# Thanks /u/ohlookaregisterbutto! | |
MAX = 12 | |
ops = {"*":lambda i,j:i*j,"/":lambda i,j:i/j,"+":lambda i,j:i+j,"-":lambda i,j:i-j} | |
print("void calculator(int num1, char op, int num2) {") | |
for op in ops: | |
for i in range(1,MAX+1): | |
for j in range(1,MAX+1): | |
print("\tif(num1 == " + str(i) + " && op == '" + op + "' && num2 == " + str(j) + ")") | |
print("\t\tprintf(\"" + str(i) + op + str(j) + "=" + str(ops[op](i,j)) + "\\n\");") | |
print("}") |
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
# a makefile becasue I've got too much time on my hands | |
# .exe becasue I'm on Windows using MSYS2 | |
a.exe: calc.c | |
gcc -o a.exe test.c | |
calc.c: | |
python gen.py > calc.c |
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
$ ./a.exe | |
1+1=2 | |
7-5=2 | |
6*7=42 | |
6/9=0.6666666666666666 |
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
#include <stdio.h> | |
#include "calc.c" | |
void main() { | |
calculator(1,'+',1); | |
calculator(7,'-',5); | |
calculator(6,'*',7); | |
calculator(6,'/',9); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment