CSE-111 • Spring 2022 • Program 1 • Overloading and operators 1 of 7$Id: asg1-dc-bigint.mm,v 1.267 2022-04-03 12:04:19-07 - - $/afs/cats.ucsc.edu/courses/cse111-wm/Assignments/asg1-dc-biginthttps://www2.ucsc.edu/courses...
Using C++11/14/17 (g++ -std=gnu++20)All programming in this course will be done C++ style, not C style.Do not use : Instead, use :char* strings <string>C arrays <vector><stdio.h>, <cstdio> <iostream>, <iomanip>pointers <shared_ptr> or <unique_ptr>union inheritance or <variant><header.h> <cheader>Include only C++ header files and use the declaration using namespace std; Include<cheader> files only when C++ header files do not provide a necessary facility.Include <header.h> files from C only when an appropriate <cheader> file does notexist. Use the script cpplint.py.perl (a wrapper for cpplint.py) to check style.The production system for all work is unix.ucsc.edu using g++. Compile withg++ -std=gnu++20 -g -O0 -Wall -Wextra -Wpedantic -Wshadow -Wold-style-castFollowing is a description of these options :• -std=gnu++20 Gnu dialect of C++20.• -g produces debugging information into object files and the binary executable.This is necessary for gdb and valgrind to use symbolic names.• -O0 reduces compilation time and makes debugging produce more expectedresults. Optimization may rearrange bugs in code in unexpected ways.• -Wall enables all the warnngs about questionable constructions.• -Wextra enables extra warnings that are not enabled with -Wall.• -Wpedantic issues all warnings required by strict ISO C++ and rejects all programsthat do not conform to ISO C++.• -Wshadow warns whenever a local variable or declaration shadows another variable,parameter, or class member.• -Wold-style-cast warns about the use of any old-style (C-style) cast. Instead,use one of : static_cast, dynamic_cast, const_cast, reinterpret_cast. Betteryet, code in suchaway as to not need casts.• -fdiagnostics-color=never prevents the compiler from using those silly annoyingcolors in diagnostics.The particular g++ compiler we will be using is-bash-1$ which g++/opt/rh/devtoolset-11/root/usr/bin/g++-bash-2$ g++ --version | grep -i g++g++ (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1)-bash-3$ uname -npounix1.lt.ucsc.edu x86_64 GNU/LinuxIf you develop on your personal system, be sure to port and test your code on theLinux timeshares. If it compiles and runs on your system, but not on the timeshares,then it does not wor k.CSE-111 • Spring 2022 • Program 1 • Overloading and operators 2 of 7OverviewThis assignment will involve overloading basic integer operators to perform arbitraryprecision integer arithmetic in the style of dc(1). Your class bigint will intermixarbitrarily with simple integer arithmetic.To begin read the man(1) page for the command dc(1) :man -s 1 dcA copy of that page is also in this directory. Your program will use the standard dcas a reference implemention and must produce exactly the same output for thecommands you have to implement :+-*/%^cdfpqIf you have any questions as to the exact format of your output, just run dc(1) andmake sure that, for the operators specified above, your program produces exactlythe same output. A useful program to compare output from your program with thatof dc(1) is diff(1), which compares the contents of two files and prints only the differences.Also look in the subdirectory misc/ for some examples.See also :• dc (computer program)https://en.wikipedia.org/wiki...(computer_program)• dc, an arbitrary precision calculatorhttps://www.gnu.org/software/...Implementation strategyAs before, you have been given starter code.(a) Makefile, debug, and util If you find you need a function which does not properlybelong to a given module, you may add it to util.(b) The module scanner reads in tokens, namely a NUMBER, an OPERATOR, or SCANEOF.Each token returns a token_t, which indicates what kind of token it is (theterminal_symbol symbol), and the string lexinfo associated with the token.Only in the case of a number is there more than one character. Note that oninput, an underscore (_) indicates a negative number. The minus sign (-) isreserved only as a binary operator. The scanner also has defined a couple ofoperator<< for printing out scanner results in debug mode. This is strictly fordebugging.(c) The main program main.cpp, has been implemented for you. For the six binaryarithmetic functions, the right operand is popped from the stack, then the leftoperand, then the result is pushed onto the stack.(d) The module iterstack can not just be the STL stack, since we want to iteratefrom top to bottom, and the STL stack does not have an iterator. A stackdepends on the operations back(), push_back(), and pop_back() in the underlyingcontainer. We could use a vector, a deque, or just a list, as long as the requisiteoperations are available.CSE-111 • Spring 2022 • Program 1 • Overloading and operators 3 of 7Class bigintThen we come to the most complex part of the assignment, namely the class bigint.Operators in this class are heavily overloaded.(a) Most of the functions take a arguments of type const bigint&, i.e., a constantreference, for the sake of efficiency. But they have to return the result byvalue.(b) The operator<< can’t be a member since its left operand is an ostream, so wemake it a friend, so that it can see the innards of a bigint. Note now dc printsreally big numbers. operator<< is used by debugging functions.(c) The function print (suitably modified) is used for actual output.(d) The relational operators == and < are coded individually as member functions.The others, !=, <=, >, and >= are defined in terms of the essential two.(e) All of the functions of bigint only work with the sign, using ubigint to do theactual computations. So bigint::operator+ and bigint::operator- will checkthe signs of the two operands then call ubigint::operator+ or ubigint::operator-,as appropriate, depending on the relative signs and magnitudes. Themultiplication and division operators just call the corresponding ubigint operators,then adjust the resulting sign according to the rule of signs.Class ubigintClass ubigint implements unsigned large integers and is where the computationalwork takes place. Class bigint takes care of the sign. Now we turn to the representationof a ubigint, which will be represented by vector of bytes.(a) Replace the declarationusing ubigvalue_t = unsigned long;withusing ubigvalue_t = vector<uint8_t>;in the header file <ubigint.h>. The type uint8_t is an unsigned 8-bit integerdefined in <cstdint>.(b) In storing the big integer, each digit is kept as an integer in the range 0 to 9 inan element of the vector. Since the arithmetic operators add and subtractwork from least significant digit to most significant digit, store the elements ofthe vector in the same order. That means, for example, that the number 4629would be stored in a vector v as : v[3]==4, v[2]==6, v[1]==2, v[0]==9. In otherwords, if v[k]==d, then the digit’s place value is d*pow(10,k). In mathematicalnotation, the value of a radix 10 (base 10) number v with n digits is :n−1i=0 vi10i= vn−110n−1vn−210n−2... + v2102v1101v0100(c) In order for the comparisons to work correctly, always store numbers in acanonical form : After computing a value from any one of the six arithmeticoperators, always trim the vector by removing all high-order zeros :while (size() > 0 and back() == 0) pop_back();CSE-111 • Spring 2022 • Program 1 • Overloading and operators 4 of 7(d) Canonical form.• Zero is represented as a vector of size zero and a positive sign.• High-order zeros are suppressed.• All digits are stored as uint8_t values in the range 0...9, not as characters inthe range ’0’...’9’.• To print a digit, cast it to an integer : cout << static_cast<int> (digit).• This can be done more easily by : cout << int (digit), which looks like actor call.(e) The scanner will produce numbers as strings, so scan each string from the endof the string, using a const_reverse_iterator (or other means) from the end ofthe string (least significant digit) to the beginning of the string (most signifi-cant digit) using push_back to append them to the vector.Implementation of operators(a) For bigint::operator+, check the signs.(1) If the signs are the same :• Call ubigint::operator+ with the unsigned numbers.• The sign of the result is the sign of either number.(2) If the signs are different :• Call ubigint::operator- with the larger number as its left number.• The sign of the result is the sign of the larger number.(b) The operator bigint::operator-, check the signs.(1) If the signs are different :• Call ubigint::operator+ with the unsigned numbers.• The sign of the result is the sign of the left number.(2) If the signs are the same :• Call ubigint::operator- with the larger number as its left number.• If the left number is larger, the sign of the result is its sign.• Else the the result has the opposite of the sign of the right number.(c) For the above bigint::operator+ and bigint::operator-, to find the ‘‘larger’’number, make use of ubigint::operator<. Since the numbers are kept incanonical form (see above), to compare them :(1) Check the size() of each vector. If different, the larger number has thegreater size.(2) If the sizes are the same, write a loop iterating from the highest-orderdigit toward the lowest-order digit, comparing digit by digit.• As soon as a difference is found, return true or false, as appriate.• If all digits are the same, then return false.(d) To implement ubigint::operator+, create a new ubigint and proceed from thelow order end to the high order end, adding digits pairwise. For any sum >=10, take the remainder and add the carry to the next digit. Use push_back toappend the new digits to the ubigint. When you run out of digits in theshorter number, continue, matching the longer vector with zeros, until it isdone. Make sure the sign of 0 is positive.CSE-111 • Spring 2022 • Program 1 • Overloading and operators 5 of 7(e) To implement ubigint::operator-, also create a new empty vector, startingfrom the low order end and continuing until the high end. If the left digit issmaller than the right digit, the subtraction will be less than zero. In thatcase, add 10 to the digit, and set the borrow to the next digit to −1. After thealgorithm is done, pop_back all high order zeros from the vector before returningit. Make sure the sign of 0 is positive.(f) To implement bigint::operator==, check to see if the signs are the same andubigint::operator== returns true.(g) To implement ubigint::operator==, just use the vector::operator== comparisonfunction.(h) To implement bigint::operator<, remember that a negative number is lessthan a positive number. If the signs are the same, use ubigint::operator< fora comparison. For positive numbers, the smaller one is less. and for negativenubmers, the larger one is less.(i) To implement ubigint::operator<, check the size() of each vector. Theshorter one is less than the longer one. If the size() are the same, scan thevectors in parallel from the most significant digit to the last significant digituntil a difference is found.(j) Implement function bigint::operator*, which uses the rule of signs to determinethe result. The number crunching is delegated to ubigint::operator*,which produces the unsigned result.(k) Multiplication in ubigint::operator* proceeds by allocating a new vectorwhose size is equal to the sum of the sizes of the other two operands. If u is avector of size m and v is a vector of size n, then in O(mn) speed, perform an outerloop over one argument and an inner loop over the other argument, adding thenew partial products to the product p as you would by hand. The algorithmcan be described as follows :p=all zerosfor i in interval [0,m):carry = 0for j in interval [0,n):digit = p[i+j] + u[i] * v[j] + carryp[i+j] = digit % 10carry = digit / 10p[i+n] = carryNote that the interval [a,b) refers to the half-open interval including a butexcluding b. This is the set {x| a<=x && x<b}. In the same way,apair of iteratorsin C++ is used to bound an interval (begin and end pair).(l) Long division is complicated if done correctly. See a paper by P. BrinchHansen, ‘‘Multiple-length division revisited : A tour of the minefield’’, Software— Practice and Experience 24, (June 1994), 579–601. Algorithms 1 to 12 areon pages 13–23, Note that in Pascal, array bounds are part of the type, whichis not true for vectors in C++.CSE-111 • Spring 2022 • Program 1 • Overloading and operators 6 of 7• multiple-length-division.pdf• http://brinch-hansen.net/pape...• http://citeseerx.ist.psu.edu/...(m) The function divide as implemented uses the ancient Egyptian division algorithm,which is slower than Hansen’s Pascal program, but is easier to understand.Replace the long values in it by vector<digit_t>. The logic is shownalso in misc/divisioncpp.cpp. The algorithm is rather slow, but the big-Oanalysis is reasonable.(n) The unsigned division function that is provided depends on two private functions,multiply_by_2 and divide_by_2, which are in-lace non-constant functions.They both perform without creating a new object.(1) To implement multiply_by_2, iterate from the low order digit, and doubleeach digit (remainder 10), carrying to the next higher digit. At the end, ifthe carry is 1, use push_back.(2) To implement divide_by_2, iterate from the low order digit, and divideeach digit by 2. Then, if the next higher digit is odd, add 5 to the currentdigit. Be careful of the end, and pop_back any remaining high orderzeros.(o) Modify operator<<, first just to print out the number all in one line. You willneed this to debug your program.(p) The function print will print numbers in the same way as dc(1) does.(q) The pow function uses other operations to raise a number to a power. If theexponent does not fit into a single long print an error message, otherwise dothe computation. The power function is not a member of either bigint or ubigint,and is just considered a library function that is implemented using moreprimitive operations.Memory leak and other problemsMake sure that you test your program completely so that it does not crash on a SegmentationFault or any other unexpected error. Since you are not using pointers,and all values are inline, there should be no memory leak. Use valgrind(1) to checkfor and eliminate uninitialized variables and memory leak.If your program is producing strange output or segmentation faults, use gdb(1) andthe debug macros in the debug module of the code/ subdirectory.What to submitSubmit source files and only source files : Makefile, README, and all of the headerand implementation files necessary to build the target executable. If gmake does notbuild ydc your program can not be tested and you lose 1/2 of the points for theassignment. Use checksource on your code to verify basic formatting.Look in the grader’s score subdirectory for instructions to graders. Read Syllabus/pair-programming/ and also submit PARTNER if you are doing pair programming.Either way submit the README described therein.CSE-111 • Spring 2022 • Program 1 • Overloading and operators 7 of 7Et cetera ( ‘´).The accuracy of the Unix utility dc(1) can be checked by :echo ’82 43/25 43+65P80P82P73P76P32P70P79P79P76P10P’ | dc