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 deleted file mode 100644 index 85f1466..0000000 --- a/calc.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include "operators.h" - -int main(){ - FILE *fp = NULL; - int operand1, operand2; - char operator = ' '; - int result, line = 0; - - fp = fopen("read.txt","r"); - if(fp!=NULL){ - fscanf(fp, "%d", &line); - - for(int i=0; i +#include +#include "my_fscanf.h" + +int main(){ + fp = fopen("points.txt","r"); + char type[10]; + double x,y; + int line; + + if(fp!=NULL){ + my_fscanf(fp, "%d", &line); + for(int i =0; i < line; i++){ + my_fscanf(fp, "%s (%f,%f)",type, &x, &y); + if(strcmp(type, "point") == 0) { + printf("X: %f:, Y: %f\n", x, y); + } + } + } + return 0; +} diff --git a/my_fscanf.c b/my_fscanf.c new file mode 100644 index 0000000..646b1e0 --- /dev/null +++ b/my_fscanf.c @@ -0,0 +1,82 @@ +#include "my_fscanf.h" +#include +#include +#include +#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); + int *s=va_arg(list, int*); + + while(isspace(c)){ + + } + unsigned int num= 0; + while(isdight(c)){ + *s = num * 10 + c -'0'; + c=getc(fp); + } + while (isspace(c)){} + format++; + ungetc(c,fp); + break; + } + case 'c':{ + char *ch = va_arg(list,char*); + char c=getc(fp); + while (isspace(c)){} + *ch = c; + format++; + break; + } + case 's':{ + char *sp = va_arg(list, char*); + int s = 1; + while(isspace(c=getc(fp))){} + + while (!isspace(c=getc(fp))){ + sp[s]=c; + s++; + } + sp[s]=0; + format++; + ungetc(c,fp); + break; + } + case 'f':{ + double *f_p=va_arg(list, double*); + double di = 0.1; + double c=getc(fp); + while(isspace(c)){} + *f_p = 0; + while (isdigit(c)){ + *f_p= *f_p * 10 + c - '0'; + c= getc(fp); + } + if (c == '.'){ + c = getc(fp); + while(isdigit(c)){ + *f_p=*f_p + (c - '0') * di; + di = di* 0.1; + c=getc(fp); + } + } + format++; + ungetc(c,fp); + break; + } + else{ + int c = getc(fp); + if(*format==c) + 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 diff --git a/points.txt b/points.txt new file mode 100644 index 0000000..8954fb7 --- /dev/null +++ b/points.txt @@ -0,0 +1,5 @@ +4 +point (1.3,2.2) +point (2.5,3.0) +point (3.2,4.4) +point (1.0,5.9) diff --git a/read.txt b/read.txt deleted file mode 100644 index 3e2bf01..0000000 --- a/read.txt +++ /dev/null @@ -1,5 +0,0 @@ -5 -123 + 456 -234 * 234 -143 - 111 -987 / 132