Ukryta treść:
Kod: Zaznacz cały
#include <iostream> //( 1)
#include <stdexcept> //( 2)
#include <cmath> //( 3)
using namespace std; //( 4)
const int d=2; //( 5)
/******************** Poly ******************/ //( 6)
template<typename S> //( 7)
class Poly{ //( 8)
protected: //( 9)
S coord[d+1]; //(10)
int degree; //(11)
/******************* PolyAux *****************/ //(12)
class PolyAux : private Poly{ //(13)
friend class Poly; //(14)
const S* first; //(15)
PolyAux(const S* A_first):first(A_first){} //(16)
S operator()(const S& x,int d) const{ //(17)
if(d<=0){ //(18)
return *first; //(19)
}else{ //(20)
return *first+x*(PolyAux(first+1))(x,d-1); //(21)
} //(22)
} //(23)
public: //(24)
PolyAux& operator,(S s){ //(25)
cout<<"operator,
";
if(this->degree>=d){ //(26)
throw out_of_range("Zly stopien"); //(27)
} //(28)
this->coord[++this->degree]=s; //(29)
return *this; //(30)
} //(31)
}; //(32)
public: //(33)
Poly():degree(-1){} //(34)
PolyAux& operator=(S s){ //(35)
degree=-1; //(36)
coord[++degree]=s; //(37)
PolyAux* ppt= //(38)
reinterpret_cast<PolyAux*>(this); //(39)
return *ppt; //(40)
} //(41)
S operator()(const S& x) const{ //(42)
if(degree<0){ //(43)
throw string("Brak wspolczynnikow"); //(44)
} //(45)
return PolyAux(coord)(x,degree); //(46)
} //(47)
static Poly deriv(const Poly& p){ //(48)
Poly pd; //(49)
for(int i=1;i<=p.degree;++i){ //(50)
(*reinterpret_cast<PolyAux*>(&pd)), //(51)
i*p.coord[i]; //(52)
} //(53)
return pd; //(54)
} //(55)
}; //(56)
static int cnt1=0,cnt2=0; //(57)
/********************* Newton ******************/ //(58)
template<typename S> //(59)
class Newton{ //(60)
int limit; //(61)
S accuracy; //(62)
S oneStep(const Poly<S>& p, S x){ //(63)
S y=p.deriv(p)(x); //(64)
if(!y) throw 0; //(65)
return x-p(x)/y;; //(66)
} //(67)
S findZero(const Poly<S>& p, S x){ //(68)
for(int i=0;i<limit;++i){ //(69)
S val=p(x); //(70)
if(val<accuracy && -val<accuracy) return x; //(71)
x=oneStep(p,x); //(72)
} //(73)
throw 0.0; //(74)
} //(75)
public: //(76)
Newton(int li,S ac):limit(li),accuracy(ac){} //(77)
S findZero(const Poly<S>& p, S s0, S s1){ //(78)
for(S s=s0;s<=s1;++s){ //(79)
try{ //(80)
return findZero(p,s); //(81)
}catch(double){ //(82)
++cnt1; //(83)
}catch(int){ //(84)
++cnt2; //(85)
} //(86)
} //(87)
throw 'Z'; //(88)
} //(89)
}; //(90)
/***************** f ****************/ //(91)
void f(){ //(92)
cout << "Mamy problem" << endl; //(93)
exit(0); //(94)
}/************ test_int ****************/ //(95)
void test_int(){ //(96)
Poly<int> p; //(97)
p=-8,-2,1; //(98)
Newton<int> n(5,1); //(99)
cout << "Zero w " << n.findZero(p,1,3) << endl; //(100)
}/*********** test_double ****************/ //(101)
void test_double(){ //(102)
Poly<double> p; //(103)
p=-8,-2,1; //(104)
Newton<double> n(5,1); //(105)
cout << "Zero w " << n.findZero(p,1,3) << endl; //(106)
}/**********************************************/ //(107)
int main(){ //(108)
set_terminate(f); //(109)
try{ //(110)
test_int(); //(111)
cout << " cnt1=" << cnt1 << //(112)
" cnt2=" << cnt2 << endl; //(113)
}catch(int){ //(114)
cout << "Zlapano int" << endl; //(115)
}catch(double){ //(116)
cout << "Zlapano double" << endl; //(117)
}catch(string){ //(118)
cout << "Zlapano string" << endl; //(119)
} //(120)
cin.ignore();
}/**********************************************/ //(121)
Kod: Zaznacz cały
Poly<int> p; //(97)
p=-8,-2,1; //(98)