标题:表达式求值的程序,谁给点建议
取消只看楼主
jiely
Rank: 1
等 级:新手上路
帖 子:106
专家分:0
注 册:2004-5-15
 问题点数:0 回复次数:0 
表达式求值的程序,谁给点建议

//我的表达式求值程序,程序能够运行,但结构化不好,谁能给点建议??谢谢各位高手了!

/* * 本程序由jiely编写 * *文件名称: *摘 要: * *作 者: *班 级: *完成日期: * */

#include<iostream.h> #include<malloc.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> #include<strstrea.h> #define TRUE 1 #define FALSE -1 #define ERROR 0 #define OVERFLOW -1 #define OK 1 #define NULL 0

#define STACKINCREMENT 10 #define STACK_SIZE 100

typedef int Status;

template<class T> class STACK { public: STACK(int); ~STACK(void); Status ClearStack(void)const; int StackEmpty(void)const; int StackFull(void)const ; Status Push(const T&); T Pop(void); T GetTop(void); private: int m_top ; int m_size ;//栈的大小 T *m_elem ;//存储栈元素 };

template<class T> STACK<T>::STACK(int size) { m_size = size; m_top = -1; m_elem = new T [m_size]; if(!m_elem) { cerr<<"内存分配失败!按任意键退出!"<<endl; exit(ERROR); }

}

template<class T> STACK<T>::~STACK(void) { delete []m_elem; }

template<class T> Status STACK<T>::ClearStack(void)const//清空链栈 { m_top = -1; return OK; }

template<class T> int STACK<T>::StackEmpty(void)const//判断栈是否为空 { return (m_top == -1); }

template<class T> Status STACK<T>::StackFull(void)const // 判断栈是否为满 { return (m_top == STACK_SIZE-1); }

template<class T> T STACK<T>:: GetTop(void) //得到栈栈顶元素 { if(StackEmpty()) { cerr<<"此栈为空栈!无任何元素!"<<endl; exit(ERROR); } else return m_elem[m_top]; }

template<class T> Status STACK<T>:: Push(const T&e)//进栈 { if(StackFull()) { cerr<<"栈满,无法插入元素!"<<endl; exit(ERROR); } else m_elem[++m_top] = e; return OK; }

template<class T> T STACK<T>::Pop()//出栈 { T e; if(StackEmpty()) { cerr<<"栈为空,无元素!"<<endl; exit(ERROR); } else e = m_elem[m_top--]; return e; }

/******************************************* ********** 后缀表达式的求值****************/ float Compute(char *str) { char ch; float x; STACK<float>FloatStack(STACK_SIZE); istrstream ins(str); for( ins>>ch;ch!='=';ins>>ch) { switch(ch) { case '+':x=FloatStack.Pop()+FloatStack.Pop();break; case '-':x=FloatStack.Pop(); x=FloatStack.Pop()-x;break; case '*':x=FloatStack.Pop()*FloatStack.Pop();break; case '/':x=FloatStack.Pop(); if(x!=0.0) { x=FloatStack.Pop()/x; } //if else { cerr<<" 被除数不能为0!"<<endl; exit(1); } //else break; default : ins.putback(ch); ins>>x; break; } //switch FloatStack.Push(x); } if(!FloatStack.StackEmpty()) { x=FloatStack.Pop(); if(FloatStack.StackEmpty()) return x; else { cout<<" 表达式输入错误!"<<endl; exit(ERROR); } //else } //if else { cout<<"\t栈为空!!!"<<endl; exit(ERROR); } //else } //end

int Precedence(char op) { switch(op) { case '+': case '-': return 1; break; case '*': case '/': //case '%': return 2; break; case '(': case '=': default: return 0; break; } }

/************************中缀转后缀的算法***********************/

void Change(char *s1,char *s2) { int j=0; char ch,k; STACK<char>CharStack(STACK_SIZE); istrstream ins(s1); CharStack.Push('='); for(ins>>ch; ch!='='; ins>>ch) { if(ch==' ') {} //if else if(ch == '(') { CharStack.Push('('); } //else if else if(ch == ')') { while(CharStack.GetTop() != '(') s2[j++]=CharStack.Pop(); CharStack.Pop(); } //else if else if(ch=='+' || ch=='-' || ch=='*' || ch=='/') { k=CharStack.GetTop(); while(Precedence(k) >= Precedence(ch)) { s2[j++]=k; CharStack.Pop(); k=CharStack.GetTop(); } //while CharStack.Push(ch); } //else if else{ if(isdigit(ch) || ch=='.') { s2[j++]=ch; } //if s2[j++]=' '; } //else } //for ch=CharStack.Pop(); while(ch != '=') { if(ch == '(') { cout<<"\t表达式输入错误!"<<endl; exit(ERROR); } //if else { s2[j++]=ch; ch=CharStack.Pop(); } //else } //while s2[j++]=' ' ; s2[j++]='='; s2[j++]='\0'; } //end

void main() { char a[40]; char b[40]; cout<<"请输入一个中缀表达式:(以'='结束)"<<endl; cin.getline(a,sizeof(a)); Change(a,b); cout<<" 其后缀表达式如下0 "<<endl; cout<<b<<endl; cout<<"求值结果为:"<<Compute(b)<<endl; }

搜索更多相关主题的帖子: 式求值 点建议 define include 
2004-06-11 16:24



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




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

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