| |

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]

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-31 00:30:01
| Show all posts
Made with vc yesterday
void CNumDlg::AddStr(CString sStr, CString *sNum, TCHAR tcUnit)
{
int t = sStr.GetLength();
CString sTemp = _T("");
for (int i=0; i<t; i++)
{
TCHAR tcTemp = sStr.GetAt(t-1-i);
switch (tcTemp)
{
case _T('0'):
if (sTemp == _T("") || sTemp.GetAt(0) == _T('zero'))
{
break;
}
else
{
sTemp.Insert(0, _T("zero"));
}
break;
case _T('1'):
sTemp.Insert(0, _T("一"));
break;
case _T('2'):
sTemp.Insert(0, _T("贰"));
break;
case _T('3'):
sTemp.Insert(0, _T("三"));
break;
case _T('4'):
sTemp.Insert(0, _T("4"));
break;
case _T('5'):
sTemp.Insert(0, _T("五"));
break;
case _T('6'):
sTemp.Insert(0, _T("Lu"));
break;
case _T('7'):
sTemp.Insert(0, _T("柒"));
break;
case _T('8'):
sTemp.Insert(0, _T("捌"));
break;
case _T('9'):
sTemp.Insert(0, _T("玖"));
break;
default:
break;
}
switch(i)
{
case 0:
break;
case 1:
if (sTemp != _T("")&&sTemp.GetAt(0) != _T('zero'))
{
sTemp.Insert(1, _T("take"));
}
break;
case 2:
if (sTemp != _T("")&&sTemp.GetAt(0) != _T('zero'))
{
sTemp.Insert(1, _T("百"));
}
break;
case 3:
if (sTemp != _T("")&&sTemp.GetAt(0) != _T('zero'))
{
sTemp.Insert(1, _T("千"));
}
break;
default:
break;
}
}
if (tcUnit != (TCHAR)0&&sTemp!= _T(""))
{
sNum->Insert(0,tcUnit);
}
else
{
if (tcUnit == _T('billion'))
{
sNum->Insert(0,tcUnit);
}
}
sNum->Insert(0,sTemp);
}

void CNumDlg::OnRead()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString sNum = _T("");
int t = m_sNum.GetLength();
while (true)
{
if (t>0&&m_sNum.GetAt(0) == _T('0'))
{
m_sNum.Delete(0);
t--;
continue;
}
break;
}
if (t<=0)
{
MessageBox(_T("Your input is wrong!"));
return;
}
for (int i = 0; i<t; i++)
{
TCHAR tc = m_sNum.GetAt(i);

if (!_istdigit(tc))
{
MessageBox(_T("Please enter a number!"));
return;
}
}
if (t<=4)
{
AddStr (m_sNum,&sNum, (TCHAR)0);
}
if (t>4&&t<=8)
{
CString sTh;
sTh = m_sNum.Right(4);
AddStr (sTh,&sNum, (TCHAR)0);
CString sM;
sM = m_sNum.Left(m_sNum.GetLength()-4);
AddStr (sM,&sNum, _T('10,000'));
}
if (t>8&&t<=12)
{
CString sTh;
sTh = m_sNum.Right(4);
AddStr (sTh,&sNum, (TCHAR)0);
CString sM;
sM = m_sNum.Mid(m_sNum.GetLength()-8, 4);
AddStr (sM,&sNum, _T('10,000'));
CString hM;
hM = m_sNum.Left(m_sNum.GetLength()-8);
AddStr (hM,&sNum, _T('billion'));
}
if (t>12&&t<=16)
{
CString sTh;
sTh = m_sNum.Right(4);
AddStr (sTh,&sNum, (TCHAR)0);
CString sM;
sM = m_sNum.Mid(m_sNum.GetLength()-8, 4);
AddStr (sM,&sNum, _T('10,000'));
CString hM;
hM = m_sNum.Mid(m_sNum.GetLength()-12, 4);
AddStr (hM,&sNum, _T('billion'));
CString mHM;
mHM = m_sNum.Left(m_sNum.GetLength()-12);
AddStr (mHM,&sNum, _T('10,000'));
}
if (sNum.GetAt(0)==_T('zero'))
{
sNum.Delete(0);
}
m_sRead = sNum;
UpdateData(FALSE);
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-3 12:15:02
| Show all posts
package num;

public class NumReader {

public static String[] n = new String[10];
To
public static String[] d = new String[6];
To
static {
n[0] = "zero";
n[1] = "One";
n[2] = "贰";
n[3] = "Three";
n[4] = "4";
n[5] = "Wu";
n[6] = "Lu";
n[7] = "7";
n[8] = "捌";
n[9] = "玖";
To
        d[0] = "";
        d[1] = "Pick up";
        d[2] = "Bai";
        d[3] = "1,000";
        d[4] = "10,000";
        d[5] = "Billion";
}

public static String readNum(String num) {
if(!checkParam(num)){
System.out.println(num + "is not a valid number");
}
StringBuffer sb = new StringBuffer();
To
int len ​​= num.length();
String[] numbers = new String[len];
String[] units = new String[len];
boolean[] numFlags = new boolean[len];
boolean[] unitFlags = new boolean[len];
To
//caculate every pos
for(int i=0;i<len;i++){
Result rs = read(num,i);
numbers[i] = rs.number;
units[i] = rs.unit;
numFlags[i] = rs.numFlag;
unitFlags[i] = rs.unitFlag;
To
// join string
if(numFlags[i]){
sb.append(numbers[i]);
}
if(unitFlags[i]){
sb.append(units[i]);
}
}
To
return sb.toString();
}

public static boolean checkParam(String num) {
try {
Double.parseDouble(num);
} catch (NumberFormatException e) {
return false;
}
return true;
}

public static Result read(String num, int pos) {
Result rs = new Result();
rs.number = readNum(num,pos);
rs.unit = readUnit(num,pos);
rs.numFlag = readNumFlag(num,pos);
rs.unitFlag = readUnitFlag(num,pos);
return rs;
}
To
public static String readNum(String num, int pos) {
int value = Integer.parseInt(num.substring(pos,pos+1));
return n[value];
}
To
public static String readUnit(String num, int pos) {
int len ​​= num.length();
int count = len-pos-1;
To
int unit = count% 4;
String unitStr = d[unit];
To
int rate = count / 4;
if( count >= 4&&count% 4 == 0){
if(rate% 2 == 0){
unitStr += d[5];
} else {
unitStr += d[4];
}
}
return unitStr;
}

public static boolean readNumFlag(String num, int pos) {
int len ​​= num.length();
int value = Integer.parseInt(num.substring(pos,pos+1));
int count = len-pos-1;
if( len %4 == 2&&value == 1&&pos == 0){
return false;
}
if(value != 0){
return true;
}
To
if (count% 4 == 0 ){
return false;
}
int nextValue = Integer.parseInt(num.substring(pos + 1,pos+2));
if(nextValue != 0){
return true;
}
return false;
}

public static boolean readUnitFlag(String num, int pos) {
int value = Integer.parseInt(num.substring(pos,pos+1));
if(value != 0){
return true;
}
int len ​​= num.length();
int count = len-pos-1;
if (count% 4 == 0 ){
return true;
}
return false;
}
To
public static class Result{
String number = null;
String unit = null;
boolean numFlag = false;
boolean unitFlag = false;
}
To
public static void main(String[] args) {
String testNum = "123456789012345678";
String str = readNum(testNum);
System.out.println(testNum);
System.out.println(str);
}
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 20:15:01
| Show all posts
After searching for a long time, I didn't see a good algorithm. I remember that I used a tree to find it. The code is very concise!
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