Super powerful calculator-want to know 1000! ?

Source: Internet
Author: User

Some days ago, the teacher made a course design in Java and made a calculator (speechless) in two weeks ),

The result is that the calculator is too simple and not challenging. It will be done in half a day,

There is still a limit on the result time, and the previous method for finding large numbers is always unstable.AlgorithmImplemented,

It can only process the addition and subtraction of two positive integers, which is relatively difficult to implement division and multiplication,

So the algorithms that used to do these big numbers are only stuck In addition and subtraction,

So, in a whim, I want to make a calculator to calculate the large number,

To enable this calculator to process the addition, subtraction, multiplication, and division of N large numbers,

So we started to work in the past two days. It took about two days to complete the work,

First, let's take a look at the overall appearance.

Its main function is to implement the basic operation of addition, subtraction, multiplication, division, and a custom factorial operation,

It mainly implements addition, subtraction, multiplication, and division between large numbers.

The solution directory is

The core of the solution is calculaterule. CS.

The methods included in this class are as follows:

It covers the complex calculation of addition, subtraction, multiplication, and division of the entire large number,

There are only four methods to make public, namely, complexadd (), complexdiv (),

Complexfloatmul (), complexfloatsub,

That is, the four methods of addition, subtraction, multiplication, and division.

This class is mainly implemented by using string,

Because a large number is processed, each number may not be in the Int or long range,

Each number is an astronomical number and may have n digits. Therefore, only strings can be used to save the value,

The addition operation is relatively simple. The addition operation for two large numbers can be split into two parts,

Split two strings that represent large numbers into decimal parts and integer parts,

Then, calculate the fractional part and integer part respectively, and then combine the two parts,

For each part of the operation, array is used,

For example, if the integer is 23252 + 232523242,

You can define two integer arrays, and then store the two strings in reverse direction to the array,

The two arrays are 25232 and 242325232 respectively, and then start from the index 0 of the array,

Add two arrays,

Here, we need to pay attention to the handling of a carry problem,

For subtraction, the idea is similar. The main concern is the borrow space problem,

Multiplication is also relatively simple and can be used

23435*2323 = 23435*2000 + 23435*300 + 23435*20 + 23435*3

Here, we can use the large number addition operation completed above.

Division is the most difficult

The main idea used is to solve the problem of division of large numbers with decimals.

For example, 234454.6565/22252.4364343

The processing of this number is achieved by converting the formula into 2344546565000/222524364343.

In this way, you only need to perform Division operations on two integers, instead of writing algorithms for floating point numbers,

However, integers and integers are also complex,

Here we mainly simulate General Division operations,

First, the integer 1/Integer 2 is solved, and this does not solve the decimal point value,

Instead, we only get the solution of the integer part after division and the remainder after division,

Then, you can obtain the fractional part after integer 1/Integer 2,

Then, combine the integer and decimal parts to complete the division operation,

Then I precise the number of decimal places to the 100 digits after the decimal point.

The basic idea of Algorithm Implementation is described above,

Then the calculator is designed,

There are no requirements for the interface, and the function is just a simple addition, subtraction, multiplication, division, and division operation,

At the same time, because I was concerned with algorithm operations for addition, subtraction, multiplication, and division of large numbers, not the calculator function,

Therefore, the result is not negative, that is, I only process the result as an integer,

It can also be said that the calculated results are absolute values.

There is also the factorial operation, because the algorithm is not refined, and the entire 1000! The calculation speed of the preceding values is very slow,

My laptop with 1.86 clock speed is 1000! It takes about 10 s, so it is best not to calculate what is 10000! And so on,

I am not responsible for the crash. I want to calculate it as 10000 in my test !, As a result, CPU 60% is computed for ten minutes,

I still don't see the result,

So don't try it easily.

At the same time, when the calculator expression is evaluated,

I didn't use the traditional stack. I used another stupid method. The algorithm efficiency is much lower than that of the stack,

However, the thought is much simpler,

That is, calculate the factorial first, then calculate the multiplication, calculate the division, then calculate the addition, and then calculate the subtraction,

Haha, it's stupid, but it's quite simple.

The basic ideas and functions are similar,

Another introduction is to add a background music, which is the moonlight of Beethoven.

Oh, good.

Add a few more.

Computing 1000!

 

1000! The result is (there are still many in textbox)

Calculate a 11/3

The result is (accurate to 100 digits after the decimal point)

 

It is estimated that there are still many bugs in this calculator. You can modify what you want,

If you are interested in this calculator or want the source code, you can leave a mailbox,

Yes. At the same time, I am not very good at the computation of addition, subtraction, multiplication, and division by the calculator,

You can discuss with each other and propose better ideas.

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.