# include
# include
# include
# include
1000//# define maxn define an array yuan largest prime
Typedef struct valueStack {
Double * data;
Int capacity;
int size;
} valueStack;//define a record number of stack
Typedef struct operationStack {
Char * data;
Int capacity;
int size;
} operationStack;//define a record operator stack
OperationStack operationStack;
ValueStack valueStack;//define two stack
Void initValue (int sz) {
Valuestack. Capacity=sz;
Valuestack. Size=0;
Valuestack data=https://bbs.csdn.net/topics/(double *) malloc (sizeof (double) * valuestack in capacity).
}//initializes a stack
Void pushValue (double x) {
Valuestack. Data [valuestack size++]=x;
}//in the stack in a
Void popValue () {
- the valuestack. Size;
}//a stack
Int emptyValue () {
Return valuestack. Size==0;
}//whether the stack is empty
Double topValue () {
Return valuestack. Data [valuestack. Size - 1];
}//return stack element
Void initOperation (int sz) {
Operationstack. Capacity=sz;
Operationstack. Size=0;
Operationstack. Data=https://bbs.csdn.net/topics/(char *) malloc (sizeof (char) * operationstack capacity);
}
Void pushOperation (char) x {
Operationstack. Data [operationstack size++]=x;
}
Void popOperation () {
-- operationstack. Size;
}
Int emptyOperation () {
Return operationstack. Size==0;
}
Char topOperation () {
Return operationstack. Data [operationstack. Size - 1];
}//function operation ditto
Char s [maxn];
//+ - */% ^ ()
{int status (char op)
If (op=='+' | | op=='-')
return 1;
Else if (op=='*' | | op=='/' | | op=='%' | | op=='^')
Return 2;
Else if (op=='(')
return 0;
return -1;
}//return symbol priority
Double calc (double x, double y, char op) {
If (op=='+')
Return x + y;
Else if (op=='-')
Return the x - y;
Else if (op=='*')
Return x * y;
Else if (op=='/')
Return the x/y;
Else if (op=='%') {
If (x!=(int) x | | y! Y=(int)) {
Printf (" cannot be % decimal Numbers \ n ");
exit(0);
}
The else {
Return (int) (int) x % y;
}
}
Else if (op=='^')
Return pow (x, y);
}//calculate the value of x op y
Void of solve (char op) {
Double x, y;
while (! EmptyOperation () & amp; & The status (topOperation ()) & gt;=the status (op)) {
X=topValue ();
PopValue ();
Y=topValue ();
PopValue ();
PushValue (calc (y, x, topOperation ()));
PopOperation ();
}
}//all priority within computing stack is not less than the value of the op
Int main () {
InitValue (1000);
InitOperation (1000);
Int I, j, len, cntNum=0, cntOp=0;
A double value=https://bbs.csdn.net/topics/0;
Double x, y, temp.
Printf (" please enter the expression: ");
The scanf (" % s ", s);
Len=strlen (s);
S [len++]='#';
for (i=0; I & lt; Len;) {
j=i;
If (s [j] & gt;='0' & amp; & S [j] <='9') {
While (j & lt; Len & amp; & (s [j] & gt;='0' & amp; & S [j] <)='9') {
Value=https://bbs.csdn.net/topics/value + s [j] - '0';
J++;
}
If (j & lt; Len & amp; & S [j]==') {
Temp=0.1;
J++;
While (j & lt; Len & amp; & (s [j] & gt;='0' & amp; & S [j] <)='9') {
The value +=[j] - '0' (s) * temp;
Temp *=0.1;
J++;
}
}
I=j;
} else {
If (the value!=0) {
CntNum++;
Value=https://bbs.csdn.net/topics/0;
}
[I] if (s=='+' | | s [I]=='-' | | s [I]=='*' | | s [I]=='/' | | s [I]=='^' | | s [I]=='%')
CntOp++;
i++;
}
}
If (cntNum! CntOp + 1)={
Printf (" expression illegal \ n ");
exit(0);
}//look at the number of operator is the number of Numbers - 1
for (i=0; I & lt; Len;) {
j=i;
If (s [j] & gt;='0' & amp; & S [j] <='9') {
While (j & lt; Len & amp; & (s [j] & gt;='0' & amp; & S [j] <)='9') {
Value=https://bbs.csdn.net/topics/value * 10 + s [j] - '0';
J++;
}//read digital
If (j & lt; Len & amp; & S [j]==') {
Temp=0.1;
J++;
While (j & lt; Len & amp; & (s [j] & gt;='0' & amp; & S [j] <)='9') {
The value +=[j] - '0' (s) * temp;
Temp *=0.1;
J++;
}
}
I=j;//change the loop variable
Read an integer}//
The else {
If (value=0 && s https://bbs.csdn.net/topics/=[I]=='(') {
[I] pushOperation (s);
i++;
continue;
}
If (the value!=0)
PushValue (value), value=https://bbs.csdn.net/topics/0;//if not within 0 join numerical stack
[I] if (s=='#') {
[I] solve (s);//if to #, is the end, you have to put the stack values count down
break;//exit loop
[I]} else if (s=='(') {
[I] pushOperation (s);//if they are left parenthesis, directly within the stack to join operator
[I]} else if (s==') ') {
while (! EmptyOperation () & amp; & TopOperation ()!='(') {
X=topValue ();
PopValue ();//remove the value stack element
Y=topValue ();
PopValue ();//remove the value stack element
PushValue (calc (y, x, topOperation ()));//add calculated values to value within the stack
PopOperation ();//remove the operator stack element
}//if it is a right parenthesis, has been operating to the left parenthesis
PopOperation ();//remove the left parenthesis
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull