ڼС
梦回起点
做你害怕做的事,你会发现:不过如此
本站基于WordPress—主题by 设计窝
冀ICP备15003737号
梦回起点
Copyright © 2015-2018 All rights reserved.

C语言计算器(可以计算带括号的数据哦)

要做的就是输入你要的式子比如(1+2+3)/4*5+6#然后回车就可以啦!另外那个Push和DPush有没有人知道怎么合并到一起呢,功能一样,只是数据类型不一样,麻烦留言告诉我,感激不尽!

#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100  /*栈的大小*/
#define STACKINCREMENT 10  /*增长的大小*/
#define MAXBUFFER 10 /*最大缓冲区*/
#define OK 0
#define ERROR -1
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
typedef char SElemType;
typedef int Status;
typedef struct{
     SElemType *base;
     SElemType *top;
     int stacksize;
}SqStack;
/*初始化一个栈*/
Status InitStack(SqStack *S){ /*构造一个空栈S*/
     S->base=(SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);//这里的S是一个指针 所以应该用->
     if(!S->base){return ERROR;}
     S->top=S->base;
     S->stacksize=STACK_INIT_SIZE;
     return OK;
}
/*获取栈顶元素*/
Status GetTop(SqStack S,SElemType *e){
     if(S.base==S.top){//这里的S是一个结构体所以应该用.
         return(OVERFLOW);
     }
     *e = *(S.top-1);
     return OK;
}
/*销毁栈*/
Status DestoryStack(SqStack *S){
     free(S->base);
     S->top=””;
     S->stacksize=0;
     S->base=””;
}
/*清空栈*/
Status ClearStack(SqStack *S){
     S->top=S->base;
}
/*判断栈是否为空*/
Status StackEmpty(SqStack S){
     if(S.base==S.top){
         return TRUE;
     }
     return FALSE;
}
/*返回一个栈的长度*/
int StackLen(SqStack *S){
     if(!S)return OVERFLOW;
     return S->top-S->base;
}
/*压栈函数*/
Status Push(SqStack *S,SElemType e){
     if(S->top-S->base>=S->stacksize){
         S->base=(SElemType *)realloc(S->base,sizeof(SElemType)*(STACKINCREMENT+S->stacksize));
         if(!S->base)return OVERFLOW;
     }
     *S->top++ = e;
}
/*出栈函数*/
Status Pop(SqStack *S,SElemType *e){
     if(S->top>S->base ){
         *e=*(–S->top);
         return OK;
     }
     return ERROR;
}
/*遍历执行函数visit*/
Status StackTraverse(SqStack *S,Status(*visit)()){
     while(S->top!=S->base){
         visit(S->top–);
     }
}
/*将中缀表达式转化为后缀表达式*/
int Change(char *str1,char *str2){
     SqStack s;
     int j=0,i=0;
     char temp;
     InitStack(&s);
     while(str1[i]!=’#’){
         while(isdigit(str1[i]) || str1[i]==’.’){
             str2[j++]=str1[i++];
             if(‘.’ != str1[i] && !isdigit(str1[i])){
                 str2[j++]=’ ‘;
             }
         }
         if(‘#’ == str1[i]){
             break;
         }else if( ‘)’ == str1[i] ){
             Pop(&s,&temp);
             while( ‘(‘ != temp){
                 str2[j++] = temp;
                 str2[j++]=’ ‘;
                 Pop(&s,&temp);
             }
         }else if(‘+’ == str1[i] || ‘-‘ == str1[i]){
             if(!StackEmpty(s)){
                
                 do{
                     Pop(&s,&temp);
                     if(‘(‘ == temp){
                         Push(&s,temp);
                     }else{
                         str2[j++] = temp;
                         str2[j++]=’ ‘;
                     }
                 }while(!StackEmpty(s) && temp != ‘(‘);
                 Push(&s,str1[i]);

            }else{
                 Push(&s,str1[i]);
             }
         }else if(‘*’ == str1[i] || ‘/’ == str1[i] || ‘(‘ == str1[i]){
             Push(&s,str1[i]);
         }else{
             printf(“您的输入有误!”);
         }
         i++;
     }
     while(!StackEmpty(s)){
         Pop(&s,&temp);
         str2[j++] = temp;
         str2[j++]=’ ‘;
     }
     str2[j++]=’#’;
     str2[j]=’\0′;
}
/*计算结果*/
typedef struct{
     double *base;
     double *top;
     int stacksize;
}DSqStack;
/*初始化一个栈*/
Status DInitStack(DSqStack *S){ /*构造一个空栈S*/
     S->base=(double *)malloc(sizeof(double)*STACK_INIT_SIZE);//这里的S是一个指针 所以应该用->
     if(!S->base){return ERROR;}
     S->top=S->base;
     S->stacksize=STACK_INIT_SIZE;
     return OK;
}
Status DPush(DSqStack *S,double e){
     if(S->top-S->base>=S->stacksize){
         S->base=(double *)realloc(S->base,sizeof(double)*(STACKINCREMENT+S->stacksize));
         if(!S->base)return OVERFLOW;
     }
     *S->top++ = e;
}
/*出栈函数*/
Status DPop(DSqStack *S,double *e){
     if(S->top>S->base ){
         *e=*(–S->top);
         return OK;
     }
     return ERROR;
}
double Calculate(char *str){
     DSqStack b;
     int i=0,j=0;
     double d,e;
     char ch;
     char strbuffer[MAXBUFFER];
     DInitStack(&b);

    while(str[j]!=’#’){
         while(isdigit(str[j]) || str[j] == ‘.’){
             strbuffer[i++]=str[j++];
             strbuffer[i]=’\0′;
             if(i>=9){
                 printf(“溢出”);
                 return OVERFLOW;
             }
             if(str[j] == ‘ ‘){
                 DPush(&b,atof(strbuffer));
                 i=0;
                 break;
             }
         }
         switch(str[j++]){
             case ‘+’:
                 DPop(&b,&d);
                 DPop(&b,&e);
                 DPush(&b,d+e);
                 break;
             case ‘-‘:
                 DPop(&b,&d);
                 DPop(&b,&e);
                 DPush(&b,e-d);
                 break;
             case ‘*’:
                 DPop(&b,&d);
                 DPop(&b,&e);
                 DPush(&b,d*e);
                 break;
             case ‘/’:
                 DPop(&b,&d);
                 DPop(&b,&e);
                 if (d!=0){
                     DPush(&b,e/d);
                     break;
                 }else{
                     printf(“溢出”);
                     system(“pause”);
                     return OVERFLOW;
                 }
         }
     }
     DPop(&b,&d);
     return d;
}
int main(){
     char *str1 = (char *)malloc(sizeof(char)*100);
     char str2[100];
     printf(“请输入正确的表达式:”);
     scanf(“%s”,str1);
     Change(str1,str2);
     str1[strlen(str1)-1]=’=’;
     printf(“%s”,str1);
     printf(“%f”,Calculate(str2));
     system(“pause”);
     return 0;
}

2015-07-24