Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program below represents decimal integers as a linked list of digits. It asks for 2 positive integers, stores them as linked lists, computes the

The program below represents decimal integers as a linked list of digits. It asks for 2 positive integers, stores them as linked lists, computes the sum, and outputs it. It then 'free()'s *all* nodes of all 3 lists.

./byDigitAdder Please enter the first positive integer: negative one Please enter the first positive integer: positive one Please enter the first positive integer: zero Please enter the first positive integer: -1 Please enter the first positive integer: 0 Please enter the second positive integer: 0 0 + 0 = 0 $ ./byDigitAdder Please enter the first positive integer: 1 Please enter the second positive integer: 0 1 + 0 = 1 $ ./byDigitAdder Please enter the first positive integer: 4 Please enter the second positive integer: 4 4 + 4 = 8 $ ./byDigitAdder Please enter the first positive integer: 5 Please enter the second positive integer: 5 5 + 5 = 10 $ ./byDigitAdder  Please enter the first positive integer: 1 Please enter the second positive integer: 9 1 + 9 = 10 $ ./byDigitAdder  Please enter the first positive integer: 2 Please enter the second positive integer: 9999 2 + 9999 = 10001 $ ./byDigitAdder  Please enter the first positive integer: 2 Please enter the second positive integer: 9999999999999999999999999999999999999999999999999999999999999999 2 + 9999999999999999999999999999999999999999999999999999999999999999 = 10000000000000000000000000000000000000000000000000000000000000001 $ ./byDigitAdder  Please enter the first positive integer: 123456789 Please enter the second positive integer: 987654321 123456789 + 987654321 = 1111111110 $ ./byDigitAdder  Please enter the first positive integer: 123456789 Please enter the second positive integer: 876543210 123456789 + 876543210 = 999999999 

/*-------------------------------------------------------------------------* *--- ---* *--- byDigitAdder.c ---* *--- ---* *--- This file defines a C program that represents positive ---* *--- decimal integers as linked lists of digits. It asks the user ---* *--- for 2 integers, computes the sum, and outputs it. ---* *--- ---* *--- ---- ---- ---- ---- ---- ---- ---- ---- ---* *--- ---* *--- ---* *-------------------------------------------------------------------------*/ #include  #include  #include  #define NUM_TEXT_LEN 256 // PURPOSE: To represent one digit of a decimal number. struct DigitNode { int digit_; // I suggest you range this in [0..9] struct DigitNode* nextPtr_; // I suggest you make this point to // the next most significant digit. }; // PURPOSE: To obtain the text of a decimal integer into array 'numberCPtr' // of length 'numberTextLen'. 'descriptionCPtr' is printed because it // tells the user the integer that is expected. Ending ' ' from // 'fgets()' is replaced with '\0'. No return value. void obtainPostiveInt(char* numberCPtr, int numberTextLen, const char* descriptionCPtr ) { // YOUR CODE HERE } // PURPOSE: To build and return a linked list IN LITTLE ENDIAN ORDER // of the decimal number whose text is pointed to by 'numberCPtr'. // If 'numberCPtr' points to the string "123" then the linked list // returned is 'digit_=3' -> 'digit_=2' -> 'digit_=1' -> NULL. struct DigitNode* numberList (const char* numberCPtr ) { // YOUR CODE HERE } // PURPOSE: To build and return a linked list IN LITTLE ENDIAN ORDER // of the decimal number that results from adding the decimal numbers // whose digits are pointed to by 'list0' and 'list1'. struct DigitNode* add (const struct DigitNode* list0, const struct DigitNode* list1 ) { // YOUR CODE HERE } // PURPOSE: To print the decimal number whose digits are pointed to by 'list'. // Note that the digits are IN LITTLE ENDIAN ORDER. No return value. void printList (const struct DigitNode* list ) { // YOUR CODE HERE } // PURPOSE: To print the nodes of 'list'. No return value. void freeList (struct DigitNode* list ) { // YOUR CODE HERE } // PURPOSE: To coordinate the running of the program. Ignores command line // arguments. Returns 'EXIT_SUCCESS' to OS. int main () { char numberText0[NUM_TEXT_LEN]; char numberText1[NUM_TEXT_LEN]; struct DigitNode* operand0List = NULL; struct DigitNode* operand1List = NULL; struct DigitNode* sumList = NULL; obtainPostiveInt(numberText0,NUM_TEXT_LEN,"first"); obtainPostiveInt(numberText1,NUM_TEXT_LEN,"second"); operand0List = numberList(numberText0); operand1List = numberList(numberText1); sumList = add(operand0List,operand1List); printList(operand0List); printf(" + "); printList(operand1List); printf(" = "); printList(sumList); printf(" "); freeList(sumList); freeList(operand1List); freeList(operand0List); return(EXIT_SUCCESS); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

1. Identify the sources for this conflict.

Answered: 1 week ago

Question

3. The group answers the questions.

Answered: 1 week ago