Please design a 10 billion Calculator

Source: Internet
Author: User

First of all, you must understand the test points of this question,

First, you must first understand the underlying details of computer principles, the bitwise operation principle of addition and subtraction, and the situation that arithmetic operations in computers will cross the border,

Second, we must have certain object-oriented design ideas.
First, the computer uses a fixed number of several bytes to store the value, so the value that can be expressed in the computer has a certain range, in order to facilitate the explanation and understanding, take an integer of the byte type as an example. It is stored in 1 byte, and the maximum value range is-128 to + 127. -1 indicates that the binary data corresponding to the memory is 11111111. If two-1 values are added together, the type upgrade during the Java operation is not considered. After the operation, a carry is generated and the binary result is 1,11111110, because the carry part is discarded because it exceeds the byte storage space, the final result is 11111110, that is,-2, this uses the overflow method to calculate the negative number. -128 in memory
The corresponding binary data in is 10000000. If two-128 values are added together, the type upgrade during the Java operation is not considered. After the operation, the binary result is 1, because the carry part is discarded because it exceeds the byte storage space, the final result is 00000000, that is, 0. This result is obviously not expected, this indicates that arithmetic operations in the computer may cross the border. The calculation results of two values cannot exceed the value range of this type in the computer. Because the types involved in expression operations in Java are automatically upgraded, we cannot use the byte type to demonstrate this problem and phenomenon,

You can use the following example program to experiment with integers:
Int A = integer. max_value;
Int B = integer. max_value;
Int sum = A + B;
System. Out. println ("A =" + A + ", B =" + B + ", sum =" + sum );
The long type is not considered first. Because the positive number range of an int is the power of 31, the maximum value is approximately 2*1000*1000*1000,
That is, the size of 2 billion,

Therefore, to implement a 10 billion calculator, we have to design a class that can be used to represent a large integer and provide the function of addition, subtraction, multiplication, and division with another integer. The approximate function is as follows:
() This class contains two member variables, one representing the symbol, and the other representing the binary number of the value using the byte array.
() There is a constructor that converts a string containing multiple digits into an internal symbol and byte array.
() Provides addition, subtraction, multiplication, division

[Java]View plaincopyprint?
  1. Public class biginteger // indicates a large integer.
  2. {
  3. Int sign; // The identifier.
  4. Byte [] Val; // The binary number of a value in a byte array
  5. // Constructor: converts a string containing multiple digits to an internal symbol and byte array.
  6. Public biginteger (string Val)
  7. {
  8. Sign =;
  9. Val =;
  10. }
  11. // +-* % Method
  12. Public biginteger add (biginteger other ){}
  13. Public biginteger subtract (biginteger other ){}
  14. Public biginteger multiply (biginteger other ){}
  15. Public biginteger divide (biginteger other ){}
  16. }
Public class biginteger // represents a large INTEGER {int sign; // identifies the byte [] Val; // represents the binary number of the value using a byte array // constructor, convert a string containing multiple digits to an internal symbol and public biginteger (string Val) {Sign =; val = ;} // +-* % method public biginteger add (biginteger other) {} public biginteger subtract (biginteger other) {} public biginteger multiply (biginteger other) {} public biginteger divide (biginteger other ){}}

 

 

Note: It is very complicated to write the complete code for this class. If you are interested, refer to
Java. Math. biginteger class source code. The interviewer also knows that no one can write the complete code of this class in a short time.
Whether you have concepts and awareness in this aspect, the most important thing is to examine your abilities.
The final result is to give up answering this question. What you need to do is to write more than others, prove that you are better than others, and you can have the ideology in this aspect.
After all, others may not even understand the meaning of the question and write nothing. You must dare to answer this question, even if you only answer a part of it.
Different from those who do not understand anything, the distance is the highest among the dwarf, and the opportunity belongs to you. In addition
Framework Code is also very important, reflecting some of the knowledge of object-oriented design, especially the method naming is very professional, the English word is very
Precision, which is also a reflection of capabilities, experience, professionalism, and English proficiency, will give a good impression.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.