Changes

MyWikiBiz, Author Your Legacy — Tuesday November 19, 2024
Jump to navigationJump to search
rework
Line 1: Line 1:  +
==MovieType.h==
 
<pre>
 
<pre>
//Programmer:Derek Elder
  −
//Assignment:Lab 2
  −
//Section:0112
  −
//Instructor:Hyman
  −
//Time:T/TH 7:30 - 11:10 AM
  −
   
#include <iostream>
 
#include <iostream>
 
#include <fstream>
 
#include <fstream>
Line 12: Line 7:  
using namespace std;
 
using namespace std;
   −
class BigInt
+
#ifndef __MovieType_H__
 +
#define __MovieType_H__
 +
 
 +
const string EndOfFile = "***";
 +
 
 +
class MovieType
 
{
 
{
friend BigInt operator+(const BigInt& num1, const BigInt& num2);
+
string m_title;
//Purpose:Operator overloaded, + operator will call AddBigInts.
+
int m_year;
//Precondition:None.
+
int m_gross;
//Postcondition:The two BigInts have been added together.
+
string m_studio;
friend ostream& operator<<(ostream& os, const BigInt& num);
+
string m_stars;
//Purpose:Operator overloaded, << operator will call PrintToBigInt.
  −
//Precondition:None.
  −
//Postcondition:The BigInt is printed.
  −
friend bool AddBigInts(const BigInt& num1, const BigInt& num2, BigInt& sum);
  −
//Purpose:Add two integers together into a sum.
  −
//Precondition:BigInts num1 and num2 are valid.
  −
//Postcondition:1.Return true if sum is valid.
  −
// 2.Return false if there is overflow.
  −
friend void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum);
  −
//Purpose:Modularize AddBigInts function to make it more efficent.
  −
//Precondition:BigInts num1 and num2 have been entered and contain good data.
  −
//Postcondition:1.Sum has been populated by good data as long as numDigits <= MAX.
  −
// 2.If there is overflow (numDigits > MAX) it will be indicated in operator+.
   
public:
 
public:
static const int MAX = 5;
+
MovieType();
BigInt();
+
//Purpose:Default Constructor.
//Default Constructor
+
//Precondition:None
//Purpose:Set numDigits to 0 to indicate an array is not populated.
+
//Postcondition:None
//Precondition:None.
+
int CompareKeys(const MovieType& movie2) const;
//Postcondition:Constructor has set numDigits to 0 indicating the array is not populated.
+
//Purpose:To compare two movies and determine where it should be inserted.
BigInt(const string& goodString);
+
//Precondition:Add function must be called and executed.
//Constructor
+
//Postcondition:String compare function is called to execute the comparison, resulting in two possible outcomes:
//Purpose:If num is intitalized in main with a value, the constructor calls StringToBigInt.
+
// 1.A positive value is returned and is added into the list.
//Precondition:None.
+
// 2.A 0 or a negative value is returned and isn't added into the list.
//Postcondition:BigInt num has been converted from a string into  an array.
+
void Initialize(string title, int year, int gross, string studio, string stars);
bool ReadBigInt(istream& is = cin);
+
//Purpose:Initializes a movie item with data, basically a constructor.
//default stream arg; ReadBigInt() means use "cin".
+
//Precondition:None
//Purpose:Read a BigInt, deposit into a string and call StringToBigInt.
+
//Postcondition:Populated with initial data.
//Precondition:None.
+
bool ReadOneMovieFromFile(istream& is = cin);
//Postcondition:1.Array is populated, data is valid.
+
//Purpose:To read one movie from a file at a time.
//             2.BigInt is too large, loops until an Int is entered that is small enough.
+
//Precondition:ReadAllMovies() must be called.
void PrintBigInt(ostream& os = cout) const;
+
//Postcondition:1.True is returned if one movie was taken from the data file and entered into the list.
//default stream arg; PrintBigInt() means use "cout".
+
// 2.False is returned if the EndOfFile flag was reached.
//Purpose:Print a BigInt in the same way it was entered by reversing the way it was stored into the array.
+
void Display(ostream&os = cout) const;
//Precondition:BigInt num is valid.
+
//Purpose:Displays one movie from the list.
//Postcondition:Data is printed on screen matching the way it was entered from the console.
+
//Precondition:List array must contain data.
private:
+
//Postcondition:Data listed to the console screen.
void StringToBigInt(const string& goodString);
  −
//Purpose:Convert from characters to numeric data
  −
//Precondition:Assume "goodString" is perfect.
  −
//Postcondition:goodString is converted into numeric data and entered into an array.
  −
int Digits[MAX];
  −
int numDigits;
   
};
 
};
 
+
int MovieType::CompareKeys(const MovieType& movie2) const
class OverFlowException
+
{
 +
return (m_title.compare(movie2.m_title));
 +
}
 +
void MovieType::Initialize(string title, int year, int gross, string studio, string stars)
 +
{
 +
m_title = title;
 +
m_year = year;
 +
m_gross = gross;
 +
m_studio = studio;
 +
m_stars = stars;
 +
}
 +
void MovieType::Display(ostream& os) const
 
{
 
{
public:
+
if(m_title == EndOfFile)
BigInt m_num1, m_num2;
  −
char m_op;
  −
OverFlowException(const BigInt& num1,const BigInt& num2, char op)
   
{
 
{
m_num1 = num1;
+
os<<"";
m_num2 = num2;
  −
m_op = op;
   
}
 
}
};
+
else
 
  −
void main()
  −
{
  −
BigInt num1, num2;//("12345");
  −
bool moreNumbers = true;
  −
char update = ' ';
  −
 
  −
while(moreNumbers)
   
{
 
{
try
+
os<<"Title:\t"<<m_title<<endl;
{
+
os<<"Year:\t"<<m_year<<endl;
num1.ReadBigInt();
+
os<<"Gross:\t$ "<<m_gross<<endl;
num2.ReadBigInt();
+
os<<"Studio:\t"<<m_studio<<endl;
cout<<"The BigInt entered is: ";
+
os<<"Stars:\t"<<m_stars<<endl;
num1.PrintBigInt();
+
os<<endl;
cout<<"The BigInt entered is: ";
  −
num2.PrintBigInt();
  −
cout<<"The sum is: "<<num1 + num2;
  −
}
  −
catch(OverFlowException ex)
  −
{
  −
cout<<"The sum has caused OVERFLOW"<<endl;
  −
}
  −
 
  −
cout<<"Do you wish to add more numbers? ";
  −
cin>>update;
  −
cin.ignore(50,'\n');
  −
if(update == 'Y' || update == 'y')
  −
{
  −
moreNumbers = true;
  −
}
  −
else
  −
{
  −
moreNumbers = false;
  −
}
   
}
 
}
cout<<"Program Terminated on request."<<endl;
   
}
 
}
 
+
bool MovieType::ReadOneMovieFromFile(istream& is)
bool BigInt::ReadBigInt(istream& is)
   
{
 
{
int len = 0;
+
int index;
bool tooBig = false;
+
string str = "";
string inputString = "";
     −
while(!tooBig) //Initialize tooBig, if integer is too big loop until BigInt entered is valid.
+
getline(is,m_title);
 +
if(m_title == EndOfFile)
 
{
 
{
cout<<"Enter an integer: ";
+
return false;
getline(cin,inputString);
+
}
 
+
else
len = (int)inputString.length(); //Length of the string entered into len
+
{
 
+
is>>m_year;
if(len > MAX)
+
is>>m_gross;
{
+
is>>m_studio;
cout<<"The number you entered is too big, try again!"<<endl;
+
getline(is,str);
tooBig = false;
+
index = (int)str.find_first_of(",",0);
}
+
if(index > 0)
 +
m_stars = str.substr(index+2,(str.length() - (index+1)));
 
else
 
else
{
+
m_stars = "None";
StringToBigInt(inputString);
+
return true;
tooBig = true;
  −
}
   
}
 
}
return true;
   
}
 
}
void BigInt::PrintBigInt(ostream& os) const
+
MovieType::MovieType()
 +
:m_title(""), m_year(-1), m_gross(-1), m_studio(""), m_stars("")
 +
{}
 +
#endif
 +
</pre>
 +
==SortedMovieList.h==
 +
<pre>
 +
#include <iostream>
 +
#include <fstream>
 +
#include <string>
 +
#include <iomanip>
 +
#include "MovieType.h"
 +
using namespace std;
 +
typedef MovieType MovieListItemType;
 +
 
 +
const int MAX = 20;
 +
int counter = 0;
 +
 
 +
class SortedMovieList
 +
{
 +
public:
 +
SortedMovieList();
 +
//Purpose:Default Constructor.
 +
//Precondition:None
 +
//Postcondition:None
 +
void listAdd(const MovieListItemType& newItem, bool& success);
 +
//Purpose:To insert a movie in the array.
 +
//Precondition:ReadAllMovies function executes.
 +
//Postcondition:listInsert function is called to find the correct position for the movie to be inserted.
 +
void listInsert(int index, MovieListItemType newItem, bool& success);
 +
//Purpose:To insert a movie in the correct position in the array.
 +
//Precondition:listAdd function is called.
 +
//Postcondition:Item is inserted into the correct place in the list.
 +
void listSubtract(const MovieListItemType& itemToRemove, bool& success);
 +
//Purpose:To remove a movie from the array.
 +
//Precondition:Remove choice is called from the menu.
 +
//Postcondition:The movie is removed from the list
 +
void listRemove(int index, bool& success);
 +
//Purpose:To remove a movie from the array.
 +
//Precondition:Remove choice is called from the menu.
 +
//Postcondition:The movie is removed from the list.
 +
void ReadAllMovies();
 +
//Purpose:Reads all of the movies in an input file.
 +
//Precondition:ReadOneMovieFromFile executes correctly.
 +
//Postcondition:The list is populated with all of the movies from the file.
 +
void DisplayList() const;
 +
//Purpose:Displays the entire list of movies.
 +
//Precondition:Array is populated from ReadAllMovies().
 +
//Postcondition:The list is displayed to the console.
 +
void Search(const MovieListItemType& itemToSearch, bool& success);
 +
//Purpose:To search through the list for the correct movie
 +
//Precondition:Search called from the menu.
 +
//Postcondition:1.If the movie is found the index where it was found is returned.
 +
// 2.If the movie is not found the user will be notified.
 +
 +
private:
 +
MovieListItemType items[MAX];
 +
int size;
 +
};
 +
 
 +
SortedMovieList::SortedMovieList()
 +
:size(0)
 +
{}
 +
void SortedMovieList::listAdd(const MovieListItemType& newItem, bool& success)
 
{
 
{
int counter = numDigits - 1;
+
int i = 0;
   −
for(int i = 0; i < numDigits; i++)
+
if(size == 0)
 +
{
 +
listInsert(i,newItem,success);
 +
return;
 +
}
 +
while((i < size) && (newItem.CompareKeys(items[i]) == 1))
 
{
 
{
cout<<Digits[counter];
+
i++;
counter--;
   
}
 
}
cout<<endl;
+
 
 +
listInsert(i,newItem,success);
 +
}
 +
void SortedMovieList::listInsert(int index, MovieListItemType newItem, bool& success)
 +
{
 +
  success = ((index >= 0) && (index <= size) && (size < MAX));
 +
 
 +
  if(success)
 +
  {
 +
  for(int pos = size; pos >= index; pos--)
 +
  {
 +
  items[pos+1] = items[pos];
 +
  }
 +
  items[index] = newItem;
 +
  size++;
 +
  counter++;
 +
  }
 
}
 
}
bool AddBigInts(const BigInt& num1, const BigInt& num2, BigInt& sum)
+
void SortedMovieList::listSubtract(const MovieListItemType& itemToRemove, bool& success)
 
{
 
{
if(num1.numDigits >= num2.numDigits)
+
int i = 0;
 +
 
 +
if(size == 0)
 
{
 
{
firstIntegerLarger(num1, num2, sum);
+
cout<<"List is empty"<<endl;
 
}
 
}
else
+
 
 +
while((i < size) && (itemToRemove.CompareKeys(items[i]) == 1))
 
{
 
{
firstIntegerLarger(num2, num1, sum);
+
i++;
 
}
 
}
   −
//If Overflow return false, otherwise return true
+
listRemove(i,success);
if(sum.numDigits > MAX)
+
}
 +
void SortedMovieList::listRemove(int index, bool& success)
 +
{
 +
success = ((index >= 0) && (index < size));
 +
 
 +
if(success)
 
{
 
{
return false;
+
  for(int fromPosition = index + 1; fromPosition < size; fromPosition++)
 +
  items[fromPosition-1] = items[fromPosition];
 +
  size--;
 +
  counter--;
 
}
 
}
else
+
}
 +
void SortedMovieList::ReadAllMovies()
 +
{
 +
MovieListItemType newItem;
 +
ifstream Input;
 +
int i = 0;
 +
bool success;
 +
string inputFile = "";
 +
 
 +
Input.clear();
 +
cout<<"Welcome, please enter the name of your Input file: ";
 +
cin>>inputFile;
 +
cin.ignore(50,'\n');
 +
Input.open(inputFile.c_str());
 +
 
 +
while(newItem.ReadOneMovieFromFile(Input))
 
{
 
{
return true;
+
this->listAdd(newItem,success);
 
}
 
}
 +
 +
Input.close();
 
}
 
}
void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum)
+
void SortedMovieList::DisplayList() const
 
