Page suivante Page précédente Table des matières

14. Annexe A example_String.cpp

Vous pouvez récupérer tous les programmes en un seul tar.gz sur Telecharger String . Pour obtenir ce fichier, dans un butineur web, sauvez ce fichier en type 'texte'.


//
// Auteur : Al Dev
// Utilisez la classe string ou cette classe
//
// Pour éviter les fuites de mémoire, une classe caractère qui gere les
// variables caractère.
// Préférez toujours l'utilisation de la classe string à char[] et char *.
//

// Pour compiler et tester ce programme 
// (en supposant que libString.a est dans le repertoire courant)
//              g++ example_String.cpp -L. -lString

#include <stdlib.h>  // pour putenv
#include "String.h"
//#include <string>  // ceci se trouve dans /usr/include/g++-2/string
//#include <cstring> // ceci se trouve dans /usr/include/g++-2/cstring et inclut /usr/include/strings.h
//
void java_string();
void java_string_buffer();
void java_string_to_numbers();
void java_string_tokenizer();
void java_string_reader();
void java_string_writer();

/////////////////////////////////////////////////
// Un exemple de programme pour présenter String 
// Note : dans cet exemple, je n'ai pas du tout
// utilisé les fonctions de manipulation de la
// memoire comme new, delete, malloc, strdup.
// La classe String gère cela automatiquement !
/////////////////////////////////////////////////

int main(int argc, char **argv)
{
        //char p_name[1024];
        //sprintf(p_name, "PROGRAM_NAME=%s", argv[0]);
        //putenv(p_name);
        //print_total_memsize(); // au debut

        java_string();
        java_string_buffer();
        java_string_to_numbers();
        java_string_tokenizer();
        java_string_reader();
        java_string_writer();

        //print_total_memsize(); // à la fin
}

