回 帖 发 新 帖 刷新版面

主题:考试!!!帮帮忙!!急!!(今天用)


[color=008080]用c语言编一个程序,实现四则运算(带括号),回车后即可算出。[/color]
[size=3][color=800000]急!!!!!!!!急!!!!!![/color][/size]

回复列表 (共10个回复)

沙发

前一段时间用C++写的一个东东,差不多

板凳

//函数解释器
// hsbds.cpp: implementation of the Use class.
//////////////////////////////////////////////////////////////////////

#include "hsbds.h"

Use::Use()
{
    data[0]=0;
}

int Use::Create(const char *ch)
{
   int ctop=0,otop=-1,dtop=0;
   while(ch[ctop])
   {
       if((ch[ctop]>='0')&&(ch[ctop]<='9'))
       {
           data[++dtop]=atof(&ch[ctop]);
   opst[++otop]=dtop;                    //用于记录数据存放位置
           char temp=ch[++ctop];                 //从下一位置开始测试
           while( ((temp>='0')&&(temp<='9'))||(temp=='.') )
               temp=ch[++ctop];
   ctop--;
       }                                         //获得常量数据
   else if(ch[ctop]=='(')
   opst[++otop]=M_left;
       else if(ch[ctop]==')')
   opst[++otop]=M_right;
       else if(ch[ctop]=='*')
   opst[++otop]=M_mul;
       else if(ch[ctop]=='/')
   opst[++otop]=M_div;
       else if(ch[ctop]=='^')
   opst[++otop]=M_pow;
       else if(ch[ctop]=='+')
       {
           if(opst[otop]==M_left)
   opst[++otop]=0;                   //用于区分是否在括号优先级内
           opst[++otop]=M_add;
       }
       else if(ch[ctop]=='-')
       {
           if(opst[otop]==M_left) opst[++otop]=0; //用于区分是否在括号优先级内
           opst[++otop]=M_sub;
       }
       else if(ch[ctop]=='x'||ch[ctop]=='X')
   opst[++otop]=M_x;                      //记录变量
       else if((ch[ctop]=='s'||ch[ctop]=='S')
   &&(ch[ctop+1]=='i'||ch[ctop]=='I')
   &&(ch[ctop+2]=='n'||ch[ctop]=='N'))
       {
           ctop+=2;
           opst[++otop]=M_sin;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='s'||ch[ctop]=='S'))
       {
           ctop+=2;
           opst[++otop]=M_cos;
       }
       else if((ch[ctop]=='e'||ch[ctop]=='E')
   &&(ch[ctop+1]=='x'||ch[ctop]=='X')
   &&(ch[ctop+2]=='p'||ch[ctop]=='P'))
       {
           ctop+=2;
           opst[++otop]=M_exp;
       }
       else if((ch[ctop]=='l'||ch[ctop]=='L')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_log;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='t'||ch[ctop]=='T')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_ctg;
       }
       else if((ch[ctop]=='t'||ch[ctop]=='T')
   &&(ch[ctop+1]=='g'||ch[ctop]=='G'))
       {
           ctop++;
           opst[++otop]=M_tg;
       }

       ctop++;
   }
   opst[++otop]=M_flag;//结束标记

   return 1;
}

void Use::infixToPostfix()
{
   int post[Max]={0};
   int temp[Max]={0};
   int ptop=0,otop=0,ttop=0;
   int op=-1;
   temp[ttop++]=M_flag;
   while(opst[otop]!=M_flag)
   {
      if(opst[otop]<1000)
          post[ptop++]=opst[otop];
      else
      {
          if(opst[otop]==M_right)
            for(op=temp[--ttop];op!=M_left&&ttop>0;op=temp[--ttop])
post[ptop++]=op;
          else
          {
            for(op=temp[--ttop];(isp(op)>icp(opst[otop]))&&(ttop>0);op=temp[--ttop])
post[ptop++]=op;
temp[ttop++]=op;
temp[ttop++]=opst[otop];
          }
      }
      otop++;
   }
   while(ttop>0)
      post[ptop++]=temp[--ttop];
   for(op=0;op<ptop;op++)
      opst[op]=post[op];
}

