Skip to content

Instantly share code, notes, and snippets.

@taishi41228
Created March 4, 2013 05:33
Show Gist options
  • Save taishi41228/5080166 to your computer and use it in GitHub Desktop.
Save taishi41228/5080166 to your computer and use it in GitHub Desktop.
1桁同士の四則演算 ref: http://qiita.com/items/916302df2fb2fe2e6a5c
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment