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