{
 
{
 
int i;
 
int i;
int remainder = 0;
     −
for(i = 0; i < smallest.numDigits; i++)
+
for(i = 0; i < size; i++)
 
{
 
{
sum.Digits[i] = largest.Digits[i] + smallest.Digits[i] + remainder;
+
items[i].Display();
   −
if(sum.Digits[i] >= 10)
+
if(i%3 == 0) //Every 3 movies, pause.
 
{
 
{
sum.Digits[i] = (sum.Digits[i] - 10);
+
cout<<"Press 'ENTER' if you wish to display more movies";
remainder = 1;
+
cin.get();
}
+
cout<<endl;
else
  −
{
  −
remainder = 0;
   
}
 
}
 
}
 
}
 +
}
 +
void SortedMovieList::Search(const MovieListItemType& itemToSearch, bool& success)
 +
{
 +
int i;
   −
for(; i < largest.numDigits; i++)
+
for(i = 0; i < size; i++)
 
{
 
{
sum.Digits[i] = largest.Digits[i] + remainder;
+
if(itemToSearch.CompareKeys(items[i]) == 0)
 +
{
 +
cout<<endl<<"The Movie has been found at index "<<i<<"."<<endl;//add ...
 +
items[i].Display();
 +
break; //If the movie is found, go no further.
 +
}
   −
if(sum.Digits[i] >= 10)
+
if(itemToSearch.CompareKeys(items[i]) == -1)
 
{
 
{
sum.Digits[i] = (sum.Digits[i] - 10);
+
cout<<endl<<"The movie you wished to search for has not been found."<<endl;
remainder = 1;
+
break; //If you have gone past the point
 +
  //where the movie should be stored in the list, go no further.
 
}
 
}
else
  −
{
  −
remainder = 0;
  −
}
   
}
 
}
   −
if(i == largest.numDigits && remainder == 1)
+
if(i == size)
 +
cout<<endl<<"The movie you wished to search for has not been found."<<endl;
 +
}
 +
</pre>
 +
==Main.cpp==
 +
<pre>
 +
#include <iostream>
 +
#include <fstream>
 +
#include <string>
 +
#include <iomanip>
 +
#include "MovieType.h"
 +
#include "SortedMovieList.h"
 +
using namespace std;
 +
 
 +
void clearScreen();
 +
//Purpose:To clear the screen.
 +
//Precondition:None
 +
//Postcondition:Screen is cleared.
 +
char displayMenuAndGetSelection();
 +
//Purpose:To display the menu and get the selection for searching.
 +
//Precondition:None
 +
//Postcondition:Search choice is entered and appropriate function is called.
 +
void addPerson(SortedMovieList& movieList);
 +
//Purpose:To add a person to the list.
 +
//Precondition:Choice must be selected from the menu.
 +
//Postcondition:List is searched and results are output to the console.
 +
void removePerson(SortedMovieList& movieList);
 +
//Purpose:To remove a person from the list.
 +
//Precondition:Choice must be selected from the menu.
 +
//Postcondition:List is searched and results are output to the console.
 +
void Pause();
 +
//Purpose:To pause the program and allow the user a break.
 +
//Precondition:None
 +
//Postcondition:None
 +
void searchList(SortedMovieList& movieList);
 +
//Purpose:To call the search function and find a movie in the list.
 +
//Precondition:Choice must be selected from the menu.
 +
//Postcondition:Search function is called.
 +
void quitProgram();
 +
//Purpose:To inform the user the program has been terminated.
 +
//Precondition:None
 +
//Postcondition:Program terminated.
 +
 
 +
void main()
 +
{
 +
char menuChoice = ' ';
 +
bool done = false;
 +
SortedMovieList movieList;
 +
 
 +
movieList.ReadAllMovies();
 +
 
 +
while(!done)
 
{
 
{
sum.Digits[i] = 1;
+
menuChoice = displayMenuAndGetSelection();
sum.numDigits = largest.numDigits + 1;
+
clearScreen();
}
+
switch(menuChoice)
else
+
{
{
+
case '1':
sum.numDigits = largest.numDigits;
+
movieList.DisplayList();
 +
Pause();
 +
done = false;
 +
break;
 +
case '2':
 +
searchList(movieList);
 +
Pause();
 +
done = false;
 +
break;
 +
case '3':
 +
removePerson(movieList);
 +
Pause();
 +
done = false;
 +
break;
 +
case '4':
 +
addPerson(movieList);
 +
Pause();
 +
done = false;
 +
break;
 +
case '5':
 +
cout<<"There are currently "<<counter<<" movies in the list."<<endl;
 +
Pause();
 +
done = false;
 +
break;
 +
case '6':
 +
quitProgram();
 +
done = true;
 +
break;
 +
default:
 +
cout<<"Incorrect choice selected"<<endl;
 +
Pause();
 +
done = false;
 +
break;
 +
}
 
}
 
}
 
}
 
}
void BigInt::StringToBigInt(const string& goodString)
+
 
 +
