| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2687 人关注过本帖
标题:数据结构猛男猛女进来啊!表达式求值
只看楼主 加入收藏
colsumman
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-6-30
收藏
 问题点数:0 回复次数:21 
数据结构猛男猛女进来啊!表达式求值
表达式求值
能完成整数的加、减、乘、除算术运算。
以字符序列的形式从键盘输入语法正确的、不含变量的整数表达式,利用给定的算符优先关系,实现对算术四则混合运算表达式的求值,并演示在求值过程中运算符栈、操作数栈、输入字符和主要操作的变化过程。
必须有两大部分:中缀表达式到后缀表达式的转换,后缀表达式的计算。
不仅能进行10以内整数的算术运算,也能进行其他整数的算术运算。

个位老大啊!救救我啊!我只知道编出10位以内的算术运算!指针又用不好,还有摸扳 用不好,高手大虾进来帮帮忙啊!
搜索更多相关主题的帖子: 式求值 数据结构 猛男 整数 算术运算 
2006-06-30 17:12
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
收藏
得分:0 

c++/C + 汇编 = 天下无敌
2006-06-30 17:16
batam
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-6-29
收藏
得分:0 
热情依然 厉害!~

比那个什么 精液死 强百倍!

[此贴子已经被作者于2006-6-30 21:37:40编辑过]

2006-06-30 21:36
colsummaneye
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-6-29
收藏
得分:0 
神仙啊!你那程序 没有并演示在求值过程中运算符栈、操作数栈、输入字符和主要操作的变化过程。

2006-07-01 02:23
colsummaneye
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-6-29
收藏
得分:0 
你能不能最好把程序精简化  谢谢了

2006-07-01 02:29
colsummaneye
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-6-29
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册


2006-07-01 02:30
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
收藏
得分:0 

你那个是直接中序表达式求值来的,不过原理都是差不多的.似乎还简单一些,等我看看有没有时间做吧

[此贴子已经被作者于2006-7-1 10:42:30编辑过]


c++/C + 汇编 = 天下无敌
2006-07-01 10:31
colsummaneye
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-6-29
收藏
得分:0 

大哥我那个号不知道怎么用不了了 本来用指针的方法来解决双栈OPTR,OPND栈中内容和操作的函数的问题,这样就避免了问题,可惜程序总是出错,错误太多了,时间又紧,我们专业70多人差不多就我一个人做这个程序设计,又没什么人可以探讨,不是一般的人可以想出来的,但是您那程序演示在求值过程中运算符栈、操作数栈、输入字符和主要操作的变化过程。有人用的数组做的,但是要求是栈实现,这下偶郁闷,求斑竹大人和个位数据结构高手帮帮我修改下程序,可以演示在求值过程中运算符栈、操作数栈、输入字符和主要操作的变化过程,小的先谢谢了


2006-07-01 20:57
colsummaneye
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-6-29
收藏
得分: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-07-01 20:58
starrysky
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 
以下是引用colsummaneye在2006-7-1 2:30:08的发言:


是用我的那个算术表达式求值演示程序运行后截的图吧,
那就好说,只要改动下原代码就可以变成中缀转后缀的演示程序了,两者相差不大。
当然,前提是你要弄懂中缀和后缀表达式的原理和区别,现在恐怕没时间给你原代码
(因为我马上要考试了,一点私心),
提示下,改动输出部分的代码,在适当的时候适当的输出的内容压入同一个栈就可以了。


我的征途是星辰大海
2006-07-01 21:01
快速回复:数据结构猛男猛女进来啊!表达式求值
数据加载中...
 
   



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

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