All pastes #512269 Raw Edit

piromanas

public cpp v1 · immutable
#512269 ·published 2007-05-26 20:31 UTC
rendered paste body
#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <cmath>#include <stdlib.h>#include <list> using namespace std;struct function {    char const * name ;    double (* call)(double,double) ;    int prior ; } ;struct node {       node *desne, *kaire;       function const *operation;       float value;       int prior ;       node() : desne(NULL), kaire(NULL), operation(0), value(0) {};       };      double cos_     (double a,double)       {return cos (a) ;}    double sin_     (double a,double)       {return sin (a) ;}double abs_     (double a,double)       {return abs (a) ;}double tan_     (double a,double)       {return cos (a) ;}double arccos_  (double a,double)       {return acos (a) ;}double arcsin_  (double a,double)       {return asin (a) ;}double arctan_  (double a,double)       {return atan (a) ;}double exp_     (double a,double)       {return exp (a) ;}double ln_      (double a,double)       {return log (a) ;}double log_     (double a,double)       {return log10 (a) ;}double sqrt_    (double a,double)       {return sqrt (a) ;}double add      (double a, double b)    {return a+b ;}double sub      (double a, double b)    {return a-b ;}double mul      (double a, double b)    {return a*b ;}double div      (double a, double b)    {return a/b ;}const function table [] = {    "cos",&cos_,4,    "sin",&sin_,4,    "abs",&abs_,4,    "tan",&tan_,4,    "arccos",&arccos_,4,    "arcsin",&arcsin_,4,    "arctan",&arctan_,4,    "exp",&exp_,4,    "ln",&ln_,4,    "log",&log_,4,    "sqrt",&sqrt_,4,    "+",&add,1,    "-",&sub,1,    "*",&mul,2,    "/",&div,2,    "^",&pow,3  } ;function const * find_op (char const * name) {    for (function const *i = table; i < table+sizeof(table)/sizeof(table[0]); ++i) {        if (string(i->name) == name) {            return i;        }    }    return NULL;}    /*template <typename Iterator> void F2(string & str, Iterator begin, Iterator end){         Iterator i, temp, i1, beg;         double x, d;         string s, ss;         for ( ;; ) {              int count = 0;             for (i = str.begin ();i != str.end ();++i) {                  if( isalpha(*i) ) {                        ++count;                        beg = i;                        while ((i != end) && (isalpha(*i))) ++i;                        s = string (beg,i);                        temp = i;                        while ((i != end) && (*i != ')')) ++i;                         ss = string(temp+1,i);                        if (isalpha(*(temp+1))) {                        //F2(ss,ss.begin(),ss.end());                        }/*                        F1(ss,ss.begin(),ss.end());                        F(ss.begin(),ss.end());                        x = atof(ss.c_str ());                        for (int a = 0;a < sizeof(table)/sizeof (table [0]);++a)                        {                         if (!strcmp (table [a].name,s.c_str()))                         d = table [a].call(x) ;                        }                        ostringstream sss;                        sss << d ;                        string s1 = sss.str();                        str.replace (beg,i+1,s1) ;                        break;                 }             }             if (count == 0) return ;         }    } template <typename Iterator> void F1(string & str, Iterator begin, Iterator end){         Iterator i, temp, i1;         float f;         for ( ;; ) {              int count = 0;             for (i = str.begin ();i != str.end ();++i) {                  if (*i == '(') {                        ++count;                        temp = i;                 }                 else if (*i == ')') {                      ++count;                      f=F(temp+1,i);                      ostringstream ss;                      ss << f ;                      string s = ss.str();                      str.replace (temp,i+1,s) ;                      break;                 }             }             if (count == 0) return ;         }}       template <typename Iterator>     node *F(Iterator begin, Iterator end) {             Iterator i ;        node *medis = new node ;        if ((i = find (begin,end,'+')) != end) {            medis->operation = find_op("+") ;            medis->kaire = F (begin,i) ;             medis->desne = F (i+1,end) ;            return medis ;        }else if ((i = find (begin,end,'-')) != end) {            medis->operation = find_op("-") ;            medis->kaire = F (begin,i) ;             medis->desne = F (i+1,end) ;            return medis ;        }else if ((i = find (begin,end,'*')) != end) {            medis->operation = find_op("*") ;            medis->kaire = F (begin,i) ;             medis->desne = F (i+1,end) ;            return medis ;        }else if ((i = find (begin,end,'/')) != end) {            medis->operation = find_op("/") ;            medis->kaire = F (begin,i) ;            medis->desne = F (i+1,end) ;            return medis ;        }else if ((i = find (begin,end,'^')) != end) {            medis->operation = find_op("^") ;            medis->kaire = F (begin,i) ;             medis->desne = F (i+1,end) ;            return medis ;          }else {             string str1 (begin,end) ;            istringstream stream (str1) ;            float i ;            stream >> i ;            medis->value = i ; 	        return medis ;                      }    }*/        template<typename iterator>void parse (iterator begin,iterator end,list<node *> & output) {    string::iterator i = begin, i1 ;    while (i != end) {        if (*i >= '0' && *i <= '9' && i != end) {            i1 = i ;            while (*i >= '0' && *i <= '9' && i != end) {                ++i ;            }            node* n = new node ;            string str1 (i1,i-1) ;            istringstream stream (str1) ;            float f ;            stream >> f ;            n->value = f ;            output.push_back(n) ;        }        else            if ( isalpha(*i) && i != end ) {                i1 = i ;                while ( isalpha(*i) && i != end ) {                    ++i ;                }                node* n = new node ;                string str1 (i1,i-1) ;                n->operation = find_op(str1.c_str()) ;                output.push_back(n) ;            }        else                if ( (*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') && i != end ) {                node* n = new node ;                n->operation = find_op(*i) ;                output.push_back(n) ;                 ++i ;            }    }            }/*template<typename iterator> void set_default(iterator begin, iterator end, list<node*> & output ) {    list<node*>::iterator i = begin;    while(i != end) {  */      float G (node *root) {        if (root) {        if (root->operation)             return root->operation->call (G (root->kaire), G (root->desne));        else             return root->value ;    }  } int main() {    string str,str1;    node *a;    cout << '#';    getline(cin,str);    //F2(str,str.begin(),str.end());    //F1(str,str.begin(),str.end());    //a=F(str.begin(),str.end());    //cout << "Ats: " << G(a) << endl;    cin.get();}