[1571] | 1 | #include <stdlib.h>
|
---|
| 2 | #include <stdio.h>
|
---|
| 3 |
|
---|
| 4 | #include <exception>
|
---|
| 5 | #include <string>
|
---|
| 6 |
|
---|
| 7 | #include <iostream.h>
|
---|
| 8 | #include <vector>
|
---|
| 9 | #include <map>
|
---|
[1863] | 10 | #include <complex>
|
---|
[1571] | 11 |
|
---|
[1863] | 12 | // :::::::::::::::::::::::::::::::::::::::::::::::::::::
|
---|
| 13 | // Test du support de la STL par le compilateur
|
---|
| 14 | // string, vector<T>, map<T>, complex<T> exception
|
---|
| 15 | // :::::::::::::::::::::::::::::::::::::::::::::::::::::
|
---|
| 16 |
|
---|
[1571] | 17 | using namespace std;
|
---|
| 18 |
|
---|
[1863] | 19 | // --------------------------------------------------------------------
|
---|
| 20 | // definition d'une classe de conversion de C (complexes) <> R (reel)
|
---|
| 21 | class convCR {
|
---|
| 22 | public:
|
---|
| 23 | inline convCR() { dv = 0.; dv_im = 0.; }
|
---|
| 24 | inline convCR(float v) { dv = (double)v; dv_im = 0.; }
|
---|
| 25 | inline convCR(double v) { dv = v; dv_im = 0.; }
|
---|
[1866] | 26 | inline convCR(complex<float> const & z) { dv = (double)z.real(); dv_im = (double)z.imag(); }
|
---|
| 27 | inline convCR(complex<double> const & z) { dv = z.real(); dv_im = z.imag(); }
|
---|
[1863] | 28 | inline float operator= (float v) { dv = (double)v; dv_im = 0.; return v; }
|
---|
| 29 | inline double operator= (double v) { dv = v; dv_im = 0.; return v; }
|
---|
[1866] | 30 | inline complex<float> operator= (complex<float> const & z)
|
---|
[1863] | 31 | { dv = (double)z.real(); dv_im = (double)z.imag(); return z; }
|
---|
[1866] | 32 | inline complex<double> operator= (complex<double> const & z)
|
---|
[1863] | 33 | { dv = z.real(); dv_im = z.imag(); return z; }
|
---|
[1571] | 34 |
|
---|
[1863] | 35 | inline operator float() const { return((float)dv); }
|
---|
| 36 | inline operator double() const { return(dv); }
|
---|
| 37 | inline operator complex<float>() const { return(complex<float>((float)dv, (float)dv_im)); }
|
---|
[1867] | 38 | inline operator complex<double>() const { return(complex<double>(dv, dv_im)); }
|
---|
| 39 | inline complex<float> ToComplexF() const { return(complex<float>((float)dv, (float)dv_im)); }
|
---|
| 40 | inline complex<double> ToComplexD() const { return(complex<double>(dv, dv_im)); }
|
---|
[1863] | 41 | double dv;
|
---|
| 42 | double dv_im; /* for holding imaginary part of a complex */
|
---|
| 43 | };
|
---|
| 44 | // --------------------------------------------------------------------
|
---|
| 45 |
|
---|
[1571] | 46 | int main(int narg, char *arg[])
|
---|
| 47 | {
|
---|
[1863] | 48 | cout << "\n :::::::: Test complex / vector et map de la stl :::::::: \n" ;
|
---|
[1571] | 49 | int rc = 0;
|
---|
| 50 | try {
|
---|
| 51 | vector<int> vi;
|
---|
| 52 | int k;
|
---|
| 53 | for(k=0; k<5; k++) vi.push_back(k*5);
|
---|
[1863] | 54 | cout << " -----> vector<int> vi.size() = " << vi.size() << endl;
|
---|
[1571] | 55 | for(k=0; k<vi.size(); k++)
|
---|
| 56 | cout << "vi[" << k << "]= " << vi[k] << endl;
|
---|
| 57 | map<string, int> msi;
|
---|
| 58 | msi["Un"] = 1;
|
---|
| 59 | msi["Deux"] = 2;
|
---|
| 60 | msi["Cinq"] = 5;
|
---|
| 61 | msi["Douze"] = 12;
|
---|
| 62 | map<string, int>::iterator it;
|
---|
[1863] | 63 | cout << " -----> map<string, int> msi.size() = " << msi.size() << endl;
|
---|
[1571] | 64 | for(it=msi.begin(); it!=msi.end(); it++)
|
---|
| 65 | cout << " S= " << (*it).first << " I=" << (*it).second << endl;
|
---|
[1863] | 66 | cout << " ----> complex numbers and convCR : " << endl;
|
---|
| 67 | complex<float> z,z1(2,2),z2(3,-1);
|
---|
| 68 | complex<double> zd,zd1(2.5,2.5),zd2(3.3,-1);
|
---|
| 69 | cout << " complex<float>: z1=" << z1 << " z2=" << z2
|
---|
| 70 | << " z1+z2=" << z1+z2 << endl;
|
---|
| 71 | cout << " complex<double>: z1=" << zd1 << " z2=" << zd2
|
---|
| 72 | << " zd1+zd2=" << zd1+zd2 << endl;
|
---|
| 73 | convCR czr, czr1(z1), czr2(zd2), czr3;
|
---|
[1866] | 74 | /* if defined(__GNUG__) && (__GNUC__ < 3) */
|
---|
| 75 | #if defined(__GNUG__)
|
---|
[1865] | 76 | complex<float> za;
|
---|
| 77 | complex<double> zb,zc;
|
---|
[1867] | 78 | za = czr1.ToComplexF(); zb = czr1.ToComplexD(); zc = czr2.ToComplexD();
|
---|
[1865] | 79 | cout << " GNUG_Version: __GNUC__ = " << __GNUC__ << endl;
|
---|
| 80 | cout << " convCR: za=(zb)=czr1" << za << " zc=" << zc
|
---|
[1866] | 81 | << " zb+zc=" << zb+zc << endl ;
|
---|
[1865] | 82 | #else
|
---|
| 83 | cout << " convCR: czr1(z1)" << (complex<float>)czr1
|
---|
[1863] | 84 | << " czr2(z2)" << (complex<double>)czr2
|
---|
| 85 | << " czr1+czr2= " << (complex<double>)czr2+(complex<double>)czr1 << endl;
|
---|
[1865] | 86 | #endif
|
---|
[1571] | 87 | }
|
---|
| 88 | catch(exception exc){
|
---|
| 89 | // Classe de base des exception standard -
|
---|
| 90 | // La methode what() n'est pas forcement conforme a la norme
|
---|
| 91 | string msg = exc.what();
|
---|
| 92 | cerr << " Catched exception : msg= " << msg << endl;
|
---|
| 93 | rc = 98;
|
---|
| 94 | }
|
---|
| 95 | catch (...) {
|
---|
| 96 | cerr << " Catched Unknown exception ! " << endl;
|
---|
| 97 | rc = 99;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[1863] | 100 | cout << " ::::::::: Exiting from vecmapstl.cc ::::::::: \n" << endl;
|
---|
[1571] | 101 | exit(rc);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|