About Arrays of programs

Source: Internet
Author: User

1. Write a program to convert an integer to a kanji reading string.

For example, "1123" is converted to "1123".

Further, can you change the amount represented in the numbers to "Chinese character expression?"

For example, the "¥123.52" converted to "one Bai San Yuan Wu angle of the three points."

Source:

Package Shuzidaxie;
Import Java.util.Scanner;
public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub

Scanner in=new Scanner (system.in);
System.out.println ("Enter a number:");
Double A;
A=in.nextdouble ();
System.out.println (Digituppercase (a));
}
public static String Digituppercase (double n) {
String fraction[] = {"Angle", "Minute"};
String digit[] = {"0", "one", "II", "three", "premises", "WU", "Lu", "Qi", "ba", "JIU"};
String unit[][] = {{"Yuan", "million", "billion"}, {"", "Pick Up", "Bai", "Thousand"}};

String head = N < 0? "Negative": "";
n = Math.Abs (n);

String s = "";
for (int i = 0; i < fraction.length; i++) {
s + = (digit[(int) (Math.floor (n * * MATH.POW (ten, I)%)] + fraction[i]). ReplaceAll ("(0.) +", "");
}
if (S.length () < 1) {
s = "whole";
}
int integerpart = (int) math.floor (n);

for (int i = 0; i < unit[0].length && integerpart > 0; i++) {
String p = "";
for (int j = 0; J < unit[1].length && n > 0; j + +) {
p = digit[integerpart%] + unit[1][j] + p;
Integerpart = INTEGERPART/10;
}
s = P.replaceall ("(0.) * 0 $ "," "). ReplaceAll (" ^$ "," 0 ") + unit[0][i] + s;
}
Return head + s.replaceall ("(0.) * 0 Yuan "," Yuan "). Replacefirst (" (0.) + "," "). ReplaceAll (" (0.) + "," 0 "). ReplaceAll (" ^ Full $ "," 0 yuan whole ");
}

}

2.

The previous introduction of the JDK provided by the BigInteger can complete the large number of calculations, if not use it, directly using the array to express large numbers, you can achieve the same function? Requirements: (1) Use your large number of classes to achieve plus and minus two functions (2) Read the BigInteger class source code, figuring out what algorithm is used to achieve subtraction four kinds of operations? (3) through the Internet to find the large number of relevant data, to your large number of classes to add multiply, divide, and other functions to seek factorial.

Package Dashu;

Import Java.util.Scanner;

public class Dashu {

public static void Main (string[] args) {
TODO auto-generated Method Stub
String s1,s2;
Scanner in=new Scanner (system.in);
System.out.println ("Enter the first large number:");
S1=in.next ();
System.out.println ("Enter a second large number:");
S2=in.next ();
System.out.println (Add (S1,S2));
System.out.println (Sub (S1,S2));

}

private static string Add (string A, string b)
{
System.out.print ("addition:" + + "+" + + b + "=");
char[] aa = new StringBuffer (a). Reverse (). ToString (). ToCharArray ();
char[] bb = new StringBuffer (b). reverse (). ToString (). ToCharArray ();
int aLen = Aa.length;
int blen = Bb.length;

int len = aLen > Blen? Alen:blen;

Int[] result = new Int[len + 1];
for (int i = 0; i < len + 1; ++i) {
int aint = i < aLen? Aa[i]-' 0 ': 0;
int bint = i < Blen? Bb[i]-' 0 ': 0;
Result[i] = aint + bint;
}

for (int i=0;i<result.length-1;++i) {
if (result[i]>=10) {
Result[i+1] + = RESULT[I]/10;
Result[i]%= 10;
}
}

Boolean flag = true;
StringBuffer sb = new StringBuffer (len);
for (int i=len;i>=0;--i) {
if (Result[i]==0&&flag) {
Continue
}else{
Flag=false;
}
Sb.append (Result[i]);
}
return sb.tostring ();
}
public static string sub (string F, string s) {
System.out.print ("Subtraction:" + F + "-" + S + "=");
Flipping and converting a string into a character array
Char[] A = new StringBuffer (f). Reverse (). ToString (). ToCharArray ();
Char[] B = new StringBuffer (s). Reverse (). ToString (). ToCharArray ();
int LenA = A.length;
int LenB = B.length;
Find Maximum length
int len = lenA > LenB? Lena:lenb;
Int[] result = new Int[len];
Indicates the positive or negative of the result
Char sign = ' + ';
Judging the positive and negative of the final result
if (LenA < LenB) {
Sign = '-';
} else if (LenA = = LenB) {
int i = lenA-1;
while (i > 0 && a[i] = = B[i]) {
i--;
}
if (A[i] < b[i]) {
Sign = '-';
}
}
Computes the result set, if the final result is positive, then a-b otherwise b-a
for (int i = 0; i < len; i++) {
int aint = i < LenA? (A[i]-' 0 '): 0;
int bint = i < LenB? (B[i]-' 0 '): 0;
if (sign = = ' + ') {
Result[i] = Aint-bint;
} else {
Result[i] = Bint-aint;
}
}
If one of the results in the collection is less than 0, borrow one forward and then add 10 to the standard. Actually, it's the equivalent of borrow subtraction.
for (int i = 0; i < result.length-1; i++) {
if (Result[i] < 0) {
Result[i + 1]-= 1;
Result[i] + = 10;
}
}

StringBuffer sb = new StringBuffer ();
If the final result is negative, the minus sign is placed first, plus the plus does not need
if (sign = = '-') {
Sb.append ('-');
}
Determine if there is a predecessor 0
Boolean flag = true;
for (int i = len-1; I >= 0; i--) {
if (result[i] = = 0 && flag) {
Continue
} else {
Flag = false;
}
Sb.append (Result[i]);
}
If there is no value in the final result set, it means that the two values are equal and eventually return 0
if (sb.tostring (). Equals ("")) {
Sb.append ("0");
}
return value
System.out.println (Sb.tostring ());
return sb.tostring ();
}
}

3.

Randomly generates 10 numbers, fills an array, then displays the array contents with a message box, computes the array element's and displays the result in a message box.

Design ideas:

The system randomly generates a sum of 10 numbers

Flow chart

Source:

Package Shuzusuiji;
Import Javax.swing.JOptionPane;
public class Shuzu {

public static void Main (string[] args) {
TODO auto-generated Method Stub
int a[],sum=0;
A=new INT[10];
String Output=new string ();
for (int i=0;i<a.length;i++)
{
a[i]= (int) (Math.random () *100);
}
output+= "a[10]={";
for (int i=0;i<a.length;i++)
{
Output+=a[i]+ "";
}
output+= "}";
for (int i=0;i<a.length;i++)
{
Sum+=a[i];
}
output+= "\nsum:" +sum;
Joptionpane.showmessagedialog (null, output, "result",
Joptionpane.plain_message);
}
}

About Arrays of programs

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.