标题:一个简单的计算器,除法运算功能不全,例如1024/2不能正确算出,有高手帮帮 ...
取消只看楼主
精英
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-10-12
结帖率:50%
已结贴  问题点数:5 回复次数:1 
一个简单的计算器,除法运算功能不全,例如1024/2不能正确算出,有高手帮帮忙!
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define  STACK_INIT_SIZE  100
#define  STACKINCREMENT   10
typedef struct
 {
         char * base;
         char * top;
         int stacksize;
 }SqStack;
enum  Status
{
    OK,
    ERROR,
};
void main()
{  
   
    Status InitStack(SqStack &S);
    Status GetTop(SqStack S,char &e);
    Status Push(SqStack &S,char e);
    Status Pop(SqStack &S,char &e);
   
    char Precede(char a,char b);
    int Operate(int a,char b,int c);
    Status In(char a);
    int change(char &a);
    SqStack OPTR,OPND;

    printf("\n\n\n\n\n\t***********本程序的功能为:用顺序栈实现整型算术表达式的求值**********\n\n");
    printf("\t运算符说明如下所示  :\n");
    printf("\t\t____________________________________________________________ \t\t\n\n");
    printf("\t\t\t加法\t减法\t乘法\t除法\t乘方\t负号\t\t\n\n");
    printf("\t\t\t'+'\t'-'\t'*'\t'/'\t'^'\t'~'\t\t\n");
    printf("\t\t___________________________________________________________\t\t");
    getchar();
    system("cls");  //切换界面
 
    int e,i=0;
   

    char a,b,c[30],d,f,x,theta;
    InitStack(OPTR);
    Push(OPTR,'#');
    InitStack(OPND);

    printf("\t\t请输入表达式并以'#'结束:");
    gets(c);
    printf("\t\t操作过程如下:\n");
    printf("_______________________________________________________________________________\n\n");
    printf("步骤            OPTR栈          OPND栈          主要操作                       \n");
    printf("_______________________________________________________________________________\n");

    GetTop(OPTR,d);
    while(c[i]!='#'||d!='#')
    {
        if(In(c[i])==ERROR)
        {
            e=change(c[i]);
            while(In(c[++i])==ERROR)/*此处实现的功能为当输入的数字为多位数时*/

            {
                e*=10;
                e+=c[i]-'0';
            }
                Push(OPND,e);
                e=0;
            
        }
   
            
            
        else
        
            
            switch(Precede(d,c[i]))
        {
            case'<':
                Push(OPTR,c[i]);
                i++;
               

                break;
            case'=':
                Pop(OPTR,x);
                i++;
            
                break;
            case'>':
                Pop(OPTR,theta);
                Pop(OPND,b);
                Pop(OPND,a);
                Push(OPND,Operate(a,theta,b));
                break;
        }
     GetTop(OPTR,d);
    }
    GetTop(OPND,f);
    printf("\n*********************************************************\n");
    printf("结果为:%d\n",f);
    printf("\n*********************************************************\n");
    printf("\nInput '' to quit and ENTER run again:");
        
}



Status InitStack(SqStack &S)
{
    S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
    if(!S.base)
        exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}

Status GetTop(SqStack S,char &e)
{
    if(S.top==S.base)
        return ERROR;
    e=*(S.top-1);
    return OK;
}


Status Push(SqStack &S,char e)
{
    if(S.top-S.base==S.stacksize)
    {
        S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
    if(!S.base)
        exit(OVERFLOW);
    S.top=S.base+S.stacksize;
    S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}


Status Pop(SqStack &S,char &e)
{
    if(S.top==S.base)
        return ERROR;
    e=*--S.top;
    return OK;
}



char Precede(char a,char b)
{
    char c,d,e;
    c='>';
    d='=';
    e='<';
    if(((a=='+'||a=='-')&&(b=='+'||b=='-'||b==')'||b=='#'))||((a=='*'||a=='/')&&(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#')))
    return (c);
    if((a=='('&&b==')')||(a=='#'&&b=='#'))
    return (d);
    else
    return (e);
 }


int Operate(int a,char b,int c)
{
   switch(b)
  {
     case'+': return (a+c);
     case'-': return (a-c);
     case'*': return (a*c);
     case'/': if(c==0)
                  printf("分母为零,error");
         else
                  return (a/c);
     default: printf("Operator error!\n");  
             exit(0);
  }

}


Status In(char a)
{
      if(a<'0'||a>'9')
          return OK;
      else
          return ERROR;   
}

int change(char &a)
{
    if(a!='+'||'-'||'*'||'/'||'('||')'||'#')
    return (a-'0');
}
搜索更多相关主题的帖子: 运算 除法 计算器 
2009-12-25 17:29
精英
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-10-12
得分:0 
回复 2楼 liangwenbc
这是我的课程设计题目,当然长了!
2009-12-25 19:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-294306-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.113968 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved