定点整数二进制运算
人生不设限-五月的鲜花
口令:123
package xiangmu;
**
*
* @author pc
*
public class Xiangmu
{
**
* @param args the
command line arguments
*
public
static void main(String[] args) {
Window win = new Window();
}
}
package xiangmu;
**
*
*
@author pc
*
import
import .*;
import .*;
import .*;
class Window
extends JFrame implements ActionListener {
JMenuBar menubar; 菜单条
JMenu
menu1,menu2,menu3,menu4; 菜单栏
JMenuItem
item1,item2,item3,item4; 菜单项
JButton
queren;
JLabel koul,bti;
JPanel p1,p2;
JPasswordField mima;
String txt=
int cishu=0;
public Window(){
nds(400, 100, 600, 400);
izable(false);窗口大小不许改变
caidan();
out(null);
koul = new
JLabel(输入口令:
nds(150, 100, 150, 100);
mima = new JPasswordField(10);
nds(250, 140, 150, 20);
t(new Font(宋体
bti =new JLabel(计算机组成原理算法实现(二)
t(new
Font(宋体改变字体大小
nds(130, -20, 380, 210);
queren = new JButton(确定
t(new
Font(宋体
nds(270, 200, 100, 30);
bled(false); 菜单访问权设置
bled(false);
bled(false);
bled(false);
ionListener(this);监听
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
(queren);
(mima);
(koul);
(bti,
ible(true);
aultCloseOperation(_ON_CLOSE);
}
void caidan(){
menubar = new
JMenuBar();
menu1 = new
JMenu(定点整数机器表示
menu2 = new
JMenu(定点整数变形补码加减法
menu3 = new
JMenu(定点整数原码乘法
menu4 = new JMenu(浮点加减法
(menu1);
(menu2);
(menu3);
(menu4);
item1 =
new JMenuItem(定点整数机器表示
item2 = new
JMenuItem(定点整数变形补码加减法
item3 = new
JMenuItem(定点整数原码乘法
item4 = new
JMenuItem(浮点加减法
(item1);
(item2);
(item3);
(item4);
nuBar(menubar);
}
@Override
public void actionPerformed(ActionEvent e)
{
String t =t();
判断口令是否正确try
if(rce()==queren){
if((txt)){
t(
bled(true); 口令正确 菜单可用
bled(true);
bled(true);
bled(true);
}
else
if(!(txt)) {
ssageDialog(rootPane, 密码错误
bled(false); 菜单访问权设置
bled(false);
bled(false);
bled(false);
t(
cishu++;
if(cishu==3) {
ssageDialog(rootPane, 密码错误次数太多
(0);
}
}
}
if(rce()==item1){
Ddxs ddxs =
new Ddxs(); e();
}else
if(rce()==item2){
Bmjj bmjj =
new Bmjj(); e();
}else
if(rce()==item3){
Ymcf ymcf
=new Ymcf(); e();
}else
if(rce()==item4){
Fdjj
fdjj=new Fdjj(); e();
}
}
}
package xiangmu;
**
*
* @author pc
*import
import .*;
import .*;
import .*;
public
class Fdjj extends JFrame implements
ActionListener {
JButton JButton1,
JButton2, JButton3, JButton4;
JLabel name,
input, input1, input2, input3, input4, input5,
input7, input6;
JTextField data, data1,
data2, data3, data4, data5, data6, data7;
public Fdjj() {
setTitle(
Toolkit tool
= getToolkit();
eenSize();
nds(400,
100, 600, 400);
out(null);
JButton1 = new JButton(输入
t(new Font(宋体
nds(100, 320, 80,
30);
JButton2 = new JButton(加法
t(new Font(宋体
nds(200, 320, 80, 30);
JButton3 = new JButton(减法
t(new
Font(宋体
nds(300, 320, 80,
30);
JButton4 = new JButton(返回
t(new Font(宋体
nds(400, 320, 80, 30);
name = new
JLabel(浮点数的加减法运算
t(new
Font(宋体
nds(130, -20, 300,
150);
input = new JLabel(第一个数的阶码:
t(new Font(宋体
nds(25, 100, 150, 30);
data = new JTextField(10);
data =new JTextField( nds(155, 105, 100, 20);
input1 = new JLabel(尾数:
t(new
Font(宋体
nds(305, 105, 80, 20);
data1 = new
JTextField(10);
nds(355, 105,
150, 20);
input2
= new JLabel(第二个数的阶码:
t(new
Font(宋体
nds(25, 150, 150, 30);
data2 = new JTextField(10);
nds(155, 155, 100, 20);
input3 = new
JLabel(尾数:
t(new Font(宋体
nds(305, 155, 80, 20);
data3 = new
JTextField(10); nds(355, 155, 150, 20);
input4 = new JLabel(加法的阶码:
t(new Font(宋体
nds(55, 200, 100, 30);
data4 = new JTextField(10);
nds(155, 205, 100, 20); bled(false);
input5
= new JLabel(尾数:
t(new
Font(宋体
nds(305, 205, 80, 20);
data5 = new JTextField(10);
nds(355, 205, 150, 20);bled(false);
input6 = new JLabel(减法的阶码为:
t(new Font(宋体
nds(55,250, 100, 30);
data6 = new JTextField(10); nds(155, 255,
100, 20); bled(false);
input7 = new
JLabel(尾数:
t(new Font(宋体
nds(305, 255, 80, 20);
data7 = new JTextField(10);
nds(355, 255, 150, 20); bled(false);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
(name);
(input); (input1);(input2);
(input3); (input4); (input5);
(input6);(input7);
(data);(data2);(data1);(data3);
(data5); (data6);(data7);(data4);
(JButton1); (JButton2);
(JButton3); (JButton4);
setVisible(true);
aultCloseOperation(_ON_CLOSE);
}
String calculate(String s1, String s2)
{
char ac = '0';
char a1[] = Array(),
a2[] = Array();
for (int i = - 1; i >= 0;
i--) {
if (a1[i] == '0' && a2[i] == '0' &&
ac == '0') {
a2[i] = '0';
ac = '0';
} else if (a1[i] == '0' && a2[i] == '0' &&
ac == '1') {
a2[i] = '1';
ac = '0';
} else if (a1[i] == '0' && a2[i] == '1' &&
ac == '0') {
a2[i] = '1';
ac = '0';
} else if (a1[i] == '1' && a2[i] == '0' &&
ac == '0') {
a2[i] = '1';
ac = '0';
} else if (a1[i] == '0' && a2[i] == '1' &&
ac == '1') {
a2[i] = '0';
ac = '1';
} else if (a1[i] == '1' && a2[i] == '1' &&
ac == '0') {
a2[i] = '0';
ac = '1';
} else if (a1[i] == '1' && a2[i] == '0' &&
ac == '1') {
a2[i] = '0';
ac = '1';
} else if (a1[i] == '1' && a2[i] == '1' &&
ac == '1') {
a2[i] = '1';
ac = '1';
}
}
return new String(a2);
}
public String jiemaQiubu(String s) * 阶码变形补码
*
{
if (With(
s =
}
if
(With(
s =
}
if (With(
String
temp = ing(1, dexOf(
char c[] = Array();
for (int i = 0; i < i++) {
if (c[i] ==
'0')
c[i] = '1';
else
c[i]
= '0';
}
temp = new String(c);
s
=
}
return s;
}
public String
weishuQiubu(String s) 尾数求变形补码(缺省整数点,多加长度个0)
{
if (With(
String str =
ing(2).replaceAll(
s =
}
if
(With(
String str = ing(3).replaceAll(
s
=
}
if (With(
String temp = null,
str = ing(3).replaceAll(
temp = ing(3,
dexOf(
char c[] = Array();
for (int i =
0; i < i++) {
if (c[i] == '0')
c[i] = '1';
else
c[i] = '0';
}
temp = new String(c);
s =
}
return s;
}
public int
toSubstract(String sj1, String sj2)* 求阶差 *
{
int sum = 0, signal = 0;
char cj2[] =
ing(0, dexOf(
for (int i = 0; i < i++) {
if (cj2[i] == '0')
cj2[i] = '1';
else
cj2[i] = '0';
}
sj2 = new
String(cj2) + ing(dexOf(
String temp =
calculate(sj1, sj2);
if (With(
第二个数阶码小
{
signal = 1;
temp =
ing(2);
}
if (With(第一个数阶码小 *
{
signal = -1;
String str = ing(2, dexOf(
char c[] = Array();
for (int i = 0; i <
i++) {
if (c[i] == '0')
c[i] = '1';
else
c[i] = '0';
}
temp
= new String(c) + ing(dexOf(
} else {
ssageDialog(this, 对阶时溢出!错误
_MESSAGE);
}
int n[] = new int[()];
for (int i = 0;
i < i++) {
if (ing(i, i + 1).equals(
n[i] = 0;
else
n[i] = 1;
}
for (int i = - 2; i >= 0; i--) {
for
(int j = i; j >= i; j--) {
n[i] = n[i] * 2;
}
}
for (int i = 0; i < i++) {
sum = sum + n[i];
}
sum = sum *
signal;
return sum;
}
public String
bumaHuanyuan(String s)* 变形补码还原真值处理,无整数点 *
{
if (With(
s =
} else if (With(
String temp = ing(2, dexOf(
char c[] =
Array();
for (int i = 0; i < i++) {
if (c[i] == '0')
c[i] = '1';
else
c[i] = '0';
}
temp = new
String(c);
s =
}
return s;
}
public String[] myAdd(String sj1, String sj2,
String st1, String st2) {
String s[] = new
String[2];
int n = 0, count = 0, len = (() -
2) 2;
n = toSubstract(sj1, sj2);* 求阶差 *
if (n < 0)* 第一个数的尾数右移 *
{
for (int i =
n; i < 0; i++) {
st1 = ing(0, 1) + ing(0,
() - 1);
}
sj1 = sj2;* 统一阶码 *
}
if (n > 0)* 第二个数的尾数右移 *
{
for (int
i = 1; i <= n; i++) {
st2 = ing(0, 1) +
ing(0, () - 1);
}
sj2 = sj1;* 统一阶码 *
}
st1 = calculate(st1, st2);* 尾数求和 *
while (!(With(规格化 *
{
if
(With(尾数左移 *
{
st1 = ing(1) +
count++;
} else if (With(尾数右移 *
{
st1 = ing(0, 1) + ing(0, () - 1);
count--;
}
}
while (count > 0)*
阶码循环减一 *
{
String temp = eAll(
temp = ing(1) +
sj1 = calculate(sj1, temp);
count--;
}
while (count < 0) {
String temp = eAll(
temp = ing(1) +
sj1 = calculate(sj1, temp);
count++;
}
sj1 = bumaHuanyuan(sj1);
st1 =
ing(0, len + 3);
if (th(尾数舍入处理 *
{
String temp = eAll(
temp = ing(1) +
st1
= calculate(st1, temp);
} else if (th(
}
st1 = ing(0, () - 1);
st1 =
bumaHuanyuan(st1);* 尾数补码还原处理 *
s[0] = sj1;
s[1] = st1;
return s;
}
public
void actionPerformed(ActionEvent e) {
if
(rce() == JButton1) {
t(null);
t(null);
t(null);
t(null);
t(null);
t(null);
t(null);
t(null);
tFocusInWindow();
}
if
(rce() == JButton2) {
String sj1 =
jiemaQiubu(t()),
sj2 = jiemaQiubu(t()), st1
= weishuQiubu(data1
.getText()), st2 =
weishuQiubu(t());
if ((() != ())
||
(() != ())) {
ssageDialog(this,
请输入长度相等的数据!错误
_MESSAGE);
} else {
String s[] = myAdd(sj1, sj2, st1, st2);
t(s[0]);
t(s[1].substring(0, 1) +
}
}
if
(rce() == JButton3) {
String sj1 =
jiemaQiubu(t()),
sj2 = jiemaQiubu(t()), st1
= weishuQiubu(data1
.getText()), st2 =
weishuQiubu(t()), temp;
if ((() != ())
|| (() != ())) {
ssageDialog(this,
请输入长度相等的数据!错误
_MESSAGE);
} else {
temp = ing(0, dexOf(
Array();
for (int i = 0; i < i++) {
if (c[i] ==
'0')
c[i] = '1';
else
c[i] = '0';
}
st2 = new String(c)
+ ing(dexOf(
String s[] = myAdd(sj1, sj2,
st1, st2);
t(s[0]);
t(s[1].substring(0, 1) +
}
} else if
(rce() == JButton4) {
Window win =new
Window();
e();
}
}
}
package xiangmu;
**
*
* @author pc
*import
import .*;
import .*;
import .*;
class Ddxs
extends JFrame implements ActionListener{
JLabel label1,label3,label2;
JTextField
f1,f2;
JButton
shuru,yma,fanma,buma,yima,fanhui;
public
Ddxs(){
nds(400, 100, 600, 400);
out(null);
izable(false);窗口大小不许改变
fanhui =new JButton(返回
t(new Font(宋体
nds(500, 320, 80, 30);
ionListener((ActionListener) this);
yma =new JButton(原码
char c[] =
t(new Font(宋体
nds(100, 320, 80, 30);
ionListener((ActionListener) this);
fanma =new JButton(反码
t(new
Font(宋体
nds(200, 320, 80, 30);
ionListener((ActionListener) this);监听
buma =new JButton(补码
t(new
Font(宋体
nds(300, 320, 80, 30);
ionListener((ActionListener) this);监听
yima =new JButton(移码
t(new
Font(宋体
nds(400, 320, 100, 30);
ionListener((ActionListener) this); 监听
shuru =new JButton(输入
t(new
Font(宋体
nds(10, 320, 80, 30);
ionListener((ActionListener) this);监听
f1 =new JTextField(
text框
f2=new JTextField(
nds(250, 140, 150, 20);
nds(250, 200,
150, 20);
bled(false);
label1=new JLabel(请输入一个二进制数:
nds(100,
100, 150, 100);
label2=new JLabel(该数对应的原码:
nds(122, 197,
100, 30);
label3=new
JLabel(定点整数的机器表示
t(new Font(宋体
nds(130, -20, 380, 210);
(label1);
(label2);
(label3);
(f1); (f2);
(shuru); (fanma); (yma); (buma); (yima);
(fanhui);
ible(true);
aultCloseOperation(_ON_CLOSE);
}
@Override
public void
actionPerformed(ActionEvent e) {
if(rce()==fanhui){
Window win =new
Window();
e();
}
else if(rce()==shuru){
t(null);
t(null);
tFocusInWindow();
}else if(rce()==yma){
t(该数对应的原码:
nds(122,
197, 120, 30);
yuanma();
}else if(rce()==fanma){
t(该数对应的反码:
fanma();
}else if(rce()==buma){
t(该数对应的补码:
buma();
}else
if(rce()==yima){
t(该数对应的移码:
yima();
}
}
private void yuanma() {
String s1
=t().trim();
int x =f(s1, 2);
if(x<0){
StringBuffer s11=new
StringBuffer(s1);
rAt(0, '1');
t(ng());
}else
if(x>0){
StringBuffer s22=new
StringBuffer(s1);
rAt(0, '0');
t(ng());
}
}
private void fanma() {
String
s =t().trim();
int x =f(s, 2);
int t=()-1;
if(x<0){
StringBuffer s1 = new StringBuffer(s);
CharAt(0);
StringBuffer s2 = new
StringBuffer(t().trim());
CharAt(0);
for(int i=0;i<();i++){
rAt(i, '1');
}
String
s21 =ng();
String s11 =ng();
int i1=f(s11, 2);
int i2 =f(s21, 2);
String s3 =ryString(i2^i1);
if(t!=()){
StringBuffer s4=new
StringBuffer(s3);
(0, '0');
s3 =ng();
}
t(
}else if(x>0){
StringBuffer
s1 = new StringBuffer(s);
CharAt(0);
String s11=ng();
t(
}
}
private void
buma() {
String s =t().trim();
int x =f(s, 2);
int t=()-1;
String sub=
int sub1=f(sub, 2);
if(x<0){
StringBuffer s1 = new StringBuffer(s);
CharAt(0);
StringBuffer s2 = new
StringBuffer(t().trim());
CharAt(0);
for(int i=0;i<();i++){
rAt(i, '1');
}
String
s21 =ng();
String s11 =ng();
int i1=f(s11, 2);
int i2 =f(s21, 2);
int sum =i2^i1;
sum+=sub1;
String s3 =ryString(sum);
if(t!=()){
StringBuffer s4=new
StringBuffer(s3);
(0, '0');
s3 =ng();
}
t(
}else if(x>0){
StringBuffer s1 = new StringBuffer(s);
CharAt(0);
String s11=ng();
t(
}
}
private void
yima() {
String s1 =t().trim();
int x= f(s1, 2);
if(x<0){
StringBuffer s12 =new
StringBuffer(s1);
CharAt(0);
int l=()-1;
char a=(l);
for(int i=l;i>0;i--){
rAt(i, (i-1));
}
rAt(0, a);
String shuchu =ng();
t(
}else if(x>0){
StringBuffer s12 =new
StringBuffer(s1);
CharAt(0);
int l=()-1;
char a=(l);
for(int i=l;i>0;i--){
rAt(i, (i-1));
}
rAt(0, a);
String shuchu =ng();
t(
}
}
}
package xiangmu;
**
*
*
@author pc
*import
import .*;
import .*;
import .*;
class Bmjj
extends JFrame implements ActionListener{
JTextField txt1,txt2,txt3,txt4;
JButton
b1,b2,b3,b4;
public Bmjj(){
nds(400, 100, 600, 400);
out(null);
izable(false);窗口大小不许改变
JLabel
label1= new JLabel(定点整数的变形补码加减运算
t(new
Font(宋体
nds(110, 1, 380, 100);
Label
JLabel label2 =new
JLabel(请输入第一个数:
t(new
Font(宋体改变字体大小
nds(130, 100, 150, 30);
JLabel label5 =new
JLabel(请输入第二个数:
t(new Font(宋体改变字体大小
nds(130, 150, 150, 30);
JLabel label3 = new JLabel(加法结果为:
t(new Font(宋体改变字体大小
nds(157, 200,
100, 30);
JLabel label4 = new
JLabel(减法结果为:
t(new
Font(宋体改变字体大小
nds(157,250, 100, 30);
TextField
txt1 =new JTextField(
nds(257,100, 150, 20);
txt2=new
JTextField( nds(257,150, 150, 20);
txt3
=new JTextField( nds(257,200, 150, 20);
txt4 =new JTextField( nds(257,250, 150, 20);
bled(false);
bled(false);
b1
= new JButton(输入 BUtton
t(new Font(宋体
nds(100, 320, 80,
30);
ionListener((ActionListener) this);
b2 = new JButton(加法
t(new
Font(宋体
nds(200, 320, 80, 30);
ionListener((ActionListener) this);
b3 =
new JButton(减法
t(new Font(宋体
nds(300, 320, 80, 30);
ionListener((ActionListener) this);
b4 =
new JButton(返回
t(new Font(宋体
nds(400, 320, 80, 30);
ionListener((ActionListener) this);
(label1);
(label5); (txt1);
(label2); (txt2);
(label3);
(txt3);
(label4); (txt4);
(b1); (b2); (b3); (b4);
ible(true);
aultCloseOperation(_ON_CLOSE);
}
@Override
public void
actionPerformed(ActionEvent e) {
String
s1=t().trim();
String s2=t().trim();
if(rce()==b1){
tDefaultFocus();
}else if(rce()==b2){
jiafa(s1,s2);
}else if(rce()==b3){
jianfa(s1,s2);
}else if(rce()== b4){
Window win = new Window();
e();
}
}
private void jiafa(String s1,
String s2) {
int x=f(s1, 2);
int y=f(s2, 2);
String sum
=ryString(x+y);
t(sum);
}
private void jianfa(String
s1, String s2) {
int x=f(s1, 2);
int y =f(s2, 2);
String jian
=ryString(x-y);
t(jian);
}
}
package xiangmu;
**
*
*
@author pc
*import
import .*;
import .*;
import .*;
class Ymcf
extends JFrame implements ActionListener{
TextField t1,t2,t3;
JButton b1,b2,b3;
public Ymcf(){
nds(400, 100, 600,
400);
out(null);
izable(false);窗口大小不许改变
JLabel
JLabel label1 = new
JLabel(请输入被乘数:
t(new Font(宋体
nds(100, 100, 120, 100);
JLabel label2
=new JLabel(请输入乘数:
t(new Font(宋体
nds(115, 150, 100, 100);
JLabel label3
= new JLabel(乘法结果为:
t(new Font(宋体
nds(115, 200, 100, 100);
JLabel label4
=new JLabel(定点整数的原码乘法
t(new Font(宋体
nds(180, -20, 380, 210);
TextField
t1=new TextField(
nds(220, 140, 200, 20);
t2 =new
TextField(
t3 =new TextField(
bled(false);
b1 = new
JButton(输入 BUtton
t(new Font(宋体
nds(100, 320, 80,
30);
ionListener((ActionListener) this);
b2 = new JButton(乘法
t(new
Font(宋体
nds(250, 320, 80, 30);
ionListener((ActionListener) this);
b3 =
new JButton(返回
t(new Font(宋体
nds(400, 320, 80, 30);
ionListener((ActionListener) this);
(label1); (t1);
(label2);
(t2);
(label3); (t3);
(b1); (b2); (b3);
(label4);
ible(true);
aultCloseOperation(_ON_CLOSE);
}
@Override
public void
actionPerformed(ActionEvent e) {
if(rce()==b1){
t(null);
t(null);
t(null);
tFocusInWindow();
}else if(rce()==b2){
String s1 =t().trim();
String s2 =t().trim();
chengfa(s1,s2);
}else if(rce()==b3){
Window win=new
Window();
e();
}
}
private void chengfa(String s1,
String s2) {
StringBuffer
fuhao1=new StringBuffer(s1);
StringBuffer fuhao2 =new StringBuffer(s2);
int x=f(s1, 2);
int y =f(s2, 2);
if((x<0&&y<0)*||((0)=='1'&&(0)=='1')*){
StringBuffer s11 = new StringBuffer(s1);
StringBuffer s21 =new StringBuffer(s2);
CharAt(0);
CharAt(0);
s1 =ng();
s2=ng();
int x1 =f(s1, 2);
int x2 =f(s2,
2);
int sub =x1*x2;
String sub1 =ryString(sub);
t(sub1);
}else
if((x<0&&y>0)*||((0)=='1'&&(0)=='0')*){
StringBuffer s11 = new StringBuffer(s1);
StringBuffer s21 =new StringBuffer(s2);
CharAt(0);
CharAt(0);
s1 =ng();
s2=ng();
int x1 =f(s1, 2);
int x2 =f(s2,
2);
int sub =x1*x2;
String sub1 =ryString(sub);
t(sub1);
}else if((x>0&&y<0)*||((0)=='0'&&(0)=='1')*){
StringBuffer s11 = new StringBuffer(s1);
StringBuffer s21 =new StringBuffer(s2);
CharAt(0);
CharAt(0);
s1 =ng();
s2=ng();
int x1 =f(s1, 2);
int x2 =f(s2,
2);
int sub =x1*x2;
String sub1 =ryString(sub);
t(sub1);
}else
if((x>0&&y>0)*||((0)=='0'&&(0)=='0')*){
StringBuffer s11 = new
StringBuffer(s1);
StringBuffer s21 =new StringBuffer(s2);
CharAt(0);
CharAt(0);
s1 =ng();
s2=ng();
int x1 =f(s1, 2);
int x2 =f(s2,
2);
int sub =x1*x2;
String sub1 =ryString(sub);
t(sub1);
}
}
}