




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PAGE2PAGE1课程设计说明书计算机组成原理算法实现(五)专业计算机科学与技术学生姓名班级学号指导教师完成日期1课程设计目的本课程设计是在学完本课程教学大纲规定的全部内容、完成所有实践环节的基础上,旨在深化学生学习的计算机组成原理课程基本知识,进一步领会计算机组成原理的一些算法,并进行具体实现,提高分析问题、解决问题的综合应用能力。2课程设计内容与要求计算机组成原理算法实现(五)能够实现机器数的真值还原(定点整数)、定点整数的单符号位补码加减运算、定点整数的原码一位乘法运算和浮点数的加减运算。3功能模块详细设计系统进入(主)窗体的设计:菜单需要在输入口令正确后方可激活使用。口令输入错误时要给出重新输入口令的提示,三次口令输入错误应该禁止使用。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); Strings=text1.getText(); booleanboo=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(); booleanboo=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); chara[]=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(); booleanboo=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); chara[]=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=newString(out); data2.setText(output); } if(i1.length!=i2.length) data1.setText("请输入数值位长度相等的数字!"); else{ out=jian(i1,i2); output=newString(out); data3.setText(output); }在上面的窗体中按“输入”按钮时,将输入焦点设置为最上面的两个文本框。输入第一个、第二个数的补码,按“加法”、“减法”按钮中的任一个,在第三、第四文本框中显示对应的补码结果。选择“返回”按钮时回到主窗口。(4)选择主窗体中“定点整数原码乘法”时进入下图所示的窗体:for(inti=beichengshu.length();i>0;i--)//按照乘数各个位判别 { if(in2[i]=='0'){ str[i]=newString(c)+newString(c); } elseif(in2[i]=='1'){ str[i]=beichengshu+newString(c); } } for(inti=1;i<=beichengshu.length();i++)//实现右移操作 { intk; char[]buwei=newchar[i]; for(k=0;k<i;k++) { buwei[k]='0'; } str[i]=newString(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{ Strings[]=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")); charc[]=temp.toCharArray(); for(inti=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.javapublicclassMainFrame{ publicstaticvoidmain(Stringargs[]){ newNewclass(null); }}Newclass.javaimportjava.awt.*;importjava.awt.event.*;importjavax.swing.JOptionPane;publicclassNewclassextendsFrameimplementsActionListener{ staticinti=0; staticintm=1; Labela1,a2,a22; TextFieldtext1; Buttonbutton1,m1,m2,m3,m4; Fontf; Newclass(Strings){ super(s); setLayout(null); f=newFont("楷体",Font.BOLD,20); m1=newButton("机器数的真值还原(定点整数)"); m2=newButton("定点整数单符号位补码加减运算"); m3=newButton("定点整数的原码乘法"); m4=newButton("浮点数的加减运算"); 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=newLabel("计算机组成原理算法实现(五)",Label.CENTER); a2=newLabel("输入口令(000):"); a22=newLabel("登陆成功",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=newTextField(18); text1.setEchoChar('*'); text1.setBounds(240,200,200,25); button1=newButton("确认"); 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(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); } publicstaticvoidmain(Stringargs[]){ newNewclass("计算机组成原理"); } publicvoidactionPerformed(ActionEvente){ Strings=newString("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){ Ljyss1=newLjys(); s1.setVisible(true); }elseif(e.getSource()==m2){ Zhengshujiajianf1=newZhengshujiajian(); f1.setTitle("定点整数单符号位补码加减运算"); f1.setVisible(true); }elseif(e.getSource()==m3){ Zhengshuchengfaf2=newZhengshuchengfa(); f2.setTitle("定点整数的原码乘法"); f2.setVisible(true); }elseif(e.getSource()==m4){ Fdysf5=newFdys(); f5.setVisible(true); } } }}Ljys.javaimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.SwingUtilities;importjava.awt.TextField;classLjysextendsFrameimplementsMouseListener{ Buttonbutton1; Buttonbutton2; Buttonbutton3; Buttonbutton4; Buttonbutton5; Buttonbutton6; Labellabel1; Labellabel2; Labellabel3; Labellabel4; Labellabel5; Labellabel6; Labellabel7; TextFieldtext1; TextFieldtext2; TextFieldtext3; Strings,s1,temp; intn1,i,j; chara[]; publicLjys(){ setTitle("机器数的真值还原(定点整数)"); button1=newButton("输入"); button2=newButton("原-->真值"); button3=newButton("反-->真值"); button4=newButton("补-->真值"); button5=newButton("移-->真值"); button6=newButton("返回"); label1=newLabel("机器数的真值还原(定点整数)"); Fontf1=newFont("",Font.TRUETYPE_FONT,25); label1.setFont(f1); label2=newLabel("请输入机器数:"); label3=newLabel("真值还原结果为:"); text1=newTextField(8); text2=newTextField(10); text2.setEditable(false); BoxbaseBox,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(newFlowLayout()); 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(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); setBounds(200,300,500,300); setVisible(true); } publicvoidmousePressed(MouseEvente){ } publicvoidmouseReleased(MouseEvente){ } publicvoidmouseEntered(MouseEvente){ } publicvoidmouseExited(MouseEvente){ } publicvoidmouseMoved(MouseEvente){ } publicvoidmouseDragged(MouseEvente){ } publicvoidmouseClicked(MouseEvente){ Componentcom=null; if(e.getSource()==button2){ com=(Component)e.getSource(); e=SwingUtilities.convertMouseEvent(com,e,this); Strings=text1.getText(); booleanboo=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); } } elseif(e.getSource()==button1){ text1.setText(""); } if(e.getSource()==button3){ s=text1.getText(); booleanboo=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); chara[]=s1.toCharArray(); for(i=0;i<=a.length-1;i++){ if(a[i]=='0') a[i]='1'; elseif(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(); booleanboo=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); chara[]=s1.toCharArray(); for(i=0;i<=a.length-1;i++){ if(a[i]=='0') a[i]='1'; elseif(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); Strings=text1.getText(); booleanboo=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); chara[]=s1.toCharArray(); for(i=0;i<=a.length-1;i++){ if(a[i]=='0') a[i]='1'; elseif(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); Newclasspp=newNewclass(null); setVisible(false); pp.setVisible(true); } }}Zhengshujiajian.javaimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassZhengshujiajianextendsFrameimplementsActionListener{ Boxbasebox,box1,box2,box3,box4,box5,box6; Buttonbutton1,button2,button3,button4; Labelname; Labelinput,input1,input2,input3; Stringfirst,next,output; TextFielddata,data1,data2,data3; publicZhengshujiajian(){ setTitle("定点整数单符号位补码加减运算"); setBounds(200,300,500,300); button1=newButton("输入"); button2=newButton("加法"); button3=newButton("减法"); button4=newButton("返回"); box1=Box.createHorizontalBox(); name=newLabel("定点整数单符号位补码加减法",Label.CENTER); box1.add(name); box2=Box.createHorizontalBox(); input=newLabel("请输入第一个数(输入为补码):"); data=newTextField(10); box2.add(input); box2.add(data); box3=Box.createHorizontalBox(); input1=newLabel("请输入第二个数(输入为补码):"); data1=newTextField(10); box3.add(input1); box3.add(data1); box4=Box.createHorizontalBox(); input2=newLabel("加法结果为(显示为补码):"); data2=newTextField(10); box4.add(input2); box4.add(data2); box5=Box.createHorizontalBox(); input3=newLabel("减法结果为(显示为补码):"); data3=newTextField(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(newFlowLayout()); add(basebox); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setVisible(true); addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ setVisible(false); } }); validate(); } char[]bu(char[]in){ charc=in[0]; if(c=='1'){ charcarry='0'; inti; chartemp[]=in; for(i=1;i<=temp.length-1;i++)/*除符号位外各位取反*/ { if(temp[i]=='0') temp[i]='1'; elseif(temp[i]=='1') temp[i]='0'; } if(temp[temp.length-1]=='0'){ temp[temp.length-1]='1'; carry='0'; }/*末位加一*/ elseif(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'; }elseif(temp[i]=='0'&&carry=='1'){ temp[i]='1'; carry='0'; }elseif(temp[i]=='1'&&carry=='0'){ temp[i]='1'; carry='0'; }elseif(temp[i]=='1'&&carry=='1'){ temp[i]='0'; carry='1'; } } in=temp; } returnin; } char[]jia(char[]in1,char[]in2){ char[]out; charcarry[]=newchar[in1.length+1];/*carry[]用来存放每位的进位*/ inti=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'; }elseif(in1[i]=='0'&&in2[i]=='1'&&carry[i+1]=='0'){ out[i]='1'; carry[i]='0'; }elseif(in1[i]=='1'&&in2[i]=='0'&&carry[i+1]=='0'){ out[i]='1'; carry[i]='0'; }elseif(in1[i]=='1'&&in2[i]=='1'&&carry[i+1]=='0'){ out[i]='0'; carry[i]='1'; }elseif(in1[i]=='0'&&in2[i]=='0'&&carry[i+1]=='1'){ out[i]='1'; carry[i]='0'; }elseif(in1[i]=='0'&&in2[i]=='1'&&carry[i+1]=='1'){ out[i]='0'; carry[i]='1'; }elseif(in1[i]=='1'&&in2[i]=='0'&&carry[i+1]=='1'){ out[i]='0'; carry[i]='1'; }elseif(in1[i]=='1'&&in2[i]=='1'&&carry[i+1]=='1'){ out[i]='1'; carry[i]='1'; } } if(carry[0]!=carry[1])/*当最高数值位进位与符号位进位不同时表溢出*/ { Stringtemp=newString("溢出"); out=temp.toCharArray(); } returnout; } char[]jian(char[]in1,char[]in2){ if(in2[0]=='1')/*将第二个数转为其相反数的补码*/ { in2=bu(in2); in2[0]='0'; } elseif(in2[0]=='0'){ in2[0]='1'; in2=bu(in2); } char[]o=jia(in1,in2);/*将减法转换为加法*/ returno; } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==button1)/*输入将光标定位在最上面文本框*/ { data.setText(null); data1.setText(null); data2.setText(null); data3.setText(null); data.requestFocusInWindow(); } elseif(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=newString(out); data2.setText(output); } } elseif(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=newString(out); data3.setText(output); } } elseif(e.getSource()==button4)/*返回*/ { newNewclass(null); setVisible(false); } }}Zhengshuchengfa.javaimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassZhengshuchengfaextendsFrameimplementsActionListener{ Boxbasebox,box1,box2,box3,box4,box5; Buttonbutton1,button2,button3; Labelname,input,input1,input2; TextFielddata,data1,data2; publicZhengshuchengfa(){ setTitle("定点整数的原码乘法"); setBounds(200,300,500,300); button1=newButton("输入"); button2=newButton("乘法"); button3=newButton("返回"); box1=Box.createHorizontalBox(); name=newLabel("定点整数的原码乘法",Label.CENTER); box1.add(name); box2=Box.createHorizontalBox(); input=newLabel("请输入被乘数(一位符号位):"); data=newTextField(10); box2.add(input); box2.add(data); box3=Box.createHorizontalBox(); input1=newLabel("请输入乘数(一位符号位):"); data1=newTextField(9); box3.add(input1); box3.add(data1); box4=Box.createHorizontalBox(); input2=newLabel("乘法结果为(一位符号位):"); data2=newTextField(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(newFlowLayout()); add(basebox); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); setVisible(true); addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ setVisible(false); } }); validate(); } char[]jia(char[]in1,char[]in2){ char[]out;//结果数组 charcarry[]=newchar[in1.length+1];//进位位 inti=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'; }elseif(in1[i]=='0'&&in2[i]=='1'&&carry[i+1]=='0'){ out[i]='1'; carry[i]='0'; }elseif(in1[i]=='1'&&in2[i]=='0'&&carry[i+1]=='0'){ out[i]='1'; carry[i]='0'; }elseif(in1[i]=='1'&&in2[i]=='1'&&carry[i+1]=='0'){ out[i]='0'; carry[i]='1'; }elseif(in1[i]=='0'&&in2[i]=='0'&&carry[i+1]=='1'){ out[i]='1'; carry[i]='0'; }elseif(in1[i]=='0'&&in2[i]=='1'&&carry[i+1]=='1'){ out[i]='0'; carry[i]='1'; }elseif(in1[i]=='1'&&in2[i]=='0'&&carry[i+1]=='1'){ out[i]='0'; carry[i]='1'; }elseif(in1[i]=='1'&&in2[i]=='1'&&carry[i+1]=='1'){ out[i]='1'; carry[i]='1'; } }//做加法运算 returnout; } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==button1)//输入开关 { data.setText(null); data1.setText(null); data2.setText(null); data.requestFocusInWindow(); } elseif(e.getSource()==button2){//做乘法 Strings1=data.getText(); Strings2=data1.getText(); Stringbeichengshu=s1.substring(1); Stringsignal="0"; Stringtemp=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"; }//判别结果符号 intcount=beichengshu.length()-1; charc[]=newchar[count+1]; for(inti=0;i<=count;i++) { c[i]='0'; } Stringstr[]=newString[beichengshu.length()+1]; str[0]=s1+newString(c); for(inti=beichengshu.length();i>0;i--)//按照乘数各个位判别 { if(in2[i]=='0'){ str[i]=newString(c)+newString(c); } elseif(in2[i]=='1'){ str[i]=beichengshu+newString(c); } } for(inti=1;i<=beichengshu.length();i++)//实现右移操作 { intk; char[]buwei=newchar[i]; for(k=0;k<i;k++) { buwei[k]='0'; } str[i]=newString(buwei) +str[i].substring(0,str[i].length()-i); } temp=newString(c)+newString(c); charresult[]=temp.toCharArray(); for(inti=1;i<str.length;i++)//各个部分积求和 { charjia1[]=str[i].toCharArray(); result=jia(result,jia1); } temp=signal+newString(result); data2.setText(temp); } } elseif(e.getSource()==button3){ newNewclass(null); setVisible(false); } }}Fdys.javaimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.awt.TextField;publicclassFdysextendsFrameimplementsActionListener{ Boxbasebox,box1,box2,box3,box4,box5,box6; Buttonbutton1,button2,button3,button4; Labelname,input,input1,input2,input3,input4,input5,input7,input6; TextFielddata,data1,data2,data3,data4,data5,data6,data7; publicFdys(){ setTitle("浮点数加减法"); Toolkittool=getToolkit(); tool.getScreenSize(); setBounds(200,300,500,300); button1=newButton("输入"); button2=newButton("加法"); button3=newButton("减法"); button4=newButton("返回"); box1=Box.createHorizontalBox();//水平支撑 name=newLabel("浮点数加减法",Label.CENTER); box1.add(name); box2=Box.createHorizontalBox(); input=newLabel("第一个数的阶码:"); data=newTextField(10); input1=newLabel("尾数:"); data1=newTextField(10); box2.add(input); box2.add(data); box2.add(input1); box2.add(data1); box3=Box.createHorizontalBox(); input2=newLabel("第二个数的阶码:"); data2=newTextField(10); input3=newLabel("尾数:"); data3=newTextField(10); box3.add(input2); box3.add(data2); box3.add(input3); box3.add(data3); box4=Box.createHorizontalBox(); input4=newLabel("加法的阶码为:"); data4=newTextField(10); input5=newLabel("尾数:"); data5=newTextField(10); box4.add(input4); box4.add(data4); box4.add(input5); box4.add(data5); box5=Box.createHorizontalBox(); input6=newLabel("减法的阶码为:"); data6=newTextField(10); input7=newLabel("尾数:"); data7=newTextField(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(newFlowLayout());//流式布局 add(basebox); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setVisible(true); addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ setVisible(false); } });//匿名类,适配器类 validate(); } Stringcalculate(Strings1,Strings2){//做加法运算 charac='0'; chara1[]=s1.toCharArray(),a2[]=s2.toCharArray(); for(inti=a1.length-1;i>=0;i--){ if(a1[i]=='0'&&a2[i]=='0'&&ac=='0'){ a2[i]='0'; ac='0'; }elseif(a1[i]=='0'&&a2[i]=='0'&&ac=='1'){ a2[i]='1'; ac='0'; }elseif(a1[i]=='0'&&a2[i]=='1'&&ac=='0'){ a2[i]='1'; ac='0'; }elseif(a1[i]=='1'&&a2[i]=='0'&&ac=='0'){ a2[i]='1'; ac='0'; }elseif(a1[i]=='0'&&a2[i]=='1'&&ac=='1'){ a2[i]='0'; ac='1'; }elseif(a1[i]=='1'&&a2[i]=='1'&&ac=='0'){ a2[i]='0'; ac='1'; }elseif(a1[i]=='1'&&a2[i]=='0'&&ac=='1'){ a2[i]='0'; ac='1'; }elseif(a1[i]=='1'&&a2[i]=='1'&&ac=='1'){ a2[i]='1'; ac='1'; } } returnnewString(a2); } publicStringjiemaQiubu(Strings)/*阶码变形补码*/ { if(s.startsWith("+")){ s="00"+s.substring(1); } if(s.startsWith("-")){//找到最后一个1,将之前的数取反 Stringtemp=s.substring(1,s.lastIndexOf("1")); charc[]=temp.toCharArray(); for(inti=0;i<c.length;i++){ if(c[i]=='0') c[i]='1'; else c[i]='0'; } temp=newString(c); s="11"+temp+s.substring(s.lastIndexOf(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年公共关系学课程重点试题及答案
- 2025年医保培训考试练习题及答案解析
- 体育经纪行业信用体系构建考核试卷
- 2025年护理三基理论基础知识复习经典题库及答案
- 2025年道路交通安全知识竞赛试题及答案
- 中草药保健食品市场环境与政策适应性研究考核试卷
- 品牌市场定位与竞争分析考核试卷
- 船舶无线电发射台考核试卷
- 白酒旅游与民族风情旅游融合发展考核试卷
- 扩建金属覆膜装饰材料、汽车零部件及其他配件项目报告表
- 2025年国家网络安全知识竞赛题库附完整答案
- 2025年度现代农业种植养殖劳务派遣服务合同
- 2025版工程招投标与合同管理规范执行及合规性审查合同
- 2025版建筑垃圾清运合同范本模板
- 老年高血压病例分析与管理
- 2025年云南楚雄州金江能源集团有限公司招聘考试笔试试卷附答案
- 大健康产业课件
- 2025年职业指导师(四级)考试综合应用试题集
- 2026届高考物理一轮复习:曲线运动 运动的合成与分解
- 医院行风教育培训课件
- 商业综合体整栋租赁运营管理协议
评论
0/150
提交评论