80.2k views
0 votes
Write a program that prompts the user for the name of two files each containing a single line that represents a decimal integercall them m and n, keep in mind that theseintegerscould be very largein absolute value, so they might not be stored neither as along nor intvariables. You should:a)Handle erroneous input graciously.b)Include a class named BigIntegerwhere you define an integer as a linked list, along with integer operations such as addition and subtraction [multiplication and division will grant you extra points] c)Test your program for each operation and store the result of each operation as a single decimal number in a separate file containing only one line\

1 Answer

0 votes

Answer:

The output is seen bellow

Step-by-step explanation:

public class BigInteger {

public class Node{

private char data;

private Node next;

public Node(char d){

this.setData(d);

this.setNext(null);

}

public char getData() {

return data;

}

public void setData(char data) {

this.data = data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

private Node root;

public BigInteger(String s){

this.root=null;

for(int i=s.length()-1;i>=0;i--){

Node node=new Node(s.charAt(i));

node.setNext(root);

root=node;

}

}

public String toString(){

String str="";

Node cur=this.root;

int i=0;

while(cur!=null){

if(i==0 && cur.getData()=='0'){

}

else{

i=1;

str+=cur.getData();

}

cur=cur.getNext();

}

return str;

}

public String reverse(String s){

String ret="";

for(int i=s.length()-1;i>=0;i--){

ret+=s.charAt(i);

}

return ret;

}

public BigInteger add(BigInteger s){

String f=this.reverse(this.toString());

String l=this.reverse(s.toString());

int max=(f.length()>l.length())?f.length():l.length();

String ret="";

int carry=0;

for(int i=0;i<max;i++){

int sum=carry;

if(i<f.length()){

sum+=(f.charAt(i)-'0');

}

if(i<l.length()){

sum+=(l.charAt(i)-'0');

}

ret+=(char)(sum%10+'0');

carry=(sum/10);

}

if(carry!=0){

ret+='1';

}

BigInteger bi=new BigInteger(this.reverse(ret));

return bi;

}

public boolean bigNum(String s1,String s2){

if(s1.length()>s2.length()){

return true;

}

else{

if(s1.length()<s2.length()){

return false;

}

else{

for(int i=0;i<s1.length();i++){

if(s1.charAt(i)!=s2.charAt(i)){

if(s1.charAt(i)>s2.charAt(i)){

return true;

}

else{

return false;

}

}

}

}

}

return false;

}

public BigInteger subtraction(BigInteger s){

String f=this.toString();

String l=s.toString();

boolean b=this.bigNum(f, l);

if(b==false){

String tmp=f;

f=l;

l=tmp;

}

f=this.reverse(f);

l=this.reverse(l);

int max=(f.length()>l.length())?f.length():l.length();

String ret="";

int borrow=0;

for(int i=0;i<max;i++){

int sum=borrow;

if(i<f.length()){

sum+=(f.charAt(i)-'0');

}

if(i<l.length()){

sum-=(l.charAt(i)-'0');

}

if(sum<0){borrow=-1;sum=10+sum;}

else{borrow=0;}

ret+=(char)(sum%10+'0');

}

if(b==false){

ret+="-";

}

BigInteger bi=new BigInteger(this.reverse(ret));

return bi;

}

public BigInteger multiplication(BigInteger s){

String f=this.toString();

String l=s.toString();

int len=l.length();

BigInteger bi=new BigInteger("");

for(int i=len-1;i>=0;i--){

//System.out.println(l.charAt(i));

BigInteger r=new BigInteger(f);

for(int j=(l.charAt(i)-'0');j>1;j--){

r=r.add(new BigInteger(f));

//System.out.print(r+" " );

}

//System.out.println();

bi=bi.add(r);

f=f+"0";

}

return bi;

}

public BigInteger division(BigInteger s){

BigInteger t=this;

BigInteger bi=new BigInteger("");

int i=0;

t=t.subtraction(s);

String str=t.toString();

while(str.charAt(0)!='-' && i<40){

//System.out.println(str+" "+(i+1));

bi=bi.add(new BigInteger("1"));

t=t.subtraction(s);

str=t.toString();

i++;

}

return bi;

}

}

-------------------

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Driver {

public static void main(String [] args){

Scanner sc=new Scanner(System.in);

String str1="";

String str2="";

System.out.print("Enter file name 1 :");

String file1=sc.next();

//String file1="datafile1.txt";

BufferedReader reader1;

try {

reader1 = new BufferedReader(new FileReader(file1));

while((str1=reader1.readLine())!=null){

break;

}

reader1.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.print("Enter file name 2 :");

String file2=sc.next();

//String file2="datafile2.txt";

BufferedReader reader2;

try {

reader2 = new BufferedReader(new FileReader(file2));

while((str2=reader2.readLine())!=null){

break;

}

reader2.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

User Jonah Benton
by
5.5k points