两个非常大的数字相加,相减(JAVA)

注意点:  

大数相加的时候没有判断数字的正负,

如果都是正数,那么就可以按照正数相加来计算,

如果是一正一负可以转换成大整数相减,
两个负数可以先保存符号,然后相加。


加法和减法的输入都不包括符号

加法

	public String bigNumberAdd(String f, String s) {
		System.out.print("加法:" + f + "+" + s + "=");
		// 翻转两个字符串,并转换成数组
		char[] a = new StringBuffer(f).reverse().toString().toCharArray();
		char[] b = new StringBuffer(s).reverse().toString().toCharArray();
		int lenA = a.length;
		int lenB = b.length;
		// 计算两个长字符串中的较长字符串的长度
		int len = lenA > lenB ? lenA : lenB;
		int[] result = new int[len + 1];
		for (int i = 0; i < len + 1; i++) {
			// 如果当前的i超过了其中的一个,就用0代替,和另一个字符数组中的数字相加
			int aint = i < lenA ? (a[i] - '0') : 0;
			int bint = i < lenB ? (b[i] - '0') : 0;
			result[i] = aint + bint;
		}
		// 处理结果集合,如果大于10的就向前一位进位,本身进行除10取余
		for (int i = 0; i < result.length; i++) {
			if (result[i] >= 10) {
				result[i + 1] += result[i] / 10;
				result[i] %= 10;
			}
		}
		StringBuffer sb = new StringBuffer();
		// 该字段用于标识是否有前置0,如果有就不要存储
		boolean flag = true;
		// 注意从后往前
		for (int i = len; i >= 0; i--) {
			if (result[i] == 0 && flag) {
				continue;
			} else {
				flag = false;
			}
			sb.append(result[i]);
		}
		// 结果
		System.out.println(sb.toString());
		return sb.toString();
	}

减法

	public static String bigNumberSub(String f, String s) {
		System.out.print("减法:" + f + "-" + s + "=");
		// 将字符串翻转并转换成字符数组
		char[] a = new StringBuffer(f).reverse().toString().toCharArray();
		char[] b = new StringBuffer(s).reverse().toString().toCharArray();
		int lenA = a.length;
		int lenB = b.length;
		// 找到最大长度
		int len = lenA > lenB ? lenA : lenB;
		int[] result = new int[len];
		// 表示结果的正负
		char sign = '+';
		// 判断最终结果的正负
		if (lenA < lenB) {
			sign = '-';
		} else if (lenA == lenB) {
			int i = lenA - 1;
			// 找到第一个不相等的
			// 注意下标0处理
			while (i > 0 && a[i] == b[i]) {
				i--;
			}
			if (a[i] < b[i]) {
				sign = '-';
			}
		}
		// 计算结果集,如果最终结果为正,那么就a-b否则的话就b-a
		for (int i = 0; i < len; i++) {
			int aint = i < lenA ? (a[i] - '0') : 0;
			int bint = i < lenB ? (b[i] - '0') : 0;
			if (sign == '+') {
				result[i] = aint - bint;
			} else {
				result[i] = bint - aint;
			}
		}
		// 如果结果集合中的某一位小于零,那么就向前一位借一,
		// 然后将本位加上10。其实就相当于借位做减法
		for (int i = 0; i < result.length - 1; i++) {
			if (result[i] < 0) {
				result[i + 1] -= 1;
				result[i] += 10;
			}
		}

		StringBuffer sb = new StringBuffer();
		// 如果最终结果为负值,就将负号放在最前面,正号则不需要
		if (sign == '-') {
			sb.append('-');
		}
		// 判断是否有前置0
		boolean flag = true;
		for (int i = len - 1; i >= 0; i--) {
			if (result[i] == 0 && flag) {
				continue;
			} else {
				flag = false;
			}
			sb.append(result[i]);
		}
		// 结果为一位且是0,被上面的for处理了
		// 如果最终结果集合中没有值,就说明是两值相等,最终返回0
		if (sb.toString().equals("")) {
			sb.append("0");
		}
		// 返回值
		System.out.println(sb.toString());
		return sb.toString();
	}


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我会为您解答这两个问题。 第一个问题: 好的,我明白了。您想了解我后面所使用的ChitGPT是一个类ChatGPT的AI,与OpenAI公司开发的ChatGPT没有关系。这是正确的。ChitGPT是由CSDN开发的一个基于GPT-2的中文AI语言模型,其目的是为了更好地服务于中文自然语言处理领域。与OpenAI公司开发的ChatGPT相比,ChitGPT在中文语言处理方面有更好的表现。 第二个问题: 好的,我明白了。您想让我帮您解决一个问题,即如何用GUI界面编写一个两个相加相减、相乘的Java程序。下面是一个简单的Java程序示例,可以根据您的需求进行修改和扩展。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { //定义各个界面组件 private JLabel firstNumLabel = new JLabel("第一个数:"); private JTextField firstNumText = new JTextField(); private JLabel secondNumLabel = new JLabel("第二个数:"); private JTextField secondNumText = new JTextField(); private JLabel resultLabel = new JLabel("结果:"); private JTextField resultText = new JTextField(); private JButton addButton = new JButton("相加"); private JButton minusButton = new JButton("相减"); private JButton multiplyButton = new JButton("相乘"); private JButton clearButton = new JButton("清空"); public Calculator() { //设置窗口属性 this.setTitle("计算器"); this.setSize(400, 250); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(null); //添加组件到窗口 firstNumLabel.setBounds(50, 20, 80, 30); this.add(firstNumLabel); firstNumText.setBounds(120, 20, 200, 30); this.add(firstNumText); secondNumLabel.setBounds(50, 60, 80, 30); this.add(secondNumLabel); secondNumText.setBounds(120, 60, 200, 30); this.add(secondNumText); resultLabel.setBounds(50, 100, 80, 30); this.add(resultLabel); resultText.setBounds(120, 100, 200, 30); resultText.setEditable(false); //结果框不可编辑 this.add(resultText); addButton.setBounds(50, 140, 80, 30); addButton.addActionListener(this); //绑定事件处理器 this.add(addButton); minusButton.setBounds(140, 140, 80, 30); minusButton.addActionListener(this); //绑定事件处理器 this.add(minusButton); multiplyButton.setBounds(230, 140, 80, 30); multiplyButton.addActionListener(this); //绑定事件处理器 this.add(multiplyButton); clearButton.setBounds(140, 180, 80, 30); clearButton.addActionListener(this); //绑定事件处理器 this.add(clearButton); //显示窗口 this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) { //相加 double a = Double.parseDouble(firstNumText.getText()); double b = Double.parseDouble(secondNumText.getText()); double result = a + b; resultText.setText(String.valueOf(result)); } else if (e.getSource() == minusButton) { //相减 double a = Double.parseDouble(firstNumText.getText()); double b = Double.parseDouble(secondNumText.getText()); double result = a - b; resultText.setText(String.valueOf(result)); } else if (e.getSource() == multiplyButton) { //相乘 double a = Double.parseDouble(firstNumText.getText()); double b = Double.parseDouble(secondNumText.getText()); double result = a * b; resultText.setText(String.valueOf(result)); } else if (e.getSource() == clearButton) { //清空 firstNumText.setText(""); secondNumText.setText(""); resultText.setText(""); } } public static void main(String[] args) { new Calculator(); } } ``` 这个程序使用了Java的GUI库Swing来创建界面,实现了两个相加相减、相乘的功能。您可以根据需要进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值