Advertisement
Mr-A

A-Engine <eval.cpp> v0.07

Aug 5th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.71 KB | None | 0 0
  1. //==========Eval.cpp===========
  2.  
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <sstream>
  6. #include "Eval.h"
  7.  
  8.  
  9.  
  10. std::string GetArg::GetArg0(std::string line, std::string tag, std::string exception)
  11. {
  12.     std::string exceptions=exception.c_str()=="ALPHS"?"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":exception;
  13.     bool doexception=exceptions.c_str()==""?false:true;
  14.     bool isechar=false;
  15.     int tagpos= line.find(tag)+tag.length();
  16.     temp1="";
  17.     for (int a=tagpos; a<line.length(); a++)
  18.     {
  19.         if (doexception)
  20.         {
  21.             for (int b=0; b<exceptions.length(); b++)
  22.             {
  23.                 if (line.substr(a, 1)==exceptions.substr(b, 1))
  24.                 {
  25.                     isechar=true;
  26.                     break;
  27.                 }
  28.             }
  29.         }
  30.         if (isdigit(line[a])||doexception)
  31.         {
  32.             for(;;)
  33.             {
  34.                 if((!isdigit(line[a]) && !doexception)||(doexception && !isechar && !isdigit(line[a])))return temp1;
  35.                 temp1+=line.substr(a++, 1);
  36.                 if(a==line.length()) return temp1;
  37.                 if (!(line.substr(a,1).c_str()==" ")) return "FAILED!";
  38.             }
  39.         }
  40.     }
  41.     return "";
  42. }
  43.  
  44. std::string GetArg::GetArg1(std::string line, std::string tag, std::string exception, int noofparas, std::string seperator)
  45. {
  46.     std::string exceptions=exception.c_str()=="ALPHS"?"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":exception;
  47.     bool doexception=exceptions.c_str()==""?false:true;
  48.     bool isechar=false;
  49.     int tagpos= line.find(tag)+tag.length();
  50.     temp1="";
  51.     for (int a=tagpos; a<line.length(); a++)
  52.     {
  53.         if (doexception)
  54.         {
  55.             for (int b=0; b<exceptions.length(); b++)
  56.             {
  57.                 if (line.substr(a, 1)==exceptions.substr(b, 1))
  58.                 {
  59.                     isechar=true;
  60.                     break;
  61.                 }
  62.             }
  63.         }
  64.         if (isdigit(line[a])||doexception)
  65.         {
  66.             for(;;)
  67.             {
  68.                 if((!isdigit(line[a]) && !doexception)||(doexception && !isechar && !isdigit(line[a])))return temp1;
  69.                 temp1+=line.substr(a++, 1);
  70.                 if(a==line.length()) return temp1;
  71.                 if (!(line.substr(a,1).c_str()==" ")) return "FAILED!";
  72.             }
  73.         }
  74.     }
  75.     return "";
  76. }
  77.  
  78.  
  79.  
  80.  
  81. bool Eval::BEval(std::string oper)
  82. {
  83.     operation=oper;
  84.     while(operation.rfind("(")!=std::string::npos)
  85.     {
  86.         temp1=operation.rfind("(");
  87.         temp3=operation.find(")", temp1);
  88.         temp2=operation.substr(temp1+1,temp3-temp1-1);
  89.  
  90.  
  91.  
  92.         temp2=ISEQUAL(operation);
  93.         temp2=ISNOTEQUAL(operation);
  94.         temp2=ISGREATER(operation);
  95.         temp2=ISSMALLER(operation);
  96.         temp2=ISGREATERE(operation);
  97.         temp2=ISSMALLERE(operation);
  98.         temp2=AND(operation);
  99.         temp2=OR(operation);
  100.  
  101.         operation.replace(temp1, temp3-temp1+1, temp2);
  102.     }
  103.     operation=ISEQUAL(operation);
  104.     operation=ISNOTEQUAL(operation);
  105.     operation=ISGREATER(operation);
  106.     operation=ISSMALLER(operation);
  107.     operation=ISGREATERE(operation);
  108.     operation=ISSMALLERE(operation);
  109.     operation=AND(operation);
  110.     operation=OR(operation);
  111. }
  112.  
  113.  
  114. std::string Eval::ISEQUAL(std::string asdf)
  115. {
  116.     std::string larg,rarg;
  117.     std::string a=asdf;
  118.     int ilarg, irarg;
  119.  
  120.     while (a.find("==")!=std::string::npos)
  121.     {
  122.         int pluspos=a.find("==");
  123.         int temposha=pluspos;
  124.  
  125.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  126.         {
  127.             larg = a.substr(temposha-1, 1)+larg;
  128.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  129.             {
  130.                 if (temposha!=0) temposha-=1;
  131.                 break;
  132.  
  133.             }
  134.             temposha--;
  135.             if (temposha==0) break;
  136.         }
  137.         ilarg=temposha;
  138.         temposha=pluspos;
  139.         while (pluspos!=0 && (isdigit(a[temposha+2])||(a.substr(temposha+2,1)=="-" && temposha==pluspos)||a.substr(temposha+2,1)=="."))
  140.         {
  141.             rarg += a.substr(temposha+2, 1);
  142.             if (temposha==a.length()) break;
  143.             temposha++;
  144.         }
  145.         irarg=temposha;
  146.         temposha=a.length();
  147.  
  148.         if (atoi(larg.c_str())==atoi(rarg.c_str()))
  149.         {
  150.             a.replace(ilarg,irarg-ilarg+2, "1");
  151.         }
  152.         else
  153.         {
  154.             a.replace(ilarg,irarg-ilarg+2, "0");
  155.         }
  156.  
  157.         larg="";
  158.         rarg="";
  159.     }
  160.     return a;
  161. }
  162.  
  163.  
  164. std::string Eval::ISNOTEQUAL(std::string asdf)
  165. {
  166.     std::string larg,rarg;
  167.     std::string a=asdf;
  168.     int ilarg, irarg;
  169.  
  170.     while (a.find("!=")!=std::string::npos)
  171.     {
  172.         int pluspos=a.find("!=");
  173.         int temposha=pluspos;
  174.  
  175.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  176.         {
  177.             larg = a.substr(temposha-1, 1)+larg;
  178.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  179.             {
  180.                 if (temposha!=0) temposha-=1;
  181.                 break;
  182.  
  183.             }
  184.             temposha--;
  185.             if (temposha==0) break;
  186.         }
  187.         ilarg=temposha;
  188.         temposha=pluspos;
  189.         while (pluspos!=0 && (isdigit(a[temposha+2])||(a.substr(temposha+2,1)=="-" && temposha==pluspos)||a.substr(temposha+2,1)=="."))
  190.         {
  191.             rarg += a.substr(temposha+2, 1);
  192.             if (temposha==a.length()) break;
  193.             temposha++;
  194.         }
  195.         irarg=temposha;
  196.         temposha=a.length();
  197.  
  198.         if (atoi(larg.c_str())!=atoi(rarg.c_str()))
  199.         {
  200.             a.replace(ilarg,irarg-ilarg+2, "1");
  201.         }
  202.         else
  203.         {
  204.             a.replace(ilarg,irarg-ilarg+2, "0");
  205.         }
  206.  
  207.         larg="";
  208.         rarg="";
  209.     }
  210.     return a;
  211. }
  212.  
  213. std::string Eval::ISGREATER(std::string asdf)
  214. {
  215.     std::string larg,rarg;
  216.     std::string a=asdf;
  217.     int ilarg, irarg;
  218.  
  219.     while (a.find(">")!=std::string::npos)
  220.     {
  221.         int pluspos=a.find(">");
  222.         int temposha=pluspos;
  223.  
  224.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  225.         {
  226.             larg = a.substr(temposha-1, 1)+larg;
  227.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  228.             {
  229.                 if (temposha!=0) temposha-=1;
  230.                 break;
  231.  
  232.             }
  233.             temposha--;
  234.             if (temposha==0) break;
  235.         }
  236.         ilarg=temposha;
  237.         temposha=pluspos;
  238.         while (pluspos!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-" && temposha==pluspos)||a.substr(temposha+1,1)=="."))
  239.         {
  240.             rarg += a.substr(temposha+1, 1);
  241.             if (temposha==a.length()) break;
  242.             temposha++;
  243.         }
  244.         irarg=temposha;
  245.         temposha=a.length();
  246.  
  247.         if (atoi(larg.c_str())>atoi(rarg.c_str()))
  248.         {
  249.             a.replace(ilarg,irarg-ilarg+1, "1");
  250.         }
  251.         else
  252.         {
  253.             a.replace(ilarg,irarg-ilarg+1, "0");
  254.         }
  255.  
  256.         larg="";
  257.         rarg="";
  258.     }
  259.     return a;
  260. }
  261.  
  262. std::string Eval::ISGREATERE(std::string asdf)
  263. {
  264.     std::string larg,rarg;
  265.     std::string a=asdf;
  266.     int ilarg, irarg;
  267.  
  268.     while (a.find(">=")!=std::string::npos)
  269.     {
  270.         int pluspos=a.find(">=");
  271.         int temposha=pluspos;
  272.  
  273.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  274.         {
  275.             larg = a.substr(temposha-1, 1)+larg;
  276.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  277.             {
  278.                 if (temposha!=0) temposha-=1;
  279.                 break;
  280.  
  281.             }
  282.             temposha--;
  283.             if (temposha==0) break;
  284.         }
  285.         ilarg=temposha;
  286.         temposha=pluspos;
  287.         while (pluspos!=0 && (isdigit(a[temposha+2])||(a.substr(temposha+2,1)=="-" && temposha==pluspos)||a.substr(temposha+2,1)=="."))
  288.         {
  289.             rarg += a.substr(temposha+2, 1);
  290.             if (temposha==a.length()) break;
  291.             temposha++;
  292.         }
  293.         irarg=temposha;
  294.         temposha=a.length();
  295.  
  296.         if (atoi(larg.c_str())>=atoi(rarg.c_str()))
  297.         {
  298.             a.replace(ilarg,irarg-ilarg+2, "1");
  299.         }
  300.         else
  301.         {
  302.             a.replace(ilarg,irarg-ilarg+2, "0");
  303.         }
  304.  
  305.         larg="";
  306.         rarg="";
  307.     }
  308.     return a;
  309. }
  310.  
  311. std::string Eval::ISSMALLER(std::string asdf)
  312. {
  313.     std::string larg,rarg;
  314.     std::string a=asdf;
  315.     int ilarg, irarg;
  316.  
  317.     while (a.find("<")!=std::string::npos)
  318.     {
  319.         int pluspos=a.find("<");
  320.         int temposha=pluspos;
  321.  
  322.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  323.         {
  324.             larg = a.substr(temposha-1, 1)+larg;
  325.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  326.             {
  327.                 if (temposha!=0) temposha-=1;
  328.                 break;
  329.  
  330.             }
  331.             temposha--;
  332.             if (temposha==0) break;
  333.         }
  334.         ilarg=temposha;
  335.         temposha=pluspos;
  336.         while (pluspos!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-" && temposha==pluspos)||a.substr(temposha+1,1)=="."))
  337.         {
  338.             rarg += a.substr(temposha+1, 1);
  339.             if (temposha==a.length()) break;
  340.             temposha++;
  341.         }
  342.         irarg=temposha;
  343.         temposha=a.length();
  344.  
  345.         if (atoi(larg.c_str())<atoi(rarg.c_str()))
  346.         {
  347.             a.replace(ilarg,irarg-ilarg+1, "1");
  348.         }
  349.         else
  350.         {
  351.             a.replace(ilarg,irarg-ilarg+1, "0");
  352.         }
  353.  
  354.         larg="";
  355.         rarg="";
  356.     }
  357.     return a;
  358. }
  359.  
  360. std::string Eval::ISSMALLERE(std::string asdf)
  361. {
  362.     std::string larg,rarg;
  363.     std::string a=asdf;
  364.     int ilarg, irarg;
  365.  
  366.     while (a.find("<=")!=std::string::npos)
  367.     {
  368.         int pluspos=a.find("<=");
  369.         int temposha=pluspos;
  370.  
  371.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  372.         {
  373.             larg = a.substr(temposha-1, 1)+larg;
  374.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  375.             {
  376.                 if (temposha!=0) temposha-=1;
  377.                 break;
  378.  
  379.             }
  380.             temposha--;
  381.             if (temposha==0) break;
  382.         }
  383.         ilarg=temposha;
  384.         temposha=pluspos;
  385.         while (pluspos!=0 && (isdigit(a[temposha+2])||(a.substr(temposha+2,1)=="-" && temposha==pluspos)||a.substr(temposha+2,1)=="."))
  386.         {
  387.             rarg += a.substr(temposha+2, 1);
  388.             if (temposha==a.length()) break;
  389.             temposha++;
  390.         }
  391.         irarg=temposha;
  392.         temposha=a.length();
  393.  
  394.         if (atoi(larg.c_str())<atoi(rarg.c_str()))
  395.         {
  396.             a.replace(ilarg,irarg-ilarg+2, "1");
  397.         }
  398.         else
  399.         {
  400.             a.replace(ilarg,irarg-ilarg+2, "0");
  401.         }
  402.  
  403.         larg="";
  404.         rarg="";
  405.     }
  406.     return a;
  407. }
  408.  
  409. std::string Eval::AND(std::string asdf)
  410. {
  411.     std::string larg,rarg;
  412.     std::string a=asdf;
  413.     int ilarg, irarg;
  414.  
  415.     while (a.find("&&")!=std::string::npos)
  416.     {
  417.         int pluspos=a.find("&&");
  418.         int temposha=pluspos;
  419.  
  420.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  421.         {
  422.             larg = a.substr(temposha-1, 1)+larg;
  423.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  424.             {
  425.                 if (temposha!=0) temposha-=1;
  426.                 break;
  427.  
  428.             }
  429.             temposha--;
  430.             if (temposha==0) break;
  431.         }
  432.         ilarg=temposha;
  433.         temposha=pluspos;
  434.         while (pluspos!=0 && (isdigit(a[temposha+2])||(a.substr(temposha+2,1)=="-" && temposha==pluspos)||a.substr(temposha+2,1)=="."))
  435.         {
  436.             rarg += a.substr(temposha+2, 1);
  437.             if (temposha==a.length()) break;
  438.             temposha++;
  439.         }
  440.         irarg=temposha;
  441.         temposha=a.length();
  442.  
  443.         if (atoi(larg.c_str())==1 && atoi(rarg.c_str())==1)
  444.         {
  445.             a.replace(ilarg,irarg-ilarg+2, "1");
  446.         }
  447.         else
  448.         {
  449.             a.replace(ilarg,irarg-ilarg+2, "0");
  450.         }
  451.  
  452.         larg="";
  453.         rarg="";
  454.     }
  455.     return a;
  456. }
  457.  
  458. std::string Eval::OR(std::string asdf)
  459. {
  460.     std::string larg,rarg;
  461.     std::string a=asdf;
  462.     int ilarg, irarg;
  463.  
  464.     while (a.find("||")!=std::string::npos)
  465.     {
  466.         int pluspos=a.find("||");
  467.         int temposha=pluspos;
  468.  
  469.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  470.         {
  471.             larg = a.substr(temposha-1, 1)+larg;
  472.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  473.             {
  474.                 if (temposha!=0) temposha-=1;
  475.                 break;
  476.  
  477.             }
  478.             temposha--;
  479.             if (temposha==0) break;
  480.         }
  481.         ilarg=temposha;
  482.         temposha=pluspos;
  483.         while (pluspos!=0 && (isdigit(a[temposha+2])||(a.substr(temposha+2,1)=="-" && temposha==pluspos)||a.substr(temposha+2,1)=="."))
  484.         {
  485.             rarg += a.substr(temposha+2, 1);
  486.             if (temposha==a.length()) break;
  487.             temposha++;
  488.         }
  489.         irarg=temposha;
  490.         temposha=a.length();
  491.  
  492.         if (atoi(larg.c_str())==1 || atoi(rarg.c_str())==1)
  493.         {
  494.             a.replace(ilarg,irarg-ilarg+2, "1");
  495.         }
  496.         else
  497.         {
  498.             a.replace(ilarg,irarg-ilarg+2, "0");
  499.         }
  500.  
  501.         larg="";
  502.         rarg="";
  503.     }
  504.     return a;
  505. }
  506.  
  507.  
  508. Eval::Eval(std::string oper)
  509. {
  510.     operation=oper;
  511.     while(operation.rfind("(")!=std::string::npos)
  512.     {
  513.         temp1=operation.rfind("(");
  514.         temp3=operation.find(")", temp1);
  515.         temp2=operation.substr(temp1+1,temp3-temp1-1);
  516.         std::string temp4=operation.substr(temp1+1,temp3-temp1-1);;
  517.         temp2=POWER(temp2);                //P
  518.         temp2=MULTIPLYORDIVIDE(temp2);     //D/M
  519.         temp2=ADDORSUBTRACT(temp2);        //A/s
  520.         if (temp4!=temp2)
  521.         {
  522.             operation.replace(temp1, temp3-temp1+1, temp2);
  523.         }
  524.         else
  525.         {
  526.             operation.replace(temp1, 1, "[");
  527.             operation.replace(temp3, 1, "]");
  528.         }
  529.     }
  530.     while (operation.find("--")!=std::string::npos)
  531.     {
  532.         operation.replace(operation.find("--"), 2, "+");
  533.         \
  534.     }
  535.     operation=POWER(operation);      //P
  536.     operation=MULTIPLYORDIVIDE(operation);
  537.     operation=ADDORSUBTRACT(operation);
  538.  
  539.     while (operation.find("[")!=std::string::npos) operation.replace(operation.find("["), 1, "(");
  540.     while (operation.find("]")!=std::string::npos) operation.replace(operation.find("]"), 1, ")");
  541. }
  542.  
  543.  
  544.  
  545.  
  546. std::string Eval::IntToStr( float n )
  547. {
  548.     std::ostringstream result;
  549.     result << n;
  550.     return result.str();
  551. }
  552.  
  553. std::string Eval::DIVIDE(std::string asdf)
  554. {
  555.     std::string larg,rarg;
  556.     std::string a=asdf;
  557.     int ilarg, irarg;
  558.  
  559.     while (a.find("/")!=std::string::npos)
  560.     {
  561.         int pluspos=a.find("/");
  562.         int temposha=pluspos;
  563.  
  564.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  565.         {
  566.             larg = a.substr(temposha-1, 1)+larg;
  567.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  568.             {
  569.                 if (temposha!=0) temposha-=1;
  570.                 break;
  571.  
  572.             }
  573.             temposha--;
  574.             if (temposha==0) break;
  575.         }
  576.         ilarg=temposha;
  577.         temposha=pluspos;
  578.         while (pluspos!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-" && temposha==pluspos)||a.substr(temposha+1,1)=="."))
  579.         {
  580.             rarg += a.substr(temposha+1, 1);
  581.             if (temposha==a.length()) break;
  582.             temposha++;
  583.         }
  584.         irarg=temposha;
  585.         temposha=a.length();
  586.         std::string quotient=IntToStr(atof(larg.c_str())/atof(rarg.c_str()));
  587.         if (quotient.find("e")!=std::string::npos)
  588.         {
  589.             quotient="0";
  590.         }
  591.         if (larg.find("-")==0)
  592.         {
  593.             if (quotient.find("-")!=std::string::npos && quotient.find("-")>1) quotient="+"+quotient;
  594.         }
  595.         a.replace(ilarg,irarg-ilarg+1, quotient);
  596.         larg="";
  597.         rarg="";
  598.     }
  599.     return a;
  600. }
  601.  
  602. std::string Eval::MULTIPLY(std::string asdf)
  603. {
  604.     std::string larg,rarg;
  605.     std::string a=asdf;
  606.     int ilarg, irarg;
  607.  
  608.     while (a.find("*")!=std::string::npos)
  609.     {
  610.         int pluspos=a.find("*");
  611.         int temposha=pluspos;
  612.  
  613.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha+1,1)==".") )
  614.         {
  615.             larg = a.substr(temposha-1, 1)+larg;
  616.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  617.             {
  618.                 if (temposha!=0) temposha-=1;
  619.                 break;
  620.  
  621.             }
  622.             temposha--;
  623.             if (temposha==0) break;
  624.         }
  625.         ilarg=temposha;
  626.         temposha=pluspos;
  627.         while (pluspos!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-" && temposha==pluspos)||a.substr(temposha+1,1)=="."))
  628.         {
  629.             rarg += a.substr(temposha+1, 1);
  630.             if (temposha==a.length()) break;
  631.             temposha++;
  632.         }
  633.         irarg=temposha;
  634.         temposha=a.length();
  635.         std::string product=IntToStr(atof(larg.c_str())*atof(rarg.c_str()));
  636.         if (larg.find("-")==0)
  637.         {
  638.             if (product.find("-")!=std::string::npos && product.find("-")>1) product="+"+product;
  639.         }
  640.         a.replace(ilarg,irarg-ilarg+1, product);
  641.         larg="";
  642.         rarg="";
  643.     }
  644.     return a;
  645. }
  646.  
  647. std::string Eval::MULTIPLYORDIVIDE(std::string asdf)
  648. {
  649.     if (asdf.find("/")!=std::string::npos && asdf.find("*")!=std::string::npos)
  650.     {
  651.         if (asdf.find("/")<asdf.find("*"))
  652.         {
  653.             asdf=DIVIDE(asdf);
  654.             asdf=MULTIPLY(asdf);
  655.             return asdf;
  656.         }
  657.         else
  658.         {
  659.             asdf=MULTIPLY(asdf);
  660.             asdf=DIVIDE(asdf);
  661.             return asdf;
  662.         }
  663.     }
  664.     else
  665.     {
  666.         asdf=DIVIDE(asdf);
  667.         asdf=MULTIPLY(asdf);
  668.         return asdf;
  669.     }
  670. }
  671.  
  672.  
  673. std::string Eval::POWER(std::string asdf)
  674. {
  675.     std::string larg,rarg;
  676.     std::string a=asdf;
  677.     int ilarg, irarg;
  678.  
  679.     while (a.find("^")!=std::string::npos)
  680.     {
  681.         int pluspos=a.find("^");
  682.         int temposha=pluspos;
  683.  
  684.         while (pluspos!=0 && isdigit(a[temposha-1]))
  685.         {
  686.             larg = a.substr(temposha-1, 1)+larg;
  687.             if (temposha==0) break;
  688.             temposha--;
  689.         }
  690.         ilarg=temposha;
  691.         temposha=pluspos;
  692.         while (pluspos!=0 && isdigit(a[temposha+1]))
  693.         {
  694.             rarg += a.substr(temposha+1, 1);
  695.             if (temposha==a.length()) break;
  696.             temposha++;
  697.         }
  698.         irarg=temposha;
  699.         temposha=a.length();
  700.         float finalanswer=1;
  701.         int tato=atof(rarg.c_str());
  702.         for (int times=0; times<tato; times++ )
  703.         {
  704.             finalanswer*=atof(larg.c_str());
  705.         }
  706.  
  707.         a.replace(ilarg,irarg-ilarg+1, IntToStr(finalanswer));
  708.         larg="";
  709.         rarg="";
  710.     }
  711.     return a;
  712. }
  713.  
  714. std::string Eval::ADD(std::string asdf)
  715. {
  716.     std::string larg,rarg;
  717.     std::string a=asdf;
  718.     int ilarg, irarg;
  719.  
  720.     while (a.find("+")!=std::string::npos)
  721.     {
  722.         int pluspos=a.find("+");
  723.         int temposha=pluspos;
  724.  
  725.         while (pluspos!=0 && (isdigit(a[temposha-1])||a.substr(temposha-1,1)=="-"||a.substr(temposha-1,1)==".") )
  726.         {
  727.             larg = a.substr(temposha-1, 1)+larg;
  728.             if (temposha==0 || a.substr(temposha-1,1)=="-")
  729.             {
  730.                 if (temposha!=0) temposha-=1;
  731.                 break;
  732.  
  733.             }
  734.             temposha--;
  735.             if (temposha==0) break;
  736.         }
  737.         ilarg=temposha;
  738.         temposha=pluspos;
  739.         while (pluspos!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-" && temposha==pluspos)||a.substr(temposha+1,1)=="."))
  740.         {
  741.             rarg += a.substr(temposha+1, 1);
  742.             if (temposha==a.length()) break;
  743.             temposha++;
  744.         }
  745.         irarg=temposha;
  746.         temposha=a.length();
  747.         float sum=atof(larg.c_str())+atof(rarg.c_str());
  748.  
  749.         a.replace(ilarg,irarg-ilarg+1, IntToStr(sum));
  750.         larg="";
  751.         rarg="";
  752.     }
  753.     return a;
  754. }
  755.  
  756.  
  757.  
  758. std::string Eval::SUBTRACT(std::string asdf)
  759. {
  760.     std::string larg,rarg;
  761.     std::string a=asdf;
  762.     int ilarg, irarg;
  763.     std::string exceptionss=":?~[]{}&$^";
  764.  
  765.     while (a.rfind("-")!=std::string::npos&&a.rfind("-")!=0 &&exceptionss.find(a.substr(a.rfind("-")-1, 1))==std::string::npos)
  766.     {
  767.         int pluspos;
  768.         pluspos=a.find("-");
  769.  
  770.         if (a.find("-")==0||exceptionss.find(a.substr(pluspos-1, 1))!=std::string::npos)pluspos=a.find("-", pluspos+1);
  771.  
  772.         int temposha=pluspos;
  773.  
  774.         while (a.rfind("-")!=0 && (isdigit(a[temposha-1])||(a.substr(temposha-1,1)=="-"&&temposha!=0)))
  775.         {
  776.             larg = a.substr(temposha-1, 1)+larg;
  777.             temposha--;
  778.             if (temposha==0) break;
  779.         }
  780.         ilarg=temposha;
  781.         temposha=pluspos;
  782.         while (a.rfind("-")!=0 && (isdigit(a[temposha+1])||(a.substr(temposha+1,1)=="-"&&temposha==1)))
  783.         {
  784.             rarg += a.substr(temposha+1, 1);
  785.             temposha++;
  786.             if (temposha==a.length()) break;
  787.         }
  788.         irarg=temposha;
  789.         temposha=a.length();
  790.  
  791.         a.replace(ilarg,irarg-ilarg+1, IntToStr((atof(larg.c_str())-atof(rarg.c_str()))));
  792.         larg="";
  793.         rarg="";
  794.     }
  795.     return a;
  796. }
  797.  
  798. std::string Eval::ADDORSUBTRACT(std::string asdf)
  799. {
  800.     if (asdf.find("-")!=std::string::npos && asdf.find("+")!=std::string::npos)
  801.     {
  802.         if (asdf.find("-")<asdf.find("+"))
  803.         {
  804.             asdf=SUBTRACT(asdf);
  805.             asdf=ADD(asdf);
  806.             return asdf;
  807.         }
  808.         else
  809.         {
  810.             asdf=ADD(asdf);
  811.             asdf=SUBTRACT(asdf);
  812.             return asdf;
  813.         }
  814.     }
  815.     else
  816.     {
  817.         asdf=ADD(asdf);
  818.         asdf=SUBTRACT(asdf);
  819.         return asdf;
  820.     }
  821. }
  822.  
  823.  
  824. int Evaluate(std::string strvalue)
  825. {
  826.     Eval Calc(strvalue);
  827.     return atoi(Calc.operation.c_str());
  828. }
  829. std::string stringify(int whatever)
  830. {
  831.     Eval Calc("0");
  832.     return Calc.IntToStr(whatever);
  833. }
  834.  
  835. bool BEvaluate(std::string strvalue)
  836. {
  837.     Eval Calc("0");
  838.     Calc.BEval(strvalue);
  839.     if (atoi(Calc.operation.c_str())==1)
  840.     {
  841.         return true;
  842.     }
  843.     else
  844.     {
  845.         return false;
  846.     }
  847.  
  848. }
  849.  
  850. std::string Evaluayh(std::string strvalue)
  851. {
  852.     Eval Calc(strvalue);
  853.     return Calc.operation;
  854. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement