forked from Classroom-IOSSD/SimpleCalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.c
More file actions
43 lines (39 loc) · 752 Bytes
/
calc.c
File metadata and controls
43 lines (39 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include "operators.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;
double operand1, operand2;
char operator = ' ';
int line = 0;
double result = 0;
fp = fopen("read.txt","r");
if(fp!=NULL){
fscanf(fp, "%d", &line);
for(int i=1; i<line; i++) {
fscanf(fp, "%lf %c %lf",&operand1, &operator, &operand2);
result = calc(operand1, operand2, operator);
printf("%lf %c %lf = %f\n", operand1, operator, operand2, result);
}
}
return 0;
}