Write a program where you will design and implement a class that uses lists to i
ID: 3887180 • Letter: W
Question
Write a program where you will design and implement a class that uses lists to implement a new data type which will be referred to as InfInt - integers which can hold extremely large integer values, much larger than c# int's (32 bits) or even c# long's (64 bits). C# ints can contain values between -2,147,483,648 and 2,147,483,647 while c# longs can hold values between - 9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. The new data type which you will create will be able to deal with integer values which can be much larger than c# longs. Your program should implement class InfInt using a list in which each node contains a data value of type int which represents a single decimal digit.
This data value will represent the multiplier of one power of ten in the number being represented. Thus the decimal number: 345 = 3*100 + 4*10 + 1 would be represented by a three node linked list in which one node contains a 5, one node contains a 4 and one node contains a 3. The place or power of ten represented by each node is understood from the order of the nodes.
Use the c# List class to hold your InfInt objects in your driver program you would create the List of InfInt objects with the following code:
var A = new List;
Your program should implement the operations of addition, subtraction, and multiplication for the InfInt data type by creating plus, minus and times methods.
Your task of writing these methods will be made much easier if you have your InfInt class implement the Comparable interface and start by writing compareTo() which can then be used in plus(), minus() and times(). You will also need to learn how to open a txt file and parse the data in c#.
Your program should be capable of reading one infinite integer into an InfInt object named A and reading the other infinite integer into an InfInt object named B and then performing the arithmetic operation and printing the result with a statement similar to: Console.WriteLine($"A + B = {A.plus(B)}”);
Note that negative numbers are possible either given explicitly or as a calculated result.
Explanation / Answer
With Math.random() we get points that are randomly distributed in the 2-by-2 square center at (0, 0). We just generate points in this region until we find one that lies inside the unit disk. We always want to generate at least one point so a do-while loop is most appropriate. We must declare x and y outside the loop since we will want to access their values after the loop terminates.
We don't use the following two flow control statements in this textbook, but include them here for completeness.
Exercises
int min = (x < y) ? x : y;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.