/* * templates_prog.cpp * * Created on: 16.04.2013 * Author: trifon */ #include using namespace std; #include "point.cpp" #include "rational.h" #include "stack.cpp" template void swap2(T& a, T& b) { T tmp = a; a = b; U x = 5; cout << "x = " << x << endl; b = tmp; } void swap2(int& a, int& b) { cout << "Специална размяна!" << endl; a += b; b = a - b; a -= b; } template void reverse(T* a, int n) { for(int i = 0; i < n/2; i++) swap2(a[i], a[n-i-1]); } template int readArray(T* a, int max) { int n; do { cout << "Въведете брой: "; cin >> n; } while (n < 0 || n >= max); // 0 <= n < max for(int i = 0; i < n; i++) { cout << "a[" << i << "] = "; cin >> a[i]; } return n; } /* template Point::Point(Point const& p) { ... } */ int main1() { int x = 2, y = 3; swap2(x, y); // !!! swap2(x,y); cout << x << ' ' << y << endl; swap2(x, y); cout << x << ' ' << y << endl; int a[10] = {0,1,2,3,4,5,6,7,8,9}; reverse(a, readArray(a, 10)); double b[10]; readArray(b, 10); double z = 4, t = 5; // !!! swap2(x, z); // !!! swap2(z, t); swap2(z, t); cout << a[1] << endl; return 0; } Rational operator*(Rational const& r1, Rational const& r2) { return Rational(r1.getNumerator() * r2.getNumerator(), r1.getDenominator() * r2.getDenominator()); } Rational operator+(Rational const& r1, Rational const& r2) { return add(r1,r2); } Rational operator-(Rational const& r1, Rational const& r2) { Rational tmp(-r2.getNumerator(), r2.getDenominator()); return r1 + tmp; } template<> double Point::distance(Point const& p) const { Rational x2 = (p.x - x)*(p.x - x); Rational y2 = (p.y - y)*(p.y - y); Rational r = x2 + y2; return sqrt((double)r.getNumerator()/r.getDenominator()); } /* ostream& operator<<(ostream& os, Point const& p) { return os << '(' << p.x << ',' << p.y << ')' << endl; } ostream& operator<<(ostream& os, Point const& p) { return os << '(' << p.x << ',' << p.y << ')' << endl; }*/ int main2() { Point p; p.getX().print(); p.getY().print(); cout << p.distance( Point( Rational(3,2), Rational(2,1) )) << endl; cout << p; Point ip1(1,2), ip2(3,4); cout << ip1.getX() << ' ' << ip1.getY() << endl; cout << ip1.distance(ip2) << endl; cout << ip1; return 0; } int main() { Stack s; for(int i = 0; i < 10; i++) s.push(i/2.0); Stack s2 = s; Stack > ss; ss.push(s); ss.push(s2); ss.print(); cout << s2.length() << endl; s2 = s; double x; s2.pop(x); s2.print(); s2 = s; s2.print(); s.print(); return 0; }