Skip to content
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
51 changes: 30 additions & 21 deletions calc.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
#include <stdio.h>
#include "operators.h"
#include "my_fscanf.h"

double calc(double op1, double op2, char op) {
double (*func)(double, double);
double ans;
switch(op) {
case '+':
func = add;
break;
case '-':
func = minus;
break;
case '*':
func = mul;
break;
case '/':
func = div;
break;
}
ans = func(op1, op2);
return ans;
}

int main(){
FILE *fp = NULL;
int operand1, operand2;
double operand1, operand2;
char operator = ' ';
int result, line = 0;
int line = 0;
double result = 0;

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);
break;
case '-':
result = minus(operand1, operator);
break;
case '*':
result = mul(operand1, operator);
case '/':
result = div(operand1, operator);
break;
}
printf("%d %c %d = %d\n",
operand1, operator, operand2, result);
my_fscanf(fp, "%d", &line);
for(int i=1; i<line; i++) {
my_fscanf(fp, "%f %c %f",&operand1, &operator, &operand2);
result = calc(operand1, operand2, operator);
printf("%f %c %f = %f\n", operand1, operator, operand2, result);
}
}
return 0;
Expand Down
42 changes: 42 additions & 0 deletions my_fscanf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#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 == '%') {
format++;
switch(*format) {
case 'd': {
int c = getc(fp);
int *pInt = va_arg(list, int*);
*pInt = c - '0';
break;
}
case 'f': {
int c = getc(fp);
while(isspace(c)) { c = getc(fp); }
double *pDouble = va_arg(list, double*);
double num = 0;
while(isdigit(c)) {
num = num*10 + c - '0';
c = getc(fp);
}
*pDouble = num;
break;
}
case 'c': {
char c = getc(fp);
char *pChar = va_arg(list, char*);
*pChar = c;
break;
}
}
}
else
format++;
}
va_end(list);
}
7 changes: 7 additions & 0 deletions my_fscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef MY_FSCANF_H
#define MY_FSCANF_H
#include <stdio.h>

void my_fscanf(FILE *fp, const char *format, ...);

#endif /* !MY_FSCANF_H */
11 changes: 5 additions & 6 deletions operators.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include "operators.h"

int add(int op1, int op2) {
double add(double op1, double op2) {
return op1+op2;
}
int minus(int op1, int op2) {
double minus(double op1, double op2) {
return op1-op2;
}
int mul(int op1, int op2) {
double mul(double op1, double op2) {
return op1*op2;
}

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