5. gets words into an array and sort with your own coded bubblesort ...
// getWordsFromLongStringInFileIntoArray_bubbleSort.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip> // re setw
using namespace std;
// optimized some ... since bubble sort is so slow anyways ...
void bubblesort( string ary[], int size );
int main()
{
// create file ...
ofstream fout( "Proverbs3.txt" );
if( fout )
{
fout << "My ,son ,forget ,not ,my ,law ,but ,let ,thine ,heart ,"
"keep ,my ,commandments ,For ,length ,of ,days ,and ,long ,life ,"
"and ,peace ,shall ,they ,add ,to ,thee ,Let ,not ,mercy ,"
"and ,truth ,forsake ,thee ,bind ,them ,about ,thy ,neck ,write ,"
"them ,upon ,the ,table ,of ,thine ,heart ,So ,shalt ,thou ,"
"find ,favour ,and ,good ,understanding ,in ,the ,sight ,of ,God ,"
"and ,man ,Trust ,in ,the ,LORD ,with ,all ,thine ,heart ,"
"and ,lean ,not ,unto ,thine ,own ,understanding ,In ,all ,thy ,"
"ways ,acknowledge ,him ,and ,he ,shall ,direct ,thy ,paths ,Be ,"
"not ,wise ,in ,thine ,own ,eyes ,fear ,the ,LORD ,and ,"
"depart ,from ,evil ,It ,shall ,be ,health ,to ,thy ,navel ,"
"and ,marrow ,to ,thy ,bones ,Honour ,the ,LORD ,with ,thy ,"
"substance ,and ,with ,the ,firstfruits ,of ,all ,thine ,increase ,So ,"
"shall ,thy ,barns ,be ,filled ,with ,plenty ,and ,thy ,presses ,"
"shall ,burst ,out ,with ,new ,wine ,My ,son ,despise ,not ,"
"the ,chastening ,of ,the ,LORD ,neither ,be ,weary ,of ,his ,"
"correction ,For ,whom ,the ,LORD ,loveth ,he ,correcteth ,even ,as ,"
"a ,father ,the ,son ,in ,whom ,he ,delighteth ,Happy ,is ,"
"the ,man ,that ,findeth ,wisdom ,and ,the ,man ,that ,getteth ,"
"understanding ,For ,the ,merchandise ,of ,it ,is ,better ,than ,the ,"
"merchandise ,of ,silver ,and ,the ,gain ,thereof ,than ,fine ,gold ,"
"She ,is ,more ,precious ,than ,rubies ,and ,all ,the ,things ,"
"thou ,canst ,desire ,are ,not ,to ,be ,compared ,unto ,her ,"
"Length ,of ,days ,is ,in ,her ,right ,hand ,and ,in ,"
"her ,left ,hand ,riches ,and ,honour ,Her ,ways ,are ,ways ,"
"of ,pleasantness ,and ,all ,her ,paths ,are ,peace ,She ,is ,"
"a ,tree ,of ,life ,to ,them ,that ,lay ,hold ,upon ,"
"her ,and ,happy ,is ,every ,one ,that ,retaineth ,her ,The ,"
"LORD ,by ,wisdom ,hath ,founded ,the ,earth ,by ,understanding ,hath ,"
"he ,established ,the ,heavens ,By ,his ,knowledge ,the ,depths ,are ,"
"broken ,up ,and ,the ,clouds ,drop ,down ,the ,dew ,My ,"
"son ,let ,not ,them ,depart ,from ,thine ,eyes ,keep ,sound ,"
"wisdom ,and ,discretion ,So ,shall ,they ,be ,life ,unto ,thy ,"
"soul ,and ,grace ,to ,thy ,neck ,Then ,shalt ,thou ,walk ,"
"in ,thy ,way ,safely ,and ,thy ,foot ,shall ,not ,stumble ,"
"When ,thou ,liest ,down ,thou ,shalt ,not ,be ,afraid ,yea ,"
"thou ,shalt ,lie ,down ,and ,thy ,sleep ,shall ,be ,sweet ,"
"Be ,not ,afraid ,of ,sudden ,fear ,neither ,of ,the ,desolation ,"
"of ,the ,wicked ,when ,it ,cometh ,For ,the ,LORD ,shall ,"
"be ,thy ,confidence ,and ,shall ,keep ,thy ,foot ,from ,being ,"
"taken ,Withhold ,not ,good ,from ,them ,to ,whom ,it ,is ,"
"due ,when ,it ,is ,in ,the ,power ,of ,thine ,hand ,"
"to ,do ,it ,Say ,not ,unto ,thy ,neighbour ,Go ,and ,"
"come ,again ,and ,to ,morrow ,I ,will ,give ,when ,thou ,"
"hast ,it ,by ,thee ,Devise ,not ,evil ,against ,thy ,neighbour ,"
"seeing ,he ,dwelleth ,securely ,by ,thee ,Strive ,not ,with ,a ,"
"man ,without ,cause ,if ,he ,have ,done ,thee ,no ,harm ,"
"Envy ,thou ,not ,the ,oppressor ,and ,choose ,none ,of ,his ,"
"ways ,For ,the ,froward ,is ,abomination ,to ,the ,LORD ,but ,"
"his ,secret ,is ,with ,the ,righteous ,The ,curse ,of ,the ,"
"LORD ,is ,in ,the ,house ,of ,the ,wicked ,but ,he ,"
"blesseth ,the ,habitation ,of ,the ,just ,Surely ,he ,scorneth ,the ,"
"scorners ,but ,he ,giveth ,grace ,unto ,the ,lowly ,The ,wise ,"
"shall ,inherit ,glory ,but ,shame ,shall ,be ,the ,promotion ,of ,"
"fools";
fout.close();
}
// get ready for array of strings ...
string* ary = 0;
string word;
int size = 0, cap = 0, begin_cap = 1; // but could set much larger here
ifstream fin( "Proverbs3.txt" );
if( fin )
{
while( getline(fin, word, ',') ) // great next word from file ...
{
// firstly ... trim off trailing space(s)
unsigned pos = word.find_first_of( ' ' );
if( pos != string::npos ) word.erase( pos );
// now ... get new memory to hold enlarged block of array elements
if( size == cap )
{
if( cap != 0 ) cap += cap; // double capacity
else cap = begin_cap; // set beginning capacity on first pass
string* nary = new string[cap]; // get memory for new enlarged array
for( int i = 0; i < size; ++i ) nary[i] = ary[i]; // copy old to new
delete [] ary; // now can free old array memory ...
ary = nary; // now can update array address to new memory location
}
ary[size] = word; // now can put this 'word' into next place in array
++size; // increment counter
}
fin.close();
cout << "Press 'Enter' to see " << size
<< " words from long file string.\n"
<< "(Note that the capacity now is " << cap << ") ... "<< flush;
cin.get();
cout << left; // set to left justify in field of width 'x' set by setw(x)
for( int i = 0; i < size; ++i ) cout << setw(16) << "'" + ary[i] + "'";
cout << "\n\nPress 'Enter' to see " << size
<< " words sorted ...\n"
<< "(Note that the capacity now is " << cap << ") ... "<< flush;
cin.get();
bubblesort( ary, size );
for( int i = 0; i < size; ++i ) cout << setw(16) << "'" + ary[i] + "'";
delete [] ary; // free all dynamic memory for array of strings
ary = 0;
size = cap = 0;
}
cout << "\n\nPress 'Enter' to continue ... " << flush;
cin.get();
}
void bubblesort( string ary[], int size )
{
bool swap;
string tmp;
do
{
swap = false; // if array is sorted ... then DONE after the next pass
// only have to check, each loop, up to to size-1 places
for( int j = 1; j < size; ++j )
{
if( ary[j-1] > ary[j] ) // swap values at j-1 with value at j
{
tmp = ary[j-1];
ary[j-1] = ary[j];
ary[j] = tmp;
swap = true;
}
}
--size;
}while( swap );
}
6. gets words into an array of pointers to string and sort with your own coded bubblesort ... (using array of pointers to speed up swaps ... can just swap pointers)
// getWordsFromLongStringInFileIntoArray_bubbleSort_pointers.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip> // re setw
using namespace std;
// optimized (some more by just swapping pointers)...
// since bubble sort is so slow anyways ...
void bubblesort( string* ary[], int size );
int main()
{
// create file ...
ofstream fout( "Proverbs3.txt" );
if( fout )
{
fout << "My ,son ,forget ,not ,my ,law ,but ,let ,thine ,heart ,"
"keep ,my ,commandments ,For ,length ,of ,days ,and ,long ,life ,"
"and ,peace ,shall ,they ,add ,to ,thee ,Let ,not ,mercy ,"
"and ,truth ,forsake ,thee ,bind ,them ,about ,thy ,neck ,write ,"
"them ,upon ,the ,table ,of ,thine ,heart ,So ,shalt ,thou ,"
"find ,favour ,and ,good ,understanding ,in ,the ,sight ,of ,God ,"
"and ,man ,Trust ,in ,the ,LORD ,with ,all ,thine ,heart ,"
"and ,lean ,not ,unto ,thine ,own ,understanding ,In ,all ,thy ,"
"ways ,acknowledge ,him ,and ,he ,shall ,direct ,thy ,paths ,Be ,"
"not ,wise ,in ,thine ,own ,eyes ,fear ,the ,LORD ,and ,"
"depart ,from ,evil ,It ,shall ,be ,health ,to ,thy ,navel ,"
"and ,marrow ,to ,thy ,bones ,Honour ,the ,LORD ,with ,thy ,"
"substance ,and ,with ,the ,firstfruits ,of ,all ,thine ,increase ,So ,"
"shall ,thy ,barns ,be ,filled ,with ,plenty ,and ,thy ,presses ,"
"shall ,burst ,out ,with ,new ,wine ,My ,son ,despise ,not ,"
"the ,chastening ,of ,the ,LORD ,neither ,be ,weary ,of ,his ,"
"correction ,For ,whom ,the ,LORD ,loveth ,he ,correcteth ,even ,as ,"
"a ,father ,the ,son ,in ,whom ,he ,delighteth ,Happy ,is ,"
"the ,man ,that ,findeth ,wisdom ,and ,the ,man ,that ,getteth ,"
"understanding ,For ,the ,merchandise ,of ,it ,is ,better ,than ,the ,"
"merchandise ,of ,silver ,and ,the ,gain ,thereof ,than ,fine ,gold ,"
"She ,is ,more ,precious ,than ,rubies ,and ,all ,the ,things ,"
"thou ,canst ,desire ,are ,not ,to ,be ,compared ,unto ,her ,"
"Length ,of ,days ,is ,in ,her ,right ,hand ,and ,in ,"
"her ,left ,hand ,riches ,and ,honour ,Her ,ways ,are ,ways ,"
"of ,pleasantness ,and ,all ,her ,paths ,are ,peace ,She ,is ,"
"a ,tree ,of ,life ,to ,them ,that ,lay ,hold ,upon ,"
"her ,and ,happy ,is ,every ,one ,that ,retaineth ,her ,The ,"
"LORD ,by ,wisdom ,hath ,founded ,the ,earth ,by ,understanding ,hath ,"
"he ,established ,the ,heavens ,By ,his ,knowledge ,the ,depths ,are ,"
"broken ,up ,and ,the ,clouds ,drop ,down ,the ,dew ,My ,"
"son ,let ,not ,them ,depart ,from ,thine ,eyes ,keep ,sound ,"
"wisdom ,and ,discretion ,So ,shall ,they ,be ,life ,unto ,thy ,"
"soul ,and ,grace ,to ,thy ,neck ,Then ,shalt ,thou ,walk ,"
"in ,thy ,way ,safely ,and ,thy ,foot ,shall ,not ,stumble ,"
"When ,thou ,liest ,down ,thou ,shalt ,not ,be ,afraid ,yea ,"
"thou ,shalt ,lie ,down ,and ,thy ,sleep ,shall ,be ,sweet ,"
"Be ,not ,afraid ,of ,sudden ,fear ,neither ,of ,the ,desolation ,"
"of ,the ,wicked ,when ,it ,cometh ,For ,the ,LORD ,shall ,"
"be ,thy ,confidence ,and ,shall ,keep ,thy ,foot ,from ,being ,"
"taken ,Withhold ,not ,good ,from ,them ,to ,whom ,it ,is ,"
"due ,when ,it ,is ,in ,the ,power ,of ,thine ,hand ,"
"to ,do ,it ,Say ,not ,unto ,thy ,neighbour ,Go ,and ,"
"come ,again ,and ,to ,morrow ,I ,will ,give ,when ,thou ,"
"hast ,it ,by ,thee ,Devise ,not ,evil ,against ,thy ,neighbour ,"
"seeing ,he ,dwelleth ,securely ,by ,thee ,Strive ,not ,with ,a ,"
"man ,without ,cause ,if ,he ,have ,done ,thee ,no ,harm ,"
"Envy ,thou ,not ,the ,oppressor ,and ,choose ,none ,of ,his ,"
"ways ,For ,the ,froward ,is ,abomination ,to ,the ,LORD ,but ,"
"his ,secret ,is ,with ,the ,righteous ,The ,curse ,of ,the ,"
"LORD ,is ,in ,the ,house ,of ,the ,wicked ,but ,he ,"
"blesseth ,the ,habitation ,of ,the ,just ,Surely ,he ,scorneth ,the ,"
"scorners ,but ,he ,giveth ,grace ,unto ,the ,lowly ,The ,wise ,"
"shall ,inherit ,glory ,but ,shame ,shall ,be ,the ,promotion ,of ,"
"fools";
fout.close();
}
// get ready for array of pointers to string ...
string** ary = 0;
string word;
int size = 0, cap = 0, begin_cap = 1; // but could set much larger here
ifstream fin( "Proverbs3.txt" );
if( fin )
{
while( getline(fin, word, ',') ) // great next word from file ...
{
// firstly ... trim off trailing space(s)
unsigned pos = word.find_first_of( ' ' );
if( pos != string::npos ) word.erase( pos );
// now ... get new memory to hold enlarged block of array elements
if( size == cap )
{
if( cap != 0 ) cap += cap; // double capacity
else cap = begin_cap; // set beginning capacity on first pass
string** nary = new string*[cap]; // get memory for new enlarged array
for( int i = 0; i < size; ++i ) nary[i] = ary[i]; // copy old to new
delete [] ary; // now can free old array memory ...
ary = nary; // now can update array address to new memory location
}
string* tmp = new string; // get address and memory for a new string
*tmp = word; // get (new copy of) 'this word' at that address ...
ary[size] = tmp; // 'pointer to this new copy' goes next place in ary
++size; // increment counter
}
fin.close();
cout << "Press 'Enter' to see " << size
<< " words from long file string.\n"
<< "(Note that the capacity now is " << cap << ") ... "<< flush;
cin.get();
cout << left; // set to left justify in field of width 'x' set by setw(x)
for( int i = 0; i < size; ++i ) cout << setw(16) << "'" + *ary[i] + "'";
cout << "\n\nPress 'Enter' to see " << size
<< " words sorted ...\n"
<< "(Note that the capacity now is " << cap << ") ... "<< flush;
cin.get();
bubblesort( ary, size );
for( int i = 0; i < size; ++i ) cout << setw(16) << "'" + *ary[i] + "'";
for( int i = size-1; i >= 0; --i ) delete ary[i]; // free each new string
delete [] ary; // free all dynamic memory for array of pointers to string
ary = 0;
size = cap = 0;
}
cout << "\n\nPress 'Enter' to continue ... " << flush;
cin.get();
}
void bubblesort( string* ary[], int size )
{
bool swap;
string* tmp;
do
{
swap = false; // if array is sorted ... then DONE after the next pass
// only have to check, each loop, up to to size-1 places
for( int j = 1; j < size; ++j )
{
if( *ary[j-1] > *ary[j] ) // then swap pointer at j-1 with pointer at j
{
tmp = ary[j-1];
ary[j-1] = ary[j];
ary[j] = tmp;
swap = true;
}
}
--size;
}while( swap );
}