1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

1桁同士の四則演算

1
Posted at
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;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?