void clearScreen()
 +
{
 +
system("cls");
 +
}
 +
char displayMenuAndGetSelection()
 +
{
 +
char choice;
 +
clearScreen();
 +
cout<<endl<<endl<<endl;
 +
cout<<"'1' -- View the list."<<endl<<endl;
 +
cout<<"'2' -- Search the list."<<endl<<endl;
 +
cout<<"'3' -- Delete a movie from the list."<<endl<<endl;
 +
cout<<"'4' -- Add a movie to the list."<<endl<<endl;
 +
cout<<"'5' -- Count the number of movies in the list."<<endl<<endl;
 +
cout<<"'6' -- Quit the program."<<endl<<endl;
 +
cin>>choice;
 +
cin.ignore(50,'\n');
 +
return choice;
 +
}
 +
void addPerson(SortedMovieList& movieList)
 +
{
 +
MovieType movie;
 +
string title, studio, stars;
 +
int year, gross;
 +
cout<<"Enter title: ";
 +
getline(cin,title,'\n');
 +
cout<<"Enter year: ";
 +
cin>>year;
 +
cout<<"Enter gross: ";
 +
cin>>gross;
 +
cout<<"Enter studio: ";
 +
cin>>studio;
 +
cout<<"Enter stars: ";
 +
cin>>stars;
 +
movie.Initialize(title,year,gross,studio,stars);
 +
bool success;
 +
movieList.listAdd(movie,success);
 +
 
 +
cout<<endl<<"The movie has been added to the list."<<endl;
 +
}
 +
void removePerson(SortedMovieList& movieList)
 
{
 
{
int counter = 0;
+
MovieType movie;
string str = "";
+
string title;
str = goodString;
+
cout<<"Enter the title of the movie you wish to remove: ";
numDigits = (int)str.length();
+
getline(cin,title,'\n');
char ch = ' ';
+
movie.Initialize(title,0,0,"","");
 +
bool success;
 +
movieList.listSubtract(movie,success);
   −
for(int i = numDigits-1; i >= 0; i--) //Reverse the string and position of numbers
+
cout<<endl<<"The movie has been removed from the list."<<endl;
{
  −
ch = str[i]; //Convert from char to numeric value
  −
Digits[counter] = ch - '0'; //char c = '2';
  −
counter++; //int n = c - '0';
  −
}
   
}
 
}
BigInt operator+(const BigInt& num1, const BigInt& num2)
+
void searchList(SortedMovieList& movieList)
 
{
 
{
BigInt sum;
+
MovieType movie;
if(AddBigInts(num1,num2,sum) == false)
+
string title;
{
+
cout<<"Enter the title of the movie you wish to search for: ";
OverFlowException ex(num1,num2,'+');
+
getline(cin,title,'\n');
throw ex;
+
bool success;
}
+
movie.Initialize(title,0,0,"","");
else
+
movieList.Search(movie,success);
return sum;
+
 
 +
cout<<endl<<"The search has been conducted."<<endl;
 
}
 
}
ostream& operator<<(ostream& os, const BigInt& num)
+
void Pause()
 
{
 
{
num.PrintBigInt(os);
+
cout<<endl<<"Press 'ENTER' to continue..."<<endl;
return os;
+
cin.get();
 
}
 
}
BigInt::BigInt()
+
void quitProgram()
:numDigits(0)
  −
{}
  −
BigInt::BigInt(const string& goodString)
   
{
 
{
StringToBigInt(goodString);
+
cout<<"Program terminated, good bye!"<<endl;
 +
cout<<endl;
 
}
 
}
 
</pre>
 
</pre>
123

edits

Navigation menu