Difference: CodeSepCom (1 vs. 2)

Revision 22018-04-19 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Separate Compilation

Digital Clock Main.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include "dtime.h"
using namespace std;

int main( )
{
    DigitalTime clock, old_clock;

    cout << "Enter the time in 24-hour notation: ";
    cin >> clock;

    old_clock = clock;
    clock.advance(15);
    if (clock == old_clock)
        cout << "Something is wrong.";
    cout << "You entered " << old_clock << endl;
    cout << "15 minutes later the time will be "
         << clock << endl;

    clock.advance(2, 15);
    cout << "2 hours and 15 minutes after that\n"
         << "the time will be "
         << clock << endl;

    return 0;
}
<-- end SyntaxHighlightingPlugin -->

Digital Clock dtime.h

>
>

Emily Dinkenson Example

main.cpp

  %CODE{"c++"}%
Deleted:
<
<
#ifndef DTIME_H_INCLUDED #define DTIME_H_INCLUDED

#include using namespace std;

class DigitalTime { public: friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2); //Returns true if time1 and time2 represent the same time; //otherwise, returns false.

DigitalTime(int the_hour, int the_minute); //Precondition: 0 <= the_hour <= 23 and 0 <= the_minute <= 59. //Initializes the time value to the_hour and the_minute.

DigitalTime( ); //Initializes the time value to 0:00 (which is midnight).

void advance(int minutes_added); //Precondition: The object has a time value. //Postcondition: The time has been changed to minutes_added minutes later.

void advance(int hours_added, int minutes_added); //Precondition: The object has a time value. //Postcondition: The time value has been advanced //hours_added hours plus minutes_added minutes.

friend istream& operator >>(istream& ins, DigitalTime& the_object); //Overloads the >> operator for input values of type DigitalTime. //Precondition: If ins is a file input stream, then ins has already been //connected to a file.

friend ostream& operator <<(ostream& outs, const DigitalTime& the_object); //Overloads the << operator for output values of type DigitalTime. //Precondition: If outs is a file output stream, then outs has already been //connected to a file. private: int hour; int minute; };

#endif // DTIME_H_INCLUDED

%ENDCODE%

Digital Clock dtime.cpp

%CODE{"c++"}% #include "dtime.h"

 #include
Changed:
<
<
#include #include #include "dtime.h"
>
>
#include #include #include #include #include #include "WordCount.h"
 using namespace std;
Added:
>
>
const string EMILY = "EmilyDickinson1.txt";
 
Changed:
<
<
//These FUNCTION DECLARATIONS are for use in the definition of //the overloaded input operator >>:
>
>
// Get words, put into vector vector getWords(string s); // Remove special characters, and make everything lower case string RemoveSpecials(string str); int findWord(vector words, string word);
 
