Question: Plz implement this task in c++: (It's great you write it on linux or MAC by using g++ compiler) 1. Add two integer number with

Plz implement this task in c++: (It's great you write it on linux or MAC by using g++ compiler)

1. Add two integer number with a given base

2. Implement Karatsuba Multiplication with a given base

3. You cannot change any of following files, you should provide implementations for the methods provided in the operations.h file. Plz provide the implementations for the functions in your own .cpp file.

?main.cpp

#include #include #include "typedef.h" //Type definitions #include "operations.h" #include "utils.h" using namespace std;

// The main method parses the arguments and check for correctness. Then calls sum and kmul method with the input values. // Finally, the return values are displayed on the command-line. int main(int nargs, char *argv[]){

//Note that in c/c++, the first argument is always the command name. if (nargs <4){ cerr << "Need three arguments. 1) I1: First integer 2) I2: Second integer 3) B: Base of I1 and I2. "< return 1; } //Parsing the base, which is the third input. unsigned int base = stoul(string(argv[3])); if (base<2 || base>10){ cerr << "Base should be an integer in [2,10]"< return 1; } //Parsing input Integers. In parsed (integer_1 and integer_2) values the zeroth index represent the most significant digit and //the least significant digit is at the integer.size()-1 location. Integer integer_1 = to_integer(argv[1],base); Integer integer_2 = to_integer(argv[2],base); //Calling school method of summation. Integer sum = add(integer_1,integer_2,base);

//Calling Karatsuba multiplication Integer kmultiplication = kmul(integer_1,integer_2,base);

//Displaying the output; first sum then multiplication. cout << to_string(sum)<<" "< return 0; }

operations.h

#pragma once #include "typedef.h"

// Add two Integers a and b given in base, and returns the output as an Integer. Integer add( Integer a, Integer b,unsigned int base);

// Multiply two Integers a and b given in base using the Karatsuba algorithm, and // returns the output as an Integer. Integer kmul( Integer a, Integer b,unsigned int base);

typedef.h

#pragma once #include #include

// Define a single digit. typedef unsigned int Digit;

// Define an integer as a vector of Digits. typedef std::vector Integer;

utils.cpp

#include "utils.h" #include "typedef.h" #include #include

Integer zeros_with_digits(unsigned int dim) { Integer out(dim,0); return out; }

void pad_back(Integer &in, unsigned int n) { for (unsigned int i = 0;i in.push_back(0); } } void pad_front( Integer &in, unsigned int n) { for (unsigned int i = 0;i in.insert(in.begin(),0); } }

std::string to_string(Integer a,bool ignore_most_sig_zeros) { std::stringstream ss; bool ignore=ignore_most_sig_zeros; for (auto it = a.begin();it!=a.end();it++){ if (ignore && *it==0) { continue; }else{ ignore=false; } ss << *it; }

return ss.str(); }

Integer to_integer(std::string str,unsigned int base) { Integer out; for (unsigned int i = 0;i auto digit = (unsigned int)str[i]-48; if (digit >=base){ throw std::invalid_argument("Input contains invalid characters"); } out.push_back(digit); }

return out; }

utils.h

#pragma once

#include "typedef.h"

#include

// Return an Integer with length dim all filled with zero digits Integer zeros_with_digits(unsigned int dim);

// Pad specified number (n) of zeros to the least significant end. void pad_back(Integer &in, unsigned int n);

// Pad specified number (n) of zeros to the most significant end. void pad_front( Integer &in, unsigned int n);

// Return the string representation of the Integer. If ignore_most_sig_zeros == true, // then any leading zeros will not be in the string. std::string to_string(Integer a,bool ignore_most_sig_zeros = false);

// Convert a sequence of digit string to an Integer with the specified base. Integer to_integer(std::string str,unsigned int base);

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!