| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 549 人关注过本帖
标题:栈应用的计算器在双层括号中出现减法时出错了!!!!!!!!!
只看楼主 加入收藏
xiaoren123
Rank: 1
来 自:浙江金华
等 级:新手上路
帖 子:22
专家分:0
注 册:2009-4-13
结帖率:100%
收藏
 问题点数:0 回复次数:0 
栈应用的计算器在双层括号中出现减法时出错了!!!!!!!!!
代码如下
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>


using namespace std;
const int maxsize=100;

template <typename Type> class stack
{
public:
 Type data[maxsize];
 int top;
 void initstack(stack *ps);
 void push(stack *ps,Type x);
 Type pop(stack *ps);
 Type gettop(stack *ps);
};

template <typename Type> void stack<Type>::initstack(stack *ps) //初始化栈
{
 ps->top=-1;
}

template <typename Type> void stack<Type>::push(stack *ps,Type x) //进栈
{

  ps->top++;
  ps->data[ps->top]=x;
}


template <typename Type> Type stack<Type>::pop(stack *ps) //出栈  
{
 Type x;

  x=ps->data[ps->top];
  ps->top--;
  return x;
}


template <typename Type> Type stack<Type>::gettop(stack *ps) //取栈顶的元素
{
 return (ps->data[ps->top]);
} //以上是一个模板stack类



int check(char x)
{
 if(x=='+'||x=='-'||x=='*'||x=='/'||x=='('||x==')'||x=='#')
 return 1;
 else
 return 0;   
}

char compare(char x,char y)//判断运算符的优先级
{
  switch(x)   
  {   
   case '+':
   case '-':{
             if(y=='+'||y=='-')
            
               return '>';
            
             else if(y=='*'||y=='/')
                     return '<';
                   else  if(y=='(')
                          return '<';
                         else  if(y==')')
                                  return '>';
                               else
                                   return '>';
            }
            break;
   case '*':
   case '/':{
             if(y=='+'||y=='-')

                return '>';

             else  if(y=='*'||y=='/')
                     return '>';
                   else  if(y=='(')
                             return '<';
                         else  if(y==')')
                                return '>';
                               else
                                  return '>';
            }
            break;
   case '(':{
            if( y==')' )
             return '=';
            else
             return '<';
            }
            break;
   case ')':{
            return '>';
            }
            break;
   case '#':{
            if(y=='#')
               return '=';
            else
               return '<';
            }
            break;
 }
}

int expr(int a,char op,int b)//计算中间表达式
{
 int result;
 
 switch(op)
 {
 case '+': result=a+b;
           break;
 case '-': result=a-b;
           break;
 case '*': result=a*b;
           break;
 case '/': result=a/b;
           break;
 }
 return result;
}

int main()
{
 int a,b,result,temp;
 char op,ch;

 stack<char> oprator;
 stack<int> oprand;
 oprator.initstack(&oprator);
 oprand.initstack(&oprand);//初始化两个栈
 
 oprator.push(&oprator,'#');//压起始符栈
 
 ch=getchar();
 while(ch!='#'||oprator.gettop(&oprator)!='#')//输入表达式
 {
  if(!check(ch))//操作数
  {
   temp=atoi(&ch);
   ch=getchar();
   while(!check(ch))
   {
    temp=temp*10+atoi(&ch);
    ch=getchar();
   }
   oprand.push(&oprand,temp);//讲运算符改成操作数,并压入栈
  }
  else
  {
   switch(compare(oprator.gettop(&oprator),ch))//比较栈顶元素
   {
   case '<':oprator.push(&oprator,ch);
            ch=getchar();
            break;
   case '>':op=oprator.pop(&oprator);
            a=oprand.pop(&oprand);
            b=oprand.pop(&oprand);
            result=expr(a,op,b);
            oprand.push(&oprand,result);
            break;
   case '=':op=oprator.pop(&oprator);
            ch=getchar();
            break;

   }
  }
 }
 
 cout<<oprand.gettop(&oprand)<<endl;

 system("pause");
 return 0;

}

运行时如5*(6-(2+3)*(3-1))# 结果为-80,哪位大虾解决下
搜索更多相关主题的帖子: 括号 减法 计算器 应用 
2010-04-11 16:23
快速回复:栈应用的计算器在双层括号中出现减法时出错了!!!!!!!!!
数据加载中...
 
   



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

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