计算机组成原理课程设计报告

时间:2024.4.30

课程设计说明书

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

1 课程设计目的

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

2 课程设计内容与要求

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

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

3 功能模块详细设计

(1)系统进入(主)窗体的设计:菜单需要在输入口令正确后方可激活使用。口令输入错误时要给出重新输入口令的提示,三次口令输入错误应该禁止使用。

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);

                     }

             

(2)选择主窗体中“定点整数真值还原”时进入下图所示的窗体:

if (e.getSource() == button2) {

                     com = (Component) e.getSource();

                     e = SwingUtilities.convertMouseEvent(com, e, this);

                     String s = text1.getText();

                     boolean boo = s.startsWith("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

}

else {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("-" + s1);

                     }

if (e.getSource() == button3) {

                     s = text1.getText();

                     boolean boo = s.startsWith("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

                     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';

                                   elseif (a[i] == '1')

                                          a[i] = '0';

                            }

            }

}

if (e.getSource() == button4) {

                     s = text1.getText();

                     boolean boo = s.startsWith("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

                     else {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            j = s1.lastIndexOf("1") + 1;

                            s1 = s.substring(1, j);

                            char a[] = s1.toCharArray();

                            for (i = 0; i <= a.length - 1; i++) {

                                   if (a[i] == '0')

                                          a[i] = '1';

                                   elseif (a[i] == '1')

                                          a[i] = '0';

                            }

              }

}

在上面的窗体中按“输入”按扭时,将输入焦点设置为最上面的一个文本框上。输入一个定点整数形式的机器数(如101010或011010)后,按“原真值”、“反真值”、“补真值”或“移真值”按扭中的任一个后,将在第二个文本框中显示对应的真值。选择“返回”按扭时回到主窗体。

(3)选择主窗体中“定点整数单符号位补码加减法”时进入下图所示的窗体:

if (i1.length != i2.length)

                            data1.setText("请输入数值位长度相等的数字!");

                     else {

                            out = jia(i1, i2);

                            output = new String(out);

                            data2.setText(output);

                     }

                  if (i1.length != i2.length)

                            data1.setText("请输入数值位长度相等的数字!");

                     else {

                            out = jian(i1, i2);

                            output = new String(out);

                            data3.setText(output);

                     }

在上面的窗体中按“输入”按钮时,将输入焦点设置为最上面的两个文本框。输入第一个、第二个数的补码,按“加法”、“减法”按钮中的任一个,在第三、第四文本框中显示对应的补码结果。选择“返回”按钮时回到主窗口。

(4)选择主窗体中“定点整数原码乘法”时进入下图所示的窗体:

for (int i = beichengshu.length(); i > 0; i--) //按照乘数各个位判别

                            {

                                   if (in2[i] == '0') {

                                          str[i] = new String(c) + new String(c);

                                   }

                                   elseif (in2[i] == '1') {

                                          str[i] = beichengshu + new String(c);

                                   }

                            }

                      for (int i = 1; i <= beichengshu.length(); i++)//实现右移操作

                            {

                                   int k;

                                   char[] buwei = newchar[i];

                                   for (k = 0; k < i; k++)

                                   {

                                          buwei[k] = '0';

                                   }

                                   str[i] = new String(buwei)

                                                 + str[i].substring(0, str[i].length() - i);

                            }

在上面的窗体中按“输入”按钮时,将输入焦点设置为最上面的两个文本框。

输入一位符号位的被乘数和一位符号位的乘数,按“乘法”按钮,在第三个文本框中显示对应的一位符号位的乘法结果。选择“返回”按钮时回到主窗口。

(5)选择主窗体中“浮点加减法”时进入下图所示的窗体:

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) + "0." + s[1].substring(1));

                     }

                     if ((sj1.length() != sj2.length())|| (st1.length() != st2.length())) {

                     JOptionPane.showMessageDialog(this, "请输入长度相等的数据!", "错误",

                                          JOptionPane.ERROR_MESSAGE);

                     }

                     else {

                            temp = st2.substring(0, st2.lastIndexOf("1"));                         

                            char c[] = temp.toCharArray();

                            for (int i = 0; i < c.length; i++) {

                                   if (c[i] == '0')

                                          c[i] = '1';

                                   else

                                          c[i] = '0';

                            }

            }    

