Advertisement
Mr-A

Eval function on C++

Aug 19th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. //eval function by A-Man a.k.a Mr-A
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <sstream>
  5.  
  6.  
  7. class Eval{
  8.     private:
  9.         int temp1, temp3;
  10.         std::string temp2;
  11.     public:
  12.         Eval(std::string);
  13.         std::string DIVIDE(std::string);
  14.         std::string MULTIPLY(std::string);
  15.         std::string ADD(std::string);
  16.         std::string SUBTRACT(std::string);
  17.         std::string IntToStr(int);
  18.         std::string operation;
  19. };
  20.  
  21.  
  22. Eval::Eval(std::string oper){
  23.     operation=oper;
  24.     while(operation.rfind("(")!=std::string::npos){
  25.         temp1=operation.rfind("(");
  26.         temp3=operation.find(")", temp1);
  27.         temp2=operation.substr(temp1+1,temp3-temp1-1);
  28.        
  29.         temp2=DIVIDE(temp2);     //D
  30.         temp2=MULTIPLY(temp2);   //M
  31.         temp2=ADD(temp2);        //A
  32.         temp2=SUBTRACT(temp2);   //S
  33.        
  34.         operation.replace(temp1, temp3-temp1+1, temp2);
  35.     }
  36.     operation=DIVIDE(operation);     //D
  37.     operation=MULTIPLY(operation);   //M
  38.     operation=ADD(operation);        //A
  39.     operation=SUBTRACT(operation);   //S
  40. }
  41.  
  42.  
  43.  
  44.  
  45. std::string Eval::IntToStr( int n )
  46.   {
  47.   std::ostringstream result;
  48.   result << n;
  49.   return result.str();
  50.   }
  51.      
  52. std::string Eval::DIVIDE(std::string asdf){
  53.             std::string larg,rarg;
  54.             std::string a=asdf;
  55.         int ilarg, irarg;
  56.  
  57.     while (a.find("/")!=std::string::npos){
  58.         int pluspos=a.find("/");
  59.         int temposha=pluspos;
  60.        
  61.         while (pluspos!=0 && isdigit(a[temposha-1])){
  62.             larg = a.substr(temposha-1, 1)+larg;
  63.             if (temposha==0) break;
  64.             temposha--;
  65.         }
  66.         ilarg=temposha;
  67.         temposha=pluspos;
  68.         while (pluspos!=0 && isdigit(a[temposha+1])){
  69.             rarg += a.substr(temposha+1, 1);
  70.             if (temposha==a.length()) break;
  71.             temposha++;
  72.         }
  73.         irarg=temposha;
  74.         temposha=a.length();
  75.  
  76.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())/atoi(rarg.c_str()))));
  77.         larg=""; rarg="";}
  78.     return a;
  79. }
  80.  
  81. std::string Eval::MULTIPLY(std::string asdf){
  82.             std::string larg,rarg;
  83.             std::string a=asdf;
  84.         int ilarg, irarg;
  85.  
  86.     while (a.find("*")!=std::string::npos){
  87.         int pluspos=a.find("*");
  88.         int temposha=pluspos;
  89.        
  90.         while (pluspos!=0 && isdigit(a[temposha-1])){
  91.             larg = a.substr(temposha-1, 1)+larg;
  92.             if (temposha==0) break;
  93.             temposha--;
  94.         }
  95.         ilarg=temposha;
  96.         temposha=pluspos;
  97.         while (pluspos!=0 && isdigit(a[temposha+1])){
  98.             rarg += a.substr(temposha+1, 1);
  99.             if (temposha==a.length()) break;
  100.             temposha++;
  101.         }
  102.         irarg=temposha;
  103.         temposha=a.length();
  104.  
  105.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())*atoi(rarg.c_str()))));
  106.         larg=""; rarg="";}
  107.     return a;
  108. }
  109.  
  110. std::string Eval::ADD(std::string asdf){
  111.             std::string larg,rarg;
  112.             std::string a=asdf;
  113.         int ilarg, irarg;
  114.  
  115.     while (a.find("+")!=std::string::npos){
  116.         int pluspos=a.find("+");
  117.         int temposha=pluspos;
  118.        
  119.         while (pluspos!=0 && isdigit(a[temposha-1])){
  120.             larg = a.substr(temposha-1, 1)+larg;
  121.             if (temposha==0) break;
  122.             temposha--;
  123.         }
  124.         ilarg=temposha;
  125.         temposha=pluspos;
  126.         while (pluspos!=0 && isdigit(a[temposha+1])){
  127.             rarg += a.substr(temposha+1, 1);
  128.             if (temposha==a.length()) break;
  129.             temposha++;
  130.         }
  131.         irarg=temposha;
  132.         temposha=a.length();
  133.  
  134.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())+atoi(rarg.c_str()))));
  135.         larg=""; rarg="";}
  136.     return a;
  137. }
  138. std::string Eval::SUBTRACT(std::string asdf){
  139.             std::string larg,rarg;
  140.             std::string a=asdf;
  141.         int ilarg, irarg;
  142.  
  143.     while (a.rfind("-")!=std::string::npos&&a.rfind("-")!=0){
  144.        
  145.         int pluspos;
  146.         pluspos=a.find("-");
  147.         if (a.find("-")==0)pluspos=a.find("-", 1);
  148.        
  149.         int temposha=pluspos;
  150.        
  151.         while (a.rfind("-")!=0 && (isdigit(a[temposha-1])||(a.substr(temposha-1,1)=="-"&&temposha==1))){
  152.             larg = a.substr(temposha-1, 1)+larg;
  153.             temposha--;
  154.             if (temposha==0) break;
  155.         }
  156.         ilarg=temposha;
  157.         temposha=pluspos;
  158.         while (a.rfind("-")!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-"&&temposha==1))){
  159.             rarg += a.substr(temposha+1, 1);
  160.             temposha++;
  161.             if (temposha==a.length()) break;
  162.         }
  163.         irarg=temposha;
  164.         temposha=a.length();
  165.  
  166.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())-atoi(rarg.c_str()))));
  167.         larg=""; rarg="";}
  168.     return a;
  169. }
  170.  
  171.  
  172. int Evaluate(std::string strvalue){
  173.     Eval Calc(strvalue);
  174.     return atoi(Calc.operation.c_str());
  175. }
  176. std::string stringify(int whatever){
  177.     Eval Calc("0");
  178.     return Calc.IntToStr(whatever);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement