1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
djyliett [7]
3 years ago
11

Write a program that prompts the user for the name of two files each containing a single line that represents a decimal integerc

all 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\
Computers and Technology
1 answer:
storchak [24]3 years ago
4 0

Answer:

The output is seen bellow

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();

      }

You might be interested in
Your brother is a video producer and is looking to buy some sort of new computing device. he needs a lot of memory and processin
mafiozo [28]
He should purchase a Desktop PC as it would allow him to install high-power parts such as RAM, Graphics Card and CPU.
3 0
2 years ago
Use the variables k, d, and s so that they can read three different values from standard input--an integer, a double, and a stri
Nat2105 [25]

Answer:

The c++ program to implement the given scenario is shown below.

#include <iostream>

using namespace std;

int main() {

   // variables declared as mentioned

   int k;

   double d;

   string s;

   cout << " Enter an integer value, a double value, a string value in the mentioned order " << endl;

   cin >> k >> d >> s;

   // variables printed in reverse order

   cout << " Reverse order " << endl;

   cout << s << " " << d << " " << k << endl;

   // variables printed in original order

   cout << " Original order " << endl;

   cout << k << " " << d << " " << s << endl;

   return 0;

}

OUTPUT

Enter an integer value, a double value, a string value in the mentioned order  

12 45.67 brainly

Reverse order  

brainly 45.67 12

Original order  

12 45.67 brainly

Explanation:

1. All the variables are declared as mentioned in the question.

int k;

   double d;

   string s;

2. The user is prompted to enter values for integer, double and string variables respectively. The input is accepted in the order mentioned above.

cin >> k >> d >> s;

3. The input is accepted in a single line since this is mentioned in the scenario that variables should be printed in the order in which they are read.

4. These variables are displayed to the standard output in reverse order – string, double, integer. The variables are displayed with exactly one space in between them.

cout << s << " " << d << " " << k << endl;

5. Then, variables are displayed in the original order – integer, double, string. The variables are displayed with exactly one space in between them.

cout << k << " " << d << " " << s << endl;

7 0
2 years ago
Social media applications' main purpose is to allow users to watch movies and TV shows, listen to music, and read books online.
Novosadov [1.4K]

Answer: I dont really know but im guessing it depends on the type of assignment you have. im thinking True.

Explanation:

4 0
3 years ago
Which of the following statements is true of alert files?
ozzi

Answer:

They are only generated by Wireshark.

3 0
2 years ago
What is most important for you to choose before you build a network?
lbvjy [14]

i'm needing help with the same question but mine gave choices, could you help please? thanks

A.  

private network

B.  

NOS

C.  

network media

D.  

network protocol

E.  

directory service

3 0
3 years ago
Other questions:
  • For Adults/Adolescents, you should call/activate EMS: Before providing CPR. After providing CPR for 2 minutes. After an AED has
    13·1 answer
  • Which is an advantage that electronic scheduling tools have over paper calendars?
    13·2 answers
  • What is an example of a transition effectl
    7·1 answer
  • 1. The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way
    12·1 answer
  • PLEASE HELP! I'm offering brainliest!
    6·1 answer
  • GIVING BRAINLIST TO WHOEVER ANSWERS
    9·1 answer
  • You are comparing cryptographic solutions to implement at your organization. Which two items should you focus on when you are ev
    12·1 answer
  • To extract detailed and comprehensive responses from your client, use the _____ questioning technique.
    5·2 answers
  • Write an application named Hurricane that outputs a hurricane’s category based on the user’s input of the wind speed. Category 5
    7·1 answer
  • Difference between switch and switch lite
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!