計算機組成原理算法實現(xiàn)-D計算機121-夏凌-副本_第1頁
計算機組成原理算法實現(xiàn)-D計算機121-夏凌-副本_第2頁
計算機組成原理算法實現(xiàn)-D計算機121-夏凌-副本_第3頁
計算機組成原理算法實現(xiàn)-D計算機121-夏凌-副本_第4頁
計算機組成原理算法實現(xiàn)-D計算機121-夏凌-副本_第5頁
已閱讀5頁,還剩29頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、 PAGE 34組成原理課程設(shè)計報告專業(yè)計算機科學(xué)與技術(shù)學(xué)生姓名夏凌班級D計算機121學(xué)號1220704132指導(dǎo)教師花小朋完成日期2015年1月21日計算機組成原理算法實現(xiàn)(三)課程設(shè)計的目的本課程設(shè)計是在學(xué)完本課程教學(xué)大綱規(guī)定的全部內(nèi)容、完成所有實踐環(huán)節(jié)的基礎(chǔ)上,旨在深化學(xué)生學(xué)習(xí)的計算機組成原理課程基本知識,進(jìn)一步領(lǐng)會計算機組成原理的一些算法,并進(jìn)行具體實現(xiàn),提高分析問題、解決問題的綜合應(yīng)用能力。課程設(shè)計內(nèi)容題目:計算機組成原理算法實現(xiàn)(三)功能:能夠?qū)崿F(xiàn)邏輯運算(邏輯非、邏輯加、邏輯乘、邏輯異)、定點整數(shù)的單符號位補碼加減運算、定點整數(shù)的原碼一位乘法運算和浮點數(shù)的加減運算。功能設(shè)計與實現(xiàn)

2、主窗體登陸窗口流程圖系統(tǒng)進(jìn)入(主)窗體的設(shè)計:菜單需要在輸入口令正確后方可激活使用。如下圖:口令輸入錯誤時給出重新輸入口令的提示,如下圖:三次口令輸入錯誤應(yīng)該禁止使用,如下圖:輸入密碼正確之后,如下圖:二進(jìn)制數(shù)邏輯運算窗體二進(jìn)制數(shù)邏輯運算流程圖在上面的窗體中按“輸入”按扭時,將輸入焦點設(shè)置為最上面的一個文本框上。依次輸入兩個(或一個)二進(jìn)制數(shù)后,按“邏輯非”、“邏輯加”、“邏輯乘”或“邏輯異”按扭中的任一個后,將在第三個文本框中顯示對應(yīng)操作的結(jié)果。選擇“返回”按扭時回到主窗體。選擇主窗體中“邏輯運算”時進(jìn)入下圖所示的窗體:定點整數(shù)單符號位補碼加減法運算窗體定點整數(shù)單符號位補碼加減法流程圖選擇主

3、窗體中“定點整數(shù)單符號位補碼加減”時進(jìn)入下圖所示的窗體:定點加、減法運算:結(jié)論:任何兩數(shù)的補碼只和等于兩數(shù)只和的補碼補碼加法 基本公式: x補 + y補 = x+y補 補碼減法 基本公式: x補 - y補x補 + -y補 當(dāng)負(fù)數(shù)用補碼表示后,符號位作為數(shù)據(jù)的一部分一起參加運算,運算器不用考慮參加加法運算的操作數(shù)的正負(fù)以及結(jié)果的正負(fù),任意數(shù)的加法就可以化作正數(shù)加法來作。溢出:在定點數(shù)機器中,數(shù)的大小超出了定點數(shù)能表示的范圍,叫溢出。(1)在定點小數(shù)機中數(shù)的表示范圍是-1x1,如果運算過程中出現(xiàn)了大于1或者小于1的情況。(2)在定點整數(shù)機(8位)中數(shù)的表示范圍是-128x127,如果運算過程中出現(xiàn)

