Notes week of Dec 12

card-struct.cpp

// struct-example
#include <iostream>
using namespace std;

const int SPADES = 0;
const int HEARTS = 1;
const int CLUBS = 2;
const int DIAMONDS = 3;

const int ACE = 1;
const int JACK = 11;
const int QUEEN = 12;
const int KING = 13;

struct card
{
    int suit;
    int rank;
}; // Strange but true: this semi is required

void print_card(card c) // struct parameter
{
    switch(c.rank)
    {
    case ACE: cout << "Ace"; break;
    case JACK: cout << "Jack"; break;
    case QUEEN: cout << "Queen"; break;
    case KING: cout << "King"; break;
    default: cout << c.rank;
    }
    cout << " of ";
    switch(c.suit)
    {
    case SPADES: cout << "Spades"; break;
    case HEARTS: cout << "Hearts"; break;
    case CLUBS: cout << "Clubs"; break;
    case DIAMONDS: cout << "Diamonds"; break;
    }
    cout << endl;
}

int main()
{
    card c1;
    c1.suit = DIAMONDS;
    c1.rank = KING;
    while( c1.rank >= ACE )
    {
        print_card(c1);
        c1.rank --;
    }
    return 0;
}

employees.cpp

// employees.cpp
#include <iostream>
#include <iomanip>
using namespace std;

struct date
{
    int year;
    int month; // 1-12
    int day;   // 1-31
};

struct employee
{
    int id;
    string firstName;
    string lastName;
    date birthDate;
    date hireDate;
    float salary;
};

ostream& operator<< (ostream& out, date d)
{
    out.fill('0');
    out << setw(4) << d.year
        << '-' << setw(2) << d.month
        << '-' << setw(2) << d.day;
    return out;
}

ostream& operator<< (ostream& out, employee e)
{
    out << e.id << ' ' << e.lastName
        << ' ' << e.firstName << ' '
        << e.salary << ' ' << e.hireDate;
    return out;
}

int main()
{
    // One way to initialize:
    date today;
    today.year = 2016;
    today.month = 12;
    today.day = 12;

    // Or use an "initializer"
    // but error-prone because MUST match
    // same order as in struct declaration.
    date today2 = {2016, 12, 12};

    // Initializer that isn't so error-prone:
    date today3 = {.year = 2016,
                   .month = 7,
                   .day = 5};

    employee me =
        { .id = 29837,
          .firstName = "Chris",
          .lastName = "League",
          .birthDate = { .year = 1973,
                         .month = 3,
                         .day = 25},
          .hireDate = today3
        };
    cout << me << endl;
    return 0;
}

sort-demo.cpp

#include <algorithm>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int SIZE = 20;

bool byLastDigit(int a, int b)
{
    int c = a%10;
    int d = b%10;
    return c<d || c==d && a<b;
}

int main()
{
    srand(time(NULL));
    vector<int> nums(SIZE);
    for(int i = 0; i < SIZE; i++)
    {
        nums.at(i) = rand();
    }
    //sort(nums.begin(), nums.end()); // default criterion
    sort(nums.begin(), nums.end(), byLastDigit);
    for(int i = 0; i < SIZE; i++)
    {
        cout << nums.at(i) << endl;
    }
    return 0;
}