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

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

目录
1 课程设计目的 0
2 课程设计内容与要求 0
3 功能模块详细设计 0
4 设计小结 (8)
参考文献 (8)
源程序 (9)
1 课程设计目的
本课程设计是在学完本课程教学大纲规定的全部内容、完成所有实践环节的基础上,旨在深化学生学习的计算机组成原理课程基本知识,进一步领会计算机组成原理的一些算法,并进行具体实现,提高分析问题、解决问题的综合应用能力。

2 课程设计内容与要求
计算机组成原理算法实现(五)
能够实现机器数的真值还原(定点整数)、定点整数的单符号位补码加减运算、定点整数的原码一位乘法运算和浮点数的加减运算。

3 功能模块详细设计
(1)系统进入(主)窗体的设计:菜单需要在输入口令正确后方可激活使用。

口令输入错误时要给出重新输入口令的提示,三次口令输入错误应该禁止使用。

if (text1.getText().equals(s)) {
i = 1;
JOptionPane.showMessageDialog(this, "口令正确,请选择菜单栏的操作",
"正确", RMATION_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';
else if (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 = stIndexOf("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';
}
}
}
在上面的窗体中按“输入”按扭时,将输入焦点设置为最上面的一个文本框上。

输入一个定点整数形式的机器数(如101010或011010)后,按“原真值”、“反真值”、“补真值”或“移真值”按扭中的任一个后,将在第二个文本框中显示对应的真值。

选择“返回”按扭时回到主窗体。

(3)选择主窗体中“定点整数单符号位补码加减法”时进入下图所示的窗体:
if (i1.length != i2.length)
data1.setText("请输入数值位长度相等的数字!");
else {
out = jia(i1, i2);
output = new String(out);
data2.setText(output);
}
i f (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);
}
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);
}
在上面的窗体中按“输入”按钮时,将输入焦点设置为最上面的两个文本框。

输入一位符号位的被乘数和一位符号位的乘数,按“乘法”按钮,在第三个文本框中显示对应的一位符号位的乘法结果。

选择“返回”按钮时回到主窗口。

(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, stIndexOf("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, "口令正确,请选择菜单栏的操作",
"正确", RMATION_MESSAGE);
text1.setEnabled(false);
text1.setVisible(false);
a22.setVisible(true);
a2.setVisible(false);
} else {
m++;
JOptionPane.showMessageDialog(this, "您输入的口令不正确", "警告",
JOptionPane.W ARNING_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.createV erticalBox();
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 = stIndexOf("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 = stIndexOf("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));。

相关文档
最新文档