Skip to content

Commit

Permalink
code commented
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvives1 committed Dec 21, 2021
1 parent 4936007 commit 852f4df
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 24 deletions.
Binary file removed Set5/32/a.out
Binary file not shown.
8 changes: 5 additions & 3 deletions Set5/32/explain.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
In this question we are asked to create a program that prints a sorted list of all the different words appearing in the input.
In this question, we are asked to create a program that prints a sorted list of all the different words appearing in the input.

We used a set to solve this exercise. It automatically sorts the words, but it does not differentiate between the new ones or the ones that already appeared.
To solve that issue we put all the words inside a map and print the map.
We used a set to solve this exercise as it implements a sorted collection of unique values, so when we add a word if it is already in the set it is not inserted and if it wasn't there it is inserted in the right place. So when we print it we can see the words in order.

Using the input: a a a b c abc acb acb ab ac
We get the output on file out1.txt
9 changes: 4 additions & 5 deletions Set5/32/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ int main()
set<string> stringSet;
string word;

while (cin >> word)
stringSet.insert(word);
while (cin >> word) // Read all word from standard input
stringSet.insert(word); // Insert each word into the set

set<string>::iterator iterator;
set<string>::iterator iterator; // Loop all the elements in the set
for (iterator = stringSet.begin(); iterator != stringSet.end(); ++iterator)
cout << *iterator << "\n";

cout << *iterator << "\n"; // Print each element
}
4 changes: 2 additions & 2 deletions Set5/32/order.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ explain.txt

main.ih
main.cc
text.hh
text.cc

out1.txt
9 changes: 9 additions & 0 deletions Set5/32/out1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// OUT 1 //

a
ab
abc
ac
acb
b
c
5 changes: 4 additions & 1 deletion Set5/33/explain.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
In this exercise we are required to print the word and how many times has this word appeared in the input.

To solve this exercise we decide to use a map, with the key as a string and the value as an int. We increment this int for every word that appears in the input.
To solve this exercise we decide to use a map as it stores unique key in an ordered way. So using each string as the key and the value is an int representing how many times it has appeared. We increment this int for every word that appears in the input.

The code is so simple because in a map every new key is auto-initialized to a default value. In our int case, it is initialized to 0.

Using the input: a a a b c abc acb acb ab ac
We get the output on file out1.txt
4 changes: 3 additions & 1 deletion Set5/33/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ int main()
map<string, int> object;
string word;

while(cin >> word) ++object[word];
while (cin >> word) // Read all word from standard input
++object[word]; // Insert each word into the map

map<string, int>::const_iterator iterator;
// Loop all the elements in the map
for (iterator = object.begin(); iterator != object.end(); ++iterator)
cout << iterator->first << " " << iterator->second << "\n";
}
Expand Down
3 changes: 3 additions & 0 deletions Set5/33/order.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
explain.txt

main.cc
main.ih

out1.txt
9 changes: 9 additions & 0 deletions Set5/33/out1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// OUT 1 //

a 3
ab 1
abc 1
ac 1
acb 2
b 1
c 1
11 changes: 7 additions & 4 deletions Set5/36/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

int main()
{
string filename = "input.txt";
string filename = "input.txt"; // Construct VectorString object
VectorString vectorString = VectorString(filename);

cout << "Capacity: " << vectorString.capacity() << endl;
// Print size and capacity
cout << "Size: " << vectorString.size() << endl;
cout << "Capacity: " << vectorString.capacity() << endl;

// Swap object with its copy
VectorString(vectorString).swap(vectorString);

cout << "Capacity: " << vectorString.capacity() << endl;
cout << "Size: " << vectorString.size() << endl;
// Print size and capacity
cout << "Size: " << vectorString.size() << endl;
cout << "Capacity: " << vectorString.capacity() << endl;
}
2 changes: 0 additions & 2 deletions Set5/36/main.ih
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "vectorString/vectorString.hh"
#include <iostream>
#include <set>
#include <fstream>
#include <vector>

using namespace std;
2 changes: 1 addition & 1 deletion Set5/36/out2.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// OUT 2 //

Size: 11
Capacity: 20
Size: 11
Capacity: 11
Size: 11
7 changes: 4 additions & 3 deletions Set5/36/vectorString/ctor1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ VectorString::VectorString(string filename)
string word;
set<string> uniqueWords;

while(file >> word)
uniqueWords.insert(word);
while(file >> word) // Read all words of the file
uniqueWords.insert(word); // Insert each word into a set

// Construct a vector with set elems
vector_d.assign(uniqueWords.begin(), uniqueWords.end());

vector_d.push_back("Word");
vector_d.push_back("Word"); // Add one additional word to the vector
}

1 change: 1 addition & 0 deletions Set5/36/vectorString/ctor2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

VectorString::VectorString(VectorString const &other)
{
// Copy other vector
vector_d.assign(other.vector_d.begin(), other.vector_d.end());
}
2 changes: 1 addition & 1 deletion Set5/36/vectorString/swap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

void VectorString::swap(VectorString &other)
{
vector_d.swap(other.vector_d);
vector_d.swap(other.vector_d); // Swap vectors
}
1 change: 0 additions & 1 deletion Set5/36/vectorString/vectorString.ih
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "vectorString.hh"
#include <iostream>
#include <vector>
#include <set>
#include <fstream>
Expand Down
5 changes: 5 additions & 0 deletions Set5/37/explain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Because NoEntry does not meet the CopyInsertable requirements of push_back.

One way of working around this limitation is not storing NoEntry objects in the vector, but pointers or std::unique_ptr<NoEntry> items. The latter requires only MoveConstructible and MoveAssignable, but not CopyInsertable.

If all constructors of your class are private, then only member functions of your class (including static members) or friends of the class can create instances of it. To push or move instances of that class into a std::vector, then that vector class will need to be declared as a friend. std::push_back(), among other things, uses a constructor of your class to add an element, but may call other functions to do that job
1 change: 1 addition & 0 deletions Set5/37/main.ih
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "privilegedOne.ih"
26 changes: 26 additions & 0 deletions Set5/37/noEntry.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef INCLUDED_NOENTRY_
#define INCLUDED_NOENTRY_

#include <string>

class NoEntry
{
friend class PrivilegedOne;
friend class PrivilegedTwo;

std::string d_name;
size_t d_area = 0;
size_t d_date = 0;

public:
std::string const &name() const;
size_t area() const;
size_t date() const;

private:
NoEntry(NoEntry const &other) = default;
NoEntry() = default;
NoEntry(std::string const &name, size_t area, size_t date);
};

#endif
Empty file added Set5/37/order.txt
Empty file.
30 changes: 30 additions & 0 deletions Set5/37/privilegedOne.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//#define XERR
#include "privilegedOne.ih"

PrivilegedOne::PrivilegedOne(string const &fname)
{
ifstream in{ fname };

while (true)
{
NoEntry next = nextEntry(in);

if (next.name().empty())
break;

d_noEntry.push_back(&next); // oops...
}
}

NoEntry PrivilegedOne::nextEntry(istream &in)
{
NoEntry ret;

in >> ret.d_name >> ret.d_area >> ret.d_date;

if (not in) // no more NoEntries: ensure
ret.d_name.clear(); // that d_name is empty

return ret;
}

20 changes: 20 additions & 0 deletions Set5/37/privilegedOne.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef INCLUDED_PRIVILEGEDONE_
#define INCLUDED_PRIVILEGEDONE_

#include <iosfwd>
#include <vector>

#include "noEntry.hh"

class PrivilegedOne
{
std::vector<NoEntry *> d_noEntry;

public:
PrivilegedOne(std::string const &fname);

private:
NoEntry nextEntry(std::istream &in); // empty name: all were read
};

#endif
7 changes: 7 additions & 0 deletions Set5/37/privilegedOne.ih
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>
#include <vector>
#include <fstream>
#include "noEntry.hh"
#include "privilegedOne.hh"

using namespace std;
Binary file added Set5/37/privilegedOne.o
Binary file not shown.

0 comments on commit 852f4df

Please sign in to comment.