diff --git a/calc b/calc deleted file mode 100755 index 023ec18..0000000 Binary files a/calc and /dev/null differ diff --git a/calc.c b/calc.c index 85f1466..695ea2e 100644 --- a/calc.c +++ b/calc.c @@ -1,32 +1,46 @@ #include #include "operators.h" +#include "my_fscanf.h" int main(){ FILE *fp = NULL; int operand1, operand2; char operator = ' '; - int result, line = 0; + int line = 0; + double result =0; + + int (*addP) (int,int); + int (*minusP) (int,int); + int (*mulP) (int,int); + double (*divP) (int, int); + + addP=add; + minusP=minus; + mulP=mul; + divP=div; + fp = fopen("read.txt","r"); if(fp!=NULL){ - fscanf(fp, "%d", &line); + my_fscanf(fp, "%d", &line); - for(int i=0; i +#include +void my_fscanf(FILE *fp, const char *format, ...) { + va_list list; + va_start(list, format); + while(*format) { + if(*format == '%'){ + format++; + switch(*format){ + case 'd':{ + int c = getc(fp); + double *Double=va_arg(list, double*); + while(isspace(c)){ + c=getc(fp); + } + unsigned int num= 0; + while(isdight(c)){ + num = num * 10 + c -'0'; + c=getc(fp); + } + *Double = num; + break; + } + case 'c':{ + char *ch = va_arg(list,char*); + char c=getc(fp); + *ch = c; + break; + } + case ' ': + break; + } + } + else + format++; + } + va_end(list); +} diff --git a/my_fscanf.h b/my_fscanf.h new file mode 100644 index 0000000..d2b9307 --- /dev/null +++ b/my_fscanf.h @@ -0,0 +1,7 @@ +#ifndef MY_FSCANF_H +#define MY_FSCANF_H +#include + +void my_fscanf(FILE *fp,const char *format, ...); + +#endif diff --git a/operators.c b/operators.c index d51cb3e..af88940 100644 --- a/operators.c +++ b/operators.c @@ -10,7 +10,8 @@ int mul(int op1, int op2) { return op1*op2; } -int div(int op1, int op2) { - return op1%op2; +double div(int op1, int op2) { + return (double)op1/(double)op2; } + diff --git a/operators.h b/operators.h index bd08f7d..e16bf60 100644 --- a/operators.h +++ b/operators.h @@ -1,4 +1,4 @@ int add(int op1, int op2); int mul(int op1, int op2); -int div(int op1, int op2); +double div(int op1, int op2); int minus(int op1, int op2); diff --git a/operators.o b/operators.o deleted file mode 100644 index 71b996c..0000000 Binary files a/operators.o and /dev/null differ