| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 771 人关注过本帖
标题:中缀表达式的算法告诉我!
只看楼主 加入收藏
风过无痕2008
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-28
收藏
 问题点数:0 回复次数:4 
中缀表达式的算法告诉我!

搜索更多相关主题的帖子: 算法 表达 
2006-04-28 15:00
激情依旧
Rank: 1
等 级:新手上路
威 望:2
帖 子:524
专家分:0
注 册:2005-4-4
收藏
得分:0 
以前发了很多.....我去找个给你

生是编程人!!!!死是编程鬼!!!!颠峰人生!!!焚尽编程!!! 爱已严重死机!情必须重新启动!情人已和服务器断开连接!网恋也需要重新拨号!-----激情依旧
2006-04-29 10:48
激情依旧
Rank: 1
等 级:新手上路
威 望:2
帖 子:524
专家分:0
注 册:2005-4-4
收藏
得分:0 

#include "stack.h"
#include <string>
#include <vector>
using namespace std;

void RPN(string,char *);
int procecn( char);
void Count( char *);

int main()
{
string exp;
char answerexp[255];
cout<< "NOTE: Enter # for infix expression to stop.\n";
for(;;)
{
cout<< "\nInfix Expression? ";
getline(cin,exp,'\n');
if( exp == "#") break;
RPN(exp,answerexp);
cout<< "RPN Expression is " << answerexp <<endl;
Count(answerexp);

}
}

int procecn(vector<char> ch)
{

switch(ch[0])
{
case '+':case '-':
return 2;
case '*':case '/':
return 3;
case '<':case '>':
if(*(ch.end()-1) == '<'||*(ch.end()-1) == '>')
return 1;
default:
return 0;
}
}


void RPN(string exp,char * answerexp)
{

Stack<char,128> s;
int j=0,i=0;
vector<char> ch,temp;
s.push('@');

while(i< exp.length())
{
ch.push_back(exp[i]);
if( ch[0] == ' ')
i++;
else if ( ch[0] == '(')
{
s.push( ch[0] );
i++;
}
else if ( ch[0] == ')')
{
while( s.gettop() != '(')
{
answerexp[j++] = s.gettop();
s.pop();
}
s.pop();
i++;
}
else if( ch[0] == '*'|| ch[0] =='/' ||
ch[0] == '-' || ch[0] == '+'|| ch[0] == '<'|| ch[0] == '>')
{
if(exp[i+1] == '<')
{
i++;
ch.push_back('<');
}
else if(exp[i+1] == '>')
{
i++;
ch.push_back('>');
}
temp.push_back(s.gettop());
if( procecn(temp) >= procecn( ch) )
{

if(s.gettop() == '<')
{
while(s.gettop() == '<')
{
answerexp[j++] = s.gettop();
s.pop();
}
}
else if( s.gettop() =='>')
{
while(s.gettop() == '>')
{
answerexp[j++] = s.gettop();
s.pop();
}
}

else{
answerexp[ j++ ] = s.gettop();
s.pop();
}
}
temp.clear();
vector<char>::iterator it = ch.begin();
for(; it != ch.end(); ++it)
s.push(*it);

i++;
}
else{
while( exp[i] >= '0' && exp[i] <= '9')
{
answerexp[j++] = exp[i];
i++;
}
}
answerexp[j++] = ' ';
ch.clear();
}

while(s.gettop()!= '@')
{
if( s.gettop() == '(')
{
cout << " Error in infix expression \n";
exit(-1);
}
else{
answerexp[j++] = s.gettop();
s.pop();
}
}
answerexp[j] = '\0';

s.pop();
}

void Count(char *answerexp)
{

int sum = 0,num1,num2,i = 0;
char ch;
Stack<int,128> s;
while ( answerexp[i] != '\0')
{
ch = answerexp[i];
switch(ch)
{
case '+':
num1 = s.gettop();
s.pop();
num2 = s.gettop();
s.pop();
s.push( num1+num2);
i++;
break;
case '-':
num1 = s.gettop();
s.pop();
num2 = s.gettop();
s.pop();
s.push( num2-num1);
i++;
break;
case '*':
num1 = s.gettop();
s.pop();
num2 = s.gettop();
s.pop();
s.push( num1*num2);
i++;
break;
case '/':
num1 = s.gettop();
s.pop();
num2 = s.gettop();
s.pop();
s.push( num2/num1);
i++;
break;
case '<':
if( answerexp[i+1] == '<')
{
i++;
num1 = s.gettop();
s.pop();
num2 = s.gettop();
s.pop();
s.push( (num2<<num1));
}
i++;
break;
case '>':
if( answerexp[i+1] =='>')
{
i++;
num1 = s.gettop();
s.pop();
num2 = s.gettop();
s.pop();
s.push( (num2>>num1) );
}
i++;
break;
case ' ': i++; break;
default:
while(ch >= '0' && ch <= '9')
{
sum = sum*10+(ch-'0');
i++;
ch = answerexp[i];
}
s.push(sum);
sum = 0;
break;
}
}
cout<<"the result is "<<s.gettop()<<endl;
s.pop();
}


//stack.h
#include "exception.h"

#ifndef STACK
#define STACK


template<typename T,int stack_capacity>
class Stack{
public:
Stack():mytop(-1){ myarray = new T[stack_capacity];}
~Stack(){ delete []myarray;}
bool empty(){ return mytop == -1;}
bool full() { return mytop == stack_capacity-1;}
void push(const T&);
void pop();
T gettop();
void display(ostream &);
private:
T *myarray;
int mytop;
};

template<typename T,int stack_capacity>
void Stack<T,stack_capacity>::push(const T &value){
if( full())
throw pushOnFull<T>(value);
else
myarray[++mytop] = value;
}

template<typename T,int stack_capacity>
void Stack<T,stack_capacity>::pop(){
if( empty())
throw popOnEmpty<T>();
else
mytop--;
}

template<typename T,int stack_capacity>
T Stack<T,stack_capacity>::gettop(){
if(empty())
throw popOnEmpty<T>();
else
return myarray[mytop];
}

template<typename T,int stack_capacity>
void Stack<T,stack_capacity>::display( ostream &os)
{
for( int i=0;i<= mytop;i++)
os<< myarray[i] <<" ";
}
#endif

//exception.h
#include <iostream>
using namespace std;

template<typename T>class Excp{
public:
virtual void print(){
cout<< "An exception has occurred"<<endl;
}

};

template<typename T>class stackExcp:public Excp<T>{
};

template<typename T>class pushOnFull:public stackExcp<T>{
public:
pushOnFull(T i):_value(i){}
int value(){ return _value;}

virtual void print()
{
cerr <<"trying to push the value "<<_value<<" on a full stack\n";
}
private:
T _value;
};

template<typename T>class popOnEmpty:public stackExcp<T>{
public:
virtual void print()
{
cerr<<"trying to pop"<<endl;
}
};


生是编程人!!!!死是编程鬼!!!!颠峰人生!!!焚尽编程!!! 爱已严重死机!情必须重新启动!情人已和服务器断开连接!网恋也需要重新拨号!-----激情依旧
2006-04-29 10:53
ljwty1985228
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-5-6
收藏
得分:0 

这么长.......................

2006-05-12 16:31
风过无痕2008
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-28
收藏
得分:0 

谢谢大家了!

2006-06-09 15:01
快速回复:中缀表达式的算法告诉我!
数据加载中...
 
   



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

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