Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Please Show output screen please Modify your program to prompt the use for a file name, then read BigIntegers from that file and keep

C++ Please

Show output screen please

Modify your program to prompt the use for a file name, then read BigIntegers from that file and keep a running total.

Program that need to be modified:

#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); //this line has been changed return result; }

std::vector add(vector v1, vector v2) {

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::vector v1 = toDigits(s1); cout << "Please Enter a Big Integer: "; cin >> s1; std::vector v2 = toDigits(s1); std::vector sum = add(v1, v2); cout << " The Sum is: "; displayDecimalDigits(sum); cout << "Thank you for Playing BigInteger!"; }

File:

245665467856549678546678 2545466554675487 34245654686546708 25465464 24546354654685675465465469546789546678901046754345464054 425465467565466785409 1233789132678978780 13223978132576976 10567007484507483087000 34056706789078798054

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

LO22.5 List the main elements of existing federal farm policy.

Answered: 1 week ago

Question

To which retailer will you send your merchandise?

Answered: 1 week ago