算术表达式求值

来源:互联网 发布:淘宝如何上传身份证 编辑:程序博客网 时间:2024/04/28 02:51

算上表表达式C语言简易实现

#include<iostream>#include<cstring>using namespace std;const int MAXSIZE=100;typedef struct stack{ char data[MAXSIZE]; int  top;}SeqStack;typedef struct Stack{ int data[MAXSIZE]; int top;}SeqIntStack;typedef struct queue{ char data[MAXSIZE]; int rear,front;}Queue;//////////////////////////整型栈////////////////////////////////////void InitIntStack(SeqIntStack *&s){ s=(SeqIntStack*)malloc(sizeof(SeqIntStack)); s->top=-1; memset(s->data,0,sizeof(int));}int Pop(SeqIntStack *&s){ if(s->top==-1) {  cout<<"the stack is empty"<<endl;  return -1; } return s->data[s->top--];}int Push(SeqIntStack *&s,int data){ if(s->top==MAXSIZE-1) {  cout<<"overflow"<<endl;  return -1; } s->data[++s->top]=data; return 0;}int GetIntTop(SeqIntStack *&s){ if(s->top==-1) {  cout<<"no data"<<endl; } return s->data[s->top];}void DeleteIntStack(SeqIntStack *&s){ free(s);}///////////////////////////////字符栈///////////////////////////////////////void InitStack(SeqStack *&s){    //memset(s.data,0,sizeof(char)); s=(SeqStack*)malloc(sizeof(SeqStack)); memset(s->data,0,sizeof(char));    s->top=-1;}int push(SeqStack *&s,char c){ if(s->top==MAXSIZE-1) {  cout<<"栈满"<<endl;  return -1; }    s->data[++s->top]=c;    return 0;}char pop(SeqStack *&s){ if(s->top==-1) {  cout<<"栈空"<<endl;  return '/0'; }    return s->data[s->top--];}char GetTop(SeqStack *&s){ if(s->top==-1) {  cout<<"取栈顶栈空"<<endl;  return '/0'; } return s->data[s->top];}void deleteStack(SeqStack *&s){  free(s);}////////////////////////////队列///////////////////////////////////////////void InitQueue(Queue *&q){ q=(Queue*)malloc(sizeof(Queue));    memset(q->data,0,sizeof(char)); q->front=q->rear=-1;// q->front=q->rear=0;//循环队列 //memset(q.data,0,sizeof(char)); }void Enqueue(Queue *&q,char ch){// if((q->rear+1)%MAXSIZE==q->front) if(q->rear+1==MAXSIZE) {  cout<<"队满"<<endl;  return ; } //q->rear=(q->rear++)%MAXSIZE; (q->rear)++; q->data[q->rear]=ch;}char Dequeue(Queue *&q){  if(q->rear==q->front) {        cout<<"队空"<<endl;  return '/0'; }// q->front=(q->front++)%MAXSIZE; (q->front)++; return q->data[q->front];}void clearQueue(Queue *&q){ free(q);}int ComputeResult(char exp[])//此处与char *exp同等效果{ int result=0; SeqIntStack *operatorStack;    SeqStack *operandStack;//运算符栈 Queue *operandQueue;//运算符队列 InitQueue(operandQueue);    InitIntStack(operatorStack); InitStack(operandStack);     char ch='/0',temp='/0'; int i=0; while((ch=exp[i])!='/0') {  if(ch>='0'&&ch<='9')  {   char tempCh[10];      int j=0;   tempCh[j++]=ch;   ch=exp[++i];            while(ch>='0'&&ch<='9')   {       tempCh[j++]=ch;    ch=exp[++i];   }            tempCh[j]='/0';   Push(operatorStack,atoi(tempCh));   i--;  }  else if(ch=='(')  {   push(operandStack,ch);  }  else if(ch==')')  {           //char temp;     temp=pop(operandStack);     while(temp!='('&&temp!='/0')     {      Enqueue(operandQueue,temp);               temp=pop(operandStack);     }             }  else if(ch==' ')  {  }  else  {           switch(ch)     {     case '+':     case '-':               //当是+或者-运算符时候将栈中的元素都弹出然后入队     // temp=GetTop(operandStack);      temp=pop(operandStack);               while(temp!='/0'&&temp!='(')      {       Enqueue(operandQueue,temp);       temp=pop(operandStack);      }      push(operandStack,ch);      break;     case '*':     case '/':      //temp=pop(operandStack);      temp=GetTop(operandStack);      if(temp=='+'||temp=='-'||temp=='/0')      {       push(operandStack,ch);      }      else      {                  //Enqueue(operandQueue,temp);       while(!(temp=='+'||temp=='-'||temp=='/0'))       {        temp=pop(operandStack);        Enqueue(operandQueue,temp);       }         }      break;     }  }  i++; }  ///  while((temp=pop(operatorStack))!='/0') //{ // cout<<temp<<endl;// } while((temp=pop(operandStack))!='/0') {  Enqueue(operandQueue,temp); } int x,y;    while((temp=Dequeue(operandQueue))!='/0') {    y=Pop(operatorStack); // cout<<y<<"  "<<operatorStack->top<<endl;  x=Pop(operatorStack);  switch(temp)  {  case '+':y=x+y;Push(operatorStack,y);break;  case '-':y=x-y;Push(operatorStack,y);break;  case '*':y=x*y;Push(operatorStack,y);break;  case '/':y=x/y;Push(operatorStack,y);break;  } }    result=GetIntTop(operatorStack);    return result;}int JudgeTheExpValidity(char exp[]){ char ch='/0'; int i=0; SeqStack *s; InitStack(s); while((ch=exp[i])!='/0') {  if((ch>='0'&&ch<='9')||ch==')'||ch=='('||ch=='+'||ch=='-'||ch=='*'||ch=='/')  {           if(ch>='0'&&ch<='9')     {             i++;     }     else if(ch=='('||ch=='+'||ch=='-'||ch=='*'||ch=='/')     {      if(ch=='(')      {           push(s,ch);      }      ch=exp[++i];      if(!(ch>='0'&&ch<='9'))      {       cout<<"不符合表达式格式,运算符或者左括号后不是操作数"<<endl;       return -1;      }      else      {       i++;      }                    }     else//ch==')'     {      if((ch=pop(s))==')')      {                  i++;      }      else      {       cout<<"没有匹配的做括号"<<endl;       return -1;      }          }  }  else  {   cout<<"表达式中有非法字符"<<endl;   return -1;  }  //i++; } return 0;}int main(){ char exp[MAXSIZE]; cout<<"please input the expression: example || 157+163+167+173+179+181+191+193+197+199+211"<<endl; cin>>exp;    while(true) {  if(-1==JudgeTheExpValidity(exp))  {   cout<<"please input the valid expression"<<endl;   cin>>exp;  }  else  {   break;  } } int result;    result=ComputeResult(exp); cout<<result<<endl;    return 0;}


原创粉丝点击