请高手帮我完善

无聊司马 2011-07-13 12:01:28
实现两个数字字符串相加的的算法 要求 :
1>当两个数字字符串中的数字都为正数时代码我已给出
2>当两个数字字符串中的数字为一正一负时,请完善一下 代码就接下面写出就行了
3>当输入的异常时报错并输出错误信息
代码如下:
import java.util.Scanner;

public class ab {
public static void main(String args[]) throws Exception {

Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
String str3 = "";

if (str1.length() > 1024 || str2.length() > 1024) {
throw new Exception("err:输入数据长度超出范围!");
}
System.out.println(case1(str1,str2));

}

/*
* 当两个数都为正数
*/
public static String case1(String first, String second) throws Exception {
String str3 = "";
String ftemp = "";
String stemp = "";
String rtemp = "";
int flag = 0;
for (int i = 0; i < (first.length() > second.length() ? first.length()
: second.length()); i++) {
if (i < first.length()) {
ftemp = first.substring(first.length() - 1 - i, first.length()
- i);
} else {
ftemp = "0";
}
if (i < second.length()) {
stemp = second.substring(second.length() - 1 - i, second
.length()
- i);
} else {
stemp = "0";
}
try {
rtemp = ""
+ (Integer.parseInt(ftemp) + Integer.parseInt(stemp) + flag);
flag = 0;
if (Integer.parseInt(rtemp) >= 10) { // 进位
rtemp = "" + (Integer.parseInt(rtemp) - 10);
flag = 1;
}
str3 = rtemp + str3;
} catch (Exception e) {
throw new Exception("err:输入格式错误");
}
}
return str3;
}
当两数为一正一负时 高手接:嘎嘎
}
...全文
61 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
无聊司马 2011-07-13
  • 打赏
  • 举报
回复
沙发自己坐
qybao 2011-07-13
  • 打赏
  • 举报
回复
for example

import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数:");
String str1 = sc.nextLine();
System.out.print("请输入第二个数:");
String str2 = sc.nextLine();

if (str1.length() > 1024 || str2.length() > 1024) {
throw new Exception("err:输入数据长度超出范围!");
}
if ((! str1.matches("[-]?\\d+")) || (! str2.matches("[-]?\\d+"))) {
throw new Exception("err:输入非法数据!");
}
if (str1.startsWith("-") && str2.startsWith("-")) {
System.out.println("-" + sum(str1.substring(1), str2.substring(1)));
} else if (str1.startsWith("-")) {
System.out.println(sub(str2, str1.substring(1)));
} else if (str2.startsWith("-")) {
System.out.println(sub(str1, str2.substring(1)));
} else {
System.out.println(sum(str1,str2));
}
}

public static String sum(String s1, String s2) {
s1 = s1.replaceAll("^0*", "");
s2 = s2.replaceAll("^0*", "");
if (s1.length()==0 && s2.length()==0) {return "0";}
else if (s1.length()==0) {return s2;}
else if (s2.length()==0) {return s1;}

char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
int[] result = new int[c1.length<c2.length ? c2.length : c1.length];
for (int i=0; i<result.length; i++) {result[i]=0;}
StringBuilder sb = new StringBuilder();
for (int i=result.length-1,j=c1.length-1,k=c2.length-1; i>=0; i--,j--,k--) {
if (j>=0) {result[i] += (c1[j]-'0');}
if (k>=0) {result[i] += (c2[k]-'0');}
if (i>0 && result[i]>9) {
result[i] = result[i]%10;
result[i-1]++;
}
sb.insert(0, result[i]);
}
while (sb.length() > 0 && sb.charAt(0) == '0') {
sb.delete(0, 1);
}

return sb.toString();
}

