Advertisement
Mr-A

A-Engine <Eval> v0.03

Dec 18th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1. #include <string>
  2. #include <stdlib.h>
  3. #include <sstream>
  4.  
  5.  
  6. class GetArg{
  7.     private:
  8.         std::string temp1;
  9.     public:
  10.         std::string GetArg0(std::string, std::string, std::string);
  11.         std::string GetArg1(std::string, std::string, std::string, int, std::string);
  12. };
  13.  
  14. std::string GetArg::GetArg0(std::string line, std::string tag, std::string exception){
  15.     std::string exceptions=exception.c_str()=="ALPHS"?"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":exception;
  16.     bool doexception=exceptions.c_str()==""?false:true;
  17.     bool isechar=false;
  18.     int tagpos= line.find(tag)+tag.length();
  19.     temp1="";
  20.     for (int a=tagpos; a<line.length(); a++){
  21.         if (doexception){
  22.             for (int b=0; b<exceptions.length(); b++){
  23.                 if (line.substr(a, 1)==exceptions.substr(b, 1)){
  24.                     isechar=true; break;
  25.                 }
  26.             }
  27.         }
  28.         if (isdigit(line[a])||doexception){
  29.             for(;;){
  30.                 if((!isdigit(line[a]) && !doexception)||(doexception && !isechar && !isdigit(line[a])))return temp1;
  31.                 temp1+=line.substr(a++, 1);
  32.                 if(a==line.length()) return temp1;
  33.                 if (!(line.substr(a,1).c_str()==" ")) return "FAILED!";
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  39. std::string GetArg::GetArg1(std::string line, std::string tag, std::string exception, int noofparas, std::string seperator){
  40.     std::string exceptions=exception.c_str()=="ALPHS"?"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":exception;
  41.     bool doexception=exceptions.c_str()==""?false:true;
  42.     bool isechar=false;
  43.     int tagpos= line.find(tag)+tag.length();
  44.     temp1="";
  45.     for (int a=tagpos; a<line.length(); a++){
  46.         if (doexception){
  47.             for (int b=0; b<exceptions.length(); b++){
  48.                 if (line.substr(a, 1)==exceptions.substr(b, 1)){
  49.                     isechar=true; break;
  50.                 }
  51.             }
  52.         }
  53.         if (isdigit(line[a])||doexception){
  54.             for(;;){
  55.                 if((!isdigit(line[a]) && !doexception)||(doexception && !isechar && !isdigit(line[a])))return temp1;
  56.                 temp1+=line.substr(a++, 1);
  57.                 if(a==line.length()) return temp1;
  58.                 if (!(line.substr(a,1).c_str()==" ")) return "FAILED!";
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65. class Eval{
  66.     private:
  67.         int temp1, temp3;
  68.         std::string temp2;
  69.     public:
  70.         Eval(std::string);
  71.         std::string DIVIDE(std::string);
  72.         std::string MULTIPLY(std::string);
  73.         std::string ADD(std::string);
  74.         std::string SUBTRACT(std::string);
  75.         std::string IntToStr(int);
  76.         std::string operation;
  77. };
  78.  
  79.  
  80. Eval::Eval(std::string oper){
  81.     operation=oper;
  82.     while(operation.rfind("(")!=std::string::npos){
  83.         temp1=operation.rfind("(");
  84.         temp3=operation.find(")", temp1);
  85.         temp2=operation.substr(temp1+1,temp3-temp1-1);
  86.        
  87.         temp2=DIVIDE(temp2);     //D
  88.         temp2=MULTIPLY(temp2);   //M
  89.         temp2=ADD(temp2);        //A
  90.         temp2=SUBTRACT(temp2);   //S
  91.        
  92.         operation.replace(temp1, temp3-temp1+1, temp2);
  93.     }
  94.     operation=DIVIDE(operation);     //D
  95.     operation=MULTIPLY(operation);   //M
  96.     operation=ADD(operation);        //A
  97.     operation=SUBTRACT(operation);   //S
  98. }
  99.  
  100.  
  101.  
  102.  
  103. std::string Eval::IntToStr( int n )
  104.   {
  105.   std::ostringstream result;
  106.   result << n;
  107.   return result.str();
  108.   }
  109.      
  110. std::string Eval::DIVIDE(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.  
  139. std::string Eval::MULTIPLY(std::string asdf){
  140.             std::string larg,rarg;
  141.             std::string a=asdf;
  142.         int ilarg, irarg;
  143.  
  144.     while (a.find("*")!=std::string::npos){
  145.         int pluspos=a.find("*");
  146.         int temposha=pluspos;
  147.        
  148.         while (pluspos!=0 && isdigit(a[temposha-1])){
  149.             larg = a.substr(temposha-1, 1)+larg;
  150.             if (temposha==0) break;
  151.             temposha--;
  152.         }
  153.         ilarg=temposha;
  154.         temposha=pluspos;
  155.         while (pluspos!=0 && isdigit(a[temposha+1])){
  156.             rarg += a.substr(temposha+1, 1);
  157.             if (temposha==a.length()) break;
  158.             temposha++;
  159.         }
  160.         irarg=temposha;
  161.         temposha=a.length();
  162.  
  163.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())*atoi(rarg.c_str()))));
  164.         larg=""; rarg="";}
  165.     return a;
  166. }
  167.  
  168. std::string Eval::ADD(std::string asdf){
  169.             std::string larg,rarg;
  170.             std::string a=asdf;
  171.         int ilarg, irarg;
  172.  
  173.     while (a.find("+")!=std::string::npos){
  174.         int pluspos=a.find("+");
  175.         int temposha=pluspos;
  176.        
  177.         while (pluspos!=0 && isdigit(a[temposha-1])){
  178.             larg = a.substr(temposha-1, 1)+larg;
  179.             if (temposha==0) break;
  180.             temposha--;
  181.         }
  182.         ilarg=temposha;
  183.         temposha=pluspos;
  184.         while (pluspos!=0 && isdigit(a[temposha+1])){
  185.             rarg += a.substr(temposha+1, 1);
  186.             if (temposha==a.length()) break;
  187.             temposha++;
  188.         }
  189.         irarg=temposha;
  190.         temposha=a.length();
  191.  
  192.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())+atoi(rarg.c_str()))));
  193.         larg=""; rarg="";}
  194.     return a;
  195. }
  196. std::string Eval::SUBTRACT(std::string asdf){
  197.             std::string larg,rarg;
  198.             std::string a=asdf;
  199.         int ilarg, irarg;
  200.  
  201.     while (a.rfind("-")!=std::string::npos&&a.rfind("-")!=0){
  202.        
  203.         int pluspos;
  204.         pluspos=a.find("-");
  205.         if (a.find("-")==0)pluspos=a.find("-", 1);
  206.        
  207.         int temposha=pluspos;
  208.        
  209.         while (a.rfind("-")!=0 && (isdigit(a[temposha-1])||(a.substr(temposha-1,1)=="-"&&temposha==1))){
  210.             larg = a.substr(temposha-1, 1)+larg;
  211.             temposha--;
  212.             if (temposha==0) break;
  213.         }
  214.         ilarg=temposha;
  215.         temposha=pluspos;
  216.         while (a.rfind("-")!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-"&&temposha==1))){
  217.             rarg += a.substr(temposha+1, 1);
  218.             temposha++;
  219.             if (temposha==a.length()) break;
  220.         }
  221.         irarg=temposha;
  222.         temposha=a.length();
  223.  
  224.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atoi(larg.c_str())-atoi(rarg.c_str()))));
  225.         larg=""; rarg="";}
  226.     return a;
  227. }
  228.  
  229.  
  230. int Evaluate(std::string strvalue){
  231.     Eval Calc(strvalue);
  232.     return atoi(Calc.operation.c_str());
  233. }
  234. std::string stringify(int whatever){
  235.     Eval Calc("0");
  236.     return Calc.IntToStr(whatever);
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement