简单的计算器 ~~ 呼呼 支持 +-/* () 运算 输入 算式 就得到结果 栈 ~~

#include <iostream>
 #define TRUE 1
 #define FALSE 0
 #define OK 1
 #define ERROR 0
 #define INFEASIBLE -1
using namespace std;
template<class T, int STACK_INIT_SIZE,int STACKINCREMENT>
class SqStack{
private:
T *base;
T *top;
int stacksize;
public:
bool InitStack(){
   base=(T *)malloc(STACK_INIT_SIZE*sizeof(T));
   if(!base)
    return false;
   top=base;
   stacksize=STACK_INIT_SIZE;
   return true;
}


bool DestroyStack(){
   base=NULL;
   return true;
}


bool ClearStack(){
   top=base;
   return true;
}


bool StackEmpty(){
   if(base==top)
    return true;
   else
    return false;
}


int StackLengh(){
   if(!base)
    return -1;
   else
    return top-base;
}


T GetTop(){
T e(0);
   e=*(top-1);
   return e;
}


bool Push(T e){
   if(top-base>=stacksize){
    base=(T *)realloc(base,(stacksize+STACKINCREMENT)*sizeof(T));
    if(!base)
     return false;
    top=base+stacksize;
    stacksize+=STACKINCREMENT;
   }
   *top=e;
   top++;
   return true;
}


T Pop(T &e){
   e=*--top;
   return e;
}
};




//================================loaca function=================================
typedef char SElemType ;
typedef int Status;
char Precede(SElemType t1,SElemType t2)
 { /* 根据教科书表3.1,判断t1,t2两符号的优先关系('#'用'\n'代替) */
   char f;
   switch(t2)
   {
     case '+':
case '-':if(t1=='('||t1=='#')
                f='<'; /* t1<t2 */
              else
                f='>'; /* t1>t2 */
              break;
     case '*':
     case '/':if(t1=='*'||t1=='/'||t1==')')
                f='>'; /* t1>t2 */
              else
                f='<'; /* t1<t2 */
              break;
     case '(':if(t1==')')
              {
                printf("括号不匹配\n");
                exit(ERROR);
              }
              else
                f='<'; /* t1<t2 */
              break;
     case ')':switch(t1)
              {
                case '(':f='='; /* t1=t2 */
                          break;
case'#':printf("缺乏左括号\n");
                          exit(ERROR);
                default :f='>'; /* t1>t2 */
              }
              break;
case'#':switch(t1)
              {
                case'\n':f='='; /* t1=t2 */
                         break;
                case'(' :printf("缺乏右括号\n");
                         exit(ERROR);
                default :f='>'; /* t1>t2 */
              }
   }
   return f;
 }


 Status In(SElemType c)
 { /* 判断c是否为7种运算符之一 */
   switch(c)
   {
     case'+' :
     case'-' :
     case'*' :
     case'/' :
     case'(' :
     case')' :
case'#':return TRUE;
     default :return FALSE;
   }
 }


 double Operate(double a,int theta,double b)
 { /* 做四则运算a theta b,返回运算结果 */
double int_a = a;
double int_b = b;
double ch_res = 0;
   switch(theta)
   {
     case'+':ch_res = (int_a+int_b);break;
     case'-':ch_res = (int_a-int_b);break;
     case'*':ch_res = (int_a*int_b);break;
case'/':ch_res = (int_a/int_b);break;
   }
   return ch_res;
 }
 void my_strcat(char * dest,char  one)
 {
char *ptemp = dest;
while(*ptemp!='\0')
{
ptemp++;
}
*ptemp=one;
++ptemp;
*ptemp = '\0';
 }


 SElemType EvaluateExpression() /* 算法3.4,有改动 */
 { /* 算术表达式求值的算符优先算法。设OPTR和OPND分别为运算符栈和运算数栈 */
char X,thef;
double a,b;
SqStack <char,500,50> OPTR;
OPTR.InitStack();


SqStack <double,500,50> OPND;
OPND.InitStack();


OPTR.Push('#'); /* 将换行符压入运算符栈OPTR的栈底(改) */
char pstore[10];//store the stack element
pstore[0] = '\0';
    char c = getchar();
   cout<<"plsese insert the parameters"<<endl;




   while(c!='#' ||OPTR.GetTop()!='#')//when it is not over
   {
  if(!In(c))
  {
  while(!In(c))//if it is not an opearator 
  {
  my_strcat(pstore,c);
  c = getchar();
  }//when we get a oprand we put it in the Opnd Stack
  double a = atof(pstore);
  OPND.Push(a);
  pstore[0] = '\0';
  }
  else//when it is an oprator 
  switch(Precede(OPTR.GetTop(),c))
  {
  case '<'://when the stack top's advance is lower than the C 
  OPTR.Push(c);
  c = getchar();
  break;
  case '=':
  OPTR.Pop(X);
 c = getchar();
  break;
  case '>':
  OPND.Pop(a);
  OPND.Pop(b);
  OPTR.Pop(thef);
  OPND.Push(Operate(b,thef,a));
  }
   }
   return OPND.GetTop();
 }
 int main()
 {
  printf("%d\n",EvaluateExpression()); 
   system("pause");
   return 1;

 }




程序写出来了  还得写点体会  忙了这么长时间 我要舒缓一下 ~~


1、这个程序主要是在 过滤上出了一点问题 ,整体思路是正确的但是strcat函数一开始用错了 ,干脆自己去写一个算了。

2、这个char[]型的东西,最后的‘\0’一定得加上 要不然有很多时候这个东西会成为死循环 因为系统找不到那个结束符

3、不要害怕模板,其实这是个很简单的东西,只要认真学过类 这个东西只不过是进行一定的传值而已

4、面向对象和面向过程都有指针的传递只不过那个指针的传递方式不一样 仅此而已

5、我找到了一个激励自己的好办法,那就是把一个大的问题进行化简,行程若干个可以进行的小任务,然后自己一步一步的把各个问题一一解决,并进行认真的记录!!

6、数据结构我感觉还差的很远~~~~

7、getchar()函数很强大~~ 第一要输入以后再调用它就开始自己读了 ~~~ 大牛函数 !!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值