| |

VerySource

 Forgot password?
 Register
Search
Author: daike1017

A java written question, thought it was very simple, but I didn't write it right for a long time! Please prawn give a c

  [Copy link]

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-28 13:15:01
| Show all posts
Since this adds a sentence to the above program initialization
    s = s.replaceFirst("^0+","");
Just remove the leading 0
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-29 16:15:02
| Show all posts
I used BASIC. However, limited by the interpretation environment, the input number cannot be >= 1 billion. But another environment can continue to expand.

10 DATA "Zero", "One", "Two", "Three", "Wu", "Wu", "Lu", "Qi", "捌", "Jiu"
20 DIM NUM$(10)
30 FOR I=0 TO 9
40 READ A$
50 NUM$(I)=A$
60 NEXT
70 DIM UNIT$(4)
80 UNIT$(1)="仟":UNIT$(2)="Bai":UNIT$(3)="Pick up"
90 DIM SC$(3)
100 SC$(1)="100 million": SC$(2)="10,000"
105 LOCATE 5,1:PRINT "not more than 1 billion";
110 LOCATE 1,1:INPUT "Please enter an Arabic number (integer)"; N
120 IF INT(N)<>N THEN PRINT "Please enter an integer": GOTO 110
125 IF N=0 THEN PRINT "Zero":END
130 N$=STR$(N): L=LEN(N$)
140 IF (L/4)=INT(L/4) THEN H=(L/4):GOTO 160
150 IF (L/4)>1 THEN H=INT(L/4)+1 ELSE H=1
160 DIM T$(3,4)
170 X=3:Y=4
180 FOR I=L TO 1 STEP -1
190 T$(X,Y)=MID$(N$,I,1)
200 Y=Y-1
210 IF Y=0 THEN Y=4:X=X-1
220 NEXT
300 SH=4-H
310 FOR A=SH TO 3
320 T=VAL(T$(A,1))
330 I=1
340 WHILE T=0
350 I=I+1
355 IF I=5 GOTO 470
360 T=VAL(T$(A,I))
370 LW=1
380 WEND
390 IF A<>SH AND H>1 AND LW=1 THEN S$="zero"
410 FOR J=I TO 4
420 T=VAL(T$(A,J))
425 IF T=0 AND VAL(T$(A,J-1))=0 THEN GOTO 440
430 S$=S$+NUM$(T)+UNIT$(J*(T<>0))
440 NEXT
445 IF ?S$,3)<>"Zero" GOTO 460
450 S$=LEFT$(S$,(LEN(S$)-3)): WL=1
460 FN$=FN$+S$+SC$(A)
465 LW=0:S$=""
470 NEXT
480 PRINT FN$
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-29 17:15:01
| Show all posts
Whoever has Wenquxing can convert the above code into BAS and pass it into Wenquxing. You can run it.
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-29 18:30:01
| Show all posts
445 IF RIGHT$(S$,3)<>"Zero" GOTO 460
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-6 22:45:02
| Show all posts
I also wrote one. I thought that there were too many digits and it didn’t make sense, so I did 15 digits.
      Another: I googled a bit, some said one trillion = one trillion, and some said one trillion = 110 billion, I temporarily think that one trillion = one trillion. The code is as follows: Please correct me if there is something wrong!

     package numberToCn;

import java.util.ArrayList;
import java.util.List;
import java.io.*;

public class NumberToCnModify {
List cnList = null, danweiList = null;
StringBuffer cnStr = null;
char[] tempStr = null;
int charLength = 0;
The
   public NumberToCnModify(){
cnList = new ArrayList();
danweiList = new ArrayList();
cnStr = new StringBuffer();
   }
   
   public void init(){
cnList.add("zero");
cnList.add("一");
cnList.add("Two");
cnList.add("Three");
cnList.add("Ran");
cnList.add("Wu");
cnList.add("Lu");
cnList.add("柒");
cnList.add("捌");
cnList.add("Jiu");
The
danweiList.add("");
danweiList.add("Pick up");
danweiList.add("百");
danweiList.add("仟");
danweiList.add("10,000");
danweiList.add("100 million");
danweiList.add("Mega");
   }
   
   public boolean isNumber(char tempChar){
boolean isNum = true;
if((tempChar<'0')||(tempChar>'9')){
System.out.println();
System.out.println("Not Number"+tempChar);
isNum=false;
}
return isNum;
   }
   
   public void getInputString(String inputStr){
tempStr = inputStr.toCharArray();
charLength = tempStr.length;
if(charLength>15){
System.out.println("The Number You Input Is Too Long!");
System.exit(0);
}
   }
   
