(i) Add and subtract two functions with your large number class (multiplication factorial not implemented)
Import Java.util.Scanner;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Test {
public static int[] Add (int[] A, int[] b) {
int digit = 0;
Int[] C = new Int[a.length];
for (int i = a.length-1; I >= 0; i--) {
C[i] = A[i] + b[i] + digit;
if (C[i] < 10)
digit = 0;
else {
C[i] = c[i]-10;
digit = 1;
}
}
return C;
}
public static int[] Sub (int[] A, int[] b, int w) {
int digit = 0;
Int[] C = new Int[a.length];
for (int i = a.length-1; I >= 0; i--) {
if (w <= 0) {
C[i] = B[i]-a[i]-digit;
if (C[i] >= 0)
digit = 0;
else {
C[i] = C[i] + 10;
digit = 1;
}
} else {
C[i] = A[i]-b[i]-digit;
if (C[i] >= 0)
digit = 0;
else {
C[i] = C[i] + 10;
digit = 1;
}
}
}
return C;
}
public static void Main (string[] args) {
int a[] = new INT[50];
int b[] = new INT[50];
int m = 0;
int n = 0;
int s = 0;
int t = 0;
int w = 0;
Scanner reader=new Scanner (system.in);
System.out.print ("Please enter a large number x:");
String X = Reader.nextline ();
System.out.print ("Please enter a large number y:");
String Y = Reader.nextline ();
m = A.length-x.length ();
n = b.length-y.length ();
Determine the size of a two large number
if (X.length () > Y.length ())
w = 1;
else if (X.length () < Y.length ())
W =-1;
Else
W = X.compareto (Y);
Convert to arrays
for (int i = 0; i < x.length (); i++) {
a[m++] = X.charat (i)-48;
}
for (int j = 0; J < Y.length (); j + +) {
b[n++] = Y.charat (j)-48;
}
Addition operation
Int[] C = Test.add (A, b);
Intercept the extra 0 in front.
for (int k = 0; k < c.length; k++) {
if (C[k] > 0) {
s = k;
Break
}
}
Result of adding large number of outputs
System.out.print ("The result of the addition of large numbers is:");
for (int i = s; i < c.length; i++) {
System.out.print (C[i]);
}
Subtraction operations
Int[] D = Test.sub (A, B, W);
for (int k = 0; k < d.length; k++) {
if (D[k] > 0) {
t = k;
Break
}
}
System.out.print ("\ n");
System.out.print ("The result of the subtraction of large numbers is:");
if (W < 0)
System.out.print ("-");
for (int i = t; i < d.length; i++) {
System.out.print (D[i]);
}
}
}
(ii) randomly generated number, fills an array, and then displays the array contents with a message box, then computes the array elements and displays the results in a message box.
design ideas: Define an array of length 10, define an empty string, let the computer randomly generate 10 numbers to populate the array, assign the array to an empty string, take out each element in the array and sum it, and output it with the array in the dialog box.
Program Flowchart:
Source program code:
Array sum Jin peigang 2015/10/28
import javax.swing.*;
Public class Sum {
Public Static void Main (String[]args) {
int a[]=new int[10]; Create an array a[10] and define its length to be 10
int sum=0;
String output= ""; Define a string
for (int i=0;i<a.length;i++)//fill array with 10 numbers generated by the computer a[10]
{a[i]= (int) (Math. Random() *100+1);
Output+=a[i]+ "";} Assigning an array to a string
output= "a[10]={" +output+ "}";
for (int i=0;i<a.length;i++)//remove array element sum
{sum+=a[i];}
Output=output+ "\ n" + "sum:" + "" +sum;
Joptionpane. Showmessagedialog (null, OUTPUT, "Sum of the array",//dialog box outputs the result
Joptionpane. information_message);
}
}
Results:
Problem:
Math. Random () does not specify whether the range can randomly generate 10 numbers
Java Job 5