double Use::Doen(double x)
{
    int otop=0,stop=0;
int op;
double shuju[20];
while((op=opst[otop])!=M_flag)
{
switch(op)
{
    case M_add:
        shuju[--stop]+=shuju[stop+1];
        break;
    case M_sub:
        shuju[--stop]-=shuju[stop+1];
        break;
    case M_mul:
        shuju[--stop]*=shuju[stop+1];
        break;
    case M_div:
        shuju[--stop]/=shuju[stop+1];
        break;
        case M_sin:
            shuju[stop]=sin(shuju[stop]);
            break;
        case M_cos:
            shuju[stop]=cos(shuju[stop]);
            break;
        case M_tg:
            shuju[stop]=sin(shuju[stop])/cos(shuju[stop]);
            break;
        case M_ctg:
            shuju[stop]=cos(shuju[stop])/sin(shuju[stop]);
            break;
        case M_exp:
            shuju[stop]=exp(shuju[stop]);
            break;
        case M_pow:
            shuju[stop]=pow(shuju[stop],shuju[stop+1]);
            break;
        case M_log:
            shuju[stop]=log(shuju[stop]);
            break;
case M_x:
shuju[++stop]=x;
break;
default:
    shuju[++stop]=data[op];
            break;
}
otop++;
}
return shuju[stop];
}


double Use::Run(double x,const char* ch)
{
  Create(ch);
  infixToPostfix();
  return Doen(x);
}


int Use::icp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 2;
   case M_mul:
   case M_div:
        return 4;
   case M_left:
        return 8;
   case M_right:
        return 1;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 6;
   case M_flag:
        return -1;
   }
   return 0;
}

int Use::isp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 3;
   case M_mul:
   case M_div:
        return 5;
   case M_left:
        return 1;
   case M_right:
        return 8;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 7;
   case M_flag:
        return -1;
   }
   return 0;
}

3 楼

本来是用来计算如f(x)的,你把x去掉就成了计算常量的了,:)

4 楼

哇,很长啊!

5 楼

感谢!!!把x去掉是什么意思呢?有点太长了,老师要求30行以内,用递归。

6 楼

//函数解释器
// hsbds.cpp: implementation of the Use class.
//////////////////////////////////////////////////////////////////////

#include "hsbds.h"

Use::Use()
{
    data[0]=0;
}

int Use::Create(const char *ch)
{
   int ctop=0,otop=-1,dtop=0;
   while(ch[ctop])
   {
       if((ch[ctop]>='0')&&(ch[ctop]<='9'))
       {
           data[++dtop]=atof(&ch[ctop]);
   opst[++otop]=dtop;                    //用于记录数据存放位置
           char temp=ch[++ctop];                 //从下一位置开始测试
           while( ((temp>='0')&&(temp<='9'))||(temp=='.') )
               temp=ch[++ctop];
   ctop--;
       }                                         //获得常量数据
   else if(ch[ctop]=='(')
   opst[++otop]=M_left;
       else if(ch[ctop]==')')
   opst[++otop]=M_right;
       else if(ch[ctop]=='*')
   opst[++otop]=M_mul;
       else if(ch[ctop]=='/')
   opst[++otop]=M_div;
       else if(ch[ctop]=='^')
   opst[++otop]=M_pow;
       else if(ch[ctop]=='+')
       {
           if(opst[otop]==M_left)
   opst[++otop]=0;                   //用于区分是否在括号优先级内
           opst[++otop]=M_add;
       }
       else if(ch[ctop]=='-')
       {
           if(opst[otop]==M_left) opst[++otop]=0; //用于区分是否在括号优先级内
           opst[++otop]=M_sub;
       }
       else if(ch[ctop]=='x'||ch[ctop]=='X')
   opst[++otop]=M_x;                      //记录变量
       else if((ch[ctop]=='s'||ch[ctop]=='S')
   &&(ch[ctop+1]=='i'||ch[ctop]=='I')
   &&(ch[ctop+2]=='n'||ch[ctop]=='N'))
       {
           ctop+=2;
           opst[++otop]=M_sin;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='s'||ch[ctop]=='S'))
       {
           ctop+=2;
           opst[++otop]=M_cos;
       }
       else if((ch[ctop]=='e'||ch[ctop]=='E')
   &&(ch[ctop+1]=='x'||ch[ctop]=='X')
   &&(ch[ctop+2]=='p'||ch[ctop]=='P'))
       {
           ctop+=2;
           opst[++otop]=M_exp;
       }
       else if((ch[ctop]=='l'||ch[ctop]=='L')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_log;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='t'||ch[ctop]=='T')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_ctg;
       }
       else if((ch[ctop]=='t'||ch[ctop]=='T')
   &&(ch[ctop+1]=='g'||ch[ctop]=='G'))
       {
           ctop++;
           opst[++otop]=M_tg;
       }

       ctop++;
   }
   opst[++otop]=M_flag;//结束标记

   return 1;
}