   public String convertToBaseCn(int startNum,int endNum,String danwei){
int allZero = 0;
int tempNum = 0;
String tempString = "";
for(int i=startNum;i<endNum;i++){
tempNum = Integer.parseInt(String.valueOf(tempStr[i]));
if(i==startNum&&tempNum==0||i==startNum+1&&tempNum==0&&Integer.parseInt(String.valueOf(tempStr[i-1]))==0||i==startNum+2&&tempNum==0&&Integer. parseInt(String.valueOf(tempStr[i-1]))==0&&Integer.parseInt(String.valueOf(tempStr[i-2]))==0||i==startNum+3&&tempNum==0&&Integer.parseInt(String .valueOf(tempStr[i-1]))==0&&Integer.parseInt(String.valueOf(tempStr[i-2]))==0&&Integer.parseInt(String.valueOf(tempStr[i-3]))==0 ){
if(i==startNum+3&&tempNum==0&&Integer.parseInt(String.valueOf(tempStr[i-1]))==0&&Integer.parseInt(String.valueOf(tempStr[i-2]))==0&&Integer.parseInt( String.valueOf(tempStr[i-3]))==0){
allZero = 1;
}
//If the first one is zero or 1, 2 both are 0 or 1, 2, 3, 1, 2, 3, 4 are 0, there is no need to print zero and corresponding units.
}else{
if(i==endNum-1&&tempNum==0||i==endNum-2&&tempNum==0&&Integer.parseInt(String.valueOf(tempStr[i+1]))==0||i==endNum-3&&tempNum== 0&&Integer.parseInt(String.valueOf(tempStr[i+1]))==0){
//If the last one is 0, and one and the following are all 0, there is no need to print 0
}else{
tempString+=cnList.get(tempNum).toString();
}
The
if(tempNum!=0){//If it is 0, there is no need to print the unit
tempString+=danweiList.get(endNum-(i+1)).toString();
}
The
}
}
The
if(allZero == 0){
tempString+=danwei;
}
The
return tempString;
   }
   
   public String convertToCn(){

int notNumber = 0;
int zeroRecord = 0;//When two or more bit segments are 0 at the same time, as long as a 0 is displayed
String tempString = "";
The
try{
The
for(int i=0;i<charLength;i++){
if(!isNumber(tempStr[i])){
notNumber = 1;
break;
}
}
The
if(notNumber == 1){
System.out.println("Please Input Number!");
return "";
}else{
if(charLength>12){
tempString = convertToBaseCn(0,charLength-12,"Mega");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString = convertToBaseCn(charLength-12, charLength-8, "100 million");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString = convertToBaseCn(charLength-8, charLength-4, "10,000");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-4,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}else if(charLength>8){
tempString=convertToBaseCn(0,charLength-8,"100 million");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-8,charLength-4,"10,000");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-4,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}else if(charLength>4){
tempString=convertToBaseCn(0,charLength-4,"10,000");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-4,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}else{
tempString=convertToBaseCn(0,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}
}
}catch(NullPointerException ne){
System.out.println("NullPointerException:"+ne.getMessage());
}catch(IndexOutOfBoundsException iobe){
System.out.println("IndexOutOfBoundsException:"+iobe.getMessage());
}finally{
The
}
return cnStr.toString();
   }
   
