Browse Source

v1.0

master
myitinos 6 years ago
parent
commit
0f8e5e8e78
3 changed files with 105 additions and 0 deletions
  1. BIN
      calc
  2. +104
    -0
      calc.c
  3. +1
    -0
      calc.sh

BIN
calc View File


+ 104
- 0
calc.c View File

@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
/*
* SIGFPE bisa nempel di CPU, perlu dicari solusi
* Test openssl lewat socat
*/
void signal_handler(int sig)
{
signal(SIGFPE, signal_handler);
system("/bin/sh");
exit(1);
}
int main(int argc, char const *argv[])
{
int i_a = 0;
int i_b = 0;
int total = 0;
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
signal(SIGFPE, signal_handler);
puts("Welcome to Online Calculator v3.1");
puts("");
puts("Operator supported: + - * /");
puts("ex: 2 + 2");
puts("ex: 2 + a //'a' to use last results");
puts("");
puts("------------------------------------");
while (1)
{
char c_a[11] = "";
char c_b[11] = "";
char opt = 0;
printf(">");
if (scanf("%10s %c %10s", c_a, &opt, c_b) < 3)
{
puts("Failed to parse expression!");
return 0;
}
else
{
int r1_a = sscanf(c_a, "%d", &i_a);
int r2_a = strcmp("a", c_a) == 0;
int r1_b = sscanf(c_b, "%d", &i_b);
int r2_b = strcmp("a", c_b) == 0;
if ((i_b == 0 && r2_a) && opt == '/')
{
puts("Floating point exception (core dumped)");
return 0;
}
if (r2_a)
{
i_a = total;
}
if (r2_b)
{
i_b = total;
}
if ((r1_a || r2_a) && (r1_b || r2_b))
{
switch (opt)
{
case '+':
total = i_a + i_b;
break;
case '-':
total = i_a - i_b;
break;
case '*':
total = i_a * i_b;
break;
case '/':
total = i_a / i_b;
break;
default:
puts("Unknown operator!");
return 0;
}
printf("=%d\n", total);
}
else
{
puts("Failed to parse operand!");
return 0;
}
}
}
return 0;
}

+ 1
- 0
calc.sh View File

@ -0,0 +1 @@
socat TCP-listen:8080,reuseaddr,fork EXEC:./calc

Loading…
Cancel
Save