// Exemple de démonstration de l'imitation de la classe
// Java String
void java_string()
{
        String aa, bb, egg;
        char  tmpaa[100];

        //bb.str_cpy("  bbSTRing  ");
        bb = "   bbSTRing   ";

        // Test de l'opérateur + à droite
        // aa + " droite "; // Aucun affichage ici !
        // Vous devez l'utiliser directement avec fprintf comme suit
        fprintf(stdout, "1) aa.val() is :%sEOF\n", (aa + " my rhs " ).val());

        // Test de l'opérateur =
        aa = " gauche " ;
        fprintf(stdout, "2) With operator= aa.val() is :%sEOF\n", aa.val());

        // Test de l'opérateur + à gauche
        // " gauche " + aa; // Aucun affichage ici !
        // Vous devez l'utiliser directement avec fprintf comme suit
        fprintf(stdout, "3) With lsh operator+, aa.val() is :%sEOF\n", (" my lhs " + aa ).val());

        // ***************** Fonctions à la Java ********************
        aa = "Some Value 2345";
        fprintf(stdout, "4) aa.charAt() is :%c %sEOF\n", aa.charAt(3), aa.val());

        aa = "Some Value 2345";
        strcpy(tmpaa, "tmpaa value");
        aa.getChars(3, 8, tmpaa, 2);
        fprintf(stdout, "5) aa.getChars() is : %s %sEOF\n", tmpaa, aa.val());

        aa = "Some Value 2345";
        fprintf(stdout, "6) aa.toCharArray() is : %sEOF\n", aa.toCharArray());

        aa = "Some2345";
        if (aa.equals("Some2345"))
                fprintf(stdout, "7) aa.equals() is true : %sEOF\n", aa.val());
        else
                fprintf(stdout, "7) aa.equals() is false : %sEOF\n", aa.val());

        aa = "testinglettercase";
        egg = "TestingLetterCase";
        if (aa.equalsIgnoreCase(egg))
                fprintf(stdout, "8) egg equals aa (case insensitive) aa.val is :%sEOF\n", aa.val());
        else
                fprintf(stdout, "8) egg not equals aa (case insensitive) aa.val is :%sEOF\n", aa.val());

        aa = "kkktestinglettercase";
        egg = "abtestingLetterCase";
        if (aa.regionMatches(true, 3, egg, 2, 7))
                fprintf(stdout, "9) regionMatches is true aa.val is :%sEOF\n", aa.val());
        else
                fprintf(stdout, "9) regionMatches is false aa.val is :%sEOF\n", aa.val());

        //aa.str_cpy(bb.val());
        aa = bb + "Some Value 2345";
        egg = aa.toUpperCase();
        fprintf(stdout, "10) egg.val is :%sEOF\n", egg.val());

        aa = bb + "Some Value 2345";
        egg = aa.toLowerCase();
        fprintf(stdout, "11) egg.val is :%sEOF\n", egg.val());

        aa = "Some Value 2345";
        egg = "Some";
        if (aa.startsWith("Some"))
        //if (aa.startsWith(egg))
                fprintf(stdout, "12) aa.startsWith() is true :%sEOF\n", aa.val());
        else
                fprintf(stdout, "12) aa.startsWith() is false :%sEOF\n", aa.val());

        aa = "Some Value 2345";
        egg = " 2345";
        if (aa.endsWith(" 2345"))
        //if (aa.endsWith(egg))
                fprintf(stdout, "13) aa.endsWith() is true :%sEOF\n", aa.val());
        else
                fprintf(stdout, "13) aa.endsWith() is false :%sEOF\n", aa.val());

        aa = "bbb Some Value 2345";
        egg = "caabc";
        if (aa.compareTo(egg) == 0)
                fprintf(stdout, "14) aa.compareTo() is zero :%sEOF\n", aa.val());
        else
        if (aa.compareTo(egg) > 0)
                fprintf(stdout, "14) aa.compareTo() is greater :%sEOF\n", aa.val());
        else
        if (aa.compareTo(egg) < 0)
                fprintf(stdout, "14) aa.compareTo() is less than :%sEOF\n", aa.val());

        aa = "bbb Some Value 2345";
        strcpy(tmpaa, "aabbb Some Value 2345");
        if (aa.compareTo(tmpaa) == 0)
                fprintf(stdout, "15) aa.compareTo() is zero :%sEOF\n", aa.val());
        else
        if (aa.compareTo(tmpaa) > 0)
                fprintf(stdout, "15) aa.compareTo() is greater :%sEOF\n", aa.val());
        else
        if (aa.compareTo(tmpaa) < 0)
                fprintf(stdout, "15) aa.compareTo() is less than :%sEOF\n", aa.val());

        aa = "bbb Some Value 2345";
        //egg = "bbb Some Value 2345";
        egg = "CCaabc";  // change values to caabc, aabc
        if (aa.compareToIgnoreCase(egg) == 0)
                fprintf(stdout, "16) aa.compareToIgnoreCase() is zero :%sEOF\n", aa.val());
        else
        if (aa.compareToIgnoreCase(egg) > 0)
                fprintf(stdout, "16) aa.compareToIgnoreCase() is greater :%sEOF\n", aa.val());
        else
        if (aa.compareToIgnoreCase(egg) < 0)
                fprintf(stdout, "16) aa.compareToIgnoreCase() is less than :%sEOF\n", aa.val());

        aa = "bbb Some Value 2345";
        //strcpy(tmpaa, "bbb Some Value 2345");
        strcpy(tmpaa, "CAABbb Some Value 2345"); // change value to caabb, aab
        if (aa.compareToIgnoreCase(tmpaa) == 0)
                fprintf(stdout, "17) aa.compareToIgnoreCase() is zero :%sEOF\n", aa.val());
        else
        if (aa.compareToIgnoreCase(tmpaa) > 0)
                fprintf(stdout, "17) aa.compareToIgnoreCase() is greater :%sEOF\n", aa.val());
        else
        if (aa.compareToIgnoreCase(tmpaa) < 0)
                fprintf(stdout, "17) aa.compareToIgnoreCase() is less than :%sEOF\n", aa.val());

        aa = "bbb Some Value 2345";
        strcpy(tmpaa, "Some");
        egg = "Value";
        fprintf(stdout, "18) aa.indexOf('S') %d :%sEOF\n", aa.indexOf('S'),  aa.val());
        fprintf(stdout, "18) aa.indexOf(tmpaa) %d :%sEOF\n", aa.indexOf(tmpaa),  aa.val());
        fprintf(stdout, "18) aa.indexOf(egg) %d :%sEOF\n", aa.indexOf(egg),  aa.val());

        aa = "bbb Some Value Some 2345";
        strcpy(tmpaa, "Some");
        egg = "Some";
        fprintf(stdout, "19) aa.lastIndexOf('S') %d :%sEOF\n", aa.lastIndexOf('S'),  aa.val());
        fprintf(stdout, "19) aa.lastIndexOf(tmpaa) %d :%sEOF\n", aa.lastIndexOf(tmpaa),  aa.val());
        fprintf(stdout, "19) aa.lastIndexOf(egg) %d :%sEOF\n", aa.lastIndexOf(egg),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "20) aa.substring(5) %s :%sEOF\n", 
                        aa.substring(5).val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        strcpy(tmpaa, "Some");
        egg = "Some";
        fprintf(stdout, "20) aa.replace('S', 'V') %s :%sEOF\n", 
                        aa.replace('S', 'V').val(),  aa.val());
        fprintf(stdout, "20) aa.replace(Som, Vzz) %s :%sEOF\n", 
                        aa.replace("Som", "Vzz").val(),  aa.val());

        aa = "   bbb Some Value Some 2345   ";
        fprintf(stdout, "21) aa.trim() :%sEOF val() :%sEOF\n", 
                        aa.trim().val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "21) aa.concat() %s val :%sEOF\n", 
                        aa.concat("add one").val(),  aa.val());

        //aa = "bbb Some Value Some 2345";
        //fprintf(stdout, "21) aa.append() %s val :%sEOF\n", 
        //              aa.append("add append").val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        egg = "jjjj";
        fprintf(stdout, "21) aa.insert(5, egg) %s val :%sEOF\n", 
                        aa.insert(5, egg).val(),  aa.val());
        fprintf(stdout, "21) aa.insert(5, ch) %s val :%sEOF\n", 
                        aa.insert(5, 'M').val(),  aa.val());

        aa = "12345678";
        fprintf(stdout, "46) aa.reverse()=%s aa.val is :%sEOF\n", aa.reverse().val(), aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "21) aa.deleteCharAt(4) %s val :%sEOF\n", 
                        aa.deleteCharAt(4).val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "22) aa.deleteStr(3,5) %s val :%sEOF\n", 
                        aa.deleteStr(3,5).val(),  aa.val());

        // ***************** Fin des fonctions à la Java ********************

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "23) aa.str_tr(bomekk, BOME) %s val :%sEOF\n", 
                        aa.tr("bomekk", "BOME").val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        aa = "$1,934 100%.234";
        fprintf(stdout, "24) aa.compress() %s val :%sEOF\n", 
                        aa.compress("$,%").val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "25) aa.xrange('a', 'j') %s val :%sEOF\n", 
                        aa.xrange('a', 'j').val(),  aa.val());
        fprintf(stdout, "25) aa.xrange('1', '8') %s val :%sEOF\n", 
                        aa.xrange('1', '8').val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "26) aa.center(15) %s val :%sEOF\n", 
                        aa.center(15).val(),  aa.val());
        fprintf(stdout, "26) aa.center(15, '*') %s val :%sEOF\n", 
                        aa.center(15, '*').val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "27) aa.space(3) %s val :%sEOF\n", 
                        aa.space(3).val(),  aa.val());

        aa = "      Some Value Some 2345";
        fprintf(stdout, "28) aa.left() %s val :%sEOF\n", 
                        aa.left().val(),  aa.val());
        fprintf(stdout, "28) aa.left(18) %s val :%sEOF\n", 
                        aa.left(18).val(),  aa.val());

        aa = "  2345    ";
        fprintf(stdout, "29) aa.right():%s val :%sEOF\n", 
                        aa.right().val(),  aa.val());
        fprintf(stdout, "29) aa.right(5):%s val :%sEOF\n", 
                        aa.right(5).val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "30) aa.overlay(12345678, 4, 10, *):%s val :%sEOF\n", 
                        aa.overlay("12345678", 4, 10, '*').val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "31) aa.at(Som) %s :%sEOF\n", 
                        aa.at("Som").val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "32) aa.before(Som) %s :%sEOF\n", 
                        aa.before("Skkkom").val(),  aa.val());

        aa = "bbb Some Value Some 2345";
        fprintf(stdout, "33) aa.after(Som) %s :%sEOF\n", 
                        aa.after("Som").val(),  aa.val());

        aa = "   bb some value   ";
        aa.ltrim(true);
        fprintf(stdout, "34) aa.val is :%sEOF\n", aa.val());

        aa = "   bb some value   ";
        aa.rtrim(true);
        fprintf(stdout, "35) aa.val() is :%sEOF\n", aa.val());

        aa = "   bb some value   ";
        aa.trim(true);
        fprintf(stdout, "36) aa.val() is :%sEOF\n", aa.val());

        aa = bb;
        aa = aa + " testing newlines \n\n\n\n";
        aa.chopall();
        fprintf(stdout, "37) aa.val() is :%sEOF\n", aa.val());

        aa = bb;
        aa = aa + " rhs ";
        fprintf(stdout, "38) aa.val() is :%sEOF\n", aa.val());

        aa = bb;
        aa = " lhs " + aa;
        fprintf(stdout, "39) aa.val() is :%sEOF\n", aa.val());

        // Sample addition of numbers
        //aa = (String) 9989 + "kkk" + 33 ;
        aa = 9999;
        fprintf(stdout, "40) aa.val() is :%sEOF\n", aa.val());

        aa = bb;
        aa = " lhs " + aa + " rhs " + " 9989 " + " 33 ";
        fprintf(stdout, "41) aa.val() is :%sEOF\n", aa.val());

        aa = " AA value ";
        aa = bb + "alkja " + " 99djd " ;
        fprintf(stdout, "42) aa.val() is :%sEOF\n", aa.val());

        aa = " AA value ";
        aa = (String) "alkja " + " 99djd " ;
        fprintf(stdout, "43) aa.val() is :%sEOF\n", aa.val());

        aa = " AA value ";
        aa += (String) " al dev test kkk... " + " al2 slkj" + " al3333 ";
        fprintf(stdout, "44) aa.val() is :%sEOF\n", aa.val());

        aa = " AA value ";
        aa = aa + " add aa " + aa + aa + aa + " 1111 " + " 2222 " + aa + aa + aa + " 3333 ";
        fprintf(stdout, "45) aa.val() is :%sEOF\n", aa.val());

        aa = "12345678";
        aa.reverse(true);
        fprintf(stdout, "46) aa.val() is :%sEOF\n", aa.val());

        aa = " AA value ";
        aa = aa + " add aa " + aa + 1111 +" "+ 2222 + " " + 3.344 + aa;
        fprintf(stdout, "47) aa.val() is :%sEOF\n", aa.val());

        aa.roundd(123456.0123456789012345, 13);
        fprintf(stdout, "48) double aa.val() is :%sEOF\n", aa.val());

        aa.roundf(123456.0123456789, 13);
        fprintf(stdout, "49) float aa.val() is :%sEOF\n", aa.val());

        // Test d'égalite
        aa = " AA value ";
        String cc(" AA value ");
        if (aa == cc)
                fprintf(stdout, "50)aa=%s and cc=%s are equal!!\n", aa.val(), cc.val());
        else
                fprintf(stdout, "51)aa=%s and cc=%s are NOT equal!!\n", aa.val(), cc.val());
        cc = "CC";
        if (aa == cc)
                fprintf(stdout, "52)aa=%s and cc=%s are equal!!\n", aa.val(), cc.val());
        else
                fprintf(stdout, "53)aa=%s and cc=%s are NOT equal!!\n", aa.val(), cc.val());
        if (aa == " AA value ")
                fprintf(stdout, "54)aa=%s and string are equal!!\n", aa.val());
        else
                fprintf(stdout, "55)aa=%s and string are NOT equal!!\n", aa.val());
        if (aa == " AA valuexxx ")
                fprintf(stdout, "56)aa=%s and string are equal!!\n", aa.val());
        else
                fprintf(stdout, "57)aa=%s and string are NOT equal!!\n", aa.val());

        aa = " AA bb value 12345678 ";
        fprintf(stdout, "58) aa.length() is :%ldEOF\n", aa.length());

        aa = " AA bb value 12345678 ";
        fprintf(stdout, "59) aa.repeat(BA, 4).val=%s aa.val() is :%sEOF\n",
                        aa.repeat("BA", 4).val(), aa.val());

        aa = "";
        aa = "aa";
        if (aa.isNull())
                fprintf(stdout, "60) aa.isNull() result=true%sEOF\n", aa.val());
        else
                fprintf(stdout, "60) aa.isNull() result=false%sEOF\n", aa.val());

        aa = " some value aa";
        aa.clear();
        fprintf(stdout, "61) aa.clear() %sEOF\n", aa.val());

        aa = "  abcd efg  hijk  lmno     ";
        fprintf(stdout, "62) aa.token():%s val :%sEOF\n", 
                        aa.token().val(),  aa.val());
        fprintf(stdout, "62) aa.token():%s val :%sEOF\n", 
                        aa.token().val(),  aa.val());
        fprintf(stdout, "62) aa.token():%s val :%sEOF\n", 
                        aa.token().val(),  aa.val());
        fprintf(stdout, "62) aa.token():%s val :%sEOF\n", 
                        aa.token().val(),  aa.val());
        fprintf(stdout, "62) aa.token():%s val :%sEOF\n", 
                        aa.token().val(),  aa.val());

        aa = " 2345 ";
        if (aa.isInteger()) // is true
                fprintf(stdout, "63) aa is a integer val :%sEOF\n", aa.val());
        else
                fprintf(stdout, "63) aa is NOT a integer val :%sEOF\n", aa.val());

        aa = " 23.045  ";
        if (aa.isNumeric()) // is true
                fprintf(stdout, "64) aa is a numeric val :%sEOF\n", aa.val());
        else
                fprintf(stdout, "64) aa is NOT a numeric val :%sEOF\n", aa.val());

        aa = " 23045  ";
        fprintf(stdout, "65) aa.toInteger()=%d val :%sEOF\n", 
                        aa.toInteger(), aa.val());

        aa = " 230.45  ";
        fprintf(stdout, "66) aa.toDouble()=%f val :%sEOF\n", 
                        aa.toDouble(), aa.val());

        aa = " testing abcdefg";
        aa.chop();
        fprintf(stdout, "68) aa.chop() aa.val is :%sEOF\n", aa.val());

        aa = " str1 str2 string3 abcdefg joe john hardy  ";
        String *strlist;
        int strcount = 0;
        strlist = aa.explode(strcount);
        for (int ii = 0; ii <= strcount; ii++)
        {
                fprintf(stdout, "69) strlist[%d] is :%sEOF\n", 
                                ii, strlist[ii].val());
        }

        aa = " some aa ";
        cout << "\n\nPlease enter a line and hit return key : ";
        aa.getline();
        fprintf(stdout, "70) aa.getline() is :%sEOF\n", aa.val());

        aa = " some aa ";
        cout << "71) Testing << operator aa is : " << aa << endl;

        aa = " some aa ";
        cout << "\n\n73) Testing >> operator. Enter value for aa : ";
        cin >> aa;
        cout << "73) Testing >> operator aa is : " << aa << endl;

        // Vous pouvez utiliser aa.val() comme une variable 'char *' dans vos programmes !
        aa = " str1 str2 string3 abcdefg joe john hardy  ";
        fprintf(stdout, "\n\n74) Test case using aa.val() as 'char []' variable... ");
        for (unsigned long tmpii = 0; tmpii < aa.length(); tmpii++)
        {
                //fprintf(stdout, "aa.val()[%ld]=%c ", tmpii, aa.val()[tmpii]);
                fprintf(stdout, "aa[%ld]=%c ", tmpii, aa[tmpii]);
        }
        fprintf(stdout, "\n");

        // Utilisation de pointeurs sur 'char *' ...
        fprintf(stdout, "\n\n75) Test case using aa.val() as 'char *' pointers... ");
        aa = " str1 str2 string3 abcdefg joe john hardy  ";
        for (char *tmpcc = aa.val(); *tmpcc != 0; tmpcc++)  
        {
                fprintf(stdout, "aa.val()=%c ", *tmpcc);
        }
        fprintf(stdout, "\n");
}

// Exemple de démonstration pour la classe StringBuffer de Java
void java_string_buffer()
{
        String str1 = "ABCD EFGHI";
        cout << "\nAssigned value to str1 " << endl;
        StringBuffer aa;
        StringBuffer bb(30);
        StringBuffer cc(str1);

        cout << "\n StringBuffer aa.length() : " << aa.length() << endl;
        cout << "\n StringBuffer aa.capacity() : " << aa.capacity() << endl;
        cout << "\n StringBuffer aa.ensureCapacity(28) : " << endl;
        aa.ensureCapacity(28);
        cout << "\n StringBuffer aa.setLength(38) : " << endl;
        aa.setLength(38);

        // En Java, vous utilisez new, en C++ supprimez new
        // StringBuffer dd = new StringBuffer("some value for string buffer");
        StringBuffer dd = StringBuffer("some value for string buffer");
        cout << "\n StringBuffer dd.charAt(3) : " << dd.charAt(3) << endl;

        dd.setCharAt(3, 'K');
        cout << "\n StringBuffer setCharAt(3) : " << dd.charAt(3) << endl;

        char ee[100];
        memset(ee, 0, 100);
        strcpy(ee, "111111111111111111111111111111111111");
        dd.getChars(2, 12, ee, 3);
        cout << "\n StringBuffer getChars(2, 12, ee, 3) : " << ee << endl;

        dd.append(str1);
        cout << "\n StringBuffer append() : " << dd << endl;

        dd.append("12345678");
        cout << "\n StringBuffer append() : " << dd << endl;

        dd.append(9);
        cout << "\n StringBuffer append() : " << dd << endl;

        dd.append(7.876);
        cout << "\n StringBuffer append() : " << dd << endl;

        dd.setLength(1);
        dd.append(" some value for dd");
        dd.insert(4, str1);
        cout << "\n StringBuffer insert() : " << dd << endl;

        dd.reverse();
        cout << "\n StringBuffer reverse() : " << dd << endl;

        dd.setLength(1);
        dd.append(" some value for dd");
        dd.deleteStr(4, 9);  // Java's delete()
        cout << "\n StringBuffer deleteStr(4,9) : " << dd << endl;

        dd.setLength(1);
        dd.append(" some value for dd");
        dd.deleteCharAt(6);
        cout << "\n StringBuffer deleteCharAt() : " << dd << endl;

        dd.setLength(1);
        dd.append(" some value for dd");
        dd.replace(3, 8, str1);
        cout << "\n StringBuffer replace() : " << dd << endl;

        dd.setLength(1);
        dd.append(" some value for dd. A quick brown fox.");
        dd.substring(8);
        cout << "\n StringBuffer substring(8) : " << dd << endl;

        dd.setLength(1);
        dd.append(" some value for dd akjla akja kjk");
        dd.substring(8, 14);
        cout << "\n StringBuffer substring(8) : " << dd << endl;
}

// Exemple de démonstration pour les fonctions de Java parseInt,
// parseLong, floatValue et doubleValue
void java_string_to_numbers()
{
        String str1;
        int ii, jj = 40, mm = 24;
        long kk;

        str1 = "342";
        cout << "\n string str1 is : " << str1.val() << endl;
        ii = Integer.parseInt(str1);
        cout << "\n ii is : " << ii << endl;
        ii = ii + jj;  // add some value
        cout << "\n ii after adding " << jj << " is : " << ii << endl;

        str1 = "9876543210";
        cout << "\n string str1 is : " << str1.val() << endl;
        kk = Long.parseLong(str1);
        cout << "\n kk is : " << kk << endl;
        kk = kk + mm;  // add some value
        cout << "\n kk after adding " << mm << " is : " << kk << endl;

        str1 = "3.1487389876";
        cout << "\n string str1 is : " << str1.val() << endl;
        // Note : C++ n'accepte pas ceci --> Float myflt = Float.valueOf(str1);
        // Remplacement par ...
        Float myflt(str1); // Float myflt = Float.valueOf(str1);
        float nn = myflt.floatValue();
        //cout << "\n float nn is : " << nn << endl;
        fprintf(stdout, "\n double nn is : %8.20f \n", nn);
        nn = nn + mm;  // add some value
        //cout << "\n kk after adding " << mm << " is : " << nn << endl;
        fprintf(stdout, "\n kk after adding %d is : %8.20f \n", mm, nn);

        str1 = "3.1487389876";
        cout << "\n string str1 is : " << str1.val() << endl;
        // Note : C++ n'accept pas ceci --> Double mydbl = Double.valueOf(str1);
        // Remplacement par ...
        Double mydbl(str1); // Double mydbl = Double.valueOf(str1);
        double pp = mydbl.doubleValue();
        //cout << "\n double pp is : " << pp << endl;
        fprintf(stdout, "\n double pp is : %8.20f \n", pp);
        pp = pp + mm;  // add some value
        //cout << "\n kk after adding " << mm << " is : " << pp << endl;
        fprintf(stdout, "\n kk after adding %d is : %8.20f \n", mm, pp);
}

// Exemple de démonstration pour la classe Java StringTokenizer
void java_string_tokenizer()
{
        String results;

        // met une expression arithmetique dans une String et cree
        // un analyseur/tokenizer pour la string
        String mathExpr = "4*3+2/4";

        //StringTokenizer st1 = new StringTokenizer(mathExpr, "*+-", true);
        StringTokenizer st1(mathExpr, "*+-", true);

        // tant qu'il reste des tokens, les afficher
        results += "Tokens of mathExpr:\r\n";
        while (st1.hasMoreTokens())
        {
                results = results + st1.nextToken() + "\r\n";
        }
        cout << "The results : " << results << endl;

        // crée une string avec des champs délimités avec des virgules
        // et crée un analyseur/tokenizer pour elle
        String commas = "field1,field2,field3,and field4";
        //StringTokenizer st2 = new StringTokenizer(commas, ",", false);
        StringTokenizer st2(commas, ",", true);

        // tant qu'il reste des tokens, les afficher
        results = "";
        results += "Comma-delimited tokens:\r\n";
        while (st2.hasMoreTokens())
        {
                results = results + st2.nextToken() + "\r\n";
        }
        cout << "The results : " << results << endl;

        // crée une string avec des champs délimités avec des virgules
        // et crée un analyseur/tokenizer pour elle
        String colon = "f1,f2,f3,f4,f5,f6,f7,f8:f9:k1:k2:k3:k4:k5:k6:k7:k8:k9";
        //StringTokenizer st3 = new StringTokenizer(colon, ",", false);
        StringTokenizer st3(colon, ",", true);

        // tant qu'il reste des tokens, les afficher
        results = "";
        results += "Comma-delimited tokens:\r\n";
        for (int ii = 0; st3.hasMoreTokens(); ii++)
        {
                if (ii == 3)
                        results = results + st3.nextToken(":") + "\r\n";
                else
                        results = results + st3.nextToken() + "\r\n";
        }
        cout << "The results : " << results << endl;
}

// Exemple de démonstration de la classe Java StringReader
void java_string_reader()
{
        String str1;
        str1 = "some test string abcdefgh ijklk m n op  EM";
        StringReader reader(str1);

        char curChar;
        while ((curChar = reader.read()) != -1)
        {
                cout << "curChar : " << curChar << endl;
        }
}

// Exemple de démonstration de la classe Java StringWriter
void java_string_writer()
{
        String str1;
        str1 = "some str";
        StringWriter writer;
        writer.write("Hello ");
        writer.write("xxxWorldxxx", 3, 8);
        str1 = writer.toString();
        cout << "str1 using toString() : " << str1 << endl;

        writer.close();
        str1 = writer.toString();
        cout << "str1 after doing close() : " << str1 << endl;

        str1 = "some str";
        writer.write(" Yet another Hello ");
        writer.write("xxxxxWorld-widexxx", 5, 15);
        str1 = writer.getBuffer();
        cout << "str1 using the getBuffer() : " << str1 << endl;
}


Page suivante Page précédente Table des matières