计算机组成原理实验 下载本文

计算机组成原理课程设计说明书

计算机组成原理算法实现(五)

1 课程设计目的

本课程设计是在学完本课程教学大纲规定的全部内容、完成所有实践环节的基础上,旨在深化学生学习的计算机组成原理课程基本知识,进一步领会计算机组成原理的一些算法,并进行具体实现,提高分析问题、解决问题的综合应用能力。

2 课程设计内容与要求 2.1 课程设计的内容

计算机组成原理算法实现(五)

2.2 课程设计的要求

能够实现机器数的真值还原(定点整数)、定点整数的单符号位补码加减运算、定点整数的原码一位乘法运算和浮点数的加减运算。

3 功能模块详细设计 3.1 系统目标

本程序含有以下几个功能模块,分别能够实现如设计内容所设计的功能。共有5类模块分为:机器数的真值还原,定点整数的单符号位补码加减运算,定点整数的原码乘法和浮点数的加减运算。分布和大概功能如图1所示:

图1 各类间的关系

系统流程图如图2所示

1

图2 系统流程图

3.2主体设计

Newclass.java

该java文件为整个程序的主类,是整个界面的主窗口。 Form2.java

该java文件实现机器数的真值还原(定点整数)。 Form3.java

实现定点整数单符号位补码加减运算。 Form4.java

实现定点整数的原码乘法运算。 Form5.java

实现浮点数的加减运算。 3.2.1 主窗体的设计 程序的主界面如图3所示

2

图3 主界面

操作要求:按照输入指令的提示输入用户进入操作算法的指令:121 当输入的口令出错时所显示的提示,如图4所示:

图4 口令错误提示

输入口令达到3次错误时,单击确定后程序自动关闭。如图5所示:

3

图5 口令错误过多提示

图6 进入主界面

3.2.2 定点整数真值还原窗体的设计

选择主窗体中“机器数的真值还原(定点整数)”时进入下图所示的窗体。在上面的窗体中按“输入”按钮时,将输入焦点设置为最上面的一个文本框上。输入一个机器数(如111001)后,按“原->真值”、“反->真值”、“补->真值”或“移->真值”按钮中的任一个后,将在第二文本框中显示对应操作的结果。选择“返回”按钮时将关闭此窗体。

算法的原理:计算机中常用的数据表示格式有两种,一是定点格式,二是浮点格式。

a)定点表示:约定机器中所有数据的小数点位置是固定的。由于约定在固定的位置,所以小数点就不再使用记号“.”来表示。

b)浮点表示:定点数表示的数的范围有限,为了扩展数的表示范围,按照科学计数法表示数据的方式,任何一个二进制数N都可以表示成如下的格式:

N=M*2e

M:尾数,是一个纯小数,决定数据的表示精度

e:指数,又称为阶码,是一个整数,决定数据的表示范围

一般书写所表示的数据称为真值,在计算机中为了表示符号位,通常把符号位和数字位一起编码来表示相应的数,形成了各种数据的存储和表示方法,这些编码称为机器码。常用的机器码有原码、反码、补码和移码。

a)原码:原码的数值部分是该数的绝对值,最高位表示符号位,最高位为0是正数,最高位为1是负数。如图7所示:

4

图7 原码的真值

b)反码:正数的反码等于原码,负数的反码等于除符号位外其余二进制数码0变成1,1变成0. 如图8所示:

正数:【x】反=【x】原=x

负数:符号位不变,其余变反

图8 反码的真值

c)补码:正数的补码等于原码,负数的补码等于反码加1.如图9所示: 正数:【x】补=【x】原 负数:【x】补=【x】反+1

5

图9 补码的真值

d)移码:是符号位取反的补码,一般用做浮点数的阶码,引入的目的是为了保证浮点数的机器码为全0. 如图10所示:

图10 移码的真值

真值还原流程图:

6

开始 键盘输入一个二进制 选择要表示的形式 返回 原码 反码 补码 移码 ‘0’转变为+0,‘1’转变为-0,数值位不变显示输出 ‘0’开头:‘0’转变为+0,数值位不变显示输出 ‘1’开头:‘1’转变为-0,数值位取反显示输出 ‘0’开头:‘0’转变为+0,数值位不变显示输出 ‘1’开头:‘1’转变为-0数值位取反,末位加1,,显示输出 ‘0’开头:‘0’转变为-0,数值位各位,末位+1显示输出 ‘1’开头:’1’转变为+0,数值位不变,显示输出 图11 真值还原流程图

3.2.3定点整数的单符号位补码加减

进行定点整数的单符号位补码加减法的实现时都是在主窗体选择对应的菜单项后进入对应窗体再进行具体操作。操作时首先选择“输入”按钮输入参与运算的数据,然后再选操作按钮。如图12所示:

算法的原理:任何两数的补码之和等于两数之和的补码 补码加法【x】补+【y】补=【x+y】补

补码减法【x】补-【y】补=【x】补+【-y】补 当负数用补码表示后,符号位作为数据的一部分一起参加运算,运算器不用考虑参加加法运算的操作数的正负以及结果的正负,任意数的加法就可以化作正数加法来作。

溢出:在定点数机器数中,数的大小超出了定点数能表示的范围,叫溢出。 a)在定点小数机中数的表示范围是-1

7

b)在定点整数机中数的表示范围是-128

1符号位“01”○,上溢出 2符号位“10”○,下溢出 3符号位“00”或者“11”○,未溢出从【y】补求【-y】补得法则是:对【y】补“包括符号位求反且最末位加1”,即可得到【-y】补

图12 定点整数的单符号位补码加减

3.2.4定点整数的原码乘法运算

进行定点整数原码乘法的实现时都是在主窗体选择对应的菜单项后进入对应窗体再进行具体操作。操作时首先选择“输入”按钮输入参与运算的数据,然后再选操作按钮。如图13所示:

算法的原理:在定点计算机中,两个原码表示的数相乘的运算规则是:乘积的符号位由两数的符号位按异或运算得到,而乘积的数值部分则是两个正数相乘之积。设n位被乘数和乘数用定点整数表示。

8

图13 定点整数的原码乘法运算

3.2.5 浮点数的加减法运算

进行定点整数单符号位补码加减法、定点整数原码乘法、浮点加减法的实现时都是在主窗体选择对应的菜单项后进入对应的窗体再进行具体操作。操作时首先选择“输入”按钮输入参与运算的数据,然后再选操作按钮。如图14所示:

算法的原理:浮点数的加减法运算分为六个步骤: a)0操作数检查

浮点加减运算过程比定点运算过程复杂。如果判知两个操作数X或y中有一个数为0,即可得知运算结果而没有必要再进行后续的一系列操作以节省运算时间。0操作数检查步骤则用来完成这一功能。参加加法运算的数据都是非零,进入下一步。

b)比较阶码大小并完成对阶

为了便于直观理解,假设两数均以补码表示,阶码采用双符号位,尾数采用单符号位。两浮点数进行加减,首先要看两数的阶码是否相同,若二数阶码不同,表示小数点位置没有对齐,此时必须使二数阶码相同,这个过程叫做对阶。对阶操作规定使尾数右移,尾数右移后阶码作相应增加,因此对阶时,总是使小阶向大阶看齐。

c)尾数进行加减运算

对阶结束后,即可进行尾数的求和运算。不论加法运算还是减法运算,都按加法进行操作,其方法与定点加减法运算完全一样。

d)结果规格化

1采用双符号位表示尾数时,○如果两符号位为01或10时,应将结果尾数右移一位,阶码加1(叫“右规”)。

2如果尾数最高数值位与符号位相同,应将尾数左移,阶码减1,直至数值○

位最高位与符号位相反(叫“左规”)。

e)舍入运算

在对阶向右规格化,尾数向右移位,这样被右移的尾数的低位部分会被丢掉,

9

从而造成一定误差,要进行舍入处理。

简单的舍入方法有两种:一种是“0舍1入”法,即如果右移时被丢掉数位的最高位为0则舍去,为1则将尾数的末位做加1运算。另一种是“恒置1”法,即只要数位被移掉,就在尾数的末尾恒置1。

f)判断结果是否溢出 阶码为00011,阶码的符号位为00,根据补码的双符号检测溢出的方法确定阶码不溢出。

图14 浮点数的加减法运算

4 设计小结

通过这次的计算机组成原理的课程设计,使我对计算机的认识有了更进一步的加深和认知。同时知道了组成原理这门课程不单单只是简单的书本上的知识,有更多的实践知识。这次实验我是用JAVA语言来编写的,过程当中碰到了很多问题,翻阅了一些书籍,同学之间相互讨论,问题最终得到了解决。

这次对于课题的能够实现机器数的真值还原(定点整数)、定点整数的单符号位补码加减运算、定点整数的原码一位乘法运算和浮点数的加减运算的设计总体来说还是比较令人满意的,不仅用到了课堂上所学到的知识,还加入了自己的一些想法与观点,而且在对编程这一方面也有了很多新的感悟与提高。 很开心自己能够靠着自己的一些努力做出这样的成果。希望自己以后能够找准自己要走的方向。不要再做无用功,盲目的去做事情。就像这次的课程设计,就是因为走了弯路盲目的去做,才导致浪费掉了很多时间。经过了这次的课程设计,我想,对于我日后的计算机的相关的学习肯定会有诸多助益与影响,为将来的学习生活打下了坚实的基础。

10

参考文献

[1] 白中英. 计算机组成原理(第五版)[M]. 北京: 科学出版社, 2010.

[2] 陈磊、陈国君、李海生、邹林达.java程序设计基础(第四版) [M]. 北京: 清华大学出版社, 2013.

[3] 王爱英. 计算机组成原理与系统结构(第五版)[M]. 北京: 清华大学出版社出版社, 2001. [4] 耿祥义、张跃平. Java程序设计(第五版)[M]. 北京: 清华大学出版社, 2010.

11

附录:

Newclass类: import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class Newclass extends Frame implements ActionListener { static int i = 0; static int m = 1; Label a1, a2, a22; //输入语句 TextField text1; //文本框 Button button1, m1, m2, m3, m4, m5; //按钮 Font f; Newclass(String s) { super(s); setLayout(null); //默认布局样式 f = new Font(\楷体\Font.BOLD, 20); //字体 m1 = new Button(\机器数的真值还原(定点整数)\ m2 = new Button(\定点整数单符号位补码加减运算\ m3 = new Button(\定点整数的原码乘法\ m4 = new Button(\浮点的加减运算\ m1.addActionListener(this); //监测控件是否正常 m2.addActionListener(this); m3.addActionListener(this); m4.addActionListener(this); m1.setBounds(10, 30, 160, 25); //在坐标系重的位置和按钮的宽度高度 m2.setBounds(175, 30, 190, 25); m3.setBounds(370, 30, 120, 25); m4.setBounds(495, 30, 95, 25); add(m1); add(m2); add(m3); add(m4); a1 = new Label(\计算机组成原理算法实现(五)\ a2 = new Label(\输入口令:\ a22 = new Label(\登陆成功\Label.CENTER); a1.setBounds(150, 100, 300, 40); a1.setBackground(Color.WHITE); a1.setFont(f); a2.setBounds(150, 200, 90, 25); a2.setBackground(Color.white); a22.setBounds(200, 200, 200, 25); a22.setForeground(Color.white); a22.setBackground(Color.red); a22.setVisible(false); text1 = new TextField(18); text1.setEchoChar('*'); text1.setBounds(240, 200, 200, 25); button1 = new Button(\确认\ button1.setBounds(270, 250, 70, 30); button1.addActionListener(this); add(a1); add(a2); add(a22); add(text1); add(button1); setBackground(Color.white); setBounds(100, 100, 640, 400); setVisible(true); validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String args[]) { new Newclass(\计算机组成原理\ } public void actionPerformed(ActionEvent e) { String s = new String(\ if (e.getSource() == button1) { while (i == 0 && m <= 3 && (!(text1.getText().equals(\ if (text1.getText().equals(s)) { i = 1; JOptionPane.showMessageDialog(this, \口令正确,请选择菜单栏的操作 \ \正确\JOptionPane.INFORMATION_MESSAGE); text1.setEnabled(false); text1.setVisible(false); a22.setVisible(true); a2.setVisible(false); } else { m++; JOptionPane.showMessageDialog(this, \您输入的口令不正确\警告\ JOptionPane.WARNING_MESSAGE); text1.setText(null); } } if (m > 3) { JOptionPane.showMessageDialog(this, \您三次口令错误,确定后退出!\警告\ JOptionPane.ERROR_MESSAGE); System.exit(0); } } if (i == 1) { if (e.getSource() == m1) { Form2 s1 = new Form2(); s1.setVisible(true); } else if (e.getSource() == m2) { Form3 f1 = new Form3(); f1.setTitle(\定点整数单符号位补码加减\ f1.setVisible(true); } else if (e.getSource() == m3) { Form4 f2 = new Form4(); f2.setTitle(\定点整数补乘法\ f2.setVisible(true); } else if (e.getSource() == m4) { Form5 f5 = new Form5(); f5.setVisible(true); } } } }

12

Form2类:

import javax.swing.*;

import java.awt.*; //引用非本包的类 import java.awt.event.*;

import javax.swing.SwingUtilities; import java.awt.TextField;

class Form2 extends Frame implements MouseListener { //定义按钮及页面布局 Button button1; Button button2; Button button3; Button button4; Button button5; Button button6; Label label1; Label label2; Label label3; //Label label4; //Label label5; //Label label6; //Label label7; TextField text1; TextField text2; //TextField text3; String s, s1, temp; int n1, i, j; char a[]; public Form2() { setTitle(\ button1 = new Button(\输入\ button2 = new Button(\原-->真值\ button3 = new Button(\反-->真值\ button4 = new Button(\补-->真值\ button5 = new Button(\移-->真值\ button6 = new Button(\返回\ label1 = new Label(\机器数的真值还原(定点整数)\ Font f1 = new Font(\ label1.setFont(f1); label2 = new Label(\请输入机器数:\ label3 = new Label(\真值还原结果为:\ text1 = new TextField(8); text2 = new TextField(10); text2.setEditable(false); Box baseBox, box1, box2, box3, box4, box5; box1 = Box.createHorizontalBox(); //创建一个从左到右显示其组建的BOX box1.add(label1); box2 = Box.createHorizontalBox(); box2.add(label2); box2.add(Box.createHorizontalStrut(10)); //添加长度为10的支架 box2.add(text1); box2.add(Box.createHorizontalStrut(10)); box3 = Box.createHorizontalBox(); box3.add(label3); box3.add(Box.createHorizontalStrut(10)); box3.add(text2); box3.add(Box.createHorizontalStrut(10)); box4 = Box.createHorizontalBox(); box5 = Box.createHorizontalBox(); box5.add(button1); box5.add(Box.createHorizontalStrut(20)); box5.add(button2); box5.add(Box.createHorizontalStrut(10)); box5.add(button3); box5.add(Box.createHorizontalStrut(10)); box5.add(button4); box5.add(Box.createHorizontalStrut(10));

box5.add(button5); box5.add(Box.createHorizontalStrut(10)); box5.add(button6); box5.add(Box.createHorizontalStrut(10)); baseBox = Box.createVerticalBox(); baseBox.add(box1); baseBox.add(Box.createVerticalStrut(10)); baseBox.add(box2); baseBox.add(Box.createVerticalStrut(10)); baseBox.add(box3); baseBox.add(Box.createVerticalStrut(10)); baseBox.add(box4); baseBox.add(Box.createVerticalStrut(10)); baseBox.add(box5); baseBox.add(Box.createVerticalStrut(10)); setLayout(new FlowLayout()); add(baseBox); text1.addMouseListener(this); text2.addMouseListener(this); button1.addMouseListener(this); button2.addMouseListener(this); button3.addMouseListener(this); button4.addMouseListener(this); button5.addMouseListener(this); button6.addMouseListener(this); addMouseListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(200, 300, 500, 300); setVisible(true); } public void mousePressed(MouseEvent e) { //按下鼠标 } public void mouseReleased(MouseEvent e) { //鼠标弹起 } public void mouseEntered(MouseEvent e) { //鼠标进入 } public void mouseExited(MouseEvent e) { //鼠标退出 } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { //鼠标拖动 } public void mouseClicked(MouseEvent e) { //鼠标右击 Component com = null; if (e.getSource() == button2) { com = (Component) e.getSource(); e = SwingUtilities.convertMouseEvent(com, e, this); //转移鼠标在窗口中的位置 String s = text1.getText(); boolean boo = s.startsWith(\ if (boo == true) { n1 = s.length(); s1 = s.substring(1, n1); text2.setText(\ } else { n1 = s.length(); s1 = s.substring(1, n1); text2.setText(\ } } else if (e.getSource() == button1) { text1.setText(\

13

e, this);

}

