Alright, i'm going to try to explain this assingment the best that i can... we have to write a program that will scan in #'s of large integers into a link list and then make it so the user can add or subtract those numbers with another set of numbers in a link list... I'm gettin errors in the addition when it adds for some reason it isn't adding correctly.. the last number it prints twice.. and if one # is 1234 and the other is 12 it will give me 46 only the last 2 numbers will be added... i tried adding leading zeros a while ago to fix that error but it never worked out correctly... there are flaws all throughout this program and i was going to do it without asking for help but it's due in a day and i have no clue how to fix it. Any help that you can provide would be great. Thanks -John
here is the integer.h file...
Code:
typedef struct integerNode{
int integer;
struct integerNode *next;
};
typedef struct integerNode1{
int integer1;
struct integerNode1 *next;
};
typedef struct integerNode2{
int integer2;
struct integerNode2 *next;
};
int AddTwoIntegers(int, int);
int ReadInt();
void PrintInteger(int);
int compare(int, int);
int SubtractIntegers(int, int);
int RecycleInteger(int);
Here's the main program's code...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "integer.h"
#ifndef _bigint_h
#define _bigint_h
int x;
int n1;
int n2;
int n;
int cnt;
int cnt1;
int cnt2;
int myint;
int sum;
int rollover;
int flag;
int result;
int temp;
struct integerNode *head = NULL;
struct integerNode *curr;
struct integerNode1 *head1 = NULL;
struct integerNode1 *curr1;
struct integerNode2 *head2 = NULL;
struct integerNode2 *curr2;
void main()
{
printf("\n\n1. Add\n2. Subtraction\n3. Quit\n\nEnter Choice: ");
scanf("%d", &x);
switch(x){
case 1:
myint = ReadInt();
cnt = 0;
for(x=0;x < myint; x++){
n1 = curr -> integer;
n2 = curr1 -> integer1;
result = AddTwoIntegers(n1, n2);
}
n = result;
PrintInteger(n);
(int)head = RecycleInteger((int)head);
main();
case 2:
myint = ReadInt();
for(x=0;x < myint; x++){
n1 = curr -> integer;
n2 = curr1 -> integer1;
flag = compare(n1, n2);
temp=flag+temp;
}
if (temp<0){
for(x=0;x < myint; x++){
n1 = curr -> integer;
n2 = curr1 -> integer1;
result = SubtractIntegers(n2, n1);
}}
else {
for(x=0;x < myint; x++){
n1 = curr -> integer;
n2 = curr1 -> integer1;
result = SubtractIntegers(n1, n2);
}
PrintInteger(n);
(int)head = RecycleInteger((int)head);
main();
case 3:
exit (0);
default:
exit (0);
}
}
}
int ReadInt()
{
cnt1=0;
while(myint = getchar()){
if (isdigit(myint) == 0){break;}}
printf("Enter the first number: ");
while(myint = getchar()){
if (isdigit(myint) == 0){break;}
curr = malloc(sizeof(struct integerNode));
curr -> integer = myint-48;
curr -> next = head;
head = curr;
cnt1++;
}
cnt2=0;
printf("Enter the second number: ");
while(myint = getchar()){
if (isdigit(myint) == 0){break;}
curr1 = malloc(sizeof(struct integerNode1));
curr1 -> integer1 = myint-48;
curr1 -> next = head1;
head1 = curr1;
cnt2++;
}
if(cnt1<cnt2){return cnt1;}
if(cnt2<cnt1){return cnt2;}
else return cnt1;
}
int AddTwoIntegers(n1, n2)
{
n1 = n1 + rollover;
sum = n1 + n2;
rollover = 0;
if (sum>9){sum = sum - 10; rollover = 1;}
curr2 = malloc(sizeof(struct integerNode2));
curr2 -> integer2 = sum;
curr2 -> next = head2;
head2 = curr2;
cnt2++;
curr1 = head1;
head1 = head1 -> next;
curr = head;
head = head -> next;
return cnt2;
}
void PrintInteger(n)
{
int i;
for(i=0;i<n-3;i++){
printf("%d", curr2->integer2);
curr2 = curr2 -> next;
}
}
int compare(n1, n2)
{
if(n1==n2){return 0;}
if(n1>n2){return 1;}
if(n2>n1){return -1;}
return 0;
}
int SubtractIntegers(n1, n2)
{
sum = n1 - n2;
curr2 = malloc(sizeof(struct integerNode2));
curr2 -> integer2 = sum;
curr2 -> next = head2;
head2 = curr2;
cnt2++;
curr1 = head1;
head1 = head1 -> next;
curr = head;
head = head -> next;
return 0;
}
int RecycleInteger(n)
{
while(head != NULL)
{
RecycleInteger((int)head->next);
free(head);
}
return 0;
}
#endif