private void chengjfa(String s1, String s2) { double s10=0,s20=0; int cifang1=-1,cifang2=-1; int weizhi1=0,weizhi2=0; String fuhao=null; //判断正负
if(s1.charAt(0)=='+'&&s2.charAt(0)=='+'||s1.charAt(0)=='-'&&s2.charAt(0)=='-'){ fuhao=\
}else if(s1.charAt(0)=='+'&&s2.charAt(0)=='-'||s1.charAt(0)=='-'&&s2.charAt(0)=='+'){ fuhao=\ }
for(int i=0;i if(s1.charAt(i)=='.') weizhi1 =i; } for(int j=0;j if(s2.charAt(j)=='.') weizhi2=j; } for(int i=weizhi1+1;i s10+=1*pow(2,cifang1); cifang1--; }else if(s1.charAt(i)=='0'){ cifang1--; } } for(int i=weizhi2;i s20+=1*pow(2,cifang2); cifang2--; }else if(s2.charAt(i)=='0'){ cifang2--; } } double xiangcheng =s10*s20; StringBuffer buffer= ejz(xiangcheng); String jieguo =buffer.toString(); t3.setText(fuhao+\ } private StringBuffer ejz(double xiangcheng) { double a= xiangcheng; int t=1; int cishu=0; StringBuffer buffer=new StringBuffer(); while(t==1){ a=a*2; if(a>1){ buffer.append('1'); cishu++; a-=1; }else if(a>0&&a<1){ buffer.append('0'); cishu++; 15 }else if(a==1){ buffer.append('1'); t=0; }else if(a==0){ t=0; }else if(cishu==6){ t=0; } } return buffer; } } Fdjj.java package xiaoshu; import java.awt.*; import java.awt.event.*; import javax.swing.*; public String jiemaQiubu(String s) //阶码求补码 { if (s.startsWith(\ //判断正负 并求补 s = \ } if (s.startsWith(\ 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 if(c[i]=='1') c[i] = '0'; } temp = new String(c); s = \从后往前到最后一个1 } return s; } public String weishuQiubu(String s) // 尾数求变形补码 { if (s.startsWith(\ String str = s.substring(3).replaceAll(\ s = \ } if (s.startsWith(\ String temp = null, str = s.substring(3).replaceAll(\ temp = s.substring(3, s.lastIndexOf(\ char c[] = temp.toCharArray(); for (int i = 0; i < c.length; i++) { //尾数求补 if (c[i] == '0') c[i] = '1'; else 16 c[i] = '0'; } temp = new String(c); s = \ } return s; } public int toSubstract(String sj1, String sj2) // 求阶差 { 中 } int sum = 0, signal = 0; char cj2[] = sj2.substring(0, sj2.lastIndexOf(\将sj2的字符放到cj2 for (int i = 0; i < cj2.length; i++) { if (cj2[i] == '0') cj2[i] = '1'; else cj2[i] = '0'; } sj2 = new String(cj2) + sj2.substring(sj2.lastIndexOf(\String temp = calculate(sj1, sj2); //计算 阶差大小 if (temp.startsWith(\ //第二个数阶码小 { signal = 1; temp = temp.substring(2); } if (temp.startsWith(\ // 第一个数阶码小 { signal = -1; String str = temp.substring(2, temp.lastIndexOf(\ char c[] = str.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else c[i] = '0'; } temp = new String(c) + temp.substring(temp.lastIndexOf(\} //将阶码差值化成整形 int n[] = new int[temp.length()]; for (int i = 0; i < n.length; i++) { if (temp.substring(i, i + 1).equals(\ n[i] = 0; else n[i] = 1; } for (int i = n.length - 2; i >= 0; i--) { for (int j = i; j >= i; j--) { n[i] = n[i] * 2; } } for (int i = 0; i < n.length; i++) { sum = sum + n[i]; } sum = sum * signal; return sum; 17 public String bumaHuanyuan(String s)// 变形补码还原真值处理,无小数点 { if (s.startsWith(\ s = \ } else if (s.startsWith(\ String temp = s.substring(2, s.lastIndexOf(\ char c[] = temp.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == '0') c[i] = '1'; else c[i] = '0'; } temp = new String(c); s = \ } 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(\ // 规格化 { if (st1.startsWith(\ // 尾数左移 { st1 = st1.substring(1) + \ count++; } else if (st1.startsWith(\尾数右移 { st1 = st1.substring(0, 1) + st1.substring(0, st1.length() - 1); count--; } } while (count > 0) // 阶码循环-1 { String temp = sj1.replaceAll(\ temp = temp.substring(1) + \ sj1 = calculate(sj1, temp); count--; } while (count < 0) { String temp = sj1.replaceAll(\ 18 temp = temp.substring(1) + \ sj1 = calculate(sj1, temp); count++; } sj1 = bumaHuanyuan(sj1); st1 = st1.substring(0, len + 3); if (st1.endsWith(\ { String temp = st1.replaceAll(\ temp = temp.substring(1) + \ st1 = calculate(st1, temp); } else if (st1.endsWith(\ } st1 = st1.substring(0, st1.length() - 1); st1 = bumaHuanyuan(st1); s[0] = sj1; s[1] = st1; return s; } 19 // 尾数补码还原处理