void Use::infixToPostfix()
{
   int post[Max]={0};
   int temp[Max]={0};
   int ptop=0,otop=0,ttop=0;
   int op=-1;
   temp[ttop++]=M_flag;
   while(opst[otop]!=M_flag)
   {
      if(opst[otop]<1000)
          post[ptop++]=opst[otop];
      else
      {
          if(opst[otop]==M_right)
            for(op=temp[--ttop];op!=M_left&&ttop>0;op=temp[--ttop])
post[ptop++]=op;
          else
          {
            for(op=temp[--ttop];(isp(op)>icp(opst[otop]))&&(ttop>0);op=temp[--ttop])
post[ptop++]=op;
temp[ttop++]=op;
temp[ttop++]=opst[otop];
          }
      }
      otop++;
   }
   while(ttop>0)
      post[ptop++]=temp[--ttop];
   for(op=0;op<ptop;op++)
      opst[op]=post[op];
}

double Use::Doen(double x)
{
    int otop=0,stop=0;
int op;
double shuju[20];
while((op=opst[otop])!=M_flag)
{
switch(op)
{
    case M_add:
        shuju[--stop]+=shuju[stop+1];
        break;
    case M_sub:
        shuju[--stop]-=shuju[stop+1];
        break;
    case M_mul:
        shuju[--stop]*=shuju[stop+1];
        break;
    case M_div:
        shuju[--stop]/=shuju[stop+1];
        break;
        case M_sin:
            shuju[stop]=sin(shuju[stop]);
            break;
        case M_cos:
            shuju[stop]=cos(shuju[stop]);
            break;
        case M_tg:
            shuju[stop]=sin(shuju[stop])/cos(shuju[stop]);
            break;
        case M_ctg:
            shuju[stop]=cos(shuju[stop])/sin(shuju[stop]);
            break;
        case M_exp:
            shuju[stop]=exp(shuju[stop]);
            break;
        case M_pow:
            shuju[stop]=pow(shuju[stop],shuju[stop+1]);
            break;
        case M_log:
            shuju[stop]=log(shuju[stop]);
            break;
case M_x:
shuju[++stop]=x;
break;
default:
    shuju[++stop]=data[op];
            break;
}
otop++;
}
return shuju[stop];
}


double Use::Run(double x,const char* ch)
{
  Create(ch);
  infixToPostfix();
  return Doen(x);
}


int Use::icp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 2;
   case M_mul:
   case M_div:
        return 4;
   case M_left:
        return 8;
   case M_right:
        return 1;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 6;
   case M_flag:
        return -1;
   }
   return 0;
}

int Use::isp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 3;
   case M_mul:
   case M_div:
        return 5;
   case M_left:
        return 1;
   case M_right:
        return 8;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 7;
   case M_flag:
        return -1;
   }
   return 0;
}

7 楼

//函数解释器
// hsbds.cpp: implementation of the Use class.
//////////////////////////////////////////////////////////////////////

#include "hsbds.h"

Use::Use()
{
    data[0]=0;
}

int Use::Create(const char *ch)
{
   int ctop=0,otop=-1,dtop=0;
   while(ch[ctop])
   {
       if((ch[ctop]>='0')&&(ch[ctop]<='9'))
       {
           data[++dtop]=atof(&ch[ctop]);
   opst[++otop]=dtop;                    //用于记录数据存放位置
           char temp=ch[++ctop];                 //从下一位置开始测试
           while( ((temp>='0')&&(temp<='9'))||(temp=='.') )
               temp=ch[++ctop];
   ctop--;
       }                                         //获得常量数据
   else if(ch[ctop]=='(')
   opst[++otop]=M_left;
       else if(ch[ctop]==')')
   opst[++otop]=M_right;
       else if(ch[ctop]=='*')
   opst[++otop]=M_mul;
       else if(ch[ctop]=='/')
   opst[++otop]=M_div;
       else if(ch[ctop]=='^')
   opst[++otop]=M_pow;
       else if(ch[ctop]=='+')
       {
           if(opst[otop]==M_left)
   opst[++otop]=0;                   //用于区分是否在括号优先级内
           opst[++otop]=M_add;
       }
       else if(ch[ctop]=='-')
       {
           if(opst[otop]==M_left) opst[++otop]=0; //用于区分是否在括号优先级内
           opst[++otop]=M_sub;
       }
       else if(ch[ctop]=='x'||ch[ctop]=='X')
   opst[++otop]=M_x;                      //记录变量
       else if((ch[ctop]=='s'||ch[ctop]=='S')
   &&(ch[ctop+1]=='i'||ch[ctop]=='I')
   &&(ch[ctop+2]=='n'||ch[ctop]=='N'))
       {
           ctop+=2;
           opst[++otop]=M_sin;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='s'||ch[ctop]=='S'))
       {
           ctop+=2;
           opst[++otop]=M_cos;
       }
       else if((ch[ctop]=='e'||ch[ctop]=='E')
   &&(ch[ctop+1]=='x'||ch[ctop]=='X')
   &&(ch[ctop+2]=='p'||ch[ctop]=='P'))
       {
           ctop+=2;
           opst[++otop]=M_exp;
       }
       else if((ch[ctop]=='l'||ch[ctop]=='L')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_log;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='t'||ch[ctop]=='T')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_ctg;
       }
       else if((ch[ctop]=='t'||ch[ctop]=='T')
   &&(ch[ctop+1]=='g'||ch[ctop]=='G'))
       {
           ctop++;
           opst[++otop]=M_tg;
       }

       ctop++;
   }
   opst[++otop]=M_flag;//结束标记

   return 1;
}

void Use::infixToPostfix()
{
   int post[Max]={0};
   int temp[Max]={0};
   int ptop=0,otop=0,ttop=0;
   int op=-1;
   temp[ttop++]=M_flag;
   while(opst[otop]!=M_flag)
   {
      if(opst[otop]<1000)
          post[ptop++]=opst[otop];
      else
      {
          if(opst[otop]==M_right)
            for(op=temp[--ttop];op!=M_left&&ttop>0;op=temp[--ttop])
post[ptop++]=op;
          else
          {
            for(op=temp[--ttop];(isp(op)>icp(opst[otop]))&&(ttop>0);op=temp[--ttop])
post[ptop++]=op;
temp[ttop++]=op;
temp[ttop++]=opst[otop];
          }
      }
      otop++;
   }
   while(ttop>0)
      post[ptop++]=temp[--ttop];
   for(op=0;op<ptop;op++)
      opst[op]=post[op];
}

double Use::Doen(double x)
{
    int otop=0,stop=0;
int op;
double shuju[20];
while((op=opst[otop])!=M_flag)
{
switch(op)
{
    case M_add:
        shuju[--stop]+=shuju[stop+1];
        break;
    case M_sub:
        shuju[--stop]-=shuju[stop+1];
        break;
    case M_mul:
        shuju[--stop]*=shuju[stop+1];
        break;
    case M_div:
        shuju[--stop]/=shuju[stop+1];
        break;
        case M_sin:
            shuju[stop]=sin(shuju[stop]);
            break;
        case M_cos:
            shuju[stop]=cos(shuju[stop]);
            break;
        case M_tg:
            shuju[stop]=sin(shuju[stop])/cos(shuju[stop]);
            break;
        case M_ctg:
            shuju[stop]=cos(shuju[stop])/sin(shuju[stop]);
            break;
        case M_exp:
            shuju[stop]=exp(shuju[stop]);
            break;
        case M_pow:
            shuju[stop]=pow(shuju[stop],shuju[stop+1]);
            break;
        case M_log:
            shuju[stop]=log(shuju[stop]);
            break;
case M_x:
shuju[++stop]=x;
break;
default:
    shuju[++stop]=data[op];
            break;
}
otop++;
}
return shuju[stop];
}


