Today, I have asked for help. I want to implement expression calculation with parentheses, addition, subtraction, multiplication, and division.
I wrote it to my sister-in-law immediately. The C language code is as follows. I used two stacks to calculate the inverse polish expression:
[C]
//
# Include <stdio. h>
# Include <stdlib. h>
// Operator stack Length
# Define OPSTACK_LENGTH 5
// The length of the operand Stack
# Define NUMSTACK_LENGTH 100
// Maximum length of the input string
# Define MAX_STRING_LENGTH 100
// Operator struct
Struct operatorStruct
{
// Operator name
Char name;
// Priority
Int priority;
// Number of objects, that is, the number of operations. For example, the single object operator is 1, and the binary operator is 2.
Int opnum;
};
Typedef struct operatorStruct OPERATOR;
// Operator Stack
OPERATOR opStack [OPSTACK_LENGTH];
// Operator stack top pointer
Int opStackTop =-1;
// Operand Stack
Double numStack [NUMSTACK_LENGTH];
// Top pointer of the operand Stack
Int numStackTop =-1;
// Obtain the priority of the operator represented by a character
Int getPriority (char name)
{
If (name = '(' | name = ')')
{
Return 0;
}
If (name = '! ')
{
Return 3;
}
If (name = '*' | name = '/')
{
Return 2;
}
If (name = '+' | name = '-')
{
Return 1;
}
Exit (1 );
}
// Obtain the number of operators represented by a character
Int getOpNum (char name)
{
If (name = '*' | name = '/' | name = '+' | name = '-')
{
Return 2;
}
If (name = '! ')
{
Return 1;
}
If (name = '(' | name = ')')
{
Return 0;
}
Exit (1 );
}
// Operator pressure Stack
Void pushOperator (OPERATOR op)
{
If (opStackTop <OPSTACK_LENGTH-1)
{
OpStack [++ opStackTop] = op;
}
Else
{
Exit (1 );
}
}
// Operator Output Stack
OPERATOR popOperator ()
{
If (opStackTop> = 0)
{
Return opStack [opStackTop --];
}
Else
{
Exit (1 );
}
}
// Operand pressure Stack
Void pushNumber (double num)
{
If (numStackTop <NUMSTACK_LENGTH-1)
{
NumStack [++ numStackTop] = num;
}
Else
{
Exit (1 );
}
}
// Operations output Stack
Double popNumber ()
{
If (numStackTop> = 0)
{
Return numStack [numStackTop --];
}
Else
{
Exit (1 );
}
}
// Convert a segment starting with 0-9 and ending with the next operator into a floating point type.
// I and the floating point String Length
Double getNumFromString (char * s, int * I)
{
Int j = 0;
Static char numstr [MAX_STRING_LENGTH];
While (* s)> = '0' & * s <= '9 ')
{
Numstr [j ++] = (* s );
S ++;
}
If (* s) = '.')
{
Numstr [j ++] = (* s );
S ++;
While (* s)> = '0' & * s <= '9 ')
{
Numstr [j ++] = (* s );
S ++;
}
}
(* I) = (* I) + j;
Numstr [j] = '\ 0 ';
Return atof (numstr );
}
// Two operands are displayed from the operand stack to complete a binary operation.
Double opertate2Num (OPERATOR op)
{
Double num2 = popNumber ();
Double num1 = popNumber ();
If (op. name = '+ ')
{
Return num1 + num2;
}
If (op. name = '-')
{
Return num1-num2;
}
If (op. name = '*')
{
Return num1 * num2;
}
If (op. name = '/')
{
Return num1 + num2;
}
Exit (1 );
}
// An operand pops up from the operand stack to complete a single operation.
Double opertate1Num (OPERATOR op)
{
Double num = popNumber ();
If (op. name = '! ')
{
Double result = 1;
While (num> 1)
{
Result * = num;
Num --;
}
Return result;
}
Exit (1 );
}
// Complete an operation for www.2cto.com
Double operate (OPERATOR op)
{
If (op. opnum = 1)
{
Return opertate1Num (op );
}
Else if (op. opnum = 2)
{
Return opertate2Num (op );
}
Exit (1 );
}
Int main ()
{
Char string [MAX_STRING_LENGTH]; // input string of the expression
Int I;
OPERATOR op, topOp; // op is an OPERATOR extracted from the current input string, and topOp is the OPERATOR at the top of the OPERATOR Stack
TopOp. name = '#';
TopOp. priority = 0;
TopOp. opnum = 0;
PushOperator (topOp); // press # as the initial Operator
Scanf ("% s", string );
For (I = 0; string [I]! = '\ 0' & string [I]! = ';)
{
// Extract a character from the input string as the start and process it until the expression ends.
If (string [I]> = '0' & string [I] <= '9 ')
{
// If it is an operand, extract the entire operand and press it into the operand stack.
PushNumber (getNumFromString (& string [I], & I ));
}
Else
{
Op. name = string [I];
Op. priority = getPriority (string [I]);
Op. opnum = getOpNum (string [I]);
TopOp = popOperator ();
If (op. name = '(')
{
// If it is '(', the operator popped up from the top of the stack is pushed back to the stack, and the current operator is pressed to the stack.
PushOperator (topOp );
PushOperator (op );
}
Else if (op. name = ')')
{
// If it is ')', the operation is performed. The result of each operation is pushed into the operand stack as an operand until the '(' pop-up operator Stack
While (topOp. name! = '(')
{
PushNumber (operate (topOp ));
TopOp = popOperator ();
}
}
Else
{
// If it is a common operator
If (topOp. name! = '#' & Op. priority <= topOp. priority)
{
// If the operator stack is not empty and the current operator has a higher priority than the top operator of the stack, an operation is performed to push the result to the operand stack.
PushNumber (operate (topOp ));
}
Else
{
// Otherwise, compress the operator popped up from the top of the stack
PushOperator (topOp );
}
// Press the current operator to stack
PushOperator (op );
}
I ++;
}
}
// Complete the remaining operations in the stack
While (topOp = popOperator (). name! = '#')
{
PushNumber (operate (topOp ));
}
// The last number left in the operand stack is the result.
Printf ("% f \ n", popNumber ());
Return 0;
}
Author: xiaohaoqiong