📄 bignumber.java
字号:
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/*
* Created on 2006-11-21
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Shenjun
* 该类实现了大整数的表示,用数组nums代表一个大整数,该大整数在数组中按反序存储,
* 12345在该类中表示为54321.
*
*
*/
/**
* @author asus
*
*/
/**
* @author asus
*
*/
public class BigNumber {
/**
* 构造函数,用于从键盘输入该大整数,N表示其大整数的最大存储空间。
* @param isInput 指定是否从键盘输入该大整数
* @param N 指定该大整数最大能存储多少位
*/
public BigNumber(boolean isInput,int N) {
//System.out.println("Input the Nums,with E end");
Capacity=N;
nums = new int[N];
if (!isInput)
return;
byte[] temp = new byte[1000];
int tempSize = 0;
//String s2;
try {
tempSize = System.in.read(temp);
tempSize = tempSize - 2;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i;
if (temp[0] == '-') {
i = 1;
setPositive(false);
} else
i = 0;
int position=tempSize-1;
while (i < tempSize) {
addToNums(position,temp[i] - 48);
//addToHead((byte) (temp[i]-48));
//System.out.println(c[i]);
i++;
position--;
}
//TODO Auto-generated constructor stub
/*
* if(numString!=null){
*
* if(!numString.endsWith("e")){ System.out.println("Format Error : e.g.
* 1234455e"); System.exit(0); }
*
* char c[]=numString.toCharArray(); int i=0; while(i
* <c.length-1&&c[i]!='e'){ addToHead(c[i]-48);
* //System.out.println(c[i]); i++; } size=c.length-1; }
*/
}
/**
* 构造函数,用于从文件读入该大整数
* @param in 指定读入大整数的文件
* @param isP 指定该大整数的正负,true为正 false负
* @param N 指定该大整数的存储空间,应该是大于文件中数字的位数。
*/
public BigNumber(String in, boolean isP,int N) {
try {
Capacity=N;
nums =new int[N];
setPositive(isP);
byte b[] = new byte[1];
FileInputStream input = new FileInputStream(in);
try {
int t = input.read(b);
int position=N-1;
while (t != -1) {
addToNums(position,b[0]);
//System.out.println(c[i]);
t = input.read(b);
position--;
}
input.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将整数i插入到数组nums中的position位置中去
* @param i
*/
public void addToNums(int position,int i) {
nums[position]=i;
size++;
}
/**
* 获取nums数组中的位于position的元素
* @param
* @return
*/
public int get(int position) {
return nums[position];
}
/**
* 将该大整数写入到文件中,只适用于结果观察
* @param out 指定写入的文件名字
*/
public void print(String out) {
try {
PrintWriter output=new PrintWriter(new BufferedWriter(new FileWriter(out)));
int i = size - 1;
if(isPositive)output.print('+');
else output.print('-');
while(i>0){
output.print(get(i));
i--;
}
output.close();
// TODO Auto-generated constructor stub
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将该大整数输入到屏幕上
*/
public void print() {
int i = size-1;
System.out.print(isPositive ? "+" : "-");
while (i>=0) {
System.out.print(get(i));
i--;
}
System.out.println();
}
/**
* 获取数组中实际存储的有效元素值
* @return Returns the size.
*/
public int getSize() {
return size;
}
/**
* 设置数组中实际存储的有效元素值,主要用于移位操作后改写数组大小
* @param s
*/
public void setSize(int s)
{
this.size=s;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
protected Object clone() throws CloneNotSupportedException {
BigNumber clone = new BigNumber(false,size);
for (int i = 0; i < this.getSize(); i++)
clone.addToNums(i,get(i));
return clone;
}
/**
* @return Returns the isPositive.
*/
public boolean isPositive() {
return isPositive;
}
/**
* @param isPositive
* The isPositive to set.
*/
public void setPositive(boolean isPositive) {
this.isPositive = isPositive;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
String s = nums.toString();
return s.toString();
}
/**
* nums 数组用于存储大整数的元素,按逆序排列
*/
private int nums[];
/**
* size 指定nums数组中实际存的元素个数
*/
private int size = 0;
/**
* Capacity 指定nums数组的容量大小
*/
private int Capacity;
/**
* 指定大整数的正负
*/
private boolean isPositive = true;
public static void main(String[] args) {
System.out.println("Input a positive number");
//BigNumber m1 = new BigNumber("num1.txt", true,1000);
BigNumber m1 = new BigNumber(true,20);
m1.print();
System.out.println(m1.getSize());
System.out.println(m1.getCapacity());
/*
* int increment=(7+8)/10; System.out.println(increment);
* increment=(7+8)%10; System.out.println(increment);
*/
}
public int getCapacity() {
return Capacity;
}
public void setCapacity(int capacity) {
Capacity = capacity;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -