我的计算器(c语言版)支持多种运算

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
 

typedef double ET;
typedef struct
{
 ET * buffer;
 int top;
 int max;
}stack;
stack * CreateStack(int);
void DestroyStack(stack *);
int push(stack *,ET);
int pop(stack *,ET *);
int GetTop(stack *,ET *);
int IsEmpty(stack *);
int IsFull(stack *);
int TrStack(stack *,int (* f)(ET *));

   /*运算优先级表*/                                                                                                                 
int code[9][9] = {{0,0,1,1,1,1,1,0,0},
                             {0,0,1,1,1,1,1,0,0},
                             {0,0,0,0,1,1,1,0,0},
                             {0,0,0,0,1,1,1,0,0},
                             {0,0,0,0,0,0,1,0,0},
                             {0,0,0,0,0,0,1,0,0},
                             {1,1,1,1,1,1,1,2,-1},
                             {0,0,0,0,0,0,-1,0,0},
                             {1,1,1,1,1,1,1,-1,2}};

main()
{
 char str[30];
 int i,n=0,flag = 0,flaga = 0,flagb = 0,flagc=0;
 double signa,signb;
 char num[30];
 double numa,numb,numc;
 double * pData;
 stack * a;
 stack * b;
 a = CreateStack(30);           /*数据栈*/
 b = CreateStack(30);          /*符号栈*/    
 pData = (double *)malloc(sizeof(double));

 printf("---MY CALCULATOR 0.4---/n");
 gets(str);

 push(b,8);                 /*设置前#号*/
 for(i=0;str[i];i++);
 str[i] = '#';                  /*设置后#号*/
 str[i+1] = '/0';

     i=0;
 while(str[i])
 {
     switch(str[i])
     {
      case '+' : signa = 0;break;     /*取未用符号*/
      case '-'  :
       if(signa == -1)signa=1;           
       if(code[1][signa]==1 || i==0)
         {
        flagb = 1;                        /*负号标志*/
        flag = 1;                          /*数据标志*/
        i++;
       }
       else signa = 1;
       break;
      case '*' : signa = 2;break;
      case '/'  : signa = 3;break;
      case '^' : signa = 4;break;
      case '~' : signa = 5;break;
      case '('  : signa = 6;break;
      case ')'  : signa = 7;break;
      case '#' : signa = 8;break;
      default :
       {
        if(str[i]!='.'&&(str[i]<'0' || str[i]>'9' )){printf("Error");return;}
        if(str[i] == '.')flagc++;
        if(flagc > 1){printf("Error");return;}
        if(flagc==1&&n==0){printf("Error");return;}
        signa = -1;
        num[n] = str[i];
        num[n+1] = '/0';
        n++;
        flag = 1;
        flaga = 1;                        /*判定数字使用与否*/
        i++;
       }
     } 
     if(flag == 0)                                /*分离符号操作与数据操作*/
     {
        if(i != 0 && flaga==1)      /*转浮点数入栈,i!=0 第一个为符号,没有数入栈*/
        {
         numa = atof(num);     /*字符串转换成浮点数*/
         if(flagb == 1)
           {
          numa = -numa;
          flagb = 0;               /*负号判别*/
           }
         push(a,numa);
            n = 0;
         flaga = 0;                   /*数使用判别*/
         flagc = 0;                   /*小数点判别*/
        }
        
        if(!GetTop(b,pData)){printf("Error");return;}
        signb = * pData;                  /*取已用入栈符号*/
        if(code[signb][signa] == 0)  /*两者比较,未用优先级低则运算*/
          {
         pop(b,pData);                  /*旧符号出栈*/

         pop(a,pData);                 /*数据出栈*/
         numa = *pData;
         if(!pop(a,pData)){printf("Error");return;}
         numb = *pData;

         switch(signb)
         {
           case 0  : numc = numb+numa;break;
           case 1  : numc = numb-numa;break;
           case 2  : numc = numb*numa;break;
           case 3  : numc = numb/numa; break;
           case 4  : numc = pow(numb, numa);break;
           case 5  : numc = sqrt(numb);break;
         }
         push(a,numc);                /*运算完毕入栈*/
          }
                                                  
           if(code[signb][signa] == 1)   /*两者比较,未用优先级高则入栈*/
          {
         push(b,signa);
         i++;                                   /*提取下一个字符*/
        }

        if(code[signb][signa] == 2)    /*两者比较,优先级相同则两两抵消*/
        {
         pop(b,pData);
         i++;                                     /*提取下一个字符*/
        }
        if(code[signb][signa] == -1){printf("Error");return;}  /*错误返回*/
     }
                flag = 0;
    }
 if(a->top > 0){printf("Error");return;}
 pop(a,pData);
    printf("answer is:%lf",* pData);   /*输出结果*/
 free(pData);                                /*释放资源*/
 DestroyStack(a);
 DestroyStack(b);
}

stack * CreateStack(int max)
{
  stack * sp;
  sp = (stack *)malloc(sizeof(stack));
  if(!sp)return 0;
  sp->buffer = (ET *)malloc(sizeof(ET)*max);
  if(!sp->buffer)
    {
   free(sp);
   return 0;
  }
  sp->top = -1;
  sp->max = max;
  return sp;
}

void DestroyStack(stack * sp)
{
  free(sp->buffer);
  free(sp);
}


int push(stack * sp,ET data)
{
  if(IsFull(sp))return 0;
  sp->top++;
  sp->buffer[sp->top] = data;
  return 1;
}

int pop(stack * sp,ET * pData)
{
  if(IsEmpty(sp))return 0;
  * pData = sp->buffer[sp->top];
  sp->top--;
  return 1;
}

int GetTop(stack * sp,ET * pData)
{
  if(IsEmpty(sp))return 0;
  * pData = sp->buffer[sp->top];
  return 1;
}

int IsEmpty(stack * sp)
{
  return sp->top == -1;
}

int IsFull(stack * sp)
{
  return sp->top == sp->max-1;
}

int TrStack(stack * sp,int (* f)(ET * pData))
{
  int i;
  for(i = 0;i < sp->top+1;i++)
    {
   if( !f(&sp->buffer[i]) )return i;
  }
  return 0;
}
int show(ET *pData)
{
 printf("%lf ",*pData);
 return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值