表达式计算(C语言版)

#include"stdlib.h"
typedef double DataType;   /*数据存放类型*/
typedef int OpType;        /*运算符存放类型*/
char *code = "+-*/()#";    /*运算符串*/
char priority[7][7] = {'>','>','<','<','<','>','>',
                                  '>','>','<','<','<','>','>',
                                  '>','>','>','>','<','>','>',
                                  '>','>','>','>','<','>','>',
                                  '<','<','<','<','<','=','>',
                                  '>','>','>','>', 0 ,'>','>',
                                   0 , 0 , 0 , 0 , 0 , 0 , 0 };      /*算符优先表*/

DataType Add(DataType a,DataType b)   /*加*/
{
  return a+b;
}

DataType Sub(DataType a,DataType b)   /*减*/
{
  return a-b; 
}

DataType Mul(DataType a,DataType b)   /*乘*/
{
  return a*b;
}

DataType Div(DataType a,DataType b)   /*除*/
{
  return a/b;
}

DataType (*func[])(DataType,DataType)={Add,Sub,Mul,Div};
                                    
typedef struct
{        
 int top;                                                   /*数据栈*/
 int max;       /*最大长度*/
 DataType *stack;
}datastack;

typedef struct
{        
 int top;                                                   /*运算符栈*/
 int max;      /*最大长度*/
 OpType *stack;
}operastack;

int DataPush(datastack *s,DataType a)                          /*数据入栈*/

    if(s->top == s->max)
  return 0;
 s->stack[s->top]=a;
 s->top++;
 return 1;
}                                                              

int DataPop(datastack *s,DataType *d)         /*数据出栈*/
{  
 if(s->top == 0)
  return 0;
 s->top--;
 *d = s->stack[s->top];
 return 1;
}

int OperaPush(operastack *s,OpType a)                         /*运算符入栈*/

    if(s->top == s->max)
  return 0;
 s->stack[s->top]=a;
 s->top++;
 return 1;
}                                                              

int OperaPop(datastack *s,OpType *op)        /*运算符出栈*/
{  
 if(s->top == 0)
  return 0;
 s->top--;
 *op = s->stack[s->top];
 return 1;
}

datastack * CreateDataStack(int n)         /*创建数据栈*/
{
    datastack *data = (datastack *)malloc(sizeof(datastack));
 if(!data)
  return 0;
 data->stack = (DataType *)malloc(sizeof(DataType)*n);
 if(!data->stack)
 {
  free(data);
  return 0;
 }
 data->top = 0;
 data->max = n;
 return data;
}

operastack * CreateOperaStack(int n)     /* 创建运算符栈*/
{
 operastack *opera = (operastack *)malloc(sizeof(operastack));
 if(!opera)
  return 0;
 opera->stack = (OpType *)malloc(sizeof(OpType)*n);
 if(!opera->stack)
 {
  free(opera);
  return 0;
 }
 opera->top = 0;
 opera->max = n;
 return opera;
}

int account(char a[],DataType *result)                                     /*表达式计算*/
{
    int i,j = 0,f = 0,k = 0,n = 100;
 char b[20];     /*用来记录数字串*/
 DataType temp = 0,temp1 = 0,temp2 = 0;  /*临时数据变量*/
 operastack *opera;
    datastack *data;
 data = CreateDataStack(n);
 if(!data)
  return 0;
 opera = CreateOperaStack(n);
 if(!opera)
  return 0;
 for(i=0;i<strlen(a);i++)
 {  
  if(a[i] == ' ')    /*遇空格则不作处理,直接跳过*/
   continue;
  if(a[i]>='0'&&a[i]<='9'||a[i] == '.')
  {
   b[j++] = a[i];
   f=1;          /*表示有待入栈数据*/
  }
  else
  { 
     if(f)
     {
      b[j] = 0;
      j = 0;
      if(!DataPush(data,atof(b)))    /*数据入栈*/
      {
       printf("Expression error!\n");
       return 0;
      }
      f=0;
     }
     for(k=0;a[i]!=code[k];k++);   /*找出运算符在运算符字符串中的位置*/
     if(opera->top == 0)   /*运算符栈为空*/
     {
               if(k == strlen(code)-1)  /*遇#,表达式结束*/  /*所有计算均已完成,返回结果*/
      *result = data->stack[--(data->top)];
               if(!OperaPush(opera,k))   /*运算符入栈*/
      {
       printf("Expression error!\n");
       return 0;
      }
     }
     else
     {    /*对待入栈运算符与栈顶运算符进行优先级比较*/
      if(priority[opera->stack[opera->top-1]][k] == '>')   /*栈顶运算符优先级高,进行计算*/
      {  
       while(1)
       {
          if(opera->stack[opera->top-1] == 5)    /*出现右括号*/
       {
          opera->top--;
          continue;
       }
          else if(opera->stack[opera->top-1] == 4)   /*出现左括号*/
       {
          opera->top--;
          i--;           /*继续下一字符判断*/
             break;
       }  /*计算,数据栈出栈栈顶两个数,运算符栈出栈一个运算符*/
       if(!DataPop(data,&temp1))
       {
        printf("Expression error!\n");
        return 0;
       }
       if(!DataPop(data,&temp2))
       {
        printf("Expression error!\n");
        return 0;
       }
          temp = func[opera->stack[--(opera->top)]](temp2,temp1);
          DataPush(data,temp); /*将计算结果入栈*/
       i--;  /*继续下一字符判断*/
       break;
       }
      }   
      else     /*栈顶运算符优先级低,新运算符入栈*/
       OperaPush(opera,k);
     }
  }
 }
 return 1;
}

你可能感兴趣的:(职场,休闲,表达式计算,C语言表达式计算,C语言计算器)