在上面的窗体中按“输入”按钮时,将输入焦点设置为最上面的四个文本框。分别输入第一个数和第二个数的阶码,按“加法”或者“减法”按钮,在下面四个文本框中显示结果。选择“返回”按钮时回到主窗口。

                    

4 设计小结

这次计算机组成原理课程设计使我的能力得到了很大的提高,此外使我对本学期所学的计算机组成原理的知识得到了提高,加深了对计算机工作原理的认识,我也体会到了作为一个大学生,要想学有所得,就得学习主动,不要什么都希望别人亲自传授,面对问题要自己去努力解决,多问问身边的同学,多动手查查,多上网找找,所以要想成功就得事事做到细心,耐心,恒心。

当然,在这个过程中,也有许多问题与困难出现。有的经过自己的思考或与他人合作能得以解决,有的也的确费了一些周折。但无论如何,这次对于课题的能够实现机器数的真值还原(定点整数)、定点整数的单符号位补码加减运算、定点整数乘法运算和浮点数的加减运算的设计总体来说还是比较令人满意的,不仅用到了课堂上所学到的知识,还加入了自己的一些想法与观点,而且在对编程这一方面也有了很多新的感悟与提高。不得不说是获益良多。

参考文献

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

[2] 黄理,洪亮,曹林有,张勇.JSP高级编程[M]. 北京:北京希望电子出版社,2001.

[3] 连洪武.Eclipse Web开发从入门到精通(实例版)[M]. 北京:清华大学出版社,2007 .

[4] 王国辉、李立文.JSP数据库系统开发完全手册[M]. 北京:人民邮电出版社,2006.

源程序

MainFrame.java

public class MainFrame {

       public static void main(String args[]) {

              new Newclass(null);

       }

}

Newclass.java

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;

       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("计算机组成原理算法实现(五)", Label.CENTER);

              a2 = new Label("输入口令(000):");

              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.green);

              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("000");

              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) {

                            Ljys s1 = new Ljys();

                            s1.setVisible(true);

                     } else if (e.getSource() == m2) {

                            Zhengshujiajian f1 = new Zhengshujiajian();

                            f1.setTitle("定点整数单符号位补码加减运算");

                            f1.setVisible(true);

                     } else if (e.getSource() == m3) {

                            Zhengshuchengfa f2 = new Zhengshuchengfa();

                            f2.setTitle("定点整数的原码乘法");

                            f2.setVisible(true);

                     } else if (e.getSource() == m4) {

                            Fdys f5 = new Fdys();

                            f5.setVisible(true);

                     }

              }

       }

}

Ljys.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.SwingUtilities;

import java.awt.TextField;

class Ljys 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 Ljys() {

              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("", Font.TRUETYPE_FONT, 25);

              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();

              box1.add(label1);

              box2 = Box.createHorizontalBox();

              box2.add(label2);

              box2.add(Box.createHorizontalStrut(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("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

                     else {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("-" + s1);

                     }

              }

              else if (e.getSource() == button1) {

                     text1.setText("");

              }

              if (e.getSource() == button3) {

                     s = text1.getText();

                     boolean boo = s.startsWith("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

                     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("-" + s1);

                     }

              }

              if (e.getSource() == button4) {

                     s = text1.getText();

                     boolean boo = s.startsWith("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

                     else {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            j = s1.lastIndexOf("1") + 1;

                            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("-" + s1 + temp);

                     }

              }

              if (e.getSource() == button5) {

                     com = (Component) e.getSource();

                     e = SwingUtilities.convertMouseEvent(com, e, this);

                     String s = text1.getText();

                     boolean boo = s.startsWith("0", 0);

                     if (boo == true) {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            text2.setText("+" + s1);

                     }

                     else {

                            n1 = s.length();

                            s1 = s.substring(1, n1);

                            j = s1.lastIndexOf("1") + 1;

                            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("-" + s1 + temp);

                     }

              }

              if (e.getSource() == button6) {

                     com = (Component) e.getSource();

                     e = SwingUtilities.convertMouseEvent(com, e, this);

                     Newclass pp = new Newclass(null);

                     setVisible(false);

                     pp.setVisible(true);

              }

       }

}

Zhengshujiajian.java

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Zhengshujiajian 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 Zhengshujiajian() {

              setTitle("定点整数单符号位补码加减运算");

              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);

              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);

              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;

       }

       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);

              }

       }

}