public static String sub(String s1, String s2) {
s1 = s1.replaceAll("^0*", "");
s2 = s2.replaceAll("^0*", "");
if (s1.length()==0 && s2.length()==0) {return "0";}
else if (s1.length()==0) {return "-" + s2;}
else if (s2.length()==0) {return s1;}

char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
boolean flg = c1.length >= c2.length;
if (c1.length == c2.length) {
for (int i=0; i<c1.length; i++) {
if (c1[i] < c2[i]) {
flg = false;
break;
}
}
}
if (!flg) {
char[] tmp = c1;
c1 = c2;
c2 = tmp;
}

int[] result = new int[c1.length<c2.length ? c2.length : c1.length];
for (int i=0; i<result.length; i++) {result[i]=0;}
StringBuilder sb = new StringBuilder();
for (int i=result.length-1,j=c1.length-1,k=c2.length-1; i>=0; i--,j--,k--) {
if (j>=0) {result[i] += (c1[j]-'0');}
if (k>=0) {result[i] -= (c2[k]-'0');}
if (i>0 && result[i]<0) {
result[i] += 10;
result[i-1]--;
}
sb.append(result[i]);
}
for (int i=sb.length()-1; i>0; i--) {
if (sb.charAt(i) == '0') {sb.delete(i, i+1);}
else {break;}
}

if (!flg) {sb.append('-');}
return sb.reverse().toString();
}
}
飞跃颠峰 2011-07-13
  • 打赏
  • 举报
回复

import java.util.Scanner;

public class ab {
public static void main(String args[]) throws Exception {

Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
String str3 = "";

if (str1.length() > 1024 || str2.length() > 1024) {
throw new Exception("err:输入数据长度超出范围!");
}

// 自动根据正负情况调用case1或case2
if (!str1.startsWith("-") && !str2.startsWith("-"))
System.out.println(case1(str1,str2));
else if (str1.startsWith("-") && str2.startsWith("-"))
System.out.println("-" + case1(str1.substring(1),str2.substring(1)));
else
System.out.println(case2(str1,str2));

}

/*
* 当两个数都为正数
*/
public static String case1(String first, String second) throws Exception {
String str3 = "";
String ftemp = "";
String stemp = "";
String rtemp = "";
int flag = 0;
for (int i = 0; i < (first.length() > second.length() ? first.length()
: second.length()); i++) {
if (i < first.length()) {
ftemp = first.substring(first.length() - 1 - i, first.length()
- i);
} else {
ftemp = "0";
}
if (i < second.length()) {
stemp = second.substring(second.length() - 1 - i, second
.length()
- i);
} else {
stemp = "0";
}
try {
rtemp = ""
+ (Integer.parseInt(ftemp) + Integer.parseInt(stemp) + flag);
flag = 0;
if (Integer.parseInt(rtemp) >= 10) { // 进位
rtemp = "" + (Integer.parseInt(rtemp) - 10);
flag = 1;
}
str3 = rtemp + str3;
} catch (Exception e) {
throw new Exception("err:输入格式错误");
}
}
return str3;
}

//当两数为一正一负时
public static String case2(String first, String second) throws Exception {
String str3="",big,small;
int flag=0,u,d;
boolean isnegative = false;
try {
String positive = (first.startsWith("-")?second:first).replaceAll("^0*(\\d+)$", "$1");
String negative = (first.startsWith("-")?first.substring(1):second.substring(1)).replaceAll("^0*(\\d+)$", "$1");
if (positive.length()>negative.length() || positive.length()==negative.length() && positive.compareTo(negative)>=0) {
big=positive;
small=negative;
} else {
big=negative;
small=positive;
isnegative = true;
}
for (int i=1; i<=big.length(); i++) { // 按位计算循环开始
u = Integer.parseInt(big.substring(big.length()-i,big.length()-i+1)) - flag;
d = i>small.length()?0:Integer.parseInt(small.substring(small.length()-i,small.length()-i+1));
if (u>=d) {
str3 = (u-d) + str3;
flag = 0;
} else {
str3 = (10+u-d) + str3;
flag = 1;
}
}
str3 = str3.replaceAll("^0*(\\d+)$", "$1"); //如果以0开始,去掉前面多余的0
} catch (Exception e) {
throw new Exception("err:输入格式错误");
}
return isnegative?"-"+str3:str3;
}
}

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