if (e.getSource() == button3) { s = text1.getText(); boolean boo = s.startsWith(\ if (boo == true) { n1 = s.length(); s1 = s.substring(1, n1); text2.setText(\ } else { n1 = s.length(); s1 = s.substring(1, n1); char a[] = s1.toCharArray(); for (i = 0; i <= a.length - 1; i++) { if (a[i] == '0') a[i] = '1'; else if (a[i] == '1') a[i] = '0'; } for (i = 0; i <= a.length - 1; i++) { s1 = String.valueOf(a); } text2.setText(\ } }

if (e.getSource() == button4) { s = text1.getText(); boolean boo = s.startsWith(\ if (boo == true) { n1 = s.length(); s1 = s.substring(1, n1); text2.setText(\ } else { n1 = s.length(); s1 = s.substring(1, n1); j = s1.lastIndexOf(\ s1 = s.substring(1, j); char a[] = s1.toCharArray(); for (i = 0; i <= a.length - 1; i++) { if (a[i] == '0') a[i] = '1'; else if (a[i] == '1') a[i] = '0'; } for (i = 0; i <= a.length - 1; i++) { s1 = String.valueOf(a); } temp = s.substring(j, n1); text2.setText(\ } }

if (e.getSource() == button5) { com = (Component) e.getSource(); e = SwingUtilities.convertMouseEvent(com,

String s = text1.getText();

boolean boo = s.startsWith(\if (boo == false) { n1 = s.length(); s1 = s.substring(1, n1); text2.setText(\} else { n1 = s.length(); s1 = s.substring(1, n1); j = s1.lastIndexOf(\ s1 = s.substring(1, j); char a[] = s1.toCharArray(); for (i = 0; i <= a.length - 1; i++) { if (a[i] == '0') a[i] = '1'; else if (a[i] == '1')

a[i] = '0'; } for (i = 0; i <= a.length - 1; i++) { s1 = String.valueOf(a); } temp = s.substring(j, n1); text2.setText(\ } } if (e.getSource() == button6) { //返回 com = (Component) e.getSource(); e = SwingUtilities.convertMouseEvent(com, e, this); Newclass pp = new Newclass(null); setVisible(false); pp.setVisible(true); } } }

Form3类

import java.awt.*; import javax.swing.*; import java.awt.event.*;

public class Form3 extends Frame implements ActionListener { Box basebox, box1, box2, box3, box4, box5, box6; Button button1, button2, button3, button4; Label name; Label input, input1, input2, input3; String first, next, output; TextField data, data1, data2, data3; public Form3() { setTitle(\ setBounds(200, 300, 500, 300); button1 = new Button(\输入\ button2 = new Button(\加法\ button3 = new Button(\减法\ button4 = new Button(\返回\ box1 = Box.createHorizontalBox(); name = new Label(\定点整数单符号位补码加减法\ box1.add(name); box2 = Box.createHorizontalBox(); input = new Label(\请输入第一个数(输入为补码):\ data = new TextField(10); box2.add(input); box2.add(data); box3 = Box.createHorizontalBox(); input1 = new Label(\请输入第二个数(输入为补码):\ data1 = new TextField(10); box3.add(input1); box3.add(data1); box4 = Box.createHorizontalBox(); input2 = new Label(\加法结果为(显示为补码):\ data2 = new TextField(10); box4.add(input2); box4.add(data2); box5 = Box.createHorizontalBox(); input3 = new Label(\减法结果为(显示为补码):\ data3 = new TextField(10); box5.add(input3); box5.add(data3); box6 = Box.createHorizontalBox(); box6.add(button1); box6.add(button2); box6.add(button3);

14

box6.add(button4); basebox = Box.createVerticalBox(); basebox.add(box1); basebox.add(Box.createVerticalStrut(20)); basebox.add(box2); basebox.add(Box.createVerticalStrut(20)); basebox.add(box3); basebox.add(Box.createVerticalStrut(20)); basebox.add(box4); basebox.add(Box.createVerticalStrut(20)); basebox.add(box5); basebox.add(Box.createVerticalStrut(20)); basebox.add(box6); setLayout(new FlowLayout()); add(basebox); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); } }); validate(); } char[] bu(char[] in) { char c = in[0]; if (c == '1') { char carry = '0'; int i; char temp[] = in; for (i = 1; i <= temp.length - 1; i++) /* 除符号位外各位取反 */ { if (temp[i] == '0') temp[i] = '1'; else if (temp[i] == '1') temp[i] = '0'; } if (temp[temp.length - 1] == '0') { temp[temp.length - 1] = '1'; carry = '0'; } /* 末位加一 */ else if (temp[temp.length - 1] == '1') { temp[temp.length - 1] = '0'; carry = '1'; } for (i = temp.length - 2; i >= 0; i--) /* 各位进位情况 */ { if (temp[i] == '0' && carry == '0') { temp[i] = '0'; carry = '0'; } else if (temp[i] == '0' && carry == '1') { temp[i] = '1'; carry = '0'; } else if (temp[i] == '1' && carry == '0') { temp[i] = '1'; carry = '0'; } else if (temp[i] == '1' && carry == '1') { temp[i] = '0'; carry = '1'; } }

in = temp; } return in; } char[] jia(char[] in1, char[] in2) { char[] out; char carry[] = new char[in1.length + 1]; /* carry[]用来存放每位的进位 */ int i = 0; out = in1; carry[carry.length - 1] = '0';/* carry[]最后一位初值为‘0’,carry[]长度比输入大一 */ for (i = in1.length - 1; i >= 0; i--) { if (in1[i] == '0' && in2[i] == '0' && carry[i + 1] == '0') { out[i] = '0'; carry[i] = '0'; } else if (in1[i] == '0' && in2[i] == '1' && carry[i + 1] == '0') { out[i] = '1'; carry[i] = '0'; } else if (in1[i] == '1' && in2[i] == '0' && carry[i + 1] == '0') { out[i] = '1'; carry[i] = '0'; } else if (in1[i] == '1' && in2[i] == '1' && carry[i + 1] == '0') { out[i] = '0'; carry[i] = '1'; } else if (in1[i] == '0' && in2[i] == '0' && carry[i + 1] == '1') { out[i] = '1'; carry[i] = '0'; } else if (in1[i] == '0' && in2[i] == '1' && carry[i + 1] == '1') { out[i] = '0'; carry[i] = '1'; } else if (in1[i] == '1' && in2[i] == '0' && carry[i + 1] == '1') { out[i] = '0'; carry[i] = '1'; } else if (in1[i] == '1' && in2[i] == '1' && carry[i + 1] == '1') { out[i] = '1'; carry[i] = '1'; } } if (carry[0] != carry[1]) /* 当最高数值位进位与符号位进位不同时表溢出 */ { String temp = new String(\溢出\ out = temp.toCharArray(); } return out; } char[] jian(char[] in1, char[] in2) { if (in2[0] == '1') /* 将第二个数转为其相反数的补码 */ { in2 = bu(in2); in2[0] = '0'; } else if (in2[0] == '0') { in2[0] = '1'; in2 = bu(in2); } char[] o = jia(in1, in2); /* 将减法转换为加法 */ return o; }

15

public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) /* 输入将光标定位在最上面文本框 */ { data.setText(null); data1.setText(null); data2.setText(null); data3.setText(null); data.requestFocusInWindow(); } else if (e.getSource() == button2) /* 加法 */ { first = data.getText(); next = data1.getText(); char[] out; char[] i1, i2; i1 = first.toCharArray(); i2 = next.toCharArray(); if (i1.length != i2.length) data1.setText(\请输入数值位长度相等的数字!\ else { out = jia(i1, i2); output = new String(out); data2.setText(output); } } else if (e.getSource() == button3) /* 减法 */ { first = data.getText(); next = data1.getText(); char[] out; char[] i1, i2; i1 = first.toCharArray(); i2 = next.toCharArray(); if (i1.length != i2.length) data1.setText(\请输入数值位长度相等的数字!\ else { out = jian(i1, i2); output = new String(out); data3.setText(output); } } else if (e.getSource() == button4)/* 返回 */ { new Newclass(null); setVisible(false); } } }

Form4类

import java.awt.*; import javax.swing.*; import java.awt.event.*;

public class Form4 extends Frame implements ActionListener { Box basebox, box1, box2, box3, box4, box5; Button button1, button2, button3; Label name, input, input1, input2; TextField data, data1, data2; public Form4() { setTitle(\ setBounds(200, 300, 500, 300); button1 = new Button(\输入\ button2 = new Button(\乘法\ button3 = new Button(\返回\ box1 = Box.createHorizontalBox(); name = new Label(\定点整数的原码乘法\Label.CENTER); box1.add(name); box2 = Box.createHorizontalBox(); input = new Label(\请输入被乘数(一位符号位):

\ data = new TextField(10); box2.add(input); box2.add(data); box3 = Box.createHorizontalBox(); input1 = new Label(\请输入乘数(一位符号位):\ data1 = new TextField(10); box3.add(input1); box3.add(data1); box4 = Box.createHorizontalBox(); input2 = new Label(\乘法结果为(一位符号位):\ data2 = new TextField(15); box4.add(input2); box4.add(data2); box5 = Box.createHorizontalBox(); box5.add(button1); box5.add(button2); box5.add(button3); basebox = Box.createVerticalBox(); basebox.add(box1); basebox.add(Box.createVerticalStrut(20)); basebox.add(box2); basebox.add(Box.createVerticalStrut(20)); basebox.add(box3); basebox.add(Box.createVerticalStrut(20)); basebox.add(box4); basebox.add(Box.createVerticalStrut(20)); basebox.add(box5); setLayout(new FlowLayout()); add(basebox); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); } }); validate(); } char[] jia(char[] in1, char[] in2) { char[] out; char carry[] = new char[in1.length + 1]; int i = 0; out = in1; carry[carry.length - 1] = '0'; for (i = in1.length - 1; i >= 0; i--) { if (in1[i] == '0' && in2[i] == '0' &&carry[i + 1] == '0') { out[i] = '0'; carry[i] = '0'; } else if (in1[i] == '0' && in2[i] == '1' && carry[i + 1] == '0') { out[i] = '1'; carry[i] = '0'; } else if (in1[i] == '1' && in2[i] == '0' && carry[i + 1] == '0') { out[i] = '1'; carry[i] = '0'; } else if (in1[i] == '1' && in2[i] == '1' && carry[i + 1] == '0') { out[i] = '0'; carry[i] = '1'; } else if (in1[i] == '0' && in2[i] == '0' && carry[i + 1] == '1') { out[i] = '1';

16

carry[i] = '0'; } else if (in1[i] == '0' && in2[i] == '1' && carry[i + 1] == '1') { out[i] = '0'; carry[i] = '1'; } else if (in1[i] == '1' && in2[i] == '0' && carry[i + 1] == '1') { out[i] = '0'; carry[i] = '1'; } else if (in1[i] == '1' && in2[i] == '1' && carry[i + 1] == '1') { out[i] = '1'; carry[i] = '1'; } } return out; } public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { data.setText(null); data1.setText(null); data2.setText(null); data.requestFocusInWindow(); } else if (e.getSource() == button2) { String s1 = data.getText(); String s2 = data1.getText(); String beichengshu = s1.substring(1); String signal = \ String temp = null; char[] in1 = s1.toCharArray(); char[] in2 = s2.toCharArray(); if (in1.length != in2.length) data1.setText(\请输入数值位长度相等的数字!\ else { if ((in1[0] == '0' && in2[0] == '0') || (in1[0] == '1' && in2[0] == '1')) { signal = \ } if ((in1[0] == '0' && in2[0] == '1') || (in1[0] == '1' && in2[0] == '0')) { signal = \ } int count = beichengshu.length() - 1; char c[] = new char[count + 1]; for (int i = 0; i <= count; i++) { c[i] = '0'; } String str[] = new String[beichengshu.length() + 1]; str[0] = s1 + new String(c); for (int i = beichengshu.length(); i > 0; i--) { if (in2[i] == '0') { str[i] = new String(c) + new String(c); } else if (in2[i] == '1') { str[i] = beichengshu + new String(c); } } for (int i = 1; i <= beichengshu.length(); i++) { int k;

char[] buwei = new char[i]; for (k = 0; k < i; k++) { buwei[k] = '0'; } str[i] = new String(buwei) + str[i].substring(0, str[i].length() - i); } temp = new String(c) + new String(c); char result[] = temp.toCharArray(); for (int i = 1; i < str.length; i++) { char jia1[] = str[i].toCharArray(); result = jia(result, jia1); } temp = signal + new String(result); data2.setText(temp); } } else if (e.getSource() == button3) { new Newclass(null); setVisible(false); } } }

Form5类

import javax.swing.*; import java.awt.*;

import java.awt.event.*; import java.awt.TextField;

public class Form5 extends Frame implements ActionListener { Box basebox, box1, box2, box3, box4, box5, box6; Button button1, button2, button3, button4; Label name, input, input1, input2, input3, input4, input5, input7, input6; TextField data, data1, data2, data3, data4, data5, data6, data7; public Form5() { setTitle(\ Toolkit tool = getToolkit(); tool.getScreenSize(); setBounds(200, 300, 500, 300); button1 = new Button(\输入\ button2 = new Button(\加法\ button3 = new Button(\减法\ button4 = new Button(\返回\ box1 = Box.createHorizontalBox(); name = new Label(\浮点数加减法\Label.CENTER); box1.add(name); box2 = Box.createHorizontalBox(); input = new Label(\第一个数的阶码:\ data = new TextField(10); input1 = new Label(\尾数:\ data1 = new TextField(10); box2.add(input); box2.add(data); box2.add(input1); box2.add(data1); box3 = Box.createHorizontalBox(); input2 = new Label(\第二个数的阶码:\ data2 = new TextField(10); input3 = new Label(\尾数:\ data3 = new TextField(10); box3.add(input2); box3.add(data2);

17

box3.add(input3); box3.add(data3); box4 = Box.createHorizontalBox(); input4 = new Label(\加法阶码:\ data4 = new TextField(10); input5 = new Label(\尾数:\ data5 = new TextField(10); box4.add(input4); box4.add(data4); box4.add(input5); box4.add(data5); box5 = Box.createHorizontalBox(); input6 = new Label(\减法的阶码为:\ data6 = new TextField(10); input7 = new Label(\补码为:\ data7 = new TextField(10); box5.add(input6); box5.add(data6); box5.add(input7); box5.add(data7); box6 = Box.createHorizontalBox(); box6.add(button1); box6.add(button2); box6.add(button3); box6.add(button4); basebox = Box.createVerticalBox(); basebox.add(box1); basebox.add(Box.createVerticalStrut(20)); basebox.add(box2); basebox.add(Box.createVerticalStrut(20)); basebox.add(box3); basebox.add(Box.createVerticalStrut(20)); basebox.add(box4); basebox.add(Box.createVerticalStrut(20)); basebox.add(box5); basebox.add(Box.createVerticalStrut(20)); basebox.add(box6); setLayout(new FlowLayout()); add(basebox); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); } }); validate(); } String calculate(String s1, String s2) { char ac = '0'; char a1[] = s1.toCharArray(), a2[] = s2.toCharArray(); for (int i = a1.length - 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 (s.startsWith(\ s = \ } if (s.startsWith(\ s = \ } if (s.startsWith(\ String temp = s.substring(1, s.lastIndexOf(\ char c[] = temp.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else c[i] = '0'; } temp = new String(c); s = \+ temp + s.substring(s.lastIndexOf(\ } return s; } public String weishuQiubu(String s)/* 尾数求变形补码(缺省小数点,多加长度个0) */ { if (s.startsWith(\ String str = s.substring(2).replaceAll(\\ s = \ } if (s.startsWith(\ String str = s.substring(3).replaceAll(\\ s = \ } if (s.startsWith(\ String temp = null, str = s.substring(3).replaceAll(\ temp = s.substring(3, s.lastIndexOf(\ char c[] = temp.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else

18

c[i] = '0'; } temp = new String(c); s = \+ temp + s.substring(s.lastIndexOf(\ } return s; } public int toSubstract(String sj1, String sj2)/* 求阶差 */ { int sum = 0, signal = 0; char cj2[] = sj2.substring(0, sj2.lastIndexOf(\ for (int i = 0; i < cj2.length; i++) { if (cj2[i] == '0') cj2[i] = '1'; else cj2[i] = '0'; } sj2 = new String(cj2) + sj2.substring(sj2.lastIndexOf(\ String temp = calculate(sj1, sj2); if (temp.startsWith(\第二个数阶码小 */ { signal = 1; temp = temp.substring(2); } if (temp.startsWith(\第一个数阶码小 */ { signal = -1; String str = temp.substring(2, temp.lastIndexOf(\ char c[] = str.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else c[i] = '0'; } temp = new String(c) + temp.substring(temp.lastIndexOf(\ } else { JOptionPane.showMessageDialog(this, \对阶时溢出!\错误\ JOptionPane.ERROR_MESSAGE); } int n[] = new int[temp.length()]; for (int i = 0; i < n.length; i++) { if (temp.substring(i, i + 1).equals(\ n[i] = 0; else n[i] = 1; } for (int i = n.length - 2; i >= 0; i--) { for (int j = i; j >= i; j--) { n[i] = n[i] * 2; } } for (int i = 0; i < n.length; i++) { sum = sum + n[i]; } sum = sum * signal; return sum; } public String bumaHuanyuan(String s)/* 变形补码还原真值处理,无小数点 */ { if (s.startsWith(\ s = \

} else if (s.startsWith(\ String temp = s.substring(2, s.lastIndexOf(\ char c[] = temp.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else c[i] = '0'; } temp = new String(c); s = \+ temp + s.substring(s.lastIndexOf(\ } return s; } public String[] myAdd(String sj1, String sj2, String st1, String st2) { String s[] = new String[2]; int n = 0, count = 0, len = (st1.length() - 2) / 2; n = toSubstract(sj1, sj2);/* 求阶差 */ if (n < 0)/* 第一个数的尾数右移 */ { for (int i = n; i < 0; i++) { st1 = st1.substring(0, 1) + st1.substring(0, st1.length() - 1); } sj1 = sj2;/* 统一阶码 */ } if (n > 0)/* 第二个数的尾数右移 */ { for (int i = 1; i <= n; i++) { st2 = st2.substring(0, 1) + st2.substring(0, st2.length() - 1); } sj2 = sj1;/* 统一阶码 */ } st1 = calculate(st1, st2);/* 尾数求和 */ while (!(st1.startsWith(\|| st1.startsWith(\规格化 */ { if (st1.startsWith(\|| st1.startsWith(\尾数左移 */ { st1 = st1.substring(1) + \ count++; } else if (st1.startsWith(\|| st1.startsWith(\尾数右移 */ { st1 = st1.substring(0, 1) + st1.substring(0, st1.length() - 1); count--; } } while (count > 0)/* 阶码循环减一 */ { String temp = sj1.replaceAll(\ temp = temp.substring(1) + \ sj1 = calculate(sj1, temp); count--; } while (count < 0) { String temp = sj1.replaceAll(\ temp = temp.substring(1) + \ sj1 = calculate(sj1, temp); count++; } sj1 = bumaHuanyuan(sj1);

19

st1 = st1.substring(0, len + 3); if (st1.endsWith(\尾数舍入处理 */ { String temp = st1.replaceAll(\ temp = temp.substring(1) + \ st1 = calculate(st1, temp); } else if (st1.endsWith(\ } st1 = st1.substring(0, st1.length() - 1); st1 = bumaHuanyuan(st1);/* 尾数补码还原处理 */ s[0] = sj1; s[1] = st1; return s; } public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { data.setText(null); data1.setText(null); data2.setText(null); data3.setText(null); data4.setText(null); data5.setText(null); data6.setText(null); data7.setText(null); data.requestFocusInWindow(); } if (e.getSource() == button2) { String sj1 = jiemaQiubu(data.getText()), sj2 = jiemaQiubu(data2.getText()), st1 = weishuQiubu(data1 .getText()), st2 = weishuQiubu(data3.getText()); if ((sj1.length() != sj2.length()) || (st1.length() != st2.length())) { JOptionPane.showMessageDialog(this, \请输入长度相等的数据!\错误\ JOptionPane.ERROR_MESSAGE); } else { String s[] = myAdd(sj1, sj2, st1, st2); data4.setText(s[0]); data5.setText(s[1].substring(0, 1) + \ } } if (e.getSource() == button3) { String sj1 = jiemaQiubu(data.getText()), sj2 = jiemaQiubu(data2.getText()), st1 = weishuQiubu(data1 .getText()), st2 = weishuQiubu(data3.getText()), temp; if ((sj1.length() != sj2.length()) || (st1.length() != st2.length())) { JOptionPane.showMessageDialog(this, \请输入长度相等的数据!\错误\ JOptionPane.ERROR_MESSAGE); } else { temp = st2.substring(0, st2.lastIndexOf(\ char c[] = temp.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else c[i] = '0';

}

st2 = new String(c) + st2.substring(st2.lastIndexOf(\ String s[] = myAdd(sj1, sj2, st1, st2); data6.setText(s[0]); data7.setText(s[1].substring(0, 1) + \ } } else if (e.getSource() == button4) { new Newclass(null); setVisible(false); } } }

20