SPOJ_4_ONP

中缀转后缀,没什么好说的

/*
 *NAME: ONP
 *LANG: C++
 */
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int n;
string s;

void solve(string s){
    int stack = 0,pos=0;
    string tmp1="",tmp2="";
    if (s[0]=='(') {s.erase(0,1);s.erase(s.size()-1,1);}
    for (int i=0;i<s.size();++i){
	if ((s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='^') && stack == 0){
	    pos=i;break;
	}
	if (s[i]=='(') ++stack;
	if (s[i]==')') --stack;
    }
    if (pos!=0){
	for (int i=0;i<pos;++i) tmp1+=s[i];
	for (int i=pos+1;i<s.size();++i) tmp2+=s[i];
	solve(tmp1);
	solve(tmp2);	
    }
    if (s.size()!=0)
	cout << s[pos];
}
int main(){
    cin >> n;
    for (int i=1;i<=n;++i){
	cin >> s;
	solve(s);
	cout << endl;
    }
    return 0;
}



你可能感兴趣的:(c,String)