Skip to content

Instantly share code, notes, and snippets.

@solarshado
Created May 1, 2020 15:12
Show Gist options
  • Save solarshado/75955fc24dbc0e88bac421a35316ce2c to your computer and use it in GitHub Desktop.
Save solarshado/75955fc24dbc0e88bac421a35316ce2c to your computer and use it in GitHub Desktop.
Silly code for a reddit thread
# 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("}")
# 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
$ ./a.exe
1+1=2
7-5=2
6*7=42
6/9=0.6666666666666666
#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