Skip to content

Instantly share code, notes, and snippets.

@ochilab
Created October 11, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/5aa8c820ce2c7ee150efa3eeae2c70c9 to your computer and use it in GitHub Desktop.
Save ochilab/5aa8c820ce2c7ee150efa3eeae2c70c9 to your computer and use it in GitHub Desktop.
簡易版逆ポーランド記法
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