4、了大于127或者小于128的情況。雙符號位法:將符號位擴展為2位,具體說就是對于正數(shù)兩個符號位是“00”,對于負(fù)數(shù)兩個符號位是“11”。兩個符號位都看作數(shù)碼一樣參加運算。兩個數(shù)相加后,其結(jié)果的符號位出現(xiàn)“01”或“10”兩種組合時,表示發(fā)生溢出。符號位“01”,上溢出符號位“10”,下溢出符號位“00”或者“11”,未溢出 從補求補的法則是:對補“包括符號位求反且最末位加1”,即可得到補。4定點整數(shù)單原碼一位乘法運算窗體定點整數(shù)的原碼乘法窗體流程圖進(jìn)行定點整數(shù)單符號位補碼加減法、定點整數(shù)原碼乘法、浮點加減法的實現(xiàn)時都是在主窗體選擇對應(yīng)的菜單項后進(jìn)入對應(yīng)窗體再進(jìn)行具體操作。操作時首先選擇“輸入”

5、按扭輸入?yún)⑴c運算的數(shù)據(jù),然后再選操作按扭。選擇主窗體中“定點整數(shù)單原碼一位乘法”時進(jìn)入下圖所示的窗體:定點乘法運算 :在定點計算機中,兩個原碼表示的數(shù)相乘的運算規(guī)則是:乘積的符號位由兩數(shù)的符號位按異或運算得到,而乘積的數(shù)值部分則是兩個正數(shù)相乘之積。設(shè)n位被乘數(shù)和乘數(shù)用定點小數(shù)表示(定點整數(shù)也同樣適用)被乘數(shù) 原f .n110 乘數(shù) 原f .n110 則乘積 原(ff)(0.n110)(0.n110) 式中,f為被乘數(shù)符號,f為乘數(shù)符號。 機器算法: 機器中一種方法是多次采用”加法移位“的方法來完成,稱為串行乘法器,它的硬件結(jié)構(gòu)簡單,但是速度慢,目前廣泛使用的是流水式陣列乘法器,稱為并行乘法器。

6、無符號數(shù)m乘以n會產(chǎn)生m*n個位積,出現(xiàn)m+n個列和,并行乘法器的關(guān)鍵是快速產(chǎn)生m*n個位積,對位積進(jìn)行相加運算產(chǎn)生m+n個列和。第一步:位積的產(chǎn)生觀察乘法運算:0*00,0*10,1*00,1*11 相當(dāng)于:ab所以m*n個位積可以由m*n個與門并行產(chǎn)生。第二步:列和的產(chǎn)生:利用全加器5浮點數(shù)的加減法運算窗體進(jìn)行定點整數(shù)單符號位補碼加減法、定點整數(shù)原碼乘法、浮點加減法的實現(xiàn)時都是在主窗體選擇對應(yīng)的菜單項后進(jìn)入對應(yīng)窗體再進(jìn)行具體操作。操作時首先選擇“輸入”按扭輸入?yún)⑴c運算的數(shù)據(jù),然后再選操作按扭。選擇主窗體中“浮點加減法”時進(jìn)入下圖所示的窗體: 浮點數(shù)的加減法運算:浮點數(shù)的加減法運算分為六個步

7、驟:(1)0操作數(shù)檢查 浮點加減運算過程比定點運算過程復(fù)雜。如果判知兩個操作數(shù)或中有一個數(shù)為0,即可得知運算結(jié)果而沒有必要再進(jìn)行后續(xù)的一系列操作以節(jié)省運算時間。0操作數(shù)檢查步驟則用來完成這一功能。參加加法運算的數(shù)據(jù)都是非零,進(jìn)入下一步。(2)比較價碼大小并完成對階為了便于直觀理解,假設(shè)兩數(shù)均以補碼表示,階碼采用雙符號位,尾數(shù)采用單符號位。 兩浮點數(shù)進(jìn)行加減,首先要看兩數(shù)的階碼是否相同,若二數(shù)階碼不同,表示小數(shù)點位置沒有對齊,此時必須使二數(shù)階碼相同,這個過程叫作對階。對階操作規(guī)定使尾數(shù)右移,尾數(shù)右移后階碼作相應(yīng)增加,因此對階時,總是使小階向大階看齊。(3)尾數(shù)進(jìn)行加減運算對階結(jié)束后,即可進(jìn)行尾數(shù)

