以下是我为java中我自己的BigInteger类编写的完整代码,我宁愿使用已有的但教授想要的。
我得它制作一个BigInt,然后将数据存储为int [],符号为1表示正数,-1表示负数。从那里我想要在被问到时添加,减去和乘以数字。如果它是典型的int,long等类型,那就没问题,但是现在数字在数组中,我需要找到一种方法来实现这些方法。
我已经尝试了很多方法,正如你所看到的那样,当这两个数字都是正数或负数时,像add这样的东西很好,但是当混合时,我没有得到它。
我需要帮助的是不知道如何做每种类型,我知道方法,格子乘法等,但我需要知道 psuedocode 我将如何做这些东西,我是完全迷失了。
顺便说一下,我知道我已经针对每种方法在这里和那里要求类似的事情,对不起,我仍然没有得到它,并且它将在几天内到期,并且我整个周末都在绞尽脑汁。
package BigInteger;
public class BigInt{
private int[] store;
private int sign;
public BigInt(){
store = new int[10];
sign = 1;
}
public BigInt(int[] data){
store = data;
}
public BigInt(int data, int maxSize){
int size = String.valueOf(data).length();
if (maxSize == 0){
store = new int[size];
}
else if(size >= maxSize){
store = new int[size];
}
else{
store = new int[maxSize];
}
if (data > 0){
sign = 1;
}else{
sign = -1;
}
for(int i = 0; i < size; i++){
store[i] = data % 10;
data = data / 10;
}
}
public BigInt(String num){
store = new int[num.length()];
try{
for(int i = 0; i < num.length();i++){
store[i] = Integer.parseInt(num.substring(i,i+1));
}
}catch(IndexOutOfBoundsException e){
System.out.println("Index out of bounds");
}
}
public BigInt add(BigInt val){
int[] bigger;
int[] smaller;
int[] dStore;
//Compare sizes and set which is bigger in first and create a new int[]
if(val.getSize() >= this.getSize()){
bigger = val.getData();
smaller = this.getData();
dStore = new int[val.getSize()+1];
}else{
bigger = this.getData();
smaller = val.getData();
dStore = new int[this.getSize()+1];
}
//Loop through till the end of small and add cells
for(int i = 0;i<smaller.length;i++){
if((this.getSign() == 1 && val.getSign() == 1) || (this.getSign() == -1 && val.getSign() == -1)){
dStore[i] = Math.abs(bigger[i] + smaller[i]);
}else if((this.getSign() == -1 || val.getSign() == -1) && (this.getSign() == 1 || val.getSign() == 1)){
if(this.getSign() < 0 && this.getSize() < val.getSize()){
smaller = this.getData();
bigger = val.getData();
bigger[i] = bigger[i] + 10;
}else if(val.getSign() < 0 && val.getSize() < this.getSize()){
smaller = val.getData();
bigger = this.getData();
bigger[i] = bigger[i] + 10;
}
dStore[i] = bigger[i] + smaller[i];
}
}
for(int i=0;i<dStore.length;i++){
if(dStore[i] >= 10){
dStore[i] = dStore[i] % 10;
dStore[i+1] = dStore[i+1] + 1;
}
}
//Finish adding numbers after small is done
for(int i=smaller.length;i<bigger.length;i++){
dStore[i] = Math.abs(bigger[i] + dStore[i]);
}
//Create new BigInt from int[]
BigInt rVal = new BigInt(dStore);
//Set sign of new BigInt
if(this.getSign() == 1 && val.getSign() == 1){
rVal.setSign(1);
}else if(this.getSign() == -1 && val.getSign() == -1){
rVal.setSign(-1);
}else if((this.getSign() == 1 && val.getSign() == -1) || (this.getSign() == -1 && val.getSign() == 1)){
if(this.getSize() > val.getSize()){
rVal.setSign(1);
}else{
rVal.setSign(-1);
}
}
return rVal;
}
public BigInt subtract(BigInt val){
int[] bigger;
int[] smaller;
int[] dStore;
int carryOver = 0;
//Compare sizes and set which is bigger in first and create a new int[]
if(val.getSize() >= this.getSize()){
bigger = val.getData();
smaller = this.getData();
dStore = new int[val.getSize()+1];
}else{
bigger = this.getData();
smaller = val.getData();
dStore = new int[this.getSize()+1];
}
//Loop through till the end of small and add cells
for(int i = 0; i < smaller.length;i++){
dStore[i] = Math.abs(bigger[i] - smaller[i]);
}
for(int i=0;i<dStore.length;i++){
if(dStore[i] >= 10){
dStore[i] = dStore[i] % 10;
dStore[i+1] = dStore[i+1] + 1;
}
}
//Finish adding numbers after small is done
for(int i=smaller.length;i<bigger.length;i++){
dStore[i] = Math.abs(bigger[i] + dStore[i]);
}
//Create new BigInt from int[]
BigInt rVal = new BigInt(dStore);
//Set sign of new BigInt
if(this.getSign() == 1 && val.getSign() == 1){
rVal.setSign(1);
}else if(this.getSign() == -1 && val.getSign() == -1){
rVal.setSign(-1);
}else if((this.getSign() == 1 && val.getSign() == -1) || (this.getSign() == -1 && val.getSign() == 1)){
if(this.getSize() > val.getSize()){
rVal.setSign(1);
}else{
rVal.setSign(-1);
}
}
return rVal;
}
public int multiply(BigInt val){
int[] bigger;
int[] smaller;
int[] dStore;
int[][] tempResult;
//Checks to see which is bigger and then adds that to bigger
if(val.getSize() >= this.getSize()){
bigger = val.getData();
smaller = this.getData();
dStore = new int[val.getSize()+this.getSize()];
}else{
bigger = this.getData();
smaller = val.getData();
dStore = new int[val.getSize()+this.getSize()];
}
tempResult = new int[smaller.length][bigger.length];
String resultString = "";
String[] tempStr = new String[smaller.length];
int[] intResults = new int[smaller.length*bigger.length];
int loop = 0;
//Makes one long string of the numbers
for(int i=smaller.length-1;i >= 0;i--){
for(int j = bigger.length-1;j >= 0;j--){
tempResult[i][j] = smaller[i] * bigger[j];
resultString = new StringBuffer(resultString).insert(resultString.length(), tempResult[i][j]).toString();
}
}
//Splits the string up into loop amount of strings smaller.length size
for(int i=0;i < resultString.length();i = i + smaller.length){
tempStr[loop] = (resultString.substring(i - loop, (i + smaller.length)));
//System.out.println(tempStr[loop]);
loop++;
}
//Adds 0's to make a full matrix
for(int i = 0; i < loop;i++){
while(tempStr[i].length() < tempStr[loop-1].length()){
tempStr[i] = new StringBuffer(tempStr[i]).insert(tempStr[i].length(), "0").toString();
}
}
int[] tempNum = new int[smaller.length];
int[] finalNum = new int[bigger.length];
for(int i=0;i<smaller.length;i++){
tempNum[i] = tempNum[i] + (Integer.parseInt((tempStr[i].substring(0,tempStr[i].length()))) % 10);
}
for(int i =0; i < smaller.length;i++){
finalNum[0] =+ tempNum[i];
}
System.out.println(tempNum[1]);
//Makes a new string that has all the digits in equal length.
resultString = "";
for(int i=0; i < smaller.length;i++){
resultString = new StringBuffer(resultString).insert(resultString.length(), tempStr[i]).toString();
}
for(int i = 0; i<resultString.length();i++){
for(int j = 0; j < 1;j++){
tempNum[j] = tempNum[j] + Integer.parseInt(resultString.substring(i,i+1));
//System.out.println(tempNum[j]);
}
}
//System.out.println(resultString);
return 0;
}
public void reverse(){
for (int left=0, right=this.store.length-1; left<right; left++, right--) {
int temp = store[left]; store[left] = store[right]; store[right] = temp;
}
}
public int getSize(){
int size = this.store.length - 1;
return size;
}
public int[] getData(){
return store;
}
public void displayData(){
for(int i=0;i<this.store.length;i++){
System.out.println(this.store[i]);
}
System.out.println("Sign: " + this.sign);
}
public int getSign(){
return this.sign;
}
public void setSign(int tempSign){
this.sign = tempSign;
}
public boolean isPositive(){
return this.sign == 1;
}
public boolean isGreater(){
return this.
}
}
答案 0 :(得分:1)
你需要考虑你在小学时的成倍增加,增加,减少和分割的方式。也就是说,你需要考虑如何用铅笔和一张纸手工完成这些操作。然后,您需要编写代码以相同的方式执行操作。重要的是,当以相同的方式执行这些操作时,您必须在您的BigInteger版本提供的阵列的每个插槽中存储一个数字。
在开始编写代码之前,您应该清楚地知道如何解决问题,然后编写代码以匹配您的解决方案。
即使您遇到了最终解决方案,但是您很难理解,因为您目前提供的代码无法实现比常规Integer更大的BigInteger。
Jim Garrison建议与TA谈话,我会推荐这个建议。这不是选择正确方法的问题。你要么误解了教授问你的任务,要么你误解了一个人如何能够代表一个大于Integer.MAX_INT的数字。对于正在学习如何编程的人来说,误解是可以接受的,但是从长远来看,要求别人提供答案会严重伤害你。如果你通过要求其他人为你做“硬”部分来跳过理解,那么你将获得你的文凭,但第一个问你一个艰难的面试问题的人会知道你只有那张纸说“文凭“,而不是对计算机的理解。