Zhengshuchengfa.java

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Zhengshuchengfa 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 Zhengshuchengfa() {

              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(9);

              box3.add(input1);

              box3.add(data1);

              box4 = Box.createHorizontalBox();

              input2 = new Label("乘法结果为(一位符号位):");

              data2 = new TextField(12);

              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';//最后一位没有进位位(为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';

                            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 = "0";

                     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 = "0";

                            }

                            if ((in1[0] == '0' && in2[0] == '1')|| (in1[0] == '1' && in2[0] == '0'))

                            {

                                   signal = "1";

                            }//判别结果符号

                            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);

              }

       }

}

Fdys.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.TextField;

public class Fdys 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 Fdys() {

              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);

              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 = "00" + s.substring(1);

              }

              if (s.startsWith("-")) {//找到最后一个1,将之前的数取反

                     String temp = s.substring(1, s.lastIndexOf("1"));

                     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 = "11" + temp + s.substring(s.lastIndexOf("1"));//写出负数的补码

              }

              return s;

       }

       public String weishuQiubu(String s)/* 尾数求变形补码(缺省小数点,多加长度个0) */

       {

             

              if (s.startsWith("+")) {

                     String str = s.substring(3).replaceAll("1", "0");

                     s = "00" + s.substring(3) + str;

              }

              if (s.startsWith("-")) {

                     String temp = null, str = s.substring(3).replaceAll("1", "0");

                     temp = s.substring(3, s.lastIndexOf("1"));

                     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 = "11" + temp + s.substring(s.lastIndexOf("1")) + str;

              }

              return s;

       }

       public int toSubstract(String sj1, String sj2)/* 求阶差 */

       {

              int sum = 0, signal = 0;

              char cj2[] = sj2.substring(0, sj2.lastIndexOf("1")).toCharArray();

              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("1"));

              String temp = calculate(sj1, sj2);//[x]补+[-y]补,temp为两数相差多少

              if (temp.startsWith("00"))/* 第二个数阶码小,则差为正数 */

              {

                     signal = 1;

                     temp = temp.substring(2);

              }

              if (temp.startsWith("11"))/* 第一个数阶码小,则差为负数 */

              {

                     signal = -1;

                     String str = temp.substring(2, temp.lastIndexOf("1"));

                     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("1"));

              }

             

              int n[] = new int[temp.length()];

              for (int i = 0; i < n.length; i++) {//将temp字符串写到n数组中

                     if (temp.substring(i, i + 1).equals("0"))

                            n[i] = 0;

                     else

                            n[i] = 1;

              }

              for (int i = n.length - 2; i >= 0; i--) {//将n数组写成十进制数

                     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("00")) {

                     s = "+" + s.substring(2);

              }

              else if (s.startsWith("11")) {

                     String temp = s.substring(2, s.lastIndexOf("1"));

                     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("1"));

              }

              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("001") || st1.startsWith("110")))/* 规格化 */

              {

                     if (st1.startsWith("000") || st1.startsWith("111"))/* 尾数左移 */

                     {

                            st1 = st1.substring(1) + "0";

                            count++;

                     }

                     else if (st1.startsWith("01") || st1.startsWith("10"))/* 尾数右移 */

                     {

                            st1 = st1.substring(0, 1) + st1.substring(0, st1.length() - 1);

                            count--;

                     }

              }

              while (count > 0)/* 阶码循环减一 */

              {

                     String temp = sj1.replaceAll("0", "1");

                     temp = temp.substring(1) + "1";

                     sj1 = calculate(sj1, temp);

                     count--;

              }

              while (count < 0) {

                     String temp = sj1.replaceAll("1", "0");

                     temp = temp.substring(1) + "1";

                     sj1 = calculate(sj1, temp);

                     count++;

              }

              sj1 = bumaHuanyuan(sj1);

              st1 = st1.substring(0, len + 3);

              if (st1.endsWith("1"))/* 尾数舍入处理 */

              {

                     String temp = st1.replaceAll("1", "0");

                     temp = temp.substring(1) + "1";

                     st1 = calculate(st1, temp);

              }

              else if (st1.endsWith("0")) {

              }

              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) + "0." + s[1].substring(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("1"));                         

                            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("1"));

                            String s[] = myAdd(sj1, sj2, st1, st2);

                            data6.setText(s[0]);

                            data7.setText(s[1].substring(0, 1) + "0." + s[1].substring(1));

                     }

              }

              else if (e.getSource() == button4) {//返回按钮

                     new Newclass(null);

                     setVisible(false);

              }

       }

}

