Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct integer {
- int* digits;
- int size;
- };
- //Preconditions: the first parameter is string that stores
- // only contains digits, doesn't start with
- // 0, and is 200 or fewer characters long.
- //Postconditions: The function will read the digits of the
- // large integer character by character,
- // convert them into integers and return a
- // pointer to the appropriate struct integer.
- struct integer* convert_integer(char* stringInt);
- //Preconditions: p is a pointer to a big integer.
- //Postconditions: The big integer pointed to by p is
- // printed out.
- void print(struct integer *p);
- //Preconditions: p and q are pointers to struct integers.
- //Postconditions: A new struct integer is created that
- // stores the sum of the integers pointed to
- // by p and q and a pointer to it is
- // returned.
- struct integer* add(struct integer *p, struct integer *q);
- //Preconditions: p and q are pointers to struct integers.
- //Postconditions: A new struct integer is created that
- // stores the absolute value of the
- // difference between the two and a pointer
- // to this is returned.
- struct integer* subtract(struct integer *p, struct integer *q);
- //Preconditions: Both parameters of the function are
- // pointers to struct integer.
- //Postconditions: The function compares the digits of two
- // numbers and returns:
- // -1 if the first number is smaller than the second,
- // 0 if the first number is equal to the second number,
- // 1 if the first number is greater than the second.
- int compare(struct integer *p, struct integer *q);
- FILE*out;
- main() {
- int numOperations = 0;
- int addOrSub = 0;
- char firstNum[201];
- char secondNum[201];
- int counter;
- int comparison;
- struct integer* operandOne;
- struct integer* operandTwo;
- struct integer* equationTotal;
- FILE*bigint;
- out = fopen ("out.txt", "w");//write
- bigint = fopen ("bigint.txt", "r");//read
- if (bigint == NULL) { printf("Error opening file"); }
- //read in the number of operations
- fscanf(bigint, "%d", &numOperations);
- for (counter=0; counter<numOperations; counter++){
- //read in first integer, then the rest of the numbers as chars
- fscanf(bigint, "%d %s %s", &addOrSub, firstNum, secondNum);
- //debug
- //printf("%s, %s\n", firstNum, secondNum);
- //debug
- //printf("%d, %s, %s\n", addOrSub, firstNum, operandTwo);
- operandOne = convert_integer(firstNum);
- operandTwo = convert_integer(secondNum);
- //debug, comparison works
- //int comparison = compare(operandOne, operandTwo);
- //printf("%d \n", comparison);
- //debug
- //print(operandOne);
- //print(operandTwo);
- if(addOrSub == 1){
- print(operandOne);
- fprintf(out, " + ");
- print(operandTwo);
- fprintf(out, " = ");
- equationTotal = add(operandOne, operandTwo);
- print(equationTotal);
- fprintf(out, "\n");
- //debug
- //print(equationTotal);
- //printf("%s + %s = %d \n", firstNum, secondNum, equationTotal);
- }
- if(addOrSub == 2){
- print(operandOne);
- fprintf(out, " - ");
- print(operandTwo);
- fprintf(out, " = ");
- equationTotal = subtract(operandOne, operandTwo);
- print(equationTotal);
- fprintf(out, "\n");
- //debug
- //print(equationTotal);
- //printf("%s + %s = %d", firstNum, secondNum, equationTotal);
- }
- }
- system("PAUSE");
- free(operandOne->digits);
- free(operandTwo->digits);
- free(equationTotal->digits);
- free(operandOne);
- free(operandTwo);
- free(equationTotal);
- fclose(out);
- fclose(bigint);
- return 0;
- }
- //Preconditions: the first parameter is string that stores
- // only contains digits, doesn't start with
- // 0, and is 200 or fewer characters long.
- //Postconditions: The function will read the digits of the
- // large integer character by character,
- // convert them into integers and return a
- // pointer to the appropriate struct integer.
- struct integer* convert_integer(char* stringInt){
- int counter = 0;
- int counter2 = 0;
- int stringLength = 0;
- struct integer *currentNumber;
- //THIS METHOD WORKS, DO NOT USE STRLEN
- while (stringInt[counter] != '\0'){
- stringLength++;
- counter++;
- }
- //debug
- //************
- //printf("%d \n", stringLength);
- //THIS DOES NOT WORK AND I DO NOT KNOW WHY, ONLY RETURNS GARBAGE.
- //stringLength = strlen(stringInt);
- //debug
- //printf("%d", stringLength);
- currentNumber = malloc(sizeof(struct integer));
- currentNumber->digits = malloc(sizeof(int)*stringLength + 1);
- currentNumber->size = stringLength;
- for (counter = stringLength - 1; counter >= 0; counter--){
- currentNumber->digits[counter2] = stringInt[counter] - '0';
- counter2++;
- }
- return currentNumber;
- }
- //Preconditions: p is a pointer to a big integer.
- //Postconditions: The big integer pointed to by p is
- // printed out.
- void print(struct integer *p){
- int counter;
- //backwards to make it readable
- for(counter = p->size - 1; counter >= 0; counter--){
- fprintf(out, "%d", p->digits[counter]);
- }
- return;
- }
- //Preconditions: p and q are pointers to struct integers.
- //Postconditions: A new struct integer is created that
- // stores the sum of the integers pointed to
- // by p and q and a pointer to it is
- // returned.
- struct integer* add(struct integer *p, struct integer *q){
- struct integer* sum;
- int carry = 0;
- int counter;
- sum = malloc(sizeof(struct integer));
- //old
- //sum->digits = malloc(sizeof(int)*p->size + q->size);
- if(q->size > p->size){
- sum->digits = malloc(sizeof(int)*p->size);
- sum->size = q->size+1;
- for(counter=0; counter < sum->size; counter++){
- sum->digits[counter] = 0;
- }
- for(counter=0; counter < q->size; counter++){
- //debug
- //sum->digits[counter] = (q->digits[counter] + p->digits[counter]);
- if(p->digits[counter] + q->digits[counter] >= 10){
- carry = 1;
- sum->digits[counter] = (p->digits[counter] + q->digits[counter] - 10);
- }
- else if(p->digits[counter] + q->digits[counter] < 10){
- sum->digits[counter] = (p->digits[counter] + q->digits[counter]);
- }
- //debug, not neccesary, program adds correctly.
- //printf("%d + %d = %d \n", p->digits[counter], q->digits[counter], sum->digits[counter]);
- //printf("carry = %d \n", carry);
- if(carry == 1){
- sum->digits[counter+1] += 1;
- }
- }
- for(counter = 0; counter < sum->size; counter++){
- if(sum->digits[counter] == 0){
- sum->size -= 1;
- }
- }
- //print(sum);
- }
- if(p->size >= q->size){
- sum->digits = malloc(sizeof(int)*p->size);
- sum->size = p->size+1;
- for(counter=0; counter < sum->size; counter++){
- sum->digits[counter] = 0;
- }
- for(counter=0; counter < p->size; counter++){
- //debug
- //sum->digits[counter] = (p->digits[counter] + q->digits[counter]);
- if(p->digits[counter] + q->digits[counter] >= 10){
- carry = 1;
- sum->digits[counter] = (p->digits[counter] + q->digits[counter] - 10);
- }
- else if(p->digits[counter] + q->digits[counter] < 10){
- sum->digits[counter] = (p->digits[counter] + q->digits[counter]);
- }
- //debug, not neccesary, program adds correctly.
- //printf("%d + %d = %d \n", p->digits[counter], q->digits[counter], sum->digits[counter]);
- //printf("carry = %d \n", carry);
- if(carry == 1){
- sum->digits[counter+1] += 1;
- }
- }
- for(counter = 0; counter < sum->size; counter++){
- if(sum->digits[counter] == 0){
- sum->size -= 1;
- }
- }
- //print(sum);
- }
- return sum;
- }
- //Preconditions: p and q are pointers to struct integers.
- //Postconditions: A new struct integer is created that
- // stores the absolute value of the
- // difference between the two and a pointer
- // to this is returned.
- struct integer* subtract(struct integer *p, struct integer *q){
- struct integer* difference;
- struct integer* numberSwap;
- int comparison;
- int counter;
- difference = malloc(sizeof(struct integer));
- numberSwap = malloc(sizeof(struct integer));
- comparison = compare(p, q);
- if(comparison == -1 || 0){
- numberSwap = p;
- p = q;
- q = numberSwap;
- comparison = 1;
- }
- if(comparison == 1){
- difference->digits = malloc(sizeof(int)*p->size);
- difference->size = p->size+1;
- for(counter=0; counter < difference->size; counter++){
- difference->digits[counter] = 0;
- }
- for(counter=0; counter < p->size; counter++){
- difference->digits[counter] = p->digits[counter] - q->digits[counter];
- }
- for(counter = 0; counter < difference->size; counter++){
- if(difference->digits[counter] == 0){
- difference->size -= 1;
- }
- }
- }
- //print(difference);
- return difference;
- }
- //Preconditions: Both parameters of the function are
- // pointers to struct integer.
- //Postconditions: The function compares the digits of two
- // numbers and returns:
- // -1 if the first number is smaller than the second,
- // 0 if the first number is equal to the second number,
- // 1 if the first number is greater than the second.
- int compare(struct integer *p, struct integer *q){
- int counter;
- if(p->size > q->size){
- return 1;
- }
- if(q->size > p->size){
- return -1;
- }
- if(p->size == q->size){
- for(counter = p->size - 1; counter >= 0; counter--){
- if(p->digits[counter] > q->digits[counter]){
- return 1;
- }
- if(q->digits[counter] > p->digits[counter]){
- return -1;
- }
- if(counter==0 && q->digits[counter] == p->digits[counter]){
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement