Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //eval function by A-Man a.k.a Mr-A
- #include <string>
- #include <stdlib.h>
- #include <sstream>
- class Eval{
- private:
- int temp1, temp3;
- std::string temp2;
- public:
- Eval(std::string);
- std::string DIVIDE(std::string);
- std::string MULTIPLY(std::string);
- std::string ADD(std::string);
- std::string SUBTRACT(std::string);
- std::string IntToStr(int);
- std::string operation;
- };
- Eval::Eval(std::string oper){
- operation=oper;
- while(operation.rfind("(")!=std::string::npos){
- temp1=operation.rfind("(");
- temp3=operation.find(")", temp1);
- temp2=operation.substr(temp1+1,temp3-temp1-1);
- temp2=DIVIDE(temp2); //D
- temp2=MULTIPLY(temp2); //M
- temp2=ADD(temp2); //A
- temp2=SUBTRACT(temp2); //S
- operation.replace(temp1, temp3-temp1+1, temp2);
- }
- operation=DIVIDE(operation); //D
- operation=MULTIPLY(operation); //M
- operation=ADD(operation); //A
- operation=SUBTRACT(operation); //S
- }
- std::string Eval::IntToStr( int n )
- {
- std::ostringstream result;
- result << n;
- return result.str();
- }
- std::string Eval::DIVIDE(std::string asdf){
- std::string larg,rarg;
- std::string a=asdf;
- int ilarg, irarg;
- while (a.find("/")!=std::string::npos){
- int pluspos=a.find("/");
- int temposha=pluspos;
- while (pluspos!=0 && isdigit(a[temposha-1])){
- larg = a.substr(temposha-1, 1)+larg;
- if (temposha==0) break;
- temposha--;
- }
- ilarg=temposha;
- temposha=pluspos;
- while (pluspos!=0 && isdigit(a[temposha+1])){
- rarg += a.substr(temposha+1, 1);
- if (temposha==a.length()) break;
- temposha++;
- }
- irarg=temposha;
- temposha=a.length();
- a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())/atoi(rarg.c_str()))));
- larg=""; rarg="";}
- return a;
- }
- std::string Eval::MULTIPLY(std::string asdf){
- std::string larg,rarg;
- std::string a=asdf;
- int ilarg, irarg;
- while (a.find("*")!=std::string::npos){
- int pluspos=a.find("*");
- int temposha=pluspos;
- while (pluspos!=0 && isdigit(a[temposha-1])){
- larg = a.substr(temposha-1, 1)+larg;
- if (temposha==0) break;
- temposha--;
- }
- ilarg=temposha;
- temposha=pluspos;
- while (pluspos!=0 && isdigit(a[temposha+1])){
- rarg += a.substr(temposha+1, 1);
- if (temposha==a.length()) break;
- temposha++;
- }
- irarg=temposha;
- temposha=a.length();
- a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())*atoi(rarg.c_str()))));
- larg=""; rarg="";}
- return a;
- }
- std::string Eval::ADD(std::string asdf){
- std::string larg,rarg;
- std::string a=asdf;
- int ilarg, irarg;
- while (a.find("+")!=std::string::npos){
- int pluspos=a.find("+");
- int temposha=pluspos;
- while (pluspos!=0 && isdigit(a[temposha-1])){
- larg = a.substr(temposha-1, 1)+larg;
- if (temposha==0) break;
- temposha--;
- }
- ilarg=temposha;
- temposha=pluspos;
- while (pluspos!=0 && isdigit(a[temposha+1])){
- rarg += a.substr(temposha+1, 1);
- if (temposha==a.length()) break;
- temposha++;
- }
- irarg=temposha;
- temposha=a.length();
- a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())+atoi(rarg.c_str()))));
- larg=""; rarg="";}
- return a;
- }
- std::string Eval::SUBTRACT(std::string asdf){
- std::string larg,rarg;
- std::string a=asdf;
- int ilarg, irarg;
- while (a.rfind("-")!=std::string::npos&&a.rfind("-")!=0){
- int pluspos;
- pluspos=a.find("-");
- if (a.find("-")==0)pluspos=a.find("-", 1);
- int temposha=pluspos;
- while (a.rfind("-")!=0 && (isdigit(a[temposha-1])||(a.substr(temposha-1,1)=="-"&&temposha==1))){
- larg = a.substr(temposha-1, 1)+larg;
- temposha--;
- if (temposha==0) break;
- }
- ilarg=temposha;
- temposha=pluspos;
- while (a.rfind("-")!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-"&&temposha==1))){
- rarg += a.substr(temposha+1, 1);
- temposha++;
- if (temposha==a.length()) break;
- }
- irarg=temposha;
- temposha=a.length();
- a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())-atoi(rarg.c_str()))));
- larg=""; rarg="";}
- return a;
- }
- int Evaluate(std::string strvalue){
- Eval Calc(strvalue);
- return atoi(Calc.operation.c_str());
- }
- std::string stringify(int whatever){
- Eval Calc("0");
- return Calc.IntToStr(whatever);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement