THIS IS PROGRAM FOR C++ I\'m given a list of programs: main.cpp main.h word.h li
ID: 3725470 • Letter: T
Question
THIS IS PROGRAM FOR C++
I'm given a list of programs:
main.cpp
main.h
word.h
list.h
I'm supposed to make a list.cpp and a word.cpp that implements list.h and word.h. This is a word counting program that reads a text file and outputs:
Word "and" occurs 3 times
Word "you" occurs 2 times
Word "are" occurs 1 times
Etc....
I have completed word.cpp but i'm having trouble with list.cpp. I will attach the source codes below.
You are only allowed to edit word.cpp and list.cpp
---------main.cpp---------
// Main definition file
#include "main.h"
using namespace std;
int main(int argc, char **argv) {
List myList;
fstream file;
// Try to open the input file
if (argc < 2) {
cerr << "?No input file provided" << endl;
return 1;
} else {
file.open(argv[1],ios::in); // Open the file
if (file.good() != true) {
cerr << "?Unable to open "" << argv[1] << """ << endl;
return 1;
}
}
// File is open. Read all words in the file
file >> myList;
// Done with the file
file.clear();
file.close();
// Display the word list
cout << myList.size() << " words read" << endl;
cout << myList << endl;
return 0;
}
----------main.h---------
// Header for main program
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "word.h"
#include "list.h"
-----------word.h--------
// Class Definition for Word class
#pragma once
#include <string>
#include <iostream>
class Word
{
public:
// Constructors
Word(); // Default constructor
Word(std::string); // Parameterized constructor
Word(const Word &); // Copy constructor
Word(Word &&); // Move constructor
~Word(); // Destructor
// Accessors
std::string getWord(); // Accessor for word string
int getCount(); // Accessor for count integer
// Mutators
void setWord(std::string); // Mutator for word string
void setCount(int); // Mutator for count
// Member Operators
Word& operator++(); // Pre-increment: Change count
Word operator++(int); // Post-increment: Change count
Word& operator=(const Word &); // Copy assignment
Word& operator=(Word&&); // Move assignment
bool operator==(const Word &);// Relational
bool operator<(const Word &); // Relational
// Friends
friend std::ostream & operator << (std::ostream &, const Word &);
friend std::istream & operator >> (std::istream &, Word &);
private:
std::string word;
int count;
};
---------word.cpp----------
#include "word.h"
//Constructors
Word::Word()
{
count = 0;
}
Word::Word(std::string w)
{
word = w;
count = 1;
}
Word::Word(const Word &w)
{
word = word.w;
count = count.w;
}
Word::Word(Word &&w)
{
word = word.w;
count = count.w;
}
//Destructor
Word::~Word();
{
//delete this;
}
//Accessors
std::string Word::getWord()
{
return word;
}
int Word::getCount()
{
return count;
}
//Mutators
void Word::setWord(std::string)
{
word = w;
}
void setCount(int c)
{
count = c;
}
//Member operators
Word& Word::operator++()
{
++count;
return *this;
}
Word& Word::operator++(int)
{
Word old(*this)
count++;
return old;
}
Word& Word::operator=(const Word &w)
{
word = w.word;
count = w.count;
return *this;
}
Word& Word::operator=(const Word&& w)
{
word = w.word;
count = w.count;
return *this;
}
bool Word::operator==(const Word &other)
{
if(word == other.word)
return true;
else
return false;
}
bool Word::operator<(const Word &other)
{
if(word < other.word)
return true;
else
return false;
}
//Friends
std::ostream & operator <<(std::ostream &out, const Word &w)
{
out << "Word "" << w.word << "" occurs " << w.count << " times";
return out;
}
std::istream & operator >>(std::istream &in, Word &w)
{
in >> w.word;
w.count = 1;
return in;
}
---------list.h-------------
#include <iomanip>
#include <string>
#include "word.h"
#define max 10000
class List
{
public:
List(); // Initialize
~List(); // Destruct
bool append(Word); // Append to list
int find(Word); // Find in list
Word at(int); // Find in position
int size(); // Size of list
friend std::ostream & operator << (std::ostream &, const List &);
friend std::istream & operator >> (std::istream &, List &);
private:
Word data[max]; // List
int used; // Elements used
int curr; // Current element
};
------list.cpp------
This is where I need help. Please and thank you!!!!!!!
Explanation / Answer
list.h
#include <iomanip>
#include <string>
#include "word.h"
#define max 10000
class List
{
public:
List(); // Initialize
~List(); // Destruct
bool append(Word); // Append to list
int find(Word); // Find in list
Word at(int); // Find in position
int size(); // Size of list
friend std::ostream & operator << (std::ostream &, List &);
friend std::ifstream & operator >> (std::ifstream &, List &);
private:
Word data[max]; // List
int used; // Elements used
int curr; // Current element
};
list.cpp
#include <fstream>
#include "list.h"
List::List()
{
used = 0;
curr = 0;
}
List::~List()
{
}
bool List::append(Word w)
{
data[used] = w;
used++;
curr++;
}
int List::find(Word w)
{
int i = 0;
while (i < used)
{
if (data[i] == w)
return i;
i++;
}
return -1;
}
Word List::at(int pos)
{
return data[pos];
}
int List::size()
{
return used;
}
std::ostream &operator<<(std::ostream &out, List &list)
{
int i = 0;
while (i < list.size())
{
out << list.at(i);
i++;
}
return out;
}
std::istream &operator>>(std::istream &in, List &list)
{
std::string word;
while (in.eof() == 0)
{
in >> word;
int i=0;
int ind = list.find(word);
if(ind==-1){
list.append(word);
}
else{
++list.at(i);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.