更多相关推荐:
计算机组成原理课程设计报告

西安科技大学课程设计报告课程名称计算机组成原理课题名称复杂模型计算机的设计专业计算机科学与技术班级计科1001班姓名李用维念文洪吴江龙李斌指导教师薛萍时间20xx年1月5号1目录一复杂模型计算机的设计任务书1二...

计算机组成原理课程设计的实验报告

长治学院课程设计报告课程名称计算机组成原理课程设计设计题目设计一台性能简单的计算机系别计算机系专业计科1101班组别第三组学生姓名学号起止日期20xx年7月4日20xx年7月10日指导教师张剑妹目录一课程设计的...

计算机组成原理课程设计报告

课程设计题目教学院专业班级姓名指导教师硬件加减法器的设计计算机学院计算机科学与技术年月日1课程设计任务书20xx20xx学年第1学期学生姓名专业班级指导教师工作部门一课程设计题目硬件加减法器的设计二课程设计内容...

计算机组成原理课程设计报告书

课程设计说明书题目设计指令系统院系计算机科学与工程学院专业班级计算机1003班学号学生姓名指导教师刘向举年1月10日20xx安徽理工大学课程设计论文任务书20xx年12月17日安徽理工大学课程设计论文安徽理工大...

计算机组成原理课设报告

沈阳工程学院计算机组成原理课程设计设计题目基本模型机的设计与实现第六组系别班级学生姓名学号指导教师职称起止日期20xx年6月25日起至20xx年6月29日止沈阳工程学院课程设计任务书课程设计题目系别班级学生姓名...

计算机组成原理课程设计报告

课程设计报告课程设计名称计算机组成原理系别三系学生姓名班级学号成绩指导教师开课时间一设计题目计算机组成原理课程设计简单模型机的微程序设计二主要内容1通过使用作者开发的微程序分析和设计仿真软件熟悉本文介绍的为基本...

计算机组成原理课程设计总结报告

大庆师范学院计算机组成原理课程设计总结报告设计题目基本模型机的模拟设计与实现子题目外部中断控制流水灯蜂鸣器学生姓名院别专业班级学号指导教师20xx年7月5日大庆师范学院课程设计任务书题目基本模型机的模拟设计与实...

计算机组成原理课程设计实验报告抑或运算 - 20xx

计算机组成原理实验报告专业计算机科学与技术班级19xx42学号姓名20xx年12月创新实验一课程设计目的1在试验六和实验八的基础上设计新的功能并设计新的微代码表实现此功能2熟悉掌握24位微代码的含义二实验设备E...

计算机组成原理课程设计任务书11-周建国

华中科技大学计算机学院计算机组成原理课程设计任务书计算机组成原理是计算机专业的核心基础课课程设计是学完该课程并进行了多个单元实验后综合利用所学的理论知识并结合在单元实验中所积累的计算机部件设计和调试方法设计出一...

浙江理工大学计算机组成原理课程设计报告

计算机组成原理课程设计报告20xx20xx第二学期第19周指导教师许建龙张芳班级12计科2班姓名学号计算机组成原理大型实验任务书计算机12级123班和实验班一实验目的深入了解计算机各种指令的执行过程以及控制器的...

计算机组成原理课程设计报告

计算机组成原理课程设计课程设计报告课程设计名称计算机组成原理系学生姓名班级学号成绩指导教师开课时间125学期计算机组成原理课程设计一设计题目计算机组成原理课程设计简单模型机的微程序设计二主要内容通过课程设计更清...

计算机组成原理设计实验报告

计算机组成原理设计实验报告学院:计算机科学与工程学院专业:网络工程班级:学号:姓名:评分:20##年5月31日试验一验证74LS181运算和逻辑功能实验目的(1)掌握算术逻辑单元(ALU)的工作原理;(2)熟悉…

计算机组成原理课程设计报告(27篇)