Monday, December 5, 2016

Lab Materials for Dec. 5

Presentation
Data
Fields: City, Province, Population, Population Growth in %
Toronto,ON,2615060,9.63
Montreal,QC,1649519,6.62
Calgary,AB,1096833,42.8
Ottawa,ON,833391,22.5
Edmonton,AB,812201,31.79
Missasauga,ON,713443,31.06
Winnipeg,MB,663617,7.3
Vancouver,BC,603502,17.41
Brampton,ON,523911,96.31
Hamilton,ON,519949,11.15


The code:

//
//  main.cpp
//  Dec5Class
//
//  Created by Mark Brautigam on 12/5/16.
//  Copyright © 2016 Mark Brautigam. All rights reserved.
//

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

struct City {
    string name;
    string province;
    int population;
    double delta;
};

string baseDir = "/Users/markb/Desktop/";

void split(const string &s, char delim, vector<string> &elems) {
    //-------------------------------------------------------
    // This function splits a line into a vector of strings
    // based on a separating character, such as a comma.
    // s is the input string
    // delim is the separating character.
    // elems is the vector that will hold the output.
    //-------------------------------------------------------
    stringstream ss;
    ss.str(s);
    string item;
    elems.clear();
    while (getline(ss, item, delim)) {
        elems.push_back(item); }
}

void readFileIntoVector (string filepath, vector<City> &vc) {
    ifstream input;
    input.open(baseDir + "canada.csv");
    string line;
    vector<string> fields;
    City city;
    while (!input.eof()) {
        // input >> line;
        getline(input, line);
        if (line.length() < 2)
            continue;
        split (line, ',', fields);
        city.name = fields[0];
        city.province = fields[1];
        city.population = atoi(fields[2].c_str());
        city.delta = atof (fields[3].c_str());
        vc.push_back(city);
    }
    return;
}

void printData(const vector<City> &vc, string fn) {
    ofstream output;
    output.open (fn);
    
    for (int i=0; i<vc.size(); i++) {
        output.width(15);
        output << left << vc[i].name << " ";
        output.width(5);
        output << left << vc[i].province << " ";
        output.width(10);
        output << right << vc[i].population << " ";
        output.width(8);
        output << right << fixed << setprecision(2) << vc[i].delta << " ";
        output << endl;
    }
    output.close();
}

bool outOfOrder(const City &a, const City &b, int sortfield) {
    switch (sortfield) {
        case 0 : return a.name       > b.name;
        case 1 : return a.province   > b.province;
        case 2 : return a.population > b.population;
        case 3 : return a.delta      > b.delta;
        default : return a.name      > b.name;
    }
}

void bubblesort (vector<City> &vc, int sortfield) {
    for (int i=0; i<vc.size(); i++) {
        for (int j=0; j<vc.size()-1; j++) {
            // if (vc[j].delta > vc[j+1].delta) {
            if (outOfOrder (vc[j], vc[j+1], sortfield)) {
                City swap = vc[j];
                vc[j] = vc[j+1];
                vc[j+1] = swap;
            }
        }
    }
}

int requestSortfield() {
    cout << "Choose a field to sort on by number:\n";
    cout << "0 : City\n";
    cout << "1 : Province\n";
    cout << "2 : Population\n";
    cout << "3 : Population Change\n";
    cout << "Your choice: ";
    int user;
    cin >> user;
    return user;
}

int main(int argc, const char * argv[]) {
    
    vector<City> cities;
    
    readFileIntoVector (baseDir + "canada.csv", cities);
    int sortfield = requestSortfield();
    bubblesort (cities, sortfield);
    
    string outputFileName =
        baseDir + "canada-sorted-" + to_string(sortfield) + ".txt";
    
    printData(cities, outputFileName);
    
    return 0;
}


No comments:

Post a Comment