import sys import os EMBED_C=""" /* Simple Calculator in C. */ /* Compiled througth ADE, the *NIX emulator, using:- */ /* gcc -Wall -o calc mycalc.c -lm */ /* This version is purely for any AMIGA Python from 1.4.0 upwards. */ /* Also tested on Python 2.7.10 and 3.5.2, on OSX 10.14.3 and Linux Mint 19. */ #include #include #include double SOLUTION; double FLOAT_1; double FLOAT_2; char OPERATOR; double calc(double FLOAT_1, double FLOAT_2, char OPERATOR) { double SOLUTION = 0.0; switch(OPERATOR) { case '+': SOLUTION = FLOAT_1 + FLOAT_2; break; case '-': SOLUTION = FLOAT_1 - FLOAT_2; break; case 'x': SOLUTION = FLOAT_1 * FLOAT_2; break; case '/': SOLUTION = FLOAT_1 / FLOAT_2; break; case '^': SOLUTION = pow(FLOAT_1, FLOAT_2); break; case 'r': FLOAT_2 = 1.0 / FLOAT_2; SOLUTION = pow(FLOAT_1, FLOAT_2); break; } return SOLUTION; } int main(int argc, char *argv[]) { char OPERATOR; double FLOAT_1; double FLOAT_2; double SOLUTION; if (argc <= 3) { printf("\\nERROR!\\n\\n"); printf("Usage: calc FLOAT_1 <|+|-|x|/|^|r|> FLOAT_2\\n"); printf("Where: '+'=ADD; '-'=SUBTRACT; 'x'=MULTIPLY; '/'=DIVIDE;\\n"); printf(" '^'=POWER; and 'r'=NTHROOT.\\n\\n"); printf("Example: calc 781.37 r 3.3, (NOTE: spaces), where is the ENTER key!\\n\\n"); /* Google result: 7.52702316103. */ exit(1); } SOLUTION = 0.0; OPERATOR = *argv[2]; FLOAT_1 = strtod(argv[1], NULL); FLOAT_2 = strtod(argv[3], NULL); SOLUTION = calc(FLOAT_1, FLOAT_2, OPERATOR); printf("%.11f\\n", SOLUTION); return(0); } """ # Create the C file. myfile = open("mycalc.c", "w") myfile.write(EMBED_C) myfile.close() # Print the whole C file to stdout. print(EMBED_C) # Compile the C file. print("Compile mycalc.c to calc using:-") print("os.system(\"gcc -Wall -o calc mycalc.c -lm\")\n") os.system("gcc -Wall -o calc mycalc.c -lm") # Now run it... print("Run calc to calculate 781.37 to root 3.3 and save in a _variable_:-") # The next few lines takes the calculation and places it into 'result'. if sys.platform == 'amiga': calc_result = os.popen("calc 781.37 r 3.3") else: calc_result = os.popen("./calc 781.37 r 3.3") result = calc_result.read() calc_result.close() # This is the result. print(result) print("Google value using 781.37**(1.0/3.3) is:-\n7.52702316103") sys.exit()