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
Binary file removed calc
Binary file not shown.
32 changes: 21 additions & 11 deletions calc.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
#include <stdio.h>
#include "operators.h"

double cal (double oper1,double oper2, double (*func)(double,double))
{
return func(oper1,oper2);
}


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

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);
for(int i=0; i<line-1; i++) {
fscanf(fp, "%lf %c %lf",&operand1, &operator, &operand2);
switch(operator) {
case '+':
result = add(operand1, operator);
func = add;
break;
case '-':
result = minus(operand1, operator);
func = minus;
break;
case '*':
result = mul(operand1, operator);
func = mul;
break;
case '/':
result = div(operand1, operator);
func = div;
break;
}
printf("%d %c %d = %d\n",
}
result = cal(operand1, operand2, func);
printf("%.0lf %c %.0lf = %lf\n",
operand1, operator, operand2, result);
}
}
Expand Down
Binary file removed calc.o
Binary file not shown.
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(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);
Binary file removed operators.o
Binary file not shown.