Course design of data structure-method of realizing expression evaluation with stack _c language

Source: Internet
Author: User
Tags aop arithmetic rewind
1. Demand Analysis
Design a program to demonstrate the process of evaluation of arithmetic expressions using operator precedence. Using the operator precedence relation, the evaluation of the arithmetic four mixed operation expression is realized.
(1) Form of input: expression, such as 2* (3+4)
Included operators can only have ' + ', '-', ' * ', '/', ' (', ') ';
(2) The form of the output: the result of the operation, for example 2* (3+4) = 14;
(3) The function that the program can achieve: Evaluate the expression and output
2. System Design
1, the stack of the abstract data type definition:
ADT stack{
Data objects: d={ai|ai∈elemset,i=1,2,..., n,n≥0}
Data relationship: r1={<ai-1,ai>|ai-1,ai∈d,i=2,..., N}
Agreed an end for the stack top, AI end for the stack bottom
Basic operations:
Push (&s,e)
Initial condition: Stack s already exists
Action Result: Insert element e As new stack top element
Pop (&s,&e)
Initial condition: Stack s already exists and is not empty
Action Result: delete s stack top element and return its value with E
}adt Stack
3, the main functions of each module:
*push (SC *s,char C): Press the character stack
*push (SF *s,float f): Put the numerical pressure stack
*pop (SC *s): the word Fu
*pop (SF *s): Rewind The value of the stack
Operate (a,theta,b): Perform ' + ', '-', ' * ', '/', ' ^ ' operations on A and B according to Theta
In (Test,*testop): Returns True if Test is an operator, otherwise returns false
Returnopord (op,*testop): if test is an operator, returns the subscript of this operator in an array
Precede (Aop,bop): Returns the priority between Aop and BOP based on the operator precedence table
EvaluateExpression (*myexpression): Evaluation of arithmetic expressions by operator precedence
The complete program code is as follows:
Copy Code code as follows:

#include "stdio.h"
#include "Stdlib.h"
#include "string.h"
#include "math.h"
#define TRUE 1
#define FALSE 0
#define OPSETSIZE 8
typedef int STATUS;
unsigned char prior[8][8] =
{//Operator precedence table
// '+' '-' '*' '/' '(' ')' '#' '^'
/* ' + '/' > ', ' > ', ' < ', ' < ', ' < ', ' > ', ' > ', ' < ',
/* '-' > ', ' > ', ' < ', ' < ', ' < ', ' > ', ' > ', ' < ',
/* ' * ' > ', ' > ', ' > ', ' > ', ' < ', ' > ', ' > ', ' < ',
/* '/' > ', ' > ', ' > ', ' > ', ' < ', ' > ', ' > ', ' < ',
/* ' (' < ', ' < ', ' < ', ' < ', ' < ', ' = ', ', ' < ',
/* * ' > ', ' > ', ' > ', ' > ', ', ' > ', ' > ', ' > ',
/* ' # '/' < ', ' < ', ' < ', ' < ', ' < ', ', ' = ', ' < ',
/* ' ^ '/' > ', ' > ', ' > ', ' > ', ' < ', ' > ', ' > ', ' > '
};
typedef struct STACKCHAR
{
char c;
struct Stackchar *next;
}SC; Stackchar Type of Node SC
typedef struct STACKFLOAT
{
float F;
struct Stackfloat *next;
}SF; Stackfloat Type of node SF
SC *push (SC *s,char c)//sc type pointer Push, return p
{
SC *p= (sc*) malloc (sizeof (SC));
p->c=c;
p->next=s;
return p;
}
SF *push (SF *s,float f)//sf type of pointer Push, return p
{
SF *p= (sf*) malloc (sizeof (SF));
p->f=f;
p->next=s;
return p;
}
SC *pop (sc *s)//sc type of pointer Pop
{
SC *q=s;
s=s->next;
Free (q);
return s;
}
SF *pop (SF *s)//sf type of pointer Pop
{
SF *q=s;
s=s->next;
Free (q);
return s;
}
Float Operate (float a,unsigned Char Theta, float B)//COMPUTE function Operate
{
Switch (theta)
{
Case ' + ': return a+b;
Case '-': return a-b;
Case ' * ': return a*b;
Case '/': return a/b;
Case ' ^ ': return pow (A,B);
Default:return 0;
}
}
Char opset[opsetsize]={' + ', '-', ' * ', '/', ' (', ') ', ' # ', ' ^ '};
Status in (char Test,char *testop)
{
int find=false;
for (int i=0; i< opsetsize; i++)
{
if (Test = = Testop[i])
find= true;
}
return to find;
}
Status Returnopord (char Op,char *testop)
{
for (int i=0; i< opsetsize; i++)
{
if (op = = Testop[i])
return i;
}
}
Char precede (char Aop, char Bop)
{
Return Prior[returnopord (Aop,opset)][returnopord (Bop,opset)];
}
Float evaluateexpression (char* myexpression)
{
An operator precedence algorithm for arithmetic expression evaluation
Set Optr and OPND respectively as operator stacks and operand stacks, op as operator set
SC *optr=null; operator Stacks, character elements
SF *opnd=null; OP arithmetic stack, real element
Char tempdata[20];
float data,a,b;
Char theta,*c,dr[]={' # ', ' n '};
Optr=push (Optr, ' # ');
C=strcat (MYEXPRESSION,DR);
strcpy (TempData, "n");//String copy function
while (*c!= ' # ' | | optr->c!= ' # ')
{
if (! In (*c, Opset))
{
Dr[0]=*c;
strcat (TEMPDATA,DR); string concatenation function
C + +;
if (In (*c, Opset))
{
Data=atof (TempData); String conversion functions (double)
Opnd=push (OPND, Data);
strcpy (TempData, "the");
}
}
else//Not operator is in stack
{
Switch (Precede (optr->c, *c))
{
Case ' < '://stack top element priority low
Optr=push (Optr, *c);
C + +;
Break
Case ' = '://parenthesis and receive next character
Optr=pop (OPTR);
C + +;
Break
Case ' > '://Rewind the stack and put the results of the operation into the stack
Theta=optr->c;optr=pop (OPTR);
B=opnd->f;opnd=pop (OPND);
A=opnd->f;opnd=pop (OPND);
Opnd=push (OPND, Operate (A, theta, b));
Break
}//switch
}
}//while
Return opnd->f;
}//evaluateexpression
int main (void)
{
Char s[128];
Puts ("Please enter an expression:");
Gets (s);
Puts ("The value of the expression is:");
printf ("%s\b=%g\n", S,evaluateexpression (s));
System ("pause");
return 0;
}

The test results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.