Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

201524653_Lab14_my_fscanf #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
/calc
Binary file removed calc
Binary file not shown.
47 changes: 25 additions & 22 deletions calc.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
#include <stdio.h>
#include "operators.h"
#include "my_fscanf.h"

int main(){
FILE *fp = NULL;
int main() {
FILE *fp = fopen("read.txt", "r");
int operand1, operand2;
char operator = ' ';
int result, line = 0;
int line = 0;
double(*calculator)(int, int);

fp = fopen("read.txt","r");
if(fp!=NULL){
fscanf(fp, "%d", &line);

for(int i=0; i<line; i++) {
fscanf(fp, "%d %c %d",&operand1, &operator, &operand2);
switch(operator) {
case '+':
result = add(operand1, operator);
fp = fopen("read.txt", "r");
if (fp != NULL) {
my_fscanf(fp, "%d", &line);

for (int i = 0; i<line && !feof(fp); i++) {
my_fscanf(fp, "%d %c %d", &operand1, &operator, &operand2);
switch (operator) {
case '+':
calculator = add;
break;
case '-':
calculator = minus;
break;
case '-':
result = minus(operand1, operator);
case '*':
calculator = mul;
break;
case '*':
result = mul(operand1, operator);
case '/':
result = div(operand1, operator);
case '/':
calculator = div;
break;
}
printf("%d %c %d = %d\n",
operand1, operator, operand2, result);
}

printf("%d %c %d = %lf\n",
operand1, operator, operand2, calculator(operand1, operand2));
}
}
return 0;
}

Binary file removed calc.o
Binary file not shown.
43 changes: 43 additions & 0 deletions my_fscanf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// file: my_fscanf.c
#include "my_fscanf.h"
#include <stdarg.h>
#include <ctype.h>
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))) {}

ungetc(c, fp);
break;
}
case 'c':
{
while (isspace(c = getc(fp))) {}

char* addr = va_arg(list, char*);
*addr = c;
break;
}
}
}
else
format++;
}
va_end(list);
}
6 changes: 6 additions & 0 deletions my_fscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// file: my_fscanf.h
#ifndef MY_FSCANF_H
#define MY_FSCANF_H
#include <stdio.h>
void my_fscanf(FILE *fp, const char *format, ...);
#endif /* !MY_FSCANF_H */
10 changes: 5 additions & 5 deletions operators.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "operators.h"

int add(int op1, int op2) {
double add(int op1, int op2) {
return op1+op2;
}
int minus(int op1, int op2) {
double minus(int op1, int op2) {
return op1-op2;
}
int mul(int op1, int op2) {
double 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;
}

8 changes: 4 additions & 4 deletions operators.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int add(int op1, int op2);
int mul(int op1, int op2);
int div(int op1, int op2);
int minus(int op1, int op2);
double add(int op1, int op2);
double mul(int op1, int op2);
double div(int op1, int op2);
double minus(int op1, int op2);
Binary file removed operators.o
Binary file not shown.