[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5

write a calculator for unlimited digits with linked list by java.

Asked by andy_09 in New to Java Programming, Java Programming Language

Tags: java

write a Number class which will allow you to do arithmetic with unlimited digits and accuracy. As an example, this class was used to compute 1000! as:

    40238726007709377354370243392300398571937486421071463254379991042993851239862902
    05920442084869694048004799886101971960586316668729948085589013238296699445909974
    24504087073759918823627727188732519779505950995276120874975462497043601418278094
    64649629105639388743788648733711918104582578364784997701247663288983595573543251
    31853239584630755574091142624174743493475534286465766116677973966688202912073791
    43853719588249808126867838374559731746136085379534524221586593201928090878297308
    43139284440328123155861103697680135730421616874760967587134831202547858932076716
    91324484262361314125087802080002616831510273418279777047846358681701643650241536
    91398281264810213092761244896359928705114964975419909342221566832572080821333186
    11681155361583654698404670897560290095053761647584772842188967964624494516076535
    34081989013854424879849599533191017233555566021394503997362807501378376153071277
    61926849034352625200015888535147331611702103968175921510907788019393178114194545
    25722386554146106289218796022383897147608850627686296714667469756291123408243920
    81601537808898939645182632436716167621791689097799119037540312746222899880051954
    44414282012187361745992642956581746628302955570299024324153181617210465832036786
    90611726015878352075151628422554026517048330422614397428693306169089796848259012
    54583271682264580665267699586526822728070757813918581788896522081643483448259932
    66043367660176999612831860788386150279465955131156552036093988180612138558600301
    43569452722420634463179746059468257310379008402443243846565724501440282188525247
    09351906209290231364932734975655139587205596542287497740114133469627154228458623
    77387538230483865688976461927383814900140767310446640259899490222221765904339901
    88601856652648506179970235619389701786004081188972991831102117122984590164192106
    88843871218556461249607987229085192968193723886426148396573822911231250241866493
    53143970137428531926649875337218940694281434118520158014123344828015051399694290
    15348307764456909907315243327828826986460278986432113908350621709500259738986355
    42771967428222487575867657523442202075736305694988250879689281627538488633969099
    59826280956121450994871701244516461260379029309120889086942028510640182154399457
    15680594187274899809425474217358240106367740459574178516082923013535808184009699
    63725242305608559037006242712434169090041536901059339838357779394109700277534720
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000

You will write a (rather feeble) calculator with only a few operations which will do basic arithmetic with these numbers. Running the calculator may give a display like:

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> e
    value: 3.14159265358979323846264338327950288419716939937511
    3.14159265358979323846264338327950288419716939937511

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> m
    value: -20000
    -62831.85307179586476925286766559005768394338798750220000

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> a
    value: .853071
    -62831.0000007958647692528676655900576839433879875022

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> c
    0

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> e
    value: .0000000000000000000000001
    .0000000000000000000000001

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> r
    -.0000000000000000000000001

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> m
    value: 200000
    -.0000000000000000000200000

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> s
    value: -1
    .99999999999999999998

    enter a value: e     add: a
    subtract: s          multiply: m
    reverse sign: r      clear: c
    quit: q
    -> q

You will note that your program will not do division. You can be thankful for this.

The Number class

Numbers will be stored in doubly-linked lists (do not use generics here). Each node will have an int value field which will hold one digit (0 through 9) and two pointer fields, prev and next. The Number class will have five fields:

    private Node low, high;
    private int digitCount = 0;
    private int decimalPlaces = 0;
    private boolean negative = false;

high points to the high-order digit's node, low points to the low-order digit's node, digitCount is the number of digits stored in the list, decimalPlaces is the number of digits (nodes) after the decimal place and negative gives the sign. For example, the number 3.1416 would be represented as:

A number of operations will be provided by public methods:

    * public Number(). A constructor which makes an "empty" Number (with no digits).
    * public Number(String str). A constructor which takes a String representation of a number (e.g. "-21.507"). Calls accept.
    * public void accept(String str). Builds a list representation of the number represented by the string.
    * public Number add(Number n). Returns a Number which represents "this + n".
    * public Number subtract(Number n). Returns a Number which represents "this - n".
    * public Number multiply(Number n). Returns a Number which represents "this * n".
    * public void reverseSign(). Reverses the value of negative.
    * public String toString(). This returns a String representation of the number. It allows you to display a Number using System.out.print().

There will be no other public methods, but you may provide any private methods you wish.

  write methods to manipulate the doubly linked list

need private void insertLow(int digit) and private void insertHigh(int digit) to create and insert new nodes at each end of the list.

write (private) Number addAbsolute(Number n), Number subtractAbsolute(Number n) and int compareToAbsolute(Number n) methods which will perform operations disregarding signs (i.e. the negative field). These can then be called by the public Number add(Number n) and public Number subtract(Number n) methods. The trickiest part of these methods is aligning the decimal points.

create a new (empty) Number sum
int carry = 0
thisPtr = low
nPtr = n.low
while (thisPtr != null)
   add the values stored in the pointed to nodes plus the carry to
                                  get int newDigit
   store newDigit % 10 in a new node inserted at the head of sum
   sum.digitCount++
   store newDigit / 10 in carry
   thisPtr = thisPtr.getPrev()
   nPtr = nPtr.getPrev()
if (carry != 0)
   store carry (1) in new node at the head of sum
   sum.digitCount++
set appropriate value for decimalPlaces in sum


    create a new (empty) Number difference
    int borrow = 0;
    thisPtr = low
    nPtr = n.low
    while (thisPtr != null)
          subtract the pointed to digits and subtract borrow from the result
                           to get int newDigit
          if (newDigit < 0)
             newDigit += 10
             borrow = 1
          else
             borrow = 0
          store newDigit in a new node inserted at the head of difference
          difference.digitCount++
    set appropriate value for decimalPlaces in difference

This code must be adjusted to align decimal points and account for different digit counts.

Multiplication is done in a slightly different way from pencil and paper. We read the first Number to be multiplied from-right-to-left and the second from-left-to-right using "Horner's method."

    create an empty Number product
    let d be the high-order digit of n
    do
       multiply (all of) this by d in a right-to-left manner (with a carry)
                            similar to addAbsolute() to get a Number partialProduct
       multiply product by 10 (just append a new low-order digit 0)
       use addAbsolute() to add partialProduct to product
       let d be the next digit to the right in n
    until you have used all digits of n
    decimalPlaces for product will be the sum of decimalPlaces for this and n
    set product.negative to get the appropriate sign

How can I start with program?  Can you help me.
[+][-]09/17/09 11:27 PM, ID: 25363267Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: New to Java Programming, Java Programming Language
Tags: java
Sign Up Now!
Solution Provided By: rpnman
Participating Experts: 2
Solution Grade: A
 
[+][-]09/18/09 05:22 AM, ID: 25365102Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/18/09 05:25 AM, ID: 25365131Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/18/09 08:37 AM, ID: 25367071Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625