[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.cpp student2.h

Chris Lattner lattner at cs.uiuc.edu
Mon Oct 4 13:01:28 PDT 2004



Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1:

student2.cpp added (r1.1)
student2.h added (r1.1)
---
Log message:

Initial checkin of all of the source


---
Diffs of the changes:  (+194 -0)

Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.cpp
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.cpp:1.1
*** /dev/null	Mon Oct  4 15:01:23 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.cpp	Mon Oct  4 15:01:13 2004
***************
*** 0 ****
--- 1,94 ----
+ // Written for undergraduate C++ course project at Dept of
+ // Computer Science, Rutgers University.
+ 
+ #include <stream.h>
+ #include <string.h>
+ #include "student2.h"
+ void bin_op_expr::print_me()
+ {
+   cout << "(";
+   first->print_me();
+   cout << op_name;
+   second->print_me();
+   cout << ")";
+ }
+ 
+ double const_expr::eval(double at)
+ {
+   return value;
+ }
+ 
+ expr *const_expr::deriv(strng var)
+ {
+   return (new const_expr(0));
+ }
+ 
+ double var_expr::eval(double at)
+ {
+   return at;
+ }
+ 
+ expr *var_expr::deriv(strng var)
+ {
+   if (1) return (new const_expr(1)); // was strcmp between var and name
+   else return (new var_expr(name));
+   
+ }
+ 
+ double prod_expr::eval(double at)
+ {
+   return (first->eval(at) * second->eval(at));
+ }
+ 
+ expr *prod_expr::deriv(strng var)
+ {
+   return new sum_expr(new prod_expr(first, second->deriv(var)),new prod_expr(second, first->deriv(var)));
+ }
+ 
+ double sum_expr::eval(double at)
+ {
+   return (first->eval(at) + second->eval(at));
+ }
+ 
+ expr *sum_expr::deriv(strng var)
+ {
+ expr *f;
+   return new sum_expr(f=first->deriv(var), second->deriv(var));
+ }
+ 
+ double quotient_expr::eval(double at)
+ {
+   return (first->eval(at) / second->eval(at));
+ }
+ 
+ expr *quotient_expr::deriv(strng var)
+ {
+   return new quotient_expr(new sum_expr(new prod_expr(second, first->deriv(var)), new prod_expr(new const_expr(-1), new prod_expr(first, second->deriv(var)))), new prod_expr(second, second));
+ }
+ 
+ main() {
+  const_expr c(8);
+  var_expr x("x");
+  prod_expr simple(new const_expr(123.45), new var_expr("y"));
+ 
+  cout << "c is ";
+  c.print_me();
+  cout << "\n      and its value at 3 is: " <<
+  c.eval(3);
+  cout << "\n      and its derivative with respect to x is: ";
+  c.deriv("x")->print_me();
+  cout << "\nx is ";
+  x.print_me();
+  cout << "\n      and its value at 3 is: " <<
+  x.eval(3);
+  cout << "\n      and its derivative with respect to x is: ";
+  x.deriv("x")->print_me();
+  cout << "\nsimple is ";
+  simple.print_me();
+  cout << "\n     and its value at 3 is: " <<
+  simple.eval(3);
+  cout << "\n     and its derivative with respect to y is: ";
+  simple.deriv("y")->print_me();
+  cout << "\n     and its derivative with respect to x is: ";
+  simple.deriv("x")->print_me();
+  }


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.h:1.1
*** /dev/null	Mon Oct  4 15:01:28 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv1/student2.h	Mon Oct  4 15:01:13 2004
***************
*** 0 ****
--- 1,100 ----
+ /* this is file class-defs.h provided to students */
+ 
+ //extern "C" {
+ //char *strcpy(char *nam, char *str);
+ //}
+ 
+ typedef char *strng;
+ 
+ /* class expr is the top of the class heirarchy for expressions. Any
+    expression object will be an instance of some derived class which has
+    class expr as its base class (or as the base class of its base class...)
+ */
+ class expr {
+  public:
+   expr(){}
+   ~expr(){}
+   virtual void print_me()     = 0;
+ 
+      /* This is the function that an expression object uses to print itself.
+       The version given here should never be run - the print function should
+       be redefined in the derived class */
+ 
+   virtual expr *deriv(char *var)  = 0;
+      /* This is the function that an expression object uses to compute its
+       derivative with respect to "var". The version given here should never
+       be run - deriv() should be redefined in the derived class */
+ 
+   virtual double eval(double at)  = 0;
+    /* This is the function that an expression object uses to compute its
+       value when all variables in it are replaced by the value of "at"
+       */
+ 
+ };
+      
+      
+ /* an expression which is a numerical constant */
+ class const_expr : public expr {
+   double value;
+  public:
+   const_expr(double v) {value = v;}
+   ~const_expr(){}
+   void print_me() {} // {printf(" %d ", value);}
+   double eval(double at);
+   expr *deriv(strng var);
+  };
+ 
+ /* an expression which is a single variable */
+ class var_expr : public expr {
+ private:
+   char *name;
+  public:
+   var_expr(strng str) {strcpy(name,str);}
+ 			/* Copy the initialization string
+                        into the name buffer. To read the documentation for
+                        strcpy() and other C string manipulation functions,
+                        type the Unix shell command "man string" */
+   ~var_expr(){}
+   void print_me()  {} // {printf(" %s ", name);}
+   double eval(double at);
+   expr *deriv(strng var);
+  };
+ 
+ /* expression resulting from applying a binary operator to two expressions */
+ class bin_op_expr : public expr {
+  public:
+   bin_op_expr(expr *e1, expr *e2) {first = e1; second = e2;}
+   ~bin_op_expr(){}
+   void print_me();
+  protected:
+   expr *first;
+   expr *second;
+   char op_name;
+  };
+ 
+ /* an expression which is the sum of two expressions */
+ class sum_expr : public bin_op_expr {
+  public:
+   sum_expr(expr *e1, expr *e2) : bin_op_expr(e1,e2) {op_name = '+';}
+   ~sum_expr(){}
+   double eval(double at);
+   expr *deriv(strng var);
+  };
+ 
+ /* an expression which is the product of two expressions */
+ class prod_expr : public bin_op_expr {
+  public:
+   prod_expr(expr *e1, expr *e2) : bin_op_expr(e1,e2) {op_name = '*';}
+   ~prod_expr(){}
+   double eval(double at);
+   expr *deriv(strng var);
+  };
+ 
+ /* an expression which is the quotient of two expressions */
+ class quotient_expr : public bin_op_expr {
+  public:
+   quotient_expr(expr *e1, expr *e2) : bin_op_expr(e1,e2) {op_name = '/';}
+   ~quotient_expr(){}
+   double eval(double at);
+   expr *deriv(strng var);
+  };






More information about the llvm-commits mailing list