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

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

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

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