递归在代数表达式中的运用

//postfix expression test whether the postfix expression is right#include<string> #include<iostream> using namespace std; int postfix(const string &str,int cur,int size); int main() { string str; cout<<"please the computing expression :"; cin>>str; int size=str.size(); int index=postfix(str,size-1,size); cout<<str<<endl; if(index>-1&&index==0) cout<<"the postifix expression is right !"<<endl; else cout<<"the postfix expression is wrong !"<<endl; return 0; } int postfix(const string &str,int cur,int size) //find the first of postfix,if one exists //precondition :the substring of str from the index cur //through the first of the string contains no blanks character //postcondition :returns the index of the first character //int the postfix expression that begins at index end of str //firstPtr should rturns -1 if no such prefix expression exists { if(cur<0||cur>=size) return -1; if(str[cur]>='a'&&str[cur]<='z') return cur; else if(str[cur]=='*'||str[cur]=='+'||str[cur]=='-'||str[cur]=='/') { int pre=postfix(str,cur-1,size); if(pre>-1) return postfix(str,pre-1,size); else return -1; } else return -1; } bool infix(const string &str,int first,int last) { if((first==last)&&(str[first]>='a')&&(str[first]<='z')) return true; else { int mid=findO(str,first,last); if(mid==-1) return false; else { bool ok=infix(str,first,mid-1); if(ok) return infix(str,mid+1,last); else return false; } } } //prefix expression test whether the prefix expression is right#include<string> #include<iostream> using namespace std; int prefix(const string &str,int cur,int size); int main() { string str; cout<<"please enter prefix expression :"; cin>>str; int size=str.size(); int index=prefix(str,0,size); cout<<str<<endl; if(index>-1&&index==size-1) cout<<"prefix expression success !"<<endl; else cout<<"prefix expression failed !"<<endl; } int prefix(const string &str,int cur,int size) //Finds the end of a prefix expression ,if one exists. //precondition :the substring of str from index cur //through the end of the string contains no blank characters //Postcondition :return the index of the lase character //int the prefix expression that begins at index first of //strExp endPtr should returns -1 if no such prefix expression exist { if(cur<0||cur>=size) return -1; if(str[cur]>='a'&&str[cur]<='z') return cur; else if(str[cur]=='*'||str[cur]=='+'||str[cur]=='-'||str[cur]=='/') { int post=prefix(str,cur+1,size); if(post>-1) return prefix(str,post+1,size); else return -1; } else return -1; } //and turn the postfix expression to the prefix expression #include<iostream> #include<string> using namespace std; void pre2post(const string &str,string &post,int &index); int main() { cout<<"please enter a right prefix expression :"; string str,post; cin>>str; cout<<endl<<str<<endl; int size=0; pre2post(str,post,size); cout<<" covert to postfix expression :"<<post<<endl; return 0; } void pre2post(const string &str,string &post,int &index) { if(index<str.size()) { char ch=str[index]; index++; if(ch>='a'&&ch<='z') post=post+ch; else { pre2post(str,post,index); pre2post(str,post,index); post=post+ch; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值