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

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

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

File size: 3.3 KB
RevLine 
[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]17using namespace std;
18
[1863]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; }
[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)); }
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
[1571]45int main(int narg, char *arg[])
46{
[1863]47 cout << "\n :::::::: Test complex / vector et map de la stl :::::::: \n" ;
[1571]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);
[1863]53 cout << " -----> vector<int> vi.size() = " << vi.size() << endl;
[1571]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;
[1863]62 cout << " -----> map<string, int> msi.size() = " << msi.size() << endl;
[1571]63 for(it=msi.begin(); it!=msi.end(); it++)
64 cout << " S= " << (*it).first << " I=" << (*it).second << endl;
[1863]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 cout << " convCR: czr1(z1)" << (complex<float>)czr1
74 << " czr2(z2)" << (complex<double>)czr2
75 << " czr1+czr2= " << (complex<double>)czr2+(complex<double>)czr1 << endl;
[1571]76 }
77 catch(exception exc){
78 // Classe de base des exception standard -
79 // La methode what() n'est pas forcement conforme a la norme
80 string msg = exc.what();
81 cerr << " Catched exception : msg= " << msg << endl;
82 rc = 98;
83 }
84 catch (...) {
85 cerr << " Catched Unknown exception ! " << endl;
86 rc = 99;
87 }
88
[1863]89 cout << " ::::::::: Exiting from vecmapstl.cc ::::::::: \n" << endl;
[1571]90 exit(rc);
91}
92
93
94
95
96
97
98
99
Note: See TracBrowser for help on using the repository browser.