scosant
12/17/2012 - 11:25 AM

projecteuler013 - large sum

projecteuler013 - large sum

/* Scott Santarromana
 * projecteuler013 - large sum
 *
 * Work out the first ten digits of the sum of the following
 * one-hundred 50-digit numbers.
 * (Numbers in input.txt)
 *
 * Solving this problem required the creation of a BigNumber class
 * since even the largest unsigned 64-bit integer type can only
 * handle a value up to 18446744073709551615 (20 places).
 */

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include "BigNumber/big_number.h"

int main(int argc, char* argv[]) {
    std::ifstream myfile("projecteuler013.txt");
    if (myfile.is_open()) {
    snslib::BigNumber result;
    while (!myfile.eof()) {
            std::string line;
            getline(myfile, line);

        line.erase(std::remove(line.begin(),
                   line.end(), '\n'), line.end());
        line.erase(std::remove(line.begin(),
                   line.end(), '\r'), line.end());

        snslib::BigNumber rhs(line);
        std::cout << "  " << result.ToString() << std::endl << " +"
              << rhs.ToString() << std::endl;
        result = result + rhs;
        std::cout
        << "  --------------------------------------------------\n  "
        << result.ToString() << "\n\n";
    }
    myfile.close();
    } else {
    std::cout << "Unable to open file";
    }
    return 0;
}