Question
Change the following program so that instead of using input from the user, the program gets it's input from a file called input.txt where the
Change the following program so that instead of using input from the user, the program gets it's input from a file called "input.txt" where the data is terminated by -1.
Example of input.txt:
957994954495949457454 774874754744875847484 80485080443358 4849850549686868696 99999999 11111111111111111876333 777777678 9999999999 5555555555 44444444444444444465 -1
And that the output is shown in both the console as
and in a output.txt as
The program is as follows:
#include
using namespace std;
class Node { public: int value; Node * next; Node(); Node(int); };
Node::Node():value(0),next(NULL){} Node::Node(int j):value(j),next(NULL){}
void Print(Node * k)
{
if(!k) return; Print(k->next); coutvalue;
}
void AddHead(Node* & head, int n)
{
Node * temp = new Node(n); temp->next = head; head=temp;
}
void Addition( Node* int1,Node* int2)
{
int carry = 0, sum; Node* result = NULL; Node* temp = NULL; Node* prev = NULL; while(int1 !=NULL || int2 != NULL)
{
sum = carry +((int1? int1->value: 0) +(int2? int2->value: 0)); carry = (sum >=10)? 1:0; sum = sum % 10; temp = new Node(sum); if(result== NULL){ result = temp;} else{ prev->next = temp;} prev = temp; if(int1) int1=int1->next; if(int2) int2=int2->next;
}
if(carry > 0) temp->next = new Node(carry); Print(result);
}
int Length(Node* head) { int i=0; while(head) { i++; head=head->next; } return i; }
int main() { ifstream fin; ofstream fout; int *num1, *num2; char ans; int j; Node* int1 = NULL; Node* int2 = NULL; fin.open("input.txt"); fout.open("output.txt"); num1=(int*)malloc(sizeof(int)); num2=(int*)malloc(sizeof(int)); fin >> *num1; while(*num1!='-1'){ fin >> *num2; fout> *num2; for(unsigned int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started