Question: I need help modifying my program to prompt the user for a file name, then read BigIntegers from that file and keep a running total.

I need help modifying my program to prompt the user for a file name, then read BigIntegers from that file and keep a running total. I need to have my program output the total to a file so that I can copy and paste the result.

Below is the the bigInt.txt file and my code.

bigInt.txt file

245665467856549678546678 2545466554675487 34245654686546708 25465464 24546354654685675465465469546789546678901046754345464054 425465467565466785409 1233789132678978780 13223978132576976 10567007484507483087000 34056706789078798054

My Code

#include #include #include

using namespace std;

int valueOf(char c) { if ((c >= '0') && (c <= '9')) { return c - '0'; } else { return c - 'a' + 10; } }

std::vector toDigits(string a) { vector result(a.length()); for (unsigned int i = 0; i < result.size(); i++) { result[i] = valueOf(a[i]); }return result; }

void displayDecimalDigits(vector a) { for (unsigned int i = 0; i < a.size(); ++i) { cout << a[i] << " "; } cout << endl; }

std::vector addLeadingZeros(vector v, int b) { for (int i = 0; i < b; i++) { v.insert(v.begin(), 0); }return v; }

std::vector trimLeadingZeros(vector v) { int count = 0; for (unsigned int i = 0; i <= v.size(); i++) { if (v[i] == 0) { ++count; } else { break; } } vector result(v.size() - count); for (unsigned int i = count, r = 0; i < v.size(); r++, i++) { result[r] = v[i]; } return result; }

std::vector normalize(vector v, int b) { vector result = v; result = addLeadingZeros(result, 1); for (int i = result.size() - 1; i >= 0; --i) { while (result[i] >= b) { result[i] -= b; result[i - 1] += 1; } } result = trimLeadingZeros(result); return result; }

std::vectoradd(vectorv1, vectorv2) { if (v1.size() > v2.size()) { int diff = v1.size() - v2.size(); v2 = addLeadingZeros(v2, diff); } else { int diff = v2.size() - v1.size(); v1 = addLeadingZeros(v1, diff); }

vector result; for (int i = 0; i < v1.size(); ++i) { result.push_back(v1[i] + v2[i]); }

result = normalize(result, 10); result = trimLeadingZeros(result); return result; }

int main() {

string s1 = "001238042"; std::vector b1 = toDigits(s1); displayDecimalDigits(b1);

b1 = trimLeadingZeros(b1); displayDecimalDigits(b1);

b1 = addLeadingZeros(b1, 4); displayDecimalDigits(b1);

b1 = normalize(b1, 10); displayDecimalDigits(b1);

cout << "Welcome to BigInteger Version 1! "; cout << "Please enter a Big Integer: "; cin >> s1; std::vectorv1 = toDigits(s1); cout << "Please enter a Big Integer: "; cin >> s1; std::vectorv2 = toDigits(s1); std::vectorsum = add(v1, v2); cout << " The sum is: "; displayDecimalDigits(sum);

cout << " Thank you for playing BigInteger!";

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!