double Use::Run(double x,const char* ch)
{
  Create(ch);
  infixToPostfix();
  return Doen(x);
}


int Use::icp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 2;
   case M_mul:
   case M_div:
        return 4;
   case M_left:
        return 8;
   case M_right:
        return 1;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 6;
   case M_flag:
        return -1;
   }
   return 0;
}

int Use::isp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 3;
   case M_mul:
   case M_div:
        return 5;
   case M_left:
        return 1;
   case M_right:
        return 8;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 7;
   case M_flag:
        return -1;
   }
   return 0;
}

8 楼

//函数解释器
// hsbds.cpp: implementation of the Use class.
//////////////////////////////////////////////////////////////////////

#include "hsbds.h"

Use::Use()
{
    data[0]=0;
}

int Use::Create(const char *ch)
{
   int ctop=0,otop=-1,dtop=0;
   while(ch[ctop])
   {
       if((ch[ctop]>='0')&&(ch[ctop]<='9'))
       {
           data[++dtop]=atof(&ch[ctop]);
   opst[++otop]=dtop;                    //用于记录数据存放位置
           char temp=ch[++ctop];                 //从下一位置开始测试
           while( ((temp>='0')&&(temp<='9'))||(temp=='.') )
               temp=ch[++ctop];
   ctop--;
       }                                         //获得常量数据
   else if(ch[ctop]=='(')
   opst[++otop]=M_left;
       else if(ch[ctop]==')')
   opst[++otop]=M_right;
       else if(ch[ctop]=='*')
   opst[++otop]=M_mul;
       else if(ch[ctop]=='/')
   opst[++otop]=M_div;
       else if(ch[ctop]=='^')
   opst[++otop]=M_pow;
       else if(ch[ctop]=='+')
       {
           if(opst[otop]==M_left)
   opst[++otop]=0;                   //用于区分是否在括号优先级内
           opst[++otop]=M_add;
       }
       else if(ch[ctop]=='-')
       {
           if(opst[otop]==M_left) opst[++otop]=0; //用于区分是否在括号优先级内
           opst[++otop]=M_sub;
       }
       else if(ch[ctop]=='x'||ch[ctop]=='X')
   opst[++otop]=M_x;                      //记录变量
       else if((ch[ctop]=='s'||ch[ctop]=='S')
   &&(ch[ctop+1]=='i'||ch[ctop]=='I')
   &&(ch[ctop+2]=='n'||ch[ctop]=='N'))
       {
           ctop+=2;
           opst[++otop]=M_sin;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='s'||ch[ctop]=='S'))
       {
           ctop+=2;
           opst[++otop]=M_cos;
       }
       else if((ch[ctop]=='e'||ch[ctop]=='E')
   &&(ch[ctop+1]=='x'||ch[ctop]=='X')
   &&(ch[ctop+2]=='p'||ch[ctop]=='P'))
       {
           ctop+=2;
           opst[++otop]=M_exp;
       }
       else if((ch[ctop]=='l'||ch[ctop]=='L')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_log;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='t'||ch[ctop]=='T')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_ctg;
       }
       else if((ch[ctop]=='t'||ch[ctop]=='T')
   &&(ch[ctop+1]=='g'||ch[ctop]=='G'))
       {
           ctop++;
           opst[++otop]=M_tg;
       }

       ctop++;
   }
   opst[++otop]=M_flag;//结束标记

   return 1;
}

void Use::infixToPostfix()
{
   int post[Max]={0};
   int temp[Max]={0};
   int ptop=0,otop=0,ttop=0;
   int op=-1;
   temp[ttop++]=M_flag;
   while(opst[otop]!=M_flag)
   {
      if(opst[otop]<1000)
          post[ptop++]=opst[otop];
      else
      {
          if(opst[otop]==M_right)
            for(op=temp[--ttop];op!=M_left&&ttop>0;op=temp[--ttop])
post[ptop++]=op;
          else
          {
            for(op=temp[--ttop];(isp(op)>icp(opst[otop]))&&(ttop>0);op=temp[--ttop])
post[ptop++]=op;
temp[ttop++]=op;
temp[ttop++]=opst[otop];
          }
      }
      otop++;
   }
   while(ttop>0)
      post[ptop++]=temp[--ttop];
   for(op=0;op<ptop;op++)
      opst[op]=post[op];
}

