Commits

Anonymous committed e7e0f0e

playing with boost

Comments (0)

Files changed (5)

c++/learning/Makefile

+CXXFLAGS=-Wall -Werror -pedantic
+
+EXAMPLES=temperature template_foo boost_erf
+all: $(EXAMPLES)
+
+temperature: temperature.o
+	$(CXX) $(CXXFLAGS) -o $@ $^
+
+template_foo: template_foo.o
+	$(CXX) $(CXXFLAGS) -o $@ $^
+
+boost_erf: boost_erf.o
+	$(CXX) $(CXXFLAGS) -o $@ $^
+
+clean:
+	rm -f *.o $(EXAMPLES)

c++/learning/boost_erf.cpp

+#include <boost/math/special_functions/erf.hpp>
+#include <boost/math/special_functions/zeta.hpp>
+#include <iostream>
+#include <vector>
+
+int main () {
+  unsigned int i;
+  double xs[] = {0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
+  std::vector<double> x (xs, xs + sizeof(xs) / sizeof(double));
+  for (i = 0; i < x.size(); i++) {
+    double erfx = boost::math::erf(x[i]);
+    double zetax = boost::math::zeta(x[i]);
+    std::cout << "x: " << x[i] << " erf(x): " << erfx << " zeta(x): " << zetax << "\n";
+  }
+  return 0;
+}

c++/learning/boost_threads.cpp

+#include <boost/thread.hpp>
+#include <boost/math/special_functions/zeta.hpp>
+#include <iostream>

c++/learning/temperature.cpp

+#include <iostream>
+
+using namespace std;
+#define TRIPLE_POINT 273.15
+
+class Temperature {
+  double k;
+public:
+  double getC();
+  double getF();
+  void setC (double);
+  void setF (double);
+};
+
+void Temperature::setC (double c) {
+  k = c + TRIPLE_POINT;
+}
+
+void Temperature::setF (double f) {
+  k = (5.0 / 9.0) * (f - 32.0) + TRIPLE_POINT;
+}
+
+double Temperature::getC () {
+  return (k - TRIPLE_POINT);
+}
+
+double Temperature::getF () {
+  return ((9.0 / 5.0) * (k - TRIPLE_POINT) + 32.0);
+}
+
+int main (int argc, char *argv[]) {
+  double f = 85.0;
+  Temperature t;
+  t.setF(f);
+  cout << t.getC() << "\n";
+  return 0;
+}

c++/learning/template_foo.cpp

+#include <iostream>
+using namespace std;
+
+template <class T>
+T GetMax (T a, T b) {
+  T result = (a>b) ? a : b;
+  return (result);
+}
+
+int main () {
+  int i=5, j=6, k;
+  long l=10, m=5, n; 
+  k=GetMax<int>(i,j);
+  n=GetMax<long>(l,m);
+  cout << k << endl;
+  cout << n << endl;
+  return 0;
+}