编程帮助,使其工作的方法
好吧,下面是我为我自己的 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.
}
}
Ok below is the full code I have written for what is suppose to be my own BigInteger class in java, I rather use what is there already but the professor wants this.
Thing is I have it making a BigInt and then store the data as an int[] and the sign as 1 for positive and -1 for negative. From there I am suppose to add, subtract, and multiply the numbers when asked. This would be fine if it was a typical type int, long, etc, but as the numbers are in an array now, I need to find a way to to these methods.
I have tried many ways, as you can see, and things like add are fine when both numbers are positive or negative, but when mixed, I am not getting it.
What I need help with is not idea of how to do each type, I know methods, lattice multiplication etc, but I need to know the psuedocode for how I would do this stuff, I am completely lost.
By the way I know I have asked for similar things here and there for each method, sorry I am still not getting it, and it is due in a couple of days, and I have racked my brain all weekend.
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.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要像小学时那样思考如何乘法、加法、减法和除法。也就是说,您需要考虑如何用铅笔和纸手动完成这些操作。然后您需要编写代码以相同的方式执行操作。重要的是,当以相同的方式执行这些操作时,您必须在您的 BigInteger 版本提供的数组的每个槽中存储一位数字。
在开始编写代码之前,您应该清楚地了解如何解决问题,然后编写与您的解决方案相匹配的代码。
即使向您提供了最终的解决方案,您也很可能无法理解它,因为您到目前为止提供的代码无法实现比常规整数更大的 BigInteger。
吉姆·加里森(Jim Garrison)建议与助教交谈,我会赞同这个建议。这不是选择正确方法的问题。您要么误解了教授要求您完成的任务,要么误解了如何表示大于 Integer.MAX_INT 的数字。对于正在学习如何编程的人来说,这两种误解都是可以接受的,但从长远来看,要求别人提供答案会对你造成严重伤害。
如果你通过要求别人为你做“困难”部分来跳过理解,你将获得文凭,但第一个问你困难面试问题的人会知道你只有一张写着“文凭”的纸,不是对计算机的理解。
You need to think about how you multiplied, added, subtracted, and divided like you did back when you were in grade school. That is, you need to think about how you would do these operations by hand with a pencil and a piece of paper. Then you need to write your code to do the operations the same way. The important part is that when doing these operations the same way, you must store one digit in each slot of the array that your version of the BigInteger provides.
Before you start writing code, you should have a clear idea of how to solve the problem, and then write the code to match your solution.
Even if you were presented with a final solution, odds are very good you would not understand it, as the code you have provided so far fails to implement a BigInteger which is larger than a regular Integer.
Jim Garrison suggested talking to a TA, and I would second the recommendation. It isn't a matter of picking the right methods. You either misunderstand the task the professor is asking you, or you misunderstand how one might be able to represent a number larger than Integer.MAX_INT. Either misunderstanding is acceptable in a person who is learning how to program, but asking someone else to provide an answer is going to hurt you badly in the long run.
If you skip understanding by asking for others to do the "hard" parts for you, you will get your Diploma, but the first person who asks you a hard interview question will know that you only have the piece of paper saying "Diploma", not the understanding of computers.