⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lstst.cpp

📁 自编的一个String类
💻 CPP
字号:
// test character string manipulation library and string list#define WANT_STREAM#include "include.h"#include "str.h"#include "gstring.h"#include <fstream.h>int main(){   Tracer TR("string-list-test");   StringList::iterator word;   // construct a string list and load it with data   StringList Fox;   Fox.push_back("The");   Fox.push_back("quick");   Fox.push_back("brown");   Fox.push_back("fox");   Fox.push_back("jumps");   Fox.push_back("over");   Fox.push_back("the");   Fox.push_back("lazy");   Fox.push_back("dog");   // print it out   cout << "*** print words and numbers of letters ***" << endl;   for (word = Fox.begin(); word != Fox.end(); ++word)      cout << *word << "     " << word->size() << endl;;   cout << endl;   // check out size for tagged lists   cout << "*** check out size for tagged lists (4 lines) ***" << endl;   cout << "Number of lines with e   (3) " << Fox("e").size() << endl;   cout << "Number of lines with z   (1) " << Fox("z").size() << endl;   cout << "Number of lines with *   (0) " << Fox("*").size() << endl;   cout << "Number of lines with mps (1) " << Fox("mps").size() << endl;   cout << endl;   // edit it   cout << "*** swap dog and fox ***" << endl;   StringList::iterator i;   i = Fox.find("fox");   if (i == Fox.end()) cout << "Can't find fox" << endl; else *i = "dog";   i = Fox.find("dog", ++i);   if (i == Fox.end()) cout << "Can't find dog" << endl; else *i = "fox";   for (word = Fox.begin(); word != Fox.end(); ++word) cout << *word << " ";   cout << endl;   // edit it   cout << "*** put quick before fox ***" << endl;   i = Fox.find("quick");   if (i == Fox.end()) cout << "Can't find quick" << endl; else Fox.erase(i);   i = Fox.find("fox");   if (i == Fox.end()) cout << "Can't find fox" << endl;   else Fox.insert_before("quick", i);   for (word = Fox.begin(); word != Fox.end(); ++word) cout << *word << " ";   cout << endl;   // edit it   cout << "*** put lazy after The ***" << endl;   i = Fox.find("The");   if (i == Fox.end()) cout << "Can't find The" << endl;   else Fox.insert_after("lazy", i);   i = Fox.find("lazy");   if (i == Fox.end()) cout << "Can't find lazy" << endl;   i = Fox.find("lazy",++i);   if (i == Fox.end()) cout << "Can't find lazy" << endl; else Fox.erase(i);   for (word = Fox.begin(); word != Fox.end(); ++word) cout << *word << " ";   cout << endl;   // search for a word that is not there   cout << "*** search for a word that is not there ***" << endl;   i = Fox.find("cat");   if (i == Fox.end()) cout << "Can't find cat" << endl;   cout << "*** print out again ***" << endl;   for (word = Fox.begin(); word != Fox.end(); ++word) cout << *word << " ";   cout << endl;   // print it out in reverse order   cout << "*** print in reverse order ***" << endl;   for (StringList::reverse_iterator rword = Fox.rbegin();      rword != Fox.rend(); ++rword)      cout << *rword << " ";   cout << endl;   // test the GString functions   cout << "*** GString tests ***" << endl;   FixedLengthString FLS(5);   ShortestString SS;   LongestString LS;   LongestString LS1;   cout << (GS + "ABC" + SS).Matches("ABCPQRST") << "  ";      cout << SS.Value() << endl;   cout << (LS + "FGH").Matches("12345FGH") << "  ";      cout << LS.Value() << endl;   cout << (SS + "ABC" + LS).Matches("XXXABCYYYABCZZZ") << "  ";      cout  << SS.Value() << "  " << LS.Value() << endl;   cout << (LS + "ABC" + SS).Matches("XXXABCYYYABCZZZ") << "  ";      cout  << LS.Value() << "  " << SS.Value() << endl;   cout << (LS + "ABC" + LS1).Matches("XXXABCYYYABCZZZ") << "  ";      cout  << LS.Value() << "  " << LS1.Value() << endl;   SS = ""; LS = "";   cout << ((SS | LS) + "ABC" + LS1).Matches("XXXABCYYYABCZZZ") << "  ";      cout  << SS.Value() << " | " << LS.Value() << "  " << LS1.Value() << endl;   cout << (GS ^ "ABC" ^ "FGH").Matches("ABC") << "  ";   cout << (GS ^ "ABC" ^ "ABC").Matches("ABC") << "  ";   cout << (GS ^ "FGH" ^ "FGH").Matches("ABC") << "  ";   cout << (DOTS + "ABC" + DOTS).Matches("XXXABCYYYABCZZZ") << "  ";   cout << (DOTS + "FGH" + DOTS).Matches("XXXABCYYYABCZZZ") << "  ";   cout <<      (SDOTS+"ABC" > DOTS+"ABC" > "ZZZ").Matches("XXXABCYYYABCZZZ") << "  ";   cout <<      (LDOTS+"ABC" > DOTS+"ABC" > "ZZZ").Matches("XXXABCYYYABCZZZ") << "  ";   cout <<      (LDOTS+"BCY" > DOTS+"ABC" > "ZZZ").Matches("XXXABCYYYABCZZZ") << "  ";   cout <<      (LDOTS+"BCY" > DOTS+"ABC" > DOTS).Matches("XXXABCYYYABCZZZ") << "  ";   cout <<      (LDOTS+"BCY" > DOTS+"ABC" > "ZZ").Matches("XXXABCYYYABCZZZ") << "  ";   cout <<      (LDOTS+"BCY" > DOTS+"ABC" > "ZZZZ").Matches("XXXABCYYYABCZZZ") << "  ";   cout << endl;   ifstream is("lstst.dat");   StringList TheData; is >> TheData;   is.close();   cout << "*** all the data ***" << endl;   cout << TheData;   cout << "*** just those with 've' ***" << endl;   cout << TheData("ve");   cout << "*** just those without 've' ***" << endl;   cout << TheData(~(DOTS + "ve" + DOTS));   cout << "*** just those beginning with 'fo' or 's' ***" << endl;   cout << TheData((GS | "fo" | "s") + ShortestString());   cout << "*** just those with 'e' ***" << endl;   cout << TheData(DOTS + "e" + DOTS);   cout << "*** just those with exactly two 'e's ***" << endl;   cout << TheData((DOTS + "e" + DOTS + "e" + DOTS)      & ~(DOTS + "e" + DOTS + "e" + DOTS + "e" + DOTS));   cout << "*** erase eleven ***" << endl;   TheData("eleven").erase();   cout << TheData;   cout << "*** restore it ***" << endl;   TheData("ten").insert_after("eleven");   cout << TheData;   cout << "*** replace all 'e's by 'k' ***" << endl;   TheData.All().sa("e","k");   cout << TheData;   cout << "*** fix all numbers with three (or more) 'e's ***" << endl;   TheData(DOTS+"k"+DOTS+"k"+DOTS+"k"+DOTS).sa("k","e");   cout << TheData;   cout << "*** fix the first 'e' ***" << endl;   TheData.All().sf("k","e");   cout << TheData;   cout << "*** fix the last 'e' ***" << endl;   TheData.All().sl("k","e");   cout << TheData;   cout << "*** replace first 'e' by 'k' and second by 'j' ***" << endl;   TheData.All().s(DOTS+"e"+DOTS+"e"+DOTS,                   DOTS+"k"+DOTS+"j"+DOTS);   cout << TheData;   cout << "*** restore them ***" << endl;   TheData.All().s(DOTS+(GS ^ "j" ^ "k")+DOTS,                   DOTS+(GS ^ "e" ^ "e")+DOTS);   TheData.All().s(DOTS+(GS ^ "j" ^ "k")+DOTS,                   DOTS+(GS ^ "e" ^ "e")+DOTS);   cout << TheData;   cout << "*** repl. 2nd 'e' by 'k' unless there is a double 'e' ***" << endl;   TheData.All().s((DOTS+"e" +DOTS+"e"+DOTS) & ~(   DOTS+"ee"+DOTS),                   (DOTS+DOTS+DOTS+"k"+DOTS) & ~(GS+ "" + "" + "" ));   cout << TheData;   cout << "*** restore them ***" << endl;   TheData.All().s(DOTS+"k"+DOTS,                   DOTS+"e"+DOTS);   cout << TheData;   cout << "*** select 3'e's & not double 'e'; replace by 11 ***" << endl;   TheData.All().s(      (   DOTS+"e"+DOTS+"e"+DOTS+"e"+DOTS) & ~(   DOTS+"ee"+DOTS),      (GS+""  +"" +""  +"" +""  +"" +"")   & "11"                );   cout << TheData;   cout << "*** restore it ***" << endl;   TheData("11").sf("11","eleven"); cout << TheData;   cout << "*** change single letter between 't' and 'n' to q ***" << endl;   TheData.All().s(DOTS + "t" + DOT + "n" + DOTS,                   DOTS + DOT + "q" + DOT + DOTS);   cout << TheData;   cout << "*** restore it ***" << endl;   TheData.All().sa("q","e"); cout << TheData;   cout << "*** numbers ***" << endl;   SubstitutionList Numbers;   Numbers << "one" << "1"       << "two" << "2"       << "three" << "3"       << "four" << "4"       << "five" << "5";   TheData.All().sa(Numbers);   cout << TheData;   cout << "*** restore ***" << endl;   Numbers.CleanUp();   Numbers << "1" << "one"       << "2" << "two"       << "3" << "three"       << "4"<< "four"       << "5" << "five";   TheData.All().sf(Numbers);   cout << TheData;   cout << "*** The fox ***" << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   cout << Fox << endl;   Fox.All().sa("quick", "fast");   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox.All().s(DOTS + "quick" + DOTS,               DOTS + "fast"  + DOTS);   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox("fox").s(DOTS + "quick" > DOTS + ".",                DOTS + "fast"  > DOTS + "?");   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox.All().s(GS + "The" + DOTS          + "fox" + DOTS,                    DOTS  + " slow grey " + DOTS  + DOTS);   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox.All().s(DOTS + (GS | "jump" | "jumps") + DOTS,               DOTS + (GS | "leap" | "leaps") + DOTS);   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   ShortestString SS1, SS2;   Fox.All().s(      GS + "The quick brown " + SS1 + " jumps over the lazy " + SS2 + ".",           DOTS               + SS2 + DOTS                    + SS1 + DOTS);   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox.All().s(      (GS | "The" | "Le") > DOTS +  "fox" > DOTS + "the" > DOTS + "." ,      (GS | "A"   | "Un") > DOTS +  "cat" > DOTS + "a"   > DOTS + "?" );   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox.All().s(      (GS | "The" | "Le") > DOTS +  "fox" > DOTS + "the" > DOTS ,      (GS | "A"   | "Un") > DOTS +  "cat" > DOTS + "a"   > DOTS );   cout << Fox << endl;   Fox.CleanUp();   is.open("fox.dat"); is >> Fox; is.close();   Fox.All().s(      GS + "The" + DOTS <  GS + "fox" + DOTS < GS + "the" + DOTS < "." ,      GS + "A"   + DOTS <  GS + "cat" + DOTS < GS + "a"   + DOTS < "?" );   cout << Fox << endl;   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -