diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2490a4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +/calc diff --git a/Makefile b/Makefile deleted file mode 100644 index 4df6d6b..0000000 --- a/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -SRCS = $(wildcard *.c) -OBJS = $(SRCS:%.c=%.o) -CFLAGS = -g -Wall -TARGET = calc -CC = gcc - -all: $(TARGET) - -$(TARGET): $(OBJS) - $(CC) $(OUTPUT_OPTION) $(CFLAGS) $^ - -clean: - $(RM) $(OBJS) $(TARGET) - - diff --git a/README.md b/README.md deleted file mode 100644 index 68b2d37..0000000 --- a/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# Simple Calculator - -This is a buggy application that will be used for Lab5 of 'Introduction to Open Source Software Development.' - - - Fork the project. - - Create a topic branch from master. - - The branch name must be your student id. (e.g. 201624000) - - Make some commits to improve the project. - - Fix the well-knwon bugs. - - Remove object and binary files. - - Add .gitignore file - - Push this branch to your GitHub project. - - Open a Pull Request on GitHub. - - The Pull Request must be submitted to master branch of Classroom-IOSSD/SimpleCalculator! - - Capture the Pull Request and upload it to PLMS. - 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() { + FILE* 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; +} \ No newline at end of file diff --git a/my_fscanf.c b/my_fscanf.c new file mode 100644 index 0000000..7949b0f --- /dev/null +++ b/my_fscanf.c @@ -0,0 +1,91 @@ +// file: my_fscanf.c +#include "my_fscanf.h" +#include +#include +void my_fscanf(FILE *fp, const char *format, ...) { + va_list list; + va_start(list, format); + while (*format) { + if (*format == '%') { + int c; + format++; + switch (*format) { + case 'd': + { + while (isspace(c = getc(fp))) {} + + int* addr = va_arg(list, int*); + *addr = 0; + while (isdigit(c)) { + *addr = *addr * 10 + c - '0'; + c = getc(fp); + } + + while (isspace(c = getc(fp))) {} + + format++; + ungetc(c, fp); + break; + } + case 'c': + { + while (isspace(c = getc(fp))) {} + + char* addr = va_arg(list, char*); + *addr = c; + format++; + break; + } + case 's': + { + char* addr = va_arg(list, char*); + int cnt = 1; + while (isspace(c = getc(fp))) {} + + addr[0] = c; + + while (!isspace(c = getc(fp))) { + addr[cnt] = c; + cnt++; + } + addr[cnt] = 0; //add null char + format++; + ungetc(c, fp); + break; + } + case 'f': + { + double* addr = va_arg(list, double*); + double decimalFraction = 0.1; + + while (isspace(c = getc(fp))) {} + *addr = 0; + while (isdigit(c)) { + *addr = *addr * 10 + c - '0'; + c = getc(fp); + } + + if (c == '.') { + c = getc(fp); + while (isdigit(c)) { + *addr = *addr + (c - '0') * decimalFraction; + decimalFraction *= 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..19d4f40 --- /dev/null +++ b/my_fscanf.h @@ -0,0 +1,6 @@ +// file: my_fscanf.h +#ifndef MY_FSCANF_H +#define MY_FSCANF_H +#include +void my_fscanf(FILE *fp, const char *format, ...); +#endif /* !MY_FSCANF_H */ diff --git a/operators.c b/operators.c deleted file mode 100644 index d51cb3e..0000000 --- a/operators.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "operators.h" - -int add(int op1, int op2) { - return op1+op2; -} -int minus(int op1, int op2) { - return op1-op2; -} -int mul(int op1, int op2) { - return op1*op2; -} - -int div(int op1, int op2) { - return op1%op2; -} - diff --git a/operators.h b/operators.h deleted file mode 100644 index bd08f7d..0000000 --- a/operators.h +++ /dev/null @@ -1,4 +0,0 @@ -int add(int op1, int op2); -int mul(int op1, int op2); -int 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..279a10c --- /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) \ No newline at end of file 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