math.c
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
double bekijo(char str[]) {
int operator, count = 0;
double num1, num2;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == '+') {
operator = 0;
count++;
} else if (str[i] == '-') {
operator = 1;
count++;
} else if (str[i] == '*') {
operator = 2;
count++;
} else if (str[i] == '/') {
operator = 3;
count++;
} else {
if (count != 1) {
num1 = atof(&str[i]);
} else {
num2 = atof(&str[i]);
}
}
}
if (operator == 0) {
return num1 + num2;
} else if (operator == 1) {
return num1 - num2;
} else if (operator == 2) {
return num1 * num2;
} else {
return num1 / num2;
}
}
int main(int argc, char *argv[]) {
char str[256] = "5/2";
printf("%s = %lf\n", str, bekijo(str));
return 0;
}