4常量表达式计算器

来源:互联网 发布:伦敦大学学院专业 知乎 编辑:程序博客网 时间:2024/06/12 01:33


#include<iostream>

#include<cstdlib>

#include<cctype>//字符串判定

 

usingnamespacestd;

constintMAX = 1024;

doubleoperation(char *str);

char *extract(char *str,int &index)

{

   char *pstr(nullptr);//处理字符串

   intnum(0);//记录一下多少对括号

   intbufindex(index);//记录下标

 

   do

   {

       switch (*(str + index))

       {

       case')':

           if (0 ==num)

           {

               ++index;

               pstr =newchar[index - bufindex];

               if (!pstr)

               {

                   throw "malloc fail";

               }

               //拷贝字符串

               strncpy_s(pstr,index -bufindex,str +bufindex,index -bufindex - 1);

               returnpstr;

           }

           else

           {

               num--;

           }

           break;

       case'(':

           num++;

           break;

       }

   } while (*(str + index++) !='\0');

   throw "errorfail";

}

 

//获得计算表达是中字符串的数字

doublegetNum(char *str,int &index)

{

   doublevalue(0.0);

 

   if (*(str + index) =='(')

   {

       char *substr(nullptr);

       substr =extract(str, ++index);

 

       value =operation(substr);

       delete[]substr;

 

       returnvalue;

   }

 

   if (!isdigit(*(str + index)))

   {

       charerror[30] ="geterror";

       throwerror;

   }

 

   //判断数值是否是数值

   while (isdigit(*(str + index)))

   {

       value = 10 *value + (*(str + index++) -'0');

   }

   //带有小数点时不做处理

   if (*(str + index) !='.')

   {

       returnvalue;

   }

   else

   {

       doubledecimals(1.0);//定义一个小数

       while (isdigit(*(str + (++index))))

       {

           decimals /= 10;

           value =value + (*(str + index) -'0') *decimals;

       }

       returnvalue;

   }

}

 

doubleterm(char *str,int &index)

{

   doublevalue(0.0);

   value =getNum(str,index);//获取数据

   while (1)

   {

       if (*(str+index) == '*')

       {

           //乘除法

           value *=getNum(str, ++index);

       }

       elseif (*(str + index) =='/')

       {

           value /=getNum(str, ++index);

       }

       else

       {

           break;

       }

   }

   returnvalue;

}

 

doubleoperation(char *str)

{

   doublevalue(0.0);

   intindex(0);

   value +=term(str,index);

   for (;;)

   {

       switch (*(str + (index++)))

       {

       case'\0':

           returnvalue;

           break;

       case'+':

           value +=term(str,index);

           break;

       case'-':

           value -=term(str,index);

           break;

       default:

           break;

       }

   }

}

 

voidremoveBlankSpace(char *str)

{

   inti(0);

   intj(0);

   //*(str + i) = *(str + j++) :通过这种方式循环获得字符串中的每个字符

   //下面的意思是获得字符不是'\0',也就是说没有到达字符串末尾

   //下面的方式是把不是空格的字符赋值给*(str+i)

   while ((*(str + i) = *(str + j++))!='\0')

   {

       if (*(str+i) != ' ')

       {

           i++;

       }

   }

   //两个下标轮替,往前移动,链表的算法一样,循环向前挖

}

 

voidmain()

{

   charstr[MAX] = { 0 };

   cout <<"请输入表达式";

   cin.getline(str,MAX);

   cout <<"\n" <<str <<endl;

 

   //去除空格

   removeBlankSpace(str);

 

   cout <<str <<endl;

 

   cout <<operation(str) << endl;

 

   cin.get();

}

运行结果:

0 0