Created
October 11, 2020 14:37
-
-
Save ochilab/5aa8c820ce2c7ee150efa3eeae2c70c9 to your computer and use it in GitHub Desktop.
簡易版逆ポーランド記法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int simpleRpn(char pformula[100]){ | |
int stack[100]; | |
int sp = 0; | |
int ans; | |
int x,y,z; | |
for (int i = 0; pformula[i] != '\0'; i++) { | |
//数値の場合 | |
char c = pformula[i]; | |
if ( '9' >= c && c >= '1' ) { | |
z=atoi(&c); | |
push(z); | |
//非数値(演算子の場合) | |
} else { | |
y = pop(); | |
x = pop(); | |
if (pformula[i] == '+') { | |
ans=x+y; | |
} else if (pformula[i] == '-') { | |
ans=x-y; | |
} else if (pformula[i] == '*') { | |
ans=x*y; | |
} else if (pformula[i] == '/'){ | |
ans=x/y;; | |
} | |
push(ans); | |
} | |
} | |
return pop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment