diff --git a/calc.c b/calc.c index 85f1466..2a46f55 100644 --- a/calc.c +++ b/calc.c @@ -1,32 +1,38 @@ #include #include "operators.h" - +#include "my_fscanf.h" int main(){ FILE *fp = NULL; int operand1, operand2; char operator = ' '; - int result, line = 0; + double result = 0; + int line=0; + int (*sp1) (int,int)=add; + int (*sp2) (int,int)=minus; + int (*sp3) (int,int)=mul; + double (*sp4) (int,int)=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; + int*d; + char *dc; + int c; + va_start(list,format); + while(*format){ + if(*format == '%'){ + format++; + switch(*format){ + case 'd': + d=va_arg(list,int*); + while(isspace(c=getc(fp))){} + unsigned int num =0; + while(isdigit(c)){ + num=num*10+c-'0'; + c=getc(fp); + } + *d=num; + break; + case 'c': + dc=va_arg(list,char*); + c=getc(fp); + *dc=c; + break; + case ' ': + break; + }; + } + else + format++; + } + va_end(list); + printf("\n"); +} diff --git a/my_fscanf.h b/my_fscanf.h new file mode 100644 index 0000000..437ce8e --- /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..d39d0db 100644 --- a/operators.c +++ b/operators.c @@ -10,7 +10,7 @@ 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/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 index 71b996c..9ce07fd 100644 Binary files a/operators.o and b/operators.o differ