Calc in C

Mahen 0 Tallied Votes 245 Views Share

Hi everyone,
I've been working on this calculator for days, and now i am making it opensource so that everyone can bring his own contribution, please comment all your codes very well.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(int argc, char **argv)
{
double a, b;
char buf[25];
if ( argc > 3 )
{
if ( sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[3], "%lf", &b) == 1 )
{
switch ( argv[2][0] )
{
case '+':
printf("%g PLUS %g = %g\n", a, b, a + b);
char first[60];
char second[60];
char result[60];
itoa(a, first, 10);
itoa(b, second, 10);
itoa(a+b, result, 10);
FILE *cpy;
cpy = fopen("c:\\calc.txt", "a");
fprintf(cpy, first);
fprintf(cpy, " PLUS ");
fprintf(cpy, second);
fprintf(cpy, " = ");
fprintf(cpy, result);
fprintf(cpy, " @ ");
fprintf(cpy, strdate(buf));
fprintf(cpy, " & ");
fprintf(cpy, strtime(buf));
fprintf(cpy, "\n");
break;
case '-':
printf("%g MINUS %g = %g\n", a, b, a - b);
char firstm[60];
char secondm[60];
char resultm[60];
itoa(a, firstm, 10);
itoa(b, secondm, 10);
itoa(a-b, resultm, 10);
FILE *cpym;
cpy = fopen("c:\\calc.txt", "a");
fprintf(cpy, firstm);
fprintf(cpy, " MINUS ");
fprintf(cpy, secondm);
fprintf(cpy, " = ");
fprintf(cpy, resultm);
fprintf(cpy, " @ ");
fprintf(cpy, strdate(buf));
fprintf(cpy, " & ");
fprintf(cpy, strtime(buf));
fprintf(cpy, "\n");
break;
case '*':
printf("%g MULTIPLY BY %g = %g\n", a, b, a * b);
char firstmu[60];
char secondmu[60];
char resultmu[60];
itoa(a, firstmu, 10);
itoa(b, secondmu, 10);
itoa(a*b, resultmu, 10);
FILE *cpymu;
cpy = fopen("c:\\calc.txt", "a");
fprintf(cpy, firstmu);
fprintf(cpy, " MULTIPLY ");
fprintf(cpy, secondmu);
fprintf(cpy, " = ");
fprintf(cpy, resultmu);
fprintf(cpy, " @ ");
fprintf(cpy, strdate(buf));
fprintf(cpy, " & ");
fprintf(cpy, strtime(buf));
fprintf(cpy, "\n");
break;
case '/':
printf("%g DIVIDE BY %g = %g\n", a, b, a / b);
char firstd[60];
char secondd[60];
char resultd[60];
itoa(a, firstd, 10);
itoa(b, secondd, 10);
itoa(a/b, resultd, 10);
FILE *cpyd;
cpy = fopen("c:\\calc.txt", "a");
fprintf(cpy, firstd);
fprintf(cpy, " DIVIDE ");
fprintf(cpy, secondd);
fprintf(cpy, " = ");
fprintf(cpy, resultd);
fprintf(cpy, " @ ");
fprintf(cpy, strdate(buf));
fprintf(cpy, " & ");
fprintf(cpy, strtime(buf));
fprintf(cpy, "\n");
break;
default:
puts("Error");
break;
}
}
}
return 0;
}