Created
March 5, 2017 04:33
-
-
Save cthbst/7d16079cffa4cbbe9b32d22bfe90343f 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
#include <bits/stdc++.h> | |
using namespace std; | |
double Eval2(char *, char *&); | |
double Eval1(char *, char *&); | |
double Eval0(char *, char *&); | |
double Eval2(char *expr, char *&end) { | |
double res=0; | |
if (*(end=expr) == '(') { | |
res = Eval0(end+1, end); | |
if (*end == ')') ++end; | |
} else { res=strtod(end, &end); } | |
return res; | |
} | |
double Eval1(char* expr, char *&end) { | |
double res = Eval2(expr, end); | |
while (*end=='*' || *end=='/') | |
(*end=='*')? (res*=Eval2(end+1, end)): (res/=Eval2(end+1, end)); | |
return res; | |
} | |
double Eval0(char* expr, char *&end) { | |
double res = Eval1(expr, end); | |
while (*end=='+' || *end=='-') | |
res +=(*end=='+')? Eval1(end+1, end): -Eval1(end+1, end); | |
return res; | |
} | |
int main() { | |
char expr[128]={0}, *pos=0; | |
printf("Exp : "); | |
scanf("%s", expr); | |
printf("Ans : %f\n", Eval0(expr, pos)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment