Program 1 solution - #include <stdio.h> #include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <stdio.h> #include <stdlib.h> #include <string.h> # struct integer { int* digits; int size; i int digit; struct integer *next; s }; } struct integer* add(struct integer* p, struct integer* q); struct integer* sub(struct integer* p, struct integer* q); void print(struct integer* number); void print_op(struct integer* op1, struct integer* op2,struct integer* answer,char op); struct integer* read_integer(char* word); int compare(struct integer *p, struct integer *q); void strip_zeros(struct integer* number); void free_list(struct integer* front); v int main() { i FILE *fin; char stringOp1[200], stringOp2[200]; struct integer *op1, *op2, *answer; int choice, i; fin = fopen("bigint.txt", "r"); // Only process if the file is opened. if (fin != NULL) { // Read in the number of operations. int numOps; fscanf(fin, "%d", &numOps); for (i=0; i<numOps; i++) { // Read in the type of operation. fscanf(fin, "%d", &choice); // Read in the two operands. fscanf(fin,"%s", stringOp1); op1 = read_integer(stringOp1); fscanf(fin,"%s", stringOp2); op2 = read_integer(stringOp2); // Take care of an addition. if (choice == 1) { answer = add(op1, op2); print_op(op1, op2, answer, '+'); } // Do subtraction. else if (choice == 2) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// This takes care of the answer. answer = sub(op1, op2); // Here we figure out which order to output the operands. // Note: This is redundant because of the work we did in answer. if (compare(op1, op2) >= 0 ) print_op(op1, op2, answer, '-'); else print_op(op2, op1, answer, '-'); } // This is subtle, but if we don't do this, then this memory ends up // not being freed and takes up space while our program is running. free(op1->digits); free(op1); free(op2->digits); free(op2); free(answer->digits); free(answer); } // end for } // Error message else printf("Sorry, bigint.txt was not found.\n"); system("PAUSE"); return 0; } // Pre-conditions: Both one and two are not NULL pointers that point to // linked lists structures that store non-negative digits
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Program 1 solution - #include &lt;stdio.h&gt; #include...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online