Skip to content
Open

lab9 #59

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
24 changes: 13 additions & 11 deletions calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@

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

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

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

12 changes: 6 additions & 6 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);