Monday, April 20, 2015

C++ compare two different txt files

//============================================================================
// Name        : Dynqueue
// Author      : Zachary Standridge
// Version     :over a billion
// Copyright   : You wouldn't download a car would you?
// Description : opens and compare two different txt files.
//============================================================================#
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Dynqueue.h"

using namespace std;

int main()
{

   //creates an input file stream object

   fstream file1 ("file1.txt", ios::in);

   //creates another input file stream object

   fstream file2 ("file2.txt", ios::in);

   //creates two queues to hold characters.

    Dynque<char>queue1;
    Dynque<char>queue2;
    char ch1, ch2;

    //read all the characters from file 1 and puts them in queue 1.

    file1.get (ch1);
    while (!file1.eof())
    {
        queue1.enqueue(ch1);
        file1.get(ch1);
    }

    //read all the characters from file 2 and puts them in queue 2.

     file2.get (ch2);
    while (!file2.eof())
    {
        queue2.enqueue(ch2);
        file2.get(ch2);
    }

    //close

    file1.close();
    file2.close();

    //compare the queues

    while(!queue1.isEmpty()&& !queue2.isEmpty())
    {
    queue1.dequeue(ch1);
    queue2.dequeue(ch2);
    cout << ch1 << "\t"<< ch2 <<endl;
        if (ch1 != ch2)
       {
        cout << "\nThe files are not identical.\n";
        return 0;
       }
    }
        cout <<"\nThe files are identical. \n" << endl << endl;
        return 0;
}
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------.h file------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
#ifndef DYNQUEUE_H_INCLUDED
#define DYNQUEUE_H_INCLUDED
#include <iostream>

using namespace std;
//stock template

template<class T>
class Dynque
{
    private:
        struct QueueNode
        {
            T value;
            QueueNode *next;
        };

        QueueNode *front;
        QueueNode *rear;
        int numItems;
    public:
        Dynque();
        ~Dynque();
        void enqueue (T);
        void dequeue (T &);
        bool isEmpty ();
        bool isFull ();
        void clear ();
};
//empty queue//
template<class T>
Dynque<T>::Dynque()
{
    front = NULL;
    rear = NULL;
    numItems = 0;
}
//deconstructor
template <class T>
Dynque<T>::~Dynque()
{
    clear();
}
//Inserts
template <class T>
void Dynque<T>::enqueue (T item )
{
    QueueNode*newNode;
    //new node
    newNode = new QueueNode;
    newNode->value = item;
    newNode->next = NULL;
    //adjust front rear
    if (isEmpty())
    {
        front = newNode;
        rear = newNode;
    }
    else
    {
        rear->next = newNode;
        rear = newNode;
    }
    //update
    numItems++;
}
template <class T>
void Dynque<T>::dequeue(T &item)
{
    QueueNode *temp;
    if (isEmpty())
    {
        cout << "The queue is empty.\n";
    }
    else
    {
        item = front -> value;
        temp = front;
        front = front->next;
        delete temp;
        //update
        numItems--;
    }
}
template<class T>
bool Dynque<T>::isEmpty()
{
    bool status;

    if (numItems >0)
        status = false;
    else
        status = true;
    return status;
}
template <class T>
void Dynque<T>::clear()
{
    T value;

    while (!isEmpty())
        dequeue(value);
}

#endif // DYNQUEUE_H_INCLUDED
---------------------------------------------------------------------------------------------------------------------
----------------------------------------------file1.txt---------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
The quick brown fox jumps over the lazy dog.

---------------------------------------------------------------------------------------------------------------------
----------------------------------------------file2.txt---------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
'I never forget a face, but in your case I’d be glad to make an exception.'




------------------------------------------------------------------------------------------------------------------------
create all files in the same folder, compile the cpp and enjoy

Linked List

//============================================================================
// Name        : Employee.cpp
// Author      : Zachary Standridge
// Version     :over a billion
// Copyright   : You wouldn't download a car would you?
// Description : Create and organize a list
//============================================================================

#include <iostream>

using namespace std;

struct  node
{
    int number;
    node *next;
};

bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, int number);
void insert(node *&head, node *&last, int number);
void remove(node *&head, node *&last);
void showList(node *current);

bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}
char menu()
{
    char choice;

    cout <<"Menu\n";
    cout <<"------------------\n";
    cout <<"1. Add Number\n";
    cout <<"2. Remove Number\n";
    cout <<"3. Show List\n";
    cout <<"4. Exit\n";

    cin >> choice;
    return choice;
}
void insertAsFirstElement(node *&head, node *&last, int number)
{
    node *temp = new node;
    temp ->number = number;
    temp ->next = NULL;
    head = temp;
    last = temp;
}
void insert(node *&head, node *&last, int number)
{
    if(isEmpty(head))
        insertAsFirstElement(head,last,number);
    else
        {
        node *temp = new node;
        temp ->number = number;
        temp ->next = NULL;
        last ->next = temp;
        last = temp;
        }
}
void remove(node *&head, node *&last)
{
    if (isEmpty(head))
        cout<<"There is nothing here\n";
    else if (head == last)
        {
        delete head;
        head == NULL;
        last == NULL;
        }
    else
    {
        node *temp = head;
        head = head->next;
        delete temp;
    }

}
void showList(node *current)
{
   if(isEmpty(current))
       cout<< "The list is empty.\n";
   else
   {
    cout << "The list contains : \n";
    while (current != NULL)
       {
           cout << current -> number << endl;
           current = current->next;
       }
   }
}

int main ()
{
    node *head = NULL;
    node *last = NULL;
    char choice;
    int number;
    do
        {
        choice = menu ();
        switch(choice)
               {
                case '1': cout<< "Please enter a number;";
                cin>>number;
                insert(head, last,number);
                break;
                case '2': remove (head, last);
                break;
                case '3': showList(head);
                break;
                default: cout<< "System exit\n";
               }
        }
        while(choice != '4');
    return 0;
}

Thursday, April 2, 2015

Employee (Amended) This time it checks for shift number, pay rate, and employee number


#include <iostream>
#include <string>
using namespace std;

class Employee
{
private:
    string employeeName;
    int employeeNumber;
    string hireDate;
    //member function to validate the E number
    void checkEmployeeNumber()
    {
        if (employeeNumber < 0 ||employeeNumber > 9999 )
            throw invalidEmployeeNumber();
    }
public:
    //exception class
    class invalidEmployeeNumber
    {};
    //constructors
    Employee (string name, int number, string hDate)
    {
        //set variables
        employeeName = name;
        employeeNumber = number;
        hireDate = hDate;

        //check the employee number
        checkEmployeeNumber();
    }
    //mutators
    void setEmployeeName (string name)
    {
        employeeName=name;
    }
    void setEmployeeNumber (int number)
    {
        employeeNumber=number;
        checkEmployeeNumber();
    }
    void setHireDate (string hDate)
    {
        hireDate = hDate;
    }
    //accessors
    string getEmployeeName() const
    {
        return employeeName;
    }
    int getEmployeeNumber() const
    {
        return employeeNumber;
    }
    string getHireDate () const
    {
        return hireDate;
    }

};
class ProductionWorker:
    public Employee
    {
    private:
        int shift;
        double hourlyPayRate;
        //member function checking shift number
        void checkShift()
        {
            if
                (shift < 1 || shift > 2)
                    throw InvalidShift();
        }
        // checking pay rate
        void checkPayRate()
        {
            if
                (hourlyPayRate < 0)
                    throw InvalidPayRate();
        }

    public:
        //exception class
        class InvalidShift
        {};
        class InvalidPayRate
        {};
        //constructors
        ProductionWorker (string name, int number, string hDate, int shiftNum, double payRate):
                Employee (name, number, hDate)
                {
                    //set the member variables
                    shift = shiftNum;
                    hourlyPayRate = payRate;
                    //check the shift and pay rate
                    checkShift();
                    checkPayRate();
                }
        //mutators
        void setShift (int shiftNum)
        {
        shift = shiftNum;
        checkShift();
        }
        void setHourlyPayRate (double payRate)
        {
        hourlyPayRate = payRate;
        checkPayRate();
        }
        //accessors
        int getShift () const
        {
            return shift;
        }
        double getHourPayRate()
        {
            return hourlyPayRate;
        }
    };
    //prototype
    void testValues (string, int, string, int, double);

    int main()
    {
//test good data
       cout <<" Testing good data....\n";
       testValues("Pedro Colon", 1234, "12/01/2000",1,550.00);
//test bad shift number
       cout <<"\n Testing bad shift number....\n";
       testValues("Pedro Colon", 1234, "12/01/2000",9,550.00);
//test bad employee number
       cout <<"\n Testing bad employee number....\n";
       testValues("Pedro Colon", 91234, "12/01/2000",1,550.00);
//test bad pay rate
       cout <<"\n Testing  bad pay rate ....\n";
       testValues("Pedro Colon", 1234, "12/01/2000",1,-550.00);
       return 0;
    }
void testValues (string name, int number, string hDate, int shift, double payRate)
{
    try
            {
            ProductionWorker (name, number, hDate, shift, payRate);
            //good stuff
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\n Good data\n" ;
            }
        catch (ProductionWorker::invalidEmployeeNumber)
            {
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\nInvalid Employee Number encountered \n";
            }
        catch (ProductionWorker::InvalidShift)
            {
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\nInvalid Shift Number encountered \n";
            }
        catch (ProductionWorker::InvalidPayRate)
            {
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\nInvalid Pay Rate encountered \n";
            }
}