8、的求和運算。不論加法運算還是減法運算,都按加法進(jìn)行操作(減法利用補碼減法轉(zhuǎn)換成補碼的加法來做),其方法與定點加減法運算完全一樣。 (4)結(jié)果規(guī)格化 a. 采用雙符號位表示尾數(shù)時,如果兩符號位為01或10時,應(yīng)將結(jié)果尾數(shù)右移一位,階碼加1(叫“右規(guī)”)。 b. 如果尾數(shù)最高數(shù)值位與符號位相同,應(yīng)將尾數(shù)左移,階碼減,直至數(shù)值位最高位與符號位相反(叫“左規(guī)”)。(5)舍入運算 在對階向右規(guī)格化,尾數(shù)向右移位,這樣被右移的尾數(shù)的低位部分會被丟掉,從而造成一定誤差,要進(jìn)行舍入處理。簡單的舍入方法有兩種:一種是“0舍1入”法,即如果右移時被丟掉數(shù)位的最高位為0則舍去,為1則將尾數(shù)的末位做加加1運算。另一種

9、是“恒置1”法,即只要數(shù)位被移掉,就在尾數(shù)的末尾恒置1。(6)判斷結(jié)果是否溢出 階碼為00 011,階碼的符號位為00,根據(jù)補碼的雙符號檢測溢出的方法確定階碼不溢出。4設(shè)計小結(jié)經(jīng)過一周計算機組成原理課程設(shè)計,使得我對于邏輯運算的理解進(jìn)一步加深。對計算機組成原理中的知識有了全新的認(rèn)識。之前的學(xué)習(xí)僅僅是理論上的,這次的課程設(shè)計讓我充分理解了之前的所學(xué)內(nèi)容,更深層次地理解了組成原理的內(nèi)容,為以后的運用打下堅實的基礎(chǔ)。本次課程設(shè)計主要用的是NetBeans用JAVA語言編寫,通過對這些工具的使用,書本上的知識遠(yuǎn)遠(yuǎn)不夠,上機實踐以后的進(jìn)一步深層次的理解,使得我么所學(xué)的知識更加牢固。一般的邏輯運算,自己通

10、過通過計算就能得出結(jié)果,而通過計算機加以實現(xiàn)卻沒那么簡單,通過實踐發(fā)現(xiàn)其中集合了很多我們平時忽略的內(nèi)容,讓我溫習(xí)了一次書本知識點,對于自己不會的內(nèi)容,我也能充分地利用網(wǎng)絡(luò)的方便快捷,給自己節(jié)約了不少時間。在此實訓(xùn)上自主能力上得到了提高,真正地做到了學(xué)以致用,更學(xué)到了很多做人的道理,對我來說受益非淺。在實踐能力上也得到了提高,真正地做到了學(xué)以致用,更學(xué)到了很多做人的道理,對我來說受益非淺。除此以外,我還學(xué)會了如何更好地與別人溝通,如何更好地去陳述自己的觀點,如何說服別人認(rèn)同自己的觀點。第一次親身感受到理論與實際的相結(jié)合,讓我大開眼界。也是對以前所學(xué)知識的一個初審吧!這次實訓(xùn)對于我以后學(xué)習(xí)、找工作

11、也真是受益菲淺,在短短的幾天中相信這些寶貴的經(jīng)驗會成為我今后成功的重要的基石。本次計算機組成原理課程設(shè)計,在老師指導(dǎo)和同學(xué)們的幫助下,我深化了解了計算機組成原理課程基本知識,進(jìn)一步領(lǐng)會計算機組成原理的一些算法,并進(jìn)行具體實現(xiàn),提高分析問題、解決問題的綜合應(yīng)用能力。 附件Main類:public class Main public static void main(String args) new Newclass(null);Newclass類:import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;pu