double Use::Doen(double x)
{
    int otop=0,stop=0;
int op;
double shuju[20];
while((op=opst[otop])!=M_flag)
{
switch(op)
{
    case M_add:
        shuju[--stop]+=shuju[stop+1];
        break;
    case M_sub:
        shuju[--stop]-=shuju[stop+1];
        break;
    case M_mul:
        shuju[--stop]*=shuju[stop+1];
        break;
    case M_div:
        shuju[--stop]/=shuju[stop+1];
        break;
        case M_sin:
            shuju[stop]=sin(shuju[stop]);
            break;
        case M_cos:
            shuju[stop]=cos(shuju[stop]);
            break;
        case M_tg:
            shuju[stop]=sin(shuju[stop])/cos(shuju[stop]);
            break;
        case M_ctg:
            shuju[stop]=cos(shuju[stop])/sin(shuju[stop]);
            break;
        case M_exp:
            shuju[stop]=exp(shuju[stop]);
            break;
        case M_pow:
            shuju[stop]=pow(shuju[stop],shuju[stop+1]);
            break;
        case M_log:
            shuju[stop]=log(shuju[stop]);
            break;
case M_x:
shuju[++stop]=x;
break;
default:
    shuju[++stop]=data[op];
            break;
}
otop++;
}
return shuju[stop];
}


double Use::Run(double x,const char* ch)
{
  Create(ch);
  infixToPostfix();
  return Doen(x);
}


int Use::icp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 2;
   case M_mul:
   case M_div:
        return 4;
   case M_left:
        return 8;
   case M_right:
        return 1;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 6;
   case M_flag:
        return -1;
   }
   return 0;
}

int Use::isp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 3;
   case M_mul:
   case M_div:
        return 5;
   case M_left:
        return 1;
   case M_right:
        return 8;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 7;
   case M_flag:
        return -1;
   }
   return 0;
}

9 楼

//函数解释器
// hsbds.cpp: implementation of the Use class.
//////////////////////////////////////////////////////////////////////

#include "hsbds.h"

Use::Use()
{
    data[0]=0;
}

int Use::Create(const char *ch)
{
   int ctop=0,otop=-1,dtop=0;
   while(ch[ctop])
   {
       if((ch[ctop]>='0')&&(ch[ctop]<='9'))
       {
           data[++dtop]=atof(&ch[ctop]);
   opst[++otop]=dtop;                    //用于记录数据存放位置
           char temp=ch[++ctop];                 //从下一位置开始测试
           while( ((temp>='0')&&(temp<='9'))||(temp=='.') )
               temp=ch[++ctop];
   ctop--;
       }                                         //获得常量数据
   else if(ch[ctop]=='(')
   opst[++otop]=M_left;
       else if(ch[ctop]==')')
   opst[++otop]=M_right;
       else if(ch[ctop]=='*')
   opst[++otop]=M_mul;
       else if(ch[ctop]=='/')
   opst[++otop]=M_div;
       else if(ch[ctop]=='^')
   opst[++otop]=M_pow;
       else if(ch[ctop]=='+')
       {
           if(opst[otop]==M_left)
   opst[++otop]=0;                   //用于区分是否在括号优先级内
           opst[++otop]=M_add;
       }
       else if(ch[ctop]=='-')
       {
           if(opst[otop]==M_left) opst[++otop]=0; //用于区分是否在括号优先级内
           opst[++otop]=M_sub;
       }
       else if(ch[ctop]=='x'||ch[ctop]=='X')
   opst[++otop]=M_x;                      //记录变量
       else if((ch[ctop]=='s'||ch[ctop]=='S')
   &&(ch[ctop+1]=='i'||ch[ctop]=='I')
   &&(ch[ctop+2]=='n'||ch[ctop]=='N'))
       {
           ctop+=2;
           opst[++otop]=M_sin;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='s'||ch[ctop]=='S'))
       {
           ctop+=2;
           opst[++otop]=M_cos;
       }
       else if((ch[ctop]=='e'||ch[ctop]=='E')
   &&(ch[ctop+1]=='x'||ch[ctop]=='X')
   &&(ch[ctop+2]=='p'||ch[ctop]=='P'))
       {
           ctop+=2;
           opst[++otop]=M_exp;
       }
       else if((ch[ctop]=='l'||ch[ctop]=='L')
   &&(ch[ctop+1]=='o'||ch[ctop]=='O')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_log;
       }
       else if((ch[ctop]=='c'||ch[ctop]=='C')
   &&(ch[ctop+1]=='t'||ch[ctop]=='T')
   &&(ch[ctop+2]=='g'||ch[ctop]=='G'))
       {
           ctop+=2;
           opst[++otop]=M_ctg;
       }
       else if((ch[ctop]=='t'||ch[ctop]=='T')
   &&(ch[ctop+1]=='g'||ch[ctop]=='G'))
       {
           ctop++;
           opst[++otop]=M_tg;
       }

       ctop++;
   }
   opst[++otop]=M_flag;//结束标记

   return 1;
}

