forked from cooper-ece160/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_file.c
45 lines (38 loc) · 922 Bytes
/
calc_file.c
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
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float calc(char *args);
int main(int argc, char *argv[]) {
char *filename = argv[1];
FILE *fp;
// create a buffer that can hold 3 strings of
// up to 255 characters each
char buffer[256];
// open file for reading
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not open %s\n", filename);
return -1;
}
// read one line at a time and send it to calc function for processing
char *s;
while ((s = fgets(buffer, 255, fp)) != NULL) {
float ans = calc(buffer);
printf("%.2f\n", ans);
}
fclose(fp);
return 0;
}
float calc(char *args) {
char op;
float x, y;
sscanf(args, "%c %f %f", &op, &x, &y);
switch (op) {
case '+' : return x + y;
case '-' : return x - y;
case 'x' : return x * y;
case '/' : return x / y;
}
printf("Error: Unrecognized operator %c\n", op);
return 0;
}