source: Sophya/trunk/Eval/COS/vecmapstl.cc@ 1865

Last change on this file since 1865 was 1865, checked in by ansari, 24 years ago

suite test sur classe complex ds vecmapstl.cc - Reza 18/01/2002

File size: 3.6 KB
Line 
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
17using namespace std;
18
19// --------------------------------------------------------------------
20// definition d'une classe de conversion de C (complexes) <> R (reel)
21class convCR {
22public:
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> z) { dv = (double)z.real(); dv_im = (double)z.imag(); }
27 inline convCR(complex<double> 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> z)
31 { dv = (double)z.real(); dv_im = (double)z.imag(); return z; }
32 inline complex<double> operator= (complex<double> 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
45int 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 complex<float> za;
75 complex<double> zb,zc;
76 za = czr1; zb = czr1; zc = czr2;
77 cout << " GNUG_Version: __GNUC__ = " << __GNUC__ << endl;
78 cout << " convCR: za=(zb)=czr1" << za << " zc=" << zc
79 << " zb+zc=" << zb+zc << endl ;
80#else
81 cout << " convCR: czr1(z1)" << (complex<float>)czr1
82 << " czr2(z2)" << (complex<double>)czr2
83 << " czr1+czr2= " << (complex<double>)czr2+(complex<double>)czr1 << endl;
84#endif
85 }
86 catch(exception exc){
87 // Classe de base des exception standard -
88 // La methode what() n'est pas forcement conforme a la norme
89 string msg = exc.what();
90 cerr << " Catched exception : msg= " << msg << endl;
91 rc = 98;
92 }
93 catch (...) {
94 cerr << " Catched Unknown exception ! " << endl;
95 rc = 99;
96 }
97
98 cout << " ::::::::: Exiting from vecmapstl.cc ::::::::: \n" << endl;
99 exit(rc);
100}
101
102
103
104
105
106
107
108
Note: See TracBrowser for help on using the repository browser.