void Use::infixToPostfix()
{
   int post[Max]={0};
   int temp[Max]={0};
   int ptop=0,otop=0,ttop=0;
   int op=-1;
   temp[ttop++]=M_flag;
   while(opst[otop]!=M_flag)
   {
      if(opst[otop]<1000)
          post[ptop++]=opst[otop];
      else
      {
          if(opst[otop]==M_right)
            for(op=temp[--ttop];op!=M_left&&ttop>0;op=temp[--ttop])
post[ptop++]=op;
          else
          {
            for(op=temp[--ttop];(isp(op)>icp(opst[otop]))&&(ttop>0);op=temp[--ttop])
post[ptop++]=op;
temp[ttop++]=op;
temp[ttop++]=opst[otop];
          }
      }
      otop++;
   }
   while(ttop>0)
      post[ptop++]=temp[--ttop];
   for(op=0;op<ptop;op++)
      opst[op]=post[op];
}

double Use::Doen(double x)
{
    int otop=0,stop=0;
int op;
double shuju[20];
while((op=opst[otop])!=M_flag)
{
switch(op)
{
    case M_add:
        shuju[--stop]+=shuju[stop+1];
        break;
    case M_sub:
        shuju[--stop]-=shuju[stop+1];
        break;
    case M_mul:
        shuju[--stop]*=shuju[stop+1];
        break;
    case M_div:
        shuju[--stop]/=shuju[stop+1];
        break;
        case M_sin:
            shuju[stop]=sin(shuju[stop]);
            break;
        case M_cos:
            shuju[stop]=cos(shuju[stop]);
            break;
        case M_tg:
            shuju[stop]=sin(shuju[stop])/cos(shuju[stop]);
            break;
        case M_ctg:
            shuju[stop]=cos(shuju[stop])/sin(shuju[stop]);
            break;
        case M_exp:
            shuju[stop]=exp(shuju[stop]);
            break;
        case M_pow:
            shuju[stop]=pow(shuju[stop],shuju[stop+1]);
            break;
        case M_log:
            shuju[stop]=log(shuju[stop]);
            break;
case M_x:
shuju[++stop]=x;
break;
default:
    shuju[++stop]=data[op];
            break;
}
otop++;
}
return shuju[stop];
}


double Use::Run(double x,const char* ch)
{
  Create(ch);
  infixToPostfix();
  return Doen(x);
}


int Use::icp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 2;
   case M_mul:
   case M_div:
        return 4;
   case M_left:
        return 8;
   case M_right:
        return 1;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 6;
   case M_flag:
        return -1;
   }
   return 0;
}

int Use::isp(int op)
{
   switch(op)
   {
   case M_add:
   case M_sub:
        return 3;
   case M_mul:
   case M_div:
        return 5;
   case M_left:
        return 1;
   case M_right:
        return 8;
   case M_sin:
   case M_cos:
   case M_tg:
   case M_ctg:
   case M_exp:
   case M_log:
   case M_pow:
        return 7;
   case M_flag:
        return -1;
   }
   return 0;
}

10 楼

就没有简短点的?

我来回复

您尚未登录,请登录后再回复。点此登录或注册