admin 管理员组

文章数量: 1184232


2024年12月21日发(作者:会用python的工资)

.

package 2decimal;

import p;

publicclass MainEntry {

publicstaticvoid main(String[] args) {

// 初始化16进制数据键值对

HashMap hmI2C = new

HashMap();

HashMap hmC2I = new

HashMap();

for(int i = 0; i < 10; i++) {

(i, (char) ('0'+i));

((char) ('0'+i), i);

}

for(int i = 0; i < 6; i++) {

(10+i, (char) ('A'+i));

((char) ('A'+i), 10+i);

}

// 调用功能接口

hexToDecimal("ECABFFFED1234D7897685DC", hmI2C,

hmC2I);

}

/**

* 实现功能函数

*/

privatestaticvoid hexToDecimal(String

hexString,

HashMap hmI2C,

HashMap hmC2I) {

String result = "0"; // 初始化结果值

for(int i = 0; i < ()-1 ; i++)

{

1页

.

if (i == 0) {

result =

additional((((i))).

toString(),

"0", hmI2C, hmC2I);

result =

decimalStrMultiple16(result, hmI2C, hmC2I);

} else {

result = additional(result,

(((i))).toString(),

hmI2C, hmC2I);

result =

decimalStrMultiple16(result, hmI2C, hmC2I);

}

}

result = additional(result,

(((

()-1))).toString(),

hmI2C, hmC2I);

n("result = "+result);

}

/**

* 乘法:

* 被乘数

*/

privatestatic String

decimalStrMultiple16(String decimalString,

HashMap hmI2C,

HashMap hmC2I) {

StringBuffer sb = new StringBuffer();

carryAndUnit cu = null;

for (int i = ()-1; i >=0;

2页

.

i--) {

if (i == ()-1) {

cu =

multipleOne((i),

0, hmI2C, hmC2I);

} else {

cu =

multipleOne((i),

cu.c, hmI2C, hmC2I);

}

(0, cu.u);

}

if (cu.c > 0) {

(0, cu.c);

}

return ng();

}

/**

* 一位乘法:

* 被乘数

* 进位

* 直接乘了16,是因为16进制转换为10进制数

*/

privatestatic carryAndUnit multipleOne(char a,

int c,

HashMap hmI2C,

HashMap hmC2I) {

int multiplicand = (a);

int multiplier = 16;

int carry = c;

int result = multiplicand*multiplier+carry;

carryAndUnit cu =

newcarryAndUnit(result/10,

3页

.

(result%10));

return cu;

}

/**

* 加法:

* 被加数

* 加数

*/

privatestatic String additional(String

decimalResult, String addend,

HashMap hmI2C,

HashMap hmC2I) {

StringBuffer sb = new StringBuffer();

carryAndUnit cu = null;

if (() == 1) {

for(int i = ()-1 ; i >= 0;

i--) {

if (i == ()-1) {

cu =

additionalOne((i),

(0), 0,

hmI2C, hmC2I);

} else {

cu =

additionalOne((i),

'0', cu.c, hmI2C, hmC2I);

}

(0, cu.u);

}

if(cu.c > 0) {

(0, cu.c);

}

} elseif (() == 2) {

4页

.

for(int i = ()-1 ; i >= 0;

i--) {

if (i == ()-1) {

cu =

additionalOne((i),

(1), 0,

hmI2C, hmC2I);

} else {

cu =

additionalOne((i),

'0', cu.c, hmI2C, hmC2I);

}

(0, cu.u);

}

if(cu.c > 0) {

(0, cu.c);

}

String tempStr = ng();

(0, ());

(0,

(()-1));

for(int i = ()-2 ; i >= 0; i--)

{

if (i == ()-2) {

cu =

additionalOne((i),

(0), 0,

hmI2C, hmC2I);

} else {

cu =

additionalOne((i),

'0', cu.c, hmI2C, hmC2I);

}

(0, cu.u);

}

5页

.

if (() == 1) {

(0, (0));

}

if(cu.c > 0) {

(0, cu.c);

}

} else {

n("addend

error");

}

return ng();

}

/**

* 一位加法:

* 被加数

* 加数

* 之前的进位

*/

privatestatic carryAndUnit additionalOne(char

a, char b, int c,

HashMap hmI2C,

HashMap hmC2I) {

int augend = (a);

int addend = (b);

int carry = c;

int result = augend+addend+carry;

carryAndUnit cu =

newcarryAndUnit(result/10,

(result%10));

return cu;

}

/**

6页

.

* 进位和最后一位值数据模型

*/

privatestaticclass carryAndUnit {

publicintc;

publiccharu;

public carryAndUnit(int c, char u){

this.c = c;

this.u

}

}

}

7页

= u;


本文标签: 进制 功能 会用 调用