#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
bool newstringXC(string &strsor);
bool newstringJJ(string &strsor);
bool resetstringJia1(string &strsor);
bool resetstringJia2(string &strsor);
bool resetstringJia3(string &strsor);
bool resetstringJia4(string &strsor);
bool newstringKH(string &strsor)
{
int index1 = strsor.rfind("(", strsor.length()-1);
if(index1 == string::npos)
return false;
int index2 = strsor.find(")", index1);
string childstr;
childstr.assign(strsor, index1+1, index2-index1-1);
while(newstringXC(childstr))
{
while((resetstringJia1(childstr))){}
while((resetstringJia2(childstr))){}
while((resetstringJia3(childstr))){}
while((resetstringJia4(childstr))){}
}
while(newstringJJ(childstr))
{
while((resetstringJia1(childstr))){}
while((resetstringJia2(childstr))){}
while((resetstringJia3(childstr))){}
while((resetstringJia4(childstr))){}
}
strsor.replace(index1, index2-index1+1, childstr.c_str(), childstr.length());
while((resetstringJia1(strsor))){}
while((resetstringJia2(strsor))){}
while((resetstringJia3(strsor))){}
while((resetstringJia4(strsor))){}
return true;
};
bool newstringXC(string &strsor)
{
int index1 = strsor.find_first_of("*/", 0);
if(index1 == string::npos)
return false;
int index2 = strsor.find_first_of("+-*/", index1+2);
if(index2 == string::npos)
index2 = strsor.length();
int index3 = strsor.find_last_of("+-*/", index1-1);
if(index3 == string::npos)
index3 = -1;
string str1;
str1.assign(strsor, index3+1, index1-index3-1);
string str2;
str2.assign(strsor, index1+1, index2-index1-1);
double dob1 = atof(str1.c_str());
double dob2 = atof(str2.c_str());
double dob3;
if(strsor.at(index1) == '*')
dob3 = dob1 * dob2;
else if(strsor.at(index1) == '/')
dob3 = dob1 / dob2;
char c_str3[50];
memset(c_str3, 0, 50);
sprintf(c_str3, "%.5f", dob3);
strsor.replace(index3 +1, index2-index3-1, c_str3, strlen(c_str3));
return true;
};
bool newstringJJ(string &strsor)
{
int index1 = strsor.find_first_of("+-", 1);
if(index1 == string::npos)
return false;
int index2 = strsor.find_first_of("+-*/", index1+1);
if(index2 == string::npos)
index2 = strsor.length();
int index3 = strsor.find_last_of("+-*/", index1-1);
if((index3 == string::npos) || (index3 == 0))
index3 = -1;
string str1;
str1.assign(strsor, index3+1, index1-index3-1);
string str2;
str2.assign(strsor, index1+1, index2-index1-1);
double dob1 = atof(str1.c_str());
double dob2 = atof(str2.c_str());
double dob3;
if(strsor.at(index1) == '+')
dob3 = dob1 + dob2;
else if(strsor.at(index1) == '-')
dob3 = dob1 - dob2;
char c_str3[50];
memset(c_str3, 0, 50);
sprintf(c_str3, "%.5f", dob3);
strsor.replace(index3 +1, index2-index3-1, c_str3, strlen(c_str3));
return true;
};
bool resetstringJia1(string &strsor)
{
int index1 = strsor.find("++");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "+", 1);
return true;
};
bool resetstringJia2(string &strsor)
{
int index1 = strsor.find("+-");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "-", 1);
return true;
};
bool resetstringJia3(string &strsor)
{
int index1 = strsor.find("-+");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "-", 1);
return true;
};
bool resetstringJia4(string &strsor)
{
int index1 = strsor.find("--");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "+", 1);
return true;
};
int main(int argc, char *argv[])
{
string m_inputstr;
cout << "Please input shuanshi: " << endl;
cin >> m_inputstr;
while(newstringKH(m_inputstr))
{
while((resetstringJia1(m_inputstr))){}
while((resetstringJia2(m_inputstr))){}
while((resetstringJia3(m_inputstr))){}
while((resetstringJia4(m_inputstr))){}
}
while(newstringXC(m_inputstr))
{
while((resetstringJia1(m_inputstr))){}
while((resetstringJia2(m_inputstr))){}
while((resetstringJia3(m_inputstr))){}
while((resetstringJia4(m_inputstr))){}
}
while(newstringJJ(m_inputstr))
{
while((resetstringJia1(m_inputstr))){}
while((resetstringJia2(m_inputstr))){}
while((resetstringJia3(m_inputstr))){}
while((resetstringJia4(m_inputstr))){}
}
cout << m_inputstr << endl;
return 0;
}
不支持数学函数和开方、乘方等
#include <stdio.h>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
bool newstringXC(string &strsor);
bool newstringJJ(string &strsor);
bool resetstringJia1(string &strsor);
bool resetstringJia2(string &strsor);
bool resetstringJia3(string &strsor);
bool resetstringJia4(string &strsor);
bool newstringKH(string &strsor)
{
int index1 = strsor.rfind("(", strsor.length()-1);
if(index1 == string::npos)
return false;
int index2 = strsor.find(")", index1);
string childstr;
childstr.assign(strsor, index1+1, index2-index1-1);
while(newstringXC(childstr))
{
while((resetstringJia1(childstr))){}
while((resetstringJia2(childstr))){}
while((resetstringJia3(childstr))){}
while((resetstringJia4(childstr))){}
}
while(newstringJJ(childstr))
{
while((resetstringJia1(childstr))){}
while((resetstringJia2(childstr))){}
while((resetstringJia3(childstr))){}
while((resetstringJia4(childstr))){}
}
strsor.replace(index1, index2-index1+1, childstr.c_str(), childstr.length());
while((resetstringJia1(strsor))){}
while((resetstringJia2(strsor))){}
while((resetstringJia3(strsor))){}
while((resetstringJia4(strsor))){}
return true;
};
bool newstringXC(string &strsor)
{
int index1 = strsor.find_first_of("*/", 0);
if(index1 == string::npos)
return false;
int index2 = strsor.find_first_of("+-*/", index1+2);
if(index2 == string::npos)
index2 = strsor.length();
int index3 = strsor.find_last_of("+-*/", index1-1);
if(index3 == string::npos)
index3 = -1;
string str1;
str1.assign(strsor, index3+1, index1-index3-1);
string str2;
str2.assign(strsor, index1+1, index2-index1-1);
double dob1 = atof(str1.c_str());
double dob2 = atof(str2.c_str());
double dob3;
if(strsor.at(index1) == '*')
dob3 = dob1 * dob2;
else if(strsor.at(index1) == '/')
dob3 = dob1 / dob2;
char c_str3[50];
memset(c_str3, 0, 50);
sprintf(c_str3, "%.5f", dob3);
strsor.replace(index3 +1, index2-index3-1, c_str3, strlen(c_str3));
return true;
};
bool newstringJJ(string &strsor)
{
int index1 = strsor.find_first_of("+-", 1);
if(index1 == string::npos)
return false;
int index2 = strsor.find_first_of("+-*/", index1+1);
if(index2 == string::npos)
index2 = strsor.length();
int index3 = strsor.find_last_of("+-*/", index1-1);
if((index3 == string::npos) || (index3 == 0))
index3 = -1;
string str1;
str1.assign(strsor, index3+1, index1-index3-1);
string str2;
str2.assign(strsor, index1+1, index2-index1-1);
double dob1 = atof(str1.c_str());
double dob2 = atof(str2.c_str());
double dob3;
if(strsor.at(index1) == '+')
dob3 = dob1 + dob2;
else if(strsor.at(index1) == '-')
dob3 = dob1 - dob2;
char c_str3[50];
memset(c_str3, 0, 50);
sprintf(c_str3, "%.5f", dob3);
strsor.replace(index3 +1, index2-index3-1, c_str3, strlen(c_str3));
return true;
};
bool resetstringJia1(string &strsor)
{
int index1 = strsor.find("++");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "+", 1);
return true;
};
bool resetstringJia2(string &strsor)
{
int index1 = strsor.find("+-");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "-", 1);
return true;
};
bool resetstringJia3(string &strsor)
{
int index1 = strsor.find("-+");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "-", 1);
return true;
};
bool resetstringJia4(string &strsor)
{
int index1 = strsor.find("--");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "+", 1);
return true;
};
int main(int argc, char *argv[])
{
string m_inputstr;
cout << "Please input shuanshi: " << endl;
cin >> m_inputstr;
while(newstringKH(m_inputstr))
{
while((resetstringJia1(m_inputstr))){}
while((resetstringJia2(m_inputstr))){}
while((resetstringJia3(m_inputstr))){}
while((resetstringJia4(m_inputstr))){}
}
while(newstringXC(m_inputstr))
{
while((resetstringJia1(m_inputstr))){}
while((resetstringJia2(m_inputstr))){}
while((resetstringJia3(m_inputstr))){}
while((resetstringJia4(m_inputstr))){}
}
while(newstringJJ(m_inputstr))
{
while((resetstringJia1(m_inputstr))){}
while((resetstringJia2(m_inputstr))){}
while((resetstringJia3(m_inputstr))){}
while((resetstringJia4(m_inputstr))){}
}
cout << m_inputstr << endl;
return 0;
}
不支持数学函数和开方、乘方等