Moreover, lz still requires operations with parameter values.
I have written about numerical expression operation before, so I can refer to it:
[emoticon evaluation]
After the above description, the difficulty lies in expression evaluation. Here we introduce the most common and simple method, stack operation.
The method of expression evaluation with stack;
First, we should give each symbol a priority:
Symbol+-*/()
In-stack level 2 4 6 0 8
Off-stack level 1 3 5 8 0
As you can see, symbols with high priority are counted first. For convenience, we define the special symbol #, which has the lowest level (Fu-1).
Put it at the bottom of the stack first, then read each character in turn. If it is a number, put it on the stack. If it is a symbol, compare the priority with the symbol at the top of the stack.
If they are equal, the stack is expanded and the next character is read. If it is large outside the stack, put it in the stack.
If the stack is large, the top element of the stack is calculated by the top two elements of the stack, and the result is input into the stack. This symbol continues to be processed (compared with the top of the stack).
Until reading the last symbol # makes the bottom of the stack # leave the stack. The top of the stack is the result of the expression.
Then, this topic becomes clear, and the rest is to turn our expression into code.
The following is an expression operation of double type (full version)
/**
* t.c
*
* Founded on March 9, 2009
* Author: T
*/
# include & ltstdio.h & gt
# include & ltctype.h & gt
# include & ltstdlib.h & gt
# include & ltstring.h & gt
# include & ltmath.h & gt
int i,j,k,n,l;
char s[200];
void U(char*s){
char * t;
int i=- 1,j=0,k,g;
//Delete extra spaces
for(t =(char *)malloc(strlen(s)+ 1); s[++ I]; )if(s[i]! = ' ')t[j++]= s[I];
//Remove redundant addition and subtraction.
for(I = j = t[j]= 0; t[I]; )if(t[i]=='+'||t[i]=='-'){
g = 1;
for(k = I; t[k]& amp; & amp(t[k]= = '+' | | t[k]= = '-'); k++)if(t[k]-'+')g =-g;
s[j++]= g & lt; 0? '-':'+';
I = k;
} else s[j++]= t[i++];
s[j++]= ')';
s[j]= 0;
//replace "atan2" with "atanz" and "log 10" with "loglO"
for(; (t=strstr(s," atan2 "))! = NULL)t[4]= ' Z ';
for(; (t=strstr(s," log 10 "))! = NULLt[4]= ' O ')t[3]= ' l ';
}
//Find the largest of the top n items.
double max(double*s,int i){
int t = * s;
for(; I-; )if(t & lt; s[I])t = s[I];
return t;
}
double min(double*s,int i){
int t = * s;
for(; I-; )if(s[I]& lt; t)t = s[I];
return t;
}
//Record function type
int R(char*s,int i){
char t[ 10];
int j;
for(j = I; 0< Johnson & Johnson Company. & ampisalpha(s[j- 1]); )j-;
strncpy(t,s+j,I-j);
t[I-j]= 0;
//Here are some mathematical functions that come with the function library.
If (! Strcmp("abs ",t)) returns1; //abs () Find the absolute value
If (! Strcmp("acos ",t)) returns 2; //acos () Find the arccosine
If (! Strcmp("asin ",t)) returns 3; //asin () Find the arcsine
If (! Strcmp("atan ",t)) returns 4; //atan () seeks arc tangent.
If (! Strcmp("atanZ ",t)) returns 5; //atan2 () finds the arc tangent and determines the quadrant according to the sign.
If (! Strcmp("ceil ",t)) returns 6; //ceil () Find the smallest integer not less than a certain value (find the upper bound).
If (! Strcmp("cos ",t)) returns 7; //cos () find cosine
If (! strcmp("cosh ",t))return 8; //cosh () find hyperbolic cosine
If (! Strcmp("exp ",t)) returns10; //exp () find the power of e.
If (! Strcmp("fabs ",t)) returns11; //fabs () Find the absolute value of floating-point number
If (! Strcmp("floor ",t)) returns12; //floor () Find the largest integer not greater than a certain value (find the lower bound).
If (! Strcmp("mod ",t)) returns13; //fmod () Find the modulus
If (! Strcmp("log ",t)) returns18; //log () natural logarithm
If (! Strcmp("loglO ",t)) returns19; //log 10 () Natural logarithm with 10 as the base.
If (! Strcmp("pow ",t)) returns 21; //pow () to power
If (! Strcmp("sin ",t)) returns 22; //sin () Find Sine
If (! Strcmp("sinh ",t)) returns 23; //sinh () Find hyperbolic sine
If (! Strcmp("sqrt ",t)) returns 24; //sqrt () Find the square root
If (! strcmp("tan ",t))return 25; //tan () tangent
If (! strcmp("tanh ",t))return 26; //tanh () Find the hyperbolic tangent value
//The following function is self-complementary.
If (! Strcmp("max ",t)) returns101; //max () found the maximum value.
If (! Strcmp("min ",t)) returns102; //min () Find the minimum value
Returns 0;
}
//Function calculation
double F(double*m,int k,int l){
Double v = * m;;
Switch (k)
Case 1: return goods (FABS (v));
Case 2: Return goods (ACOS (V));
Case 3: Return (asin (v));
Case 4: Return (Atan (V));
Situation 5: Return (atan2(m[ 1], v)););
Case 6: Return (CEIL (V));
Case 7: Return (cos (v));
Case 8: Return (cosh (v));
Case10: return (exp (v));
Case 1 1: return (FABS (v));
Case 12: Return of goods (five floors);
Case 13: return (fmod(m[ 1], v)););
Case 18: return (log (v));
Case 19: return (log10 (v));
Case 21:return (pow (m [1], v)););
Case 22: Return (crime (5));
Case 23: Return (sinh (v));
Case 24: Return (sqrt (v));
Case 25: Return of Goods (Tan (5));
Case 26: Return (tanh (v));
Case 10 1: return(max(m, l)););
Case 102: return(min(m, l)););
}
Return to v;
}
//expression calculation
//o[] is the stack of operation symbols, n[] is the stack of numbers, r[] is the stack of functions, and m[] is the stack of commas.
Double c (character)
int r[ 128],o[ 128]={-2},i= 1,j=0,k=- 1,l;
double n[64],t,m[64];
char v[ 128],c;
v[',']= 2;
v['+']= 4; v['-']= 5;
v[' * ']= 6; v['/']= 7;
v['^']=8;
v['(']= 10; v[')']= 0;
memset(n,0,5 12);
For (the United States); (c = s[++k]); ){
for(; Issafa (c); )c = s[++k];
if(isdigit(c)||c== ' . '){
n[j]= atof(s+k);
for(; is digit(s[k+ 1])| | s[k+ 1]= = ','; )k++;
}
Otherwise {
for(l = 0; v[c]/2 & lt; = o[I- 1]/2; ){
t = n[j]; n[j-]= 0; I-;
if(o[i]==0){
m[l++]= t;
n[j]=F(m,r[i],l);
Break;
}
if(o[I]= = 1)m[l++]= t;
if(o[I]= = 4)n[j]+= t;
if(o[I]= = 5)n[j]-= t;
if(o[I]= = 6)n[j]* = t;
if(o[I]= = 7)n[j]/= t;
if(o[i]==8)n[j]=pow(n[j],t);
}
if(v[c]){
r[i]=R(s,k);
o[i++]=c-'('? c-','? v[c]: 1:0;
j++;
}
}
}
Returns n [0];
}
int main(){
Obtain;
printf("%f\n ",C(s));
Returns 0;
}