12、blic 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(定點整數(shù)單符號位補碼加減運算);

13、m3 = new Button(定點整數(shù)的原碼一位乘法);m4 = new Button(浮點的加減運算); m5=new Button(幫助);m1.addActionListener(this);m2.addActionListener(this);m3.addActionListener(this);m4.addActionListener(this);m5.addActionListener(this); m1.setBounds(5,30,90,25);m2.setBounds(100,30,190,25);m3.setBounds(300,30,180,25);m4.setBoun

14、ds(490,30,120,25);m5.setBounds(620,30,70,25);add(m1);add(m2);add(m3);add(m4); add(m5);a1 = new Label(計算機組成原理算法實現(xiàn)(三), Label.CENTER);a2 = new Label(輸入口令:);a22 = new Label(密碼正確, Label.CENTER);a1.setBounds(200, 100, 300, 40);a1.setBackground(Color.green);a1.setFont(f);a2.setBounds(200, 200, 60, 25);a2.s

15、etBackground(Color.orange);a22.setBounds(250, 200, 200, 25);a22.setForeground(Color.white);a22.setBackground(Color.blue);a22.setVisible(false);text1 = new TextField(18);text1.setEchoChar(*);text1.setBounds(300, 200, 200, 25);button1 = new Button(確認(rèn));button1.setBounds(300, 250, 70, 30);button1.addAct

16、ionListener(this);add(a1);add(a2);add(a22);add(text1);add(button1);setBackground(Color.white);setBounds(200, 200, 710, 400);setVisible(true);validate();addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););public static void main(String args) new Newclass(計算

17、機組成原理);public void actionPerformed(ActionEvent e) String s = new String(2);if (e.getSource() = button1) while (i = 0 & 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)

18、; else if (e.getSource() = m2) Form3 f1 = new Form3();f1.setTitle(定點整數(shù)單符號位補碼加減);f1.setVisible(true); else if (e.getSource() = m3) Form4 f2 = new Form4();f2.setTitle(定點整數(shù)補乘法);f2.setVisible(true); else if (e.getSource() = m4) Form5 f5 = new Form5();f5.setVisible(true); else if(e.getSource()=m5)Help h1

19、=new Help(幫助);h1.setVisible(true);Form2類:import java.awt.*;import javax.swing.JOptionPane;import javax.swing.*;import java.awt.event.*;public class Form2 extends JFrame implements ActionListenerint me=0;JLabel b1,b2,b3,b4;Font f;JTextField t1,t2,t3; JButton button1,button2,button3,button4,button5,bu

20、tton6;Form2(String s) super(s);setLayout(null);f=new Font(楷體,Font.BOLD,20);b1=new JLabel( 邏輯運算 ,JLabel.CENTER);b2=new JLabel(請輸入第一個數(shù):);b3=new JLabel(請輸入第二個數(shù):);b4=new JLabel(結(jié)果:);b1.setBounds(180,30,250,40);b1.setFont(f);b1.setBackground(Color.white);b2.setBounds(100,120,120,25);b3.setBounds(100,150,

21、120,25);b2.setBackground(Color.white);b3.setBackground(Color.white);b4.setBounds(100,180,120,25);t1=new JTextField(15);t2=new JTextField(15);t3=new JTextField(15);t1.setBounds(250,120,200,25);t2.setBounds(250,150,200,25);t3.setBounds(250,180,200,25);button1=new JButton(輸 入);button2=new JButton(邏輯非);

22、button3=new JButton(邏輯加);button4=new JButton(邏輯乘);button5=new JButton(邏輯異);button6=new JButton(返 回);button1.setBounds(60,300,80,25);button2.setBounds(140,300,80,25);button3.setBounds(220,300,80,25);button4.setBounds(300,300,80,25);button5.setBounds(380,300,80,25);button6.setBounds(460,300,80,25);set

23、Bounds(100,100,600,400);setBackground(Color.white); add(b1);add(b2);add(b3);add(b4);add(t1);add(t2);add(t3);add(button1);add(button2);add(button3);add(button4); add(button5);add(button6);button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addAction

24、Listener(this);button5.addActionListener(this);button6.addActionListener(this);validate();addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) setVisible(false); );public void actionPerformed(ActionEvent e)if(e.getSource()=button1) me=1;t1.requestFocusInWindow(); if(e.getSo

25、urce()=button2) b4.setText(邏輯非:); String sg=t1.getText();int index=sg.indexOf(+);int l=sg.length(); StringBuffer s=new StringBuffer(sg);for(int i=0;il;i+)if (s.charAt(i)=0)s.setCharAt(i,1);elses.setCharAt(i,0);String ss=s.substring(index+1);t3.setText(ss);else if(e.getSource()=button3)b4.setText(邏輯加

26、:);String sg=t1.getText();String sg2=t2.getText();if(sg.length()!=sg2.length()JOptionPane.showMessageDialog(this, 您輸入的兩個參數(shù)長度不一樣,警告對話框,JOptionPane.WARNING_MESSAGE);else int index=sg.indexOf(+);StringBuffer s=new StringBuffer(sg);StringBuffer s2=new StringBuffer(sg2);for(int i=0;isg.length();i+) if(s.

27、charAt(i)=1|s2.charAt(i)=1)s.setCharAt(i,1);elses.setCharAt(i,0); String ss=s.substring(index+1); t3.setText(ss); else if(e.getSource()=button4)b4.setText(該數(shù)邏輯乘為:);String sg=t1.getText();String sg2=t2.getText();if(sg.length()!=sg2.length()JOptionPane.showMessageDialog(this, 您輸入的兩個參數(shù)長度不一樣,警告對話框,JOpti

28、onPane.WARNING_MESSAGE);else int index=sg.indexOf(+);StringBuffer s=new StringBuffer(sg);StringBuffer s2=new StringBuffer(sg2);for(int i=0;isg.length();i+) if(s.charAt(i)=1&s2.charAt(i)=1)s.setCharAt(i,1);elses.setCharAt(i,0); String ss=s.substring(index+1); t3.setText(ss); else if(e.getSource()=but

29、ton5)b4.setText(該數(shù)的邏輯異為:);String sg=t1.getText();String sg2=t2.getText();if(sg.length()!=sg2.length()JOptionPane.showMessageDialog(this, 您輸入的兩個參數(shù)長度不一樣,警告對話框,JOptionPane.WARNING_MESSAGE);else int index=sg.indexOf(+);StringBuffer s=new StringBuffer(sg);StringBuffer s2=new StringBuffer(sg2);for(int i=0

30、;isg.length();i+) if(s.charAt(i)=s2.charAt(i)s.setCharAt(i,0);elses.setCharAt(i,1); String ss=s.substring(index+1); t3.setText(ss); else if(e.getSource()=button6)this.dispose();Form3類:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Form3 extends Frame implements ActionLis

31、tener 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(form3);setBounds(200, 300, 500, 300);button1 = new Button(輸入);button2

32、= new Button(加法);button3 = new Button(減法);button4 = new Button(返回);box1 = Box.createHorizontalBox();name = new Label(定點整數(shù)單符號位補碼加減法, Label.CENTER);box1.add(name);box2 = Box.createHorizontalBox();input = new Label(請輸入第一個數(shù)(輸入為補碼):);data = new TextField(10);box2.add(input);box2.add(data);box3 = Box.crea

33、teHorizontalBox();input1 = new Label(請輸入第二個數(shù)(輸入為補碼):);data1 = new TextField(10);box3.add(input1);box3.add(data1);box4 = Box.createHorizontalBox();input2 = new Label(加法結(jié)果為(顯示為補碼):);data2 = new TextField(10);box4.add(input2);box4.add(data2);box5 = Box.createHorizontalBox();input3 = new Label(減法結(jié)果為(顯示為

34、補碼):);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.crea

35、teVerticalStrut(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.addActio

36、nListener(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 = in0;if (c = 1) char carry = 0;int i;char temp = in;for (i = 1; i =

37、0; i-) /* 各位進(jìn)位情況 */if (tempi = 0 & carry = 0) tempi = 0;carry = 0; else if (tempi = 0 & carry = 1) tempi = 1;carry = 0; else if (tempi = 1 & carry = 0) tempi = 1;carry = 0; else if (tempi = 1 & carry = 1) tempi = 0;carry = 1;in = temp;return in;char jia(char in1, char in2) char out;char carry = new

38、charin1.length + 1; /* carry用來存放每位的進(jìn)位 */int i = 0;out = in1;carrycarry.length - 1 = 0;/* carry最后一位初值為0 ,carry長度比輸入大一 */for (i = in1.length - 1; i = 0; i-) if (in1i = 0 & in2i = 0 & carryi + 1 = 0) outi = 0;carryi = 0; else if (in1i = 0 & in2i = 1 & carryi + 1 = 0) outi = 1;carryi = 0; else if (in1i

39、= 1 & in2i = 0 & carryi + 1 = 0) outi = 1;carryi = 0; else if (in1i = 1 & in2i = 1 & carryi + 1 = 0) outi = 0;carryi = 1; else if (in1i = 0 & in2i = 0 & carryi + 1 = 1) outi = 1;carryi = 0; else if (in1i = 0 & in2i = 1 & carryi + 1 = 1) outi = 0;carryi = 1; else if (in1i = 1 & in2i = 0 & carryi + 1

40、= 1) outi = 0;carryi = 1; else if (in1i = 1 & in2i = 1 & carryi + 1 = 1) outi = 1;carryi = 1;if (carry0 != carry1) /* 當(dāng)最高數(shù)值位進(jìn)位與符號位進(jìn)位不同時表溢出 */String temp = new String(溢出);out = temp.toCharArray();return out;char jian(char in1, char in2) if (in20 = 1) /* 將第二個數(shù)轉(zhuǎn)為其相反數(shù)的補碼 */in2 = bu(in2);in20 = 0; else i

41、f (in20 = 0) in20 = 1;in2 = bu(in2);char o = jia(in1, in2); /* 將減法轉(zhuǎn)換為加法 */return o;public void actionPerformed(ActionEvent e) if (e.getSource() = button1) /* 輸入將光標(biāo)定位在最上面文本框 */data.setText(null);data1.setText(null);data2.setText(null);data3.setText(null);data.requestFocusInWindow(); else if (e.getSou

42、rce() = 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(請輸入數(shù)值位長度相等的數(shù)字!);else out = jia(i1, i2);output = new String(out);data2.setText(output); else if (e.getSource() = button

43、3) /* 減法 */first = data.getText();next = data1.getText();char out;char i1, i2;i1 = first.toCharArray();i2 = next.toCharArray();if (i1.length != i2.length)data1.setText(請輸入數(shù)值位長度相等的數(shù)字!);else out = jian(i1, i2);output = new String(out);data3.setText(output); else if (e.getSource() = button4)/* 返回 */new

44、 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

45、;public Form4() setTitle(form4);setBounds(200, 300, 500, 300);button1 = new Button(輸入);button2 = new Button(乘法);button3 = new Button(返回);box1 = Box.createHorizontalBox();name = new Label(定點整數(shù)的原碼乘法, Label.CENTER);box1.add(name);box2 = Box.createHorizontalBox();input = new Label(請輸入被乘數(shù):);data = new Te

46、xtField(13);box2.add(input);box2.add(data);box3 = Box.createHorizontalBox();input1 = new Label(請輸入乘數(shù):);data1 = new TextField(11);box3.add(input1);box3.add(data1);box4 = Box.createHorizontalBox();input2 = new Label(乘法結(jié)果為:);data2 = new TextField(11);box4.add(input2);box4.add(data2);box5 = Box.createHo

47、rizontalBox();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);basebo

48、x.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

49、););validate();char jia(char in1, char in2) char out;char carry = new charin1.length + 1; int i = 0;out = in1;carrycarry.length - 1 = 0;for (i = in1.length - 1; i = 0; i-) if (in1i = 0 & in2i = 0 & carryi + 1 = 0) outi = 0;carryi = 0; else if (in1i = 0 & in2i = 1 & carryi + 1 = 0) outi = 1;carryi =

50、0; else if (in1i = 1 & in2i = 0 & carryi + 1 = 0) outi = 1;carryi = 0; else if (in1i = 1 & in2i = 1 & carryi + 1 = 0) outi = 0;carryi = 1; else if (in1i = 0 & in2i = 0 & carryi + 1 = 1) outi = 1;carryi = 0; else if (in1i = 0 & in2i = 1 & carryi + 1 = 1) outi = 0;carryi = 1; else if (in1i = 1 & in2i

51、= 0 & carryi + 1 = 1) outi = 0;carryi = 1; else if (in1i = 1 & in2i = 1 & carryi + 1 = 1) outi = 1;carryi = 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.getSourc

52、e() = 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(請輸入數(shù)值位長度相等的數(shù)字!);else if (in10 = 0 & in20 = 0)| (in10 = 1

53、& in20 = 1)signal = +;if (in10 = 0 & in20 = 1)| (in10 = 1 & in20 = 0)signal = -;int count = beichengshu.length() - 1;char c = new charcount + 1;for (int i = 0; i 0; i-) if (in2i = 0) stri = new String(c) + new String(c); else if (in2i = 1) stri = beichengshu + new String(c);for (int i = 1; i = beich

54、engshu.length(); i+)int k;char buwei = new chari;for (k = 0; k i; k+)buweik = 0;stri = new String(buwei)+ stri.substring(0, stri.length() - i);temp = new String(c) + new String(c);char result = temp.toCharArray();for (int i = 1; i = 0; i-) if (a1i = 0 & a2i = 0 & ac = 0) a2i = 0;ac = 0; else if (a1i

55、 = 0 & a2i = 0 & ac = 1) a2i = 1;ac = 0; else if (a1i = 0 & a2i = 1 & ac = 0) a2i = 1;ac = 0; else if (a1i = 1 & a2i = 0 & ac = 0) a2i = 1;ac = 0; else if (a1i = 0 & a2i = 1 & ac = 1) a2i = 0;ac = 1; else if (a1i = 1 & a2i = 1 & ac = 0) a2i = 0;ac = 1; else if (a1i = 1 & a2i = 0 & ac = 1) a2i = 0;ac

56、 = 1; else if (a1i = 1 & a2i = 1 & ac = 1) a2i = 1;ac = 1;return new String(a2);public String jiemaQiubu(String s)/* 階碼變形補碼 */if (s.startsWith(0) | s.startsWith(1) s = 00 + s;if (s.startsWith(+) s = 00 + s.substring(1);if (s.startsWith(-) String temp = s.substring(1, s.lastIndexOf(1);char c = temp.t

57、oCharArray();for (int i = 0; i c.length; i+) if (ci = 0)ci = 1;elseci = 0;temp = new String(c);s = 11 + temp + s.substring(s.lastIndexOf(1);return s;public String weishuQiubu(String s)/* 尾數(shù)求變形補碼(缺省小數(shù)點,多加長度個0) */if (s.startsWith(0) String str = s.substring(2).replaceAll(1, 0);s = 00 + s.substring(2)

58、+ str;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 (ci = 0)ci = 1;else

59、ci = 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 (cj2i = 0)cj2i = 1;elsecj2i = 0;sj2

60、 = new String(cj2) + sj2.substring(sj2.lastIndexOf(1);String temp = calculate(sj1, sj2);if (temp.startsWith(00)/* 第二個數(shù)階碼小 */signal = 1;temp = temp.substring(2);if (temp.startsWith(11)/* 第一個數(shù)階碼小 */signal = -1;String str = temp.substring(2, temp.lastIndexOf(1);char c = str.toCharArray();for (int i = 0

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論