Changed:
<
<
void read_hour(istream& ins, int& the_hour); //Precondition: Next input in the stream ins is a time in 24-hour notation, //like 9:45 or 14:45. //Postcondition: the_hour has been set to the hour part of the time. //The colon has been discarded and the next input to be read is the minute.
>
>
int main() { vector wordList; ifstream poems; vector words; int wordCount = 0;
 
Changed:
<
<
void read_minute(istream& ins, int& the_minute); //Reads the minute from the stream ins after read_hour has read the hour.
>
>
poems.open(EMILY.c_str());
 
Changed:
<
<
int digit_to_int(char c); //Precondition: c is one of the digits '0' through '9'. //Returns the integer for the digit; for example, digit_to_int('3') returns 3.

bool operator ==(const DigitalTime& time1, const DigitalTime& time2) { return (time1.hour = time2.hour && time1.minute = time2.minute); }

//Uses iostream and cstdlib: DigitalTime::DigitalTime(int the_hour, int the_minute) { if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59) { cout << "Illegal argument to DigitalTime constructor."; exit(1);

>
>
string s; while (poems.fail() && poems.eof()) { getline(poems, s); s = RemoveSpecials(s); words = getWords(s); for (int i = 0; i < words.size(); i++) { string word = words.at(i); //cout << "w:" << word << endl; int index = findWord(wordList,word); if (index >= 0) { wordList.at(index).inc(); } else { WordCount w = WordCount(word); wordList.push_back(w);
  }
Deleted:
<
<
else { hour = the_hour; minute = the_minute;
  } }
Changed:
<
<
DigitalTime::DigitalTime( ) : hour(0), minute(0) { //Body intentionally empty.
>
>
sort(wordList.begin(),wordList.end()); for (int i =0;i<wordList.size(); i++) { cout << wordList[i].getWord() << ":" << wordList[i].getCount() << endl;
 }
Deleted:
<
<
void DigitalTime::advance(int minutes_added) { int gross_minutes = minute + minutes_added; minute = gross_minutes%60;

int hour_adjustment = gross_minutes/60; hour = (hour + hour_adjustment)%24;

 }
Changed:
<
<
void DigitalTime::advance(int hours_added, int minutes_added) { hour = (hour + hours_added)%24; advance(minutes_added);
>
>
vector getWords(string s) { istringstream iss(s); vector words; do { string word; iss >> word; if (word.length() > 0) { words.push_back(word); //cout << "w:" << word << endl;
 }
Added:
>
>
} while (iss);
 
Changed:
<
<
//Uses iostream: ostream& operator <<(ostream& outs, const DigitalTime& the_object) { outs << the_object.hour << ':'; if (the_object.minute < 10) outs << '0'; outs << the_object.minute; return outs; }
>
>
return words;
 
Deleted:
<
<
//Uses iostream: istream& operator >>(istream& ins, DigitalTime& the_object) { read_hour(ins, the_object.hour); read_minute(ins, the_object.minute); return ins;
 }
Changed:
<
<
int digit_to_int(char c) { return ( static_cast(c) - static_cast('0') );
>
>
string RemoveSpecials(string str) { int i = 0, len = str.length(); while (i < len) { char c = str[i]; if (((c >= '0')&&(c <= '9')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= 'a')&&(c <= 'z')) || c = '\'' || c = ' ') { if ((c >= 'A')&&(c <= 'Z')) str[i] += 32; //Assuming dictionary contains small letters only. ++i; } else { str.erase(i, 1); --len;
 }
Changed:
<
<
//Uses iostream, cctype, and cstdlib: void read_minute(istream& ins, int& the_minute) { char c1, c2; ins >> c1 >> c2;

if (!(isdigit(c1) && isdigit(c2)))

{ cout << "Error illegal input to read_minute\n"; exit(1);

>
>
} return str;
  }
Changed:
<
<
the_minute = digit_to_int(c1)*10 + digit_to_int(c2);
>
>
int findWord(vector words, string word) {
 
Changed:
<
<
if (the_minute < 0 || the_minute > 59) { cout << "Error illegal input to read_minute\n"; exit(1);
>
>
for (int i = 0; i < words.size(); i++) { if (word == words[i].getWord()) { return i;
  } }
Changed:
<
<
//Uses iostream, cctype, and cstdlib: void read_hour(istream& ins, int& the_hour) { char c1, c2; ins >> c1 >> c2; if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':' ) ) ) { cout << "Error illegal input to read_hour\n"; exit(1);
>
>
return -1;
  }
Added:
>
>
%ENDCODE%

WordCount.h

%CODE{"c++"}% #include
 
Changed:
<
<
if (isdigit(c1) && c2 == ':') { the_hour = digit_to_int(c1);
>
>
using namespace std; #ifndef WORDCOUNT_H #define WORDCOUNT_H class WordCount { private: string word; int count; public: WordCount (); WordCount(string W); void inc(); int getCount(); string getWord(); friend bool operator<(WordCount wc1, WordCount wc2); }; #endif %ENDCODE%

WordCount.cpp

%CODE{"c++"}% #include "WordCount.h" WordCount::WordCount (){ word = ""; count = 0;
  }
Changed:
<
<
else //(isdigit(c1) && isdigit(c2)) { the_hour = digit_to_int(c1)*10 + digit_to_int(c2); ins >> c2;//discard ':' if (c2 = ':') { cout << "Error illegal input to read_hour\n"; exit(1);
>
>
WordCount::WordCount(string w){ word = w; count = 1;
  }
Added:
>
>
void WordCount::inc() { count++;
  }
Changed:
<
<
if ( the_hour < 0 || the_hour > 23 ) { cout << "Error illegal input to read_hour\n"; exit(1);
>
>
int WordCount::getCount(){ return count;
  }
Added:
>
>
string WordCount::getWord(){ return word;
 }
Added:
>
>
bool operator<(WordCount wc1, WordCount wc2) { return (wc1.count>wc2.count); }
 %ENDCODE%

Revision 12015-12-03 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Separate Compilation

Digital Clock Main.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include "dtime.h"
using namespace std;

int main( )
{
    DigitalTime clock, old_clock;

    cout << "Enter the time in 24-hour notation: ";
    cin >> clock;

    old_clock = clock;
    clock.advance(15);
    if (clock == old_clock)
        cout << "Something is wrong.";
    cout << "You entered " << old_clock << endl;
    cout << "15 minutes later the time will be "
         << clock << endl;

    clock.advance(2, 15);
    cout << "2 hours and 15 minutes after that\n"
         << "the time will be "
         << clock << endl;

    return 0;
}
<-- end SyntaxHighlightingPlugin -->

Digital Clock dtime.h

<-- SyntaxHighlightingPlugin -->
#ifndef DTIME_H_INCLUDED
#define DTIME_H_INCLUDED

#include <iostream>
using namespace std;

class DigitalTime
{
public:
    friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2);
    //Returns true if time1 and time2 represent the same time;
    //otherwise, returns false.

    DigitalTime(int the_hour, int the_minute);
    //Precondition: 0 <= the_hour <= 23 and 0 <= the_minute <= 59.
    //Initializes the time value to the_hour and the_minute.

    DigitalTime( );
    //Initializes the time value to 0:00 (which is midnight).

    void advance(int minutes_added);
    //Precondition: The object has a time value.
    //Postcondition: The time has been changed to minutes_added minutes later.

    void advance(int hours_added, int minutes_added);
    //Precondition: The object has a time value.
    //Postcondition: The time value has been advanced
    //hours_added hours plus minutes_added minutes.

    friend istream& operator >>(istream& ins, DigitalTime& the_object);
    //Overloads the >> operator for input values of type DigitalTime.
    //Precondition: If ins is a file input stream, then ins has already been
    //connected to a file.

    friend ostream& operator <<(ostream& outs, const DigitalTime& the_object);
    //Overloads the << operator for output values of type DigitalTime.
    //Precondition: If outs is a file output stream, then outs has already been
    //connected to a file.
private:
    int hour;
    int minute;
};


#endif // DTIME_H_INCLUDED
<-- end SyntaxHighlightingPlugin -->

Digital Clock dtime.cpp

<-- SyntaxHighlightingPlugin -->
#include "dtime.h"

#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime.h"
using namespace std;

//These FUNCTION DECLARATIONS are for use in the definition of
//the overloaded input operator >>:

void read_hour(istream& ins, int& the_hour);
//Precondition: Next input in the stream ins is a time in 24-hour notation,
//like 9:45 or 14:45.
//Postcondition: the_hour has been set to the hour part of the time.
//The colon has been discarded and the next input to be read is the minute.

void read_minute(istream& ins, int& the_minute);
//Reads the minute from the stream ins after read_hour has read the hour.

int digit_to_int(char c);
//Precondition: c is one of the digits '0' through '9'.
//Returns the integer for the digit; for example, digit_to_int('3') returns 3.

bool operator ==(const DigitalTime& time1, const DigitalTime& time2)
{
    return (time1.hour == time2.hour && time1.minute == time2.minute);
}

//Uses iostream and cstdlib:
DigitalTime::DigitalTime(int the_hour, int the_minute)
{
    if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59)
    {
        cout << "Illegal argument to DigitalTime constructor.";
        exit(1);
    }

    else
    {
        hour = the_hour;
        minute = the_minute;
    }
}
DigitalTime::DigitalTime( ) : hour(0), minute(0)
{
    //Body intentionally empty.
}


void DigitalTime::advance(int minutes_added)
{
    int gross_minutes = minute + minutes_added;
    minute = gross_minutes%60;

    int hour_adjustment = gross_minutes/60;
    hour = (hour + hour_adjustment)%24;
}

void DigitalTime::advance(int hours_added, int minutes_added)
{
    hour = (hour + hours_added)%24;
    advance(minutes_added);
}

//Uses iostream:
ostream& operator <<(ostream& outs, const DigitalTime& the_object)
{
    outs << the_object.hour << ':';
    if (the_object.minute < 10)
        outs << '0';
    outs << the_object.minute;
    return outs;
}

//Uses iostream:
istream& operator >>(istream& ins, DigitalTime& the_object)
{
    read_hour(ins, the_object.hour);
    read_minute(ins, the_object.minute);
    return ins;
}

int digit_to_int(char c)
{
    return ( static_cast<int>(c) - static_cast<int>('0') );
}

//Uses iostream, cctype, and cstdlib:
void read_minute(istream& ins, int& the_minute)
{
    char c1, c2;
    ins >> c1 >> c2;

    if (!(isdigit(c1) && isdigit(c2)))


    {
        cout << "Error illegal input to read_minute\n";
        exit(1);
    }

    the_minute = digit_to_int(c1)*10 + digit_to_int(c2);

    if (the_minute < 0 || the_minute > 59)
    {
        cout << "Error illegal input to read_minute\n";
        exit(1);
    }
}

//Uses iostream, cctype, and cstdlib:
void read_hour(istream& ins, int& the_hour)
{
    char c1, c2;
    ins >> c1 >> c2;
    if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':' ) ) )
    {
        cout << "Error illegal input to read_hour\n";
        exit(1);
    }

    if (isdigit(c1) && c2 == ':')
    {
        the_hour = digit_to_int(c1);
    }
    else //(isdigit(c1) && isdigit(c2))
    {
        the_hour = digit_to_int(c1)*10 + digit_to_int(c2);
        ins >> c2;//discard ':'
        if (c2 != ':')
        {
            cout << "Error illegal input to read_hour\n";
            exit(1);
        }
    }
    if ( the_hour < 0 || the_hour > 23 )
    {
        cout << "Error illegal input to read_hour\n";
        exit(1);
    }
}
<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback