很久很久之前写的,写的乱的一塌糊涂啊。。
float cal(vector<string>&);
bool scan(vector<string>&);
vector<string> sp(string& s_t);
bool isdig(char ch);
int main()
{
//define
stack<string> s_te;
vector<string> s_result;
string temp;
string input;
vector<string> done;
char* Error="未发现左括号!程序异常终止!\n";
int i=0;
char Flag='y';
//分离每个数字及运算符到字符串
while(Flag!='Q') {
cout<<"请输入正确的表达式:\n";
//初始化
i=0;
input.clear();
s_result.clear();
done.clear();
cin.sync();
cin>>input;
done=sp(input);
temp+='#';
s_te.push(temp);
temp.clear();
//改写成后缀表达式
for(int k=0; k<done.size(); k++) {
if(isdig(done[k][0])||done[k].size()>1&&done[k][0]=='-') {
s_result.push_back(done[k]);
//若为数字直接压入后缀表达式栈
} else if(done[k]=="(")
s_te.push(done[k]);
else if(done[k]=="*"||done[k]=="/") {
if(s_te.top()=="#"||s_te.top()=="+"||s_te.top()=="-"||s_te.top()=="(")
//若优先级比运算符栈顶高,直接入运算符栈
s_te.push(done[k]);
else {
s_result.push_back(s_te.top());
s_te.pop();
s_te.push(done[k]);
//反之将运算符栈弹出栈顶运算符入后缀表达式栈,然后将该操作符入栈
}
} else if(done[k]=="+"||done[k]=="-") {
if(s_te.top()=="#"||s_te.top()=="(")
s_te.push(done[k]);
else {
s_result.push_back(s_te.top());
s_te.pop();
s_te.push(done[k]);
}
} else if(done[k]==")") {
while(s_te.top()!="(") {
s_result.push_back(s_te.top());
s_te.pop();
try {
if(s_te.size()==1&&s_te.top()!="(")
throw Error;
} catch(const char* s) {
cout<<s;
abort();
}
}
s_te.pop();
//若为),则弹出运算符栈中在(上面的运算符并压入后缀表达式栈,并将(弹出
} else if(done[k]=="#") {
while(!s_te.empty()) {
s_result.push_back(s_te.top());
s_te.pop();
//遇到#则表达式扫描完毕,将运算符栈中所有操作符压入后缀表达式栈
}
}
}
s_result.reserve(s_result.size());
if(scan(s_result))
cout<<"="<<cal(s_result)<<endl;
cout<<"谢谢使用!\nCoded sR\n";
cout<<"输入任意值继续进行计算,输入Q退出:\n";
cin>>Flag;
}
cout<<"Bye!";
return 0;
}
float cal(vector<string>& it)
//后缀表达式运算函数
{
stack<float> st_ans;
float l_val,r_val,ans;
for(int k=0; it[k]!="#"; k++) {
if(isdig(it[k][0])||it[k].size()>1&&it[k][0]=='-')
st_ans.push(stof(it[k]));
else {
if(it[k]=="+") {
l_val=st_ans.top();
st_ans.pop();
r_val=st_ans.top();
st_ans.pop();
ans=l_val+r_val;
st_ans.push(ans);
} else if(it[k]=="-") {
r_val=st_ans.top();
st_ans.pop();
l_val=st_ans.top();
st_ans.pop();
ans=l_val-r_val;
st_ans.push(ans);
} else if(it[k]=="*") {
r_val=st_ans.top();
st_ans.pop();
l_val=st_ans.top();
st_ans.pop();
ans=l_val*r_val;
st_ans.push(ans);
} else if(it[k]=="/") {
r_val=st_ans.top();
st_ans.pop();
l_val=st_ans.top();
st_ans.pop();
ans=l_val/r_val;
st_ans.push(ans);
}
}
}
return st_ans.top();
}
bool scan(vector<string>& it)
{
for(int i=0; i<it.size(); i++) {
if(it[i]=="(") {
cout<<"表达式有误!\n";
return 0;
}
}
return 1;
}
vector<string> sp(string& s_t)
{
vector<string> v_result;
string s_mid;
s_t+='#';
int i=0;
while(i<s_t.size()-1) {
while(isdig(s_t[i])) {
s_mid+=s_t[i];
i++;
if(!isdig(s_t[i])) {
v_result.push_back(s_mid);
s_mid.clear();
}
}
if(!isdig(s_t[i])&&s_t[i]!='#') {
if(s_t[i]=='('&&s_t[i+1]=='-') {
v_result.push_back("(");
i++;
while(s_t[i]!=')') {
s_mid+=s_t[i];
i++;
}
v_result.push_back(s_mid);
s_mid.clear();
} else {
s_mid+=s_t[i];
v_result.push_back(s_mid);
s_mid.clear();
i++;
}
}
}
s_mid+='#';
v_result.push_back(s_mid);
return v_result;
}
bool isdig(char ch)
{
if(ch>47&&ch<58||ch=='.')
return 1;
else
return 0;
}