//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; } } }
递归在代数表达式中的运用
最新推荐文章于 2022-03-09 20:37:24 发布