   public static void main(String[] args){
NumberToCnModify ntc = new NumberToCnModify();
ntc.init();
ntc.getInputString("300340000000130");
System.out.println(ntc.convertToCn());
   }
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-7 18:30:01
| Show all posts
I have taught you masters!
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-7 23:15:02
| Show all posts
Supports "000123423400000000000130"


import java.math.BigInteger;
import java.util.Scanner;

public class NumToChinese {

public static void ToChinese(BigInteger num) {
String n[] = {"Zero","One", "Two", "Three", "Wu", "Wu", "Lu", "柒", "捌", "玖"};
String unit[] = {"", "Pickup", "Bai", "仟"};
String unit1[] = {"100 million", "Ten thousand" };
StringBuilder chi = new StringBuilder();

for (int i = 0; i <num.toString().length(); i++) {
chi.append(n[Integer.parseInt(String.valueOf(num.toString().charAt(i)))]);
chi.append(unit[(num.toString().length()-i-1)% 4]); // add unit
if ((num.toString().length()-i)% 4 == 1) // add unit 1
{
chi.append(unit1[(int) Math.floor((double) (num.toString().length()-i) / 4)% 2]);
}
}
String ch = chi.toString();
ch = ch.replaceAll("零千", "zero");
ch = ch.replaceAll("Zero Bai", "Zero");
ch = ch.replaceAll("zero pick", "zero");
while(ch.indexOf("Zero Zero")>0)
{
ch = ch.replaceAll("zero zero", "zero");
}
ch = ch.replaceAll("Zero Ten Thousand", "Ten Thousand");
ch = ch.replaceAll("Zero Billion", "Billion");
ch = ch.replaceAll("Billion", "Billion");
System.out.println(ch.substring(0, ch.length()-1));
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true)
{
BigInteger num = in.nextBigInteger();
if(num.equals(0))
{
System.exit(0);
}
ToChinese(num);
}
}
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-12 09:30:01
| Show all posts
Mark, I used to write a number in C++ to English capital, I don’t know if it helps, the code is as follows:

//EnglishNumber.h

#pragma once
#include<string>
#include<cassert>
#include "ValueTraits.h"

using namespace std;

class EnglishNumber
{
public:
    EnglishNumber(){}
    ~EnglishNumber(){}
    template<typename ValueType>
        const string NumberName(const ValueType&) const;
private:
    template<typename ValueType>
        const string BaseDigit(const ValueType&) const;
    template<typename ValueType>
        const string TensDigit(const ValueType&) const;
    template<typename ValueType>
        const string HundredsDigit(const ValueType&) const;
    template<typename ValueType>
        const string MoreDigit(const ValueType&) const;
};

template<typename ValueType>
    const string EnglishNumber::BaseDigit(const ValueType&iNumber) const{
        const static string BaseNumName[] = {"zero",
            "one","two","three","four","five","six",
            "seven","eight","nine","ten","eleven",
            "twelve","thirteen","fourteen","fifteen",
            "sixteen","seventeen","eighteen","nineteen"};
        assert(iNumber >= 0&&iNumber <20&&"Function BaseNumber");
        return BaseNumName[ValueTraits<ValueType>::Long(iNumber)];
    }

template<typename ValueType>
    const string EnglishNumber::TensDigit(const ValueType&iNumber) const{
        const static string TensDigitName[] = {
            "twenty","thirty","forty","fifty",
            "sixty","seventy","eighty","ninety"};
        assert(iNumber >= 0&&iNumber <100&&"Function TensDigit");
        if(iNumber <20)
            return BaseDigit(iNumber);
        ValueType h(iNumber),l(iNumber);
        h = iNumber / 10;
        l = iNumber% 10;
        if(0 == l)
            return TensDigitName[ValueTraits<ValueType>::Long(h)-2];
        return TensDigitName[ValueTraits<ValueType>::Long(h)-2] + "-" + BaseDigit(l);
    }

template<typename ValueType>
    const string EnglishNumber::HundredsDigit(const ValueType&iNumber) const{
        assert(iNumber >= 0&&iNumber <1000&&"Function HundredsDigit");
        if(iNumber <100)
            return TensDigit(iNumber);
        ValueType h,l;
        h = iNumber / 100;
        l = iNumber% 100;
        if(0 == l)
            return BaseDigit(h) + "hundred";
        return BaseDigit(h) + "hundred and" + TensDigit(l);
    }

template<typename ValueType>
    const string EnglishNumber::MoreDigit(const ValueType&iNumber) const{
        const static string MoreName[] = {"thousand","million","billion"};
        assert(iNumber >= 0&&"Function MoreNumber");
        if(iNumber <1000)
            return HundredsDigit(iNumber);
        ValueType n;
        n = iNumber% 1000;
        string result;
        if(0 != n)
            result = HundredsDigit(n);
        n = iNumber / 1000;
        int i = 0;
        do{
            ValueType l = n% 1000;
            n /= 1000;
            if(0 != l)
                if(0 == result.size())
                    result = HundredsDigit(l) + "" + MoreName[i];
                else
                    result = HundredsDigit(l) + "" + MoreName[i] + ", "+ result;
        }while(n> 0&&++i <3);
        if(n> 0)
            if(0 == result.size())
                result = MoreDigit(n) + "trillion";
            else
                result = MoreDigit(n) + "trillion," + result;
        return result;
    }

template<typename ValueType>
    const string EnglishNumber::NumberName(const ValueType&iNumber) const{
        if(iNumber <0)
            return "minus "+ MoreDigit(-iNumber);
        return MoreDigit(iNumber);
    }

//ValueTraits.h

#pragma once

template<typename T>struct ValueTraits
{
static long Long(const T&v)
{
return v;
}
};
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-17 03:15:01
| Show all posts
If there are too many numbers, how do you count them later? I think it should be a predetermined unit; for example, tens of thousands of tens of billions of tens of thousands of tens of thousands, and then insert each bit of the string into it
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-18 08:00:01
| Show all posts
A lot of algorithms, a lot of cattle...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list