Purpose of this program take base and two number then multiply that numbers in that base.I used Dev-c Shed compiler for gcc.It takes the numbers and base from text in the attachment.Please help....It doesnt have syntax error but it has logic error...
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
FILE *fin, *fout;
typedef struct dgt{
short int number;
struct dgt *next;
}digit;
digit* convert(int base, digit* number);//Converting numbers from their base to decimal
digit* getNumber();//Taking Multiplier and Multiplicand to put Linked List
digit* multiply(digit* n1, digit* n2, int base);//Mulyiply numbers in their base
void add(digit* n1, digit* n2, int base);//Used for add numbers after the multiplying and converting
digit* createNode(short int number);//Create node to use in the Linked List
int getBase();//Take base from text
void freeLL(digit* LL); //Delete empty LL after using
void correctNumber(digit* number,int base);//After multiplying edit numbers to their base
int main(){
digit *n1base, *n2base, *n1dec, *n2dec, *resultBase, *resultDec;
int base;
if((fin = fopen("input.txt","r"))==NULL){
fprintf(stderr,"Cannot open file\n");
exit(1);
}
if((fout = fopen("output.txt","w"))==NULL){
fprintf(stderr,"Cannot create file\n");
exit(1);
}
while(!feof(fin)){
base = getBase();
getc(fin);
n1base = getNumber();
n2base = getNumber();
// n1dec = convert(base, n1base);
// n2dec = convert(base, n2base);
resultBase = multiply(n1base, n2base,base);
resultDec = multiply(n1dec, n2dec,base);
}
fclose(fin);
fclose(fout);
}
int getBase(){
int base;
fscanf(fin,"%d",&base);
if( base<2 || base>10 ){
fprintf(stderr,"\nThe base should be between 2 and 10\nYou entered %d\n",base);
exit(1);
}
return base;
}
digit* convert(int base, digit* number){
digit* num=number,*temp;
digit* current=createNode(0);
digit* basePtr=createNode(base);
digit* baseMul=createNode(1);
digit* decimal,*pr;
decimal=createNode(0);
while(num!=NULL){
current->number=num->number;
pr=multiply(current,baseMul,10);
add(decimal,pr,10);
num=num->next;
temp=multiply(baseMul, basePtr, 10);
freeLL(baseMul);
baseMul=temp;
}
return decimal;
}
digit* getNumber(int base){
digit* number=NULL, *tempNum=NULL;
char c;
char c2[2]="";
short int temp=0;
c=8;
while(c!=10){
c=getc(fin);
if(c==10||c==EOF) return number;
if(!isdigit(c)){
fprintf(stderr,"\nOnly digits allowed.\nThere is a(n) %c in your file",c);
exit(1);
}
c2[0]=c;
temp=atoi(c2);
if(temp>=base){
fprintf(stderr,"\nYour base is smaller than one of your inputs:%d\n",temp);
exit(1);
}
number=createNode(temp);
number->next=tempNum;
tempNum=number;
// printf("%c",c);
}
puts("\n");
return number;
}
digit* multiply(digit* number1, digit* number2, int base){
digit *pr1, *pr2,*pr3, *result, *n1=number1, *n2=number2;
int i=0, la,lb,len;
la=getLength(number1);
lb=getLength(number2);
len=la>lb ? la : lb;
len++;
for(i;i<len;i++){
pr3=createNode(0);
pr3->next=pr2;
pr2=pr3;
}
for(i=0;i<len;i++){
result=createNode(0);
result->next=pr1;
pr1=result;
}
while(n2!=NULL){
while(n1!=NULL){
pr2->number=n2->number*n1->number;//printf("\npr2=%d\n",pr2->number);
pr2=pr2->next;
n1=n1->next;
}
n1=number1;
correctNumber(pr3,base);
add(pr1,pr3,base);
pr1=pr1->next;
n2=n2->next;
}
return result;
}
digit* createNode(short int number){
digit* numPtr = (digit*)malloc(sizeof(digit));
numPtr->number = number;
numPtr->next=NULL;
return numPtr;
}
void add(digit* number1, digit* number2, int base){
int i=0;
short int a,b;
int len,la,lb;
digit* result=NULL,*temp=NULL;
digit* n1=number1, *n2=number2;
la=getLength(number1);
lb=getLength(number2);
len=1+ la>lb ? la : lb;
for(i;i<len;i++){
result=createNode(0);
result->next=temp;
temp=result;
}
while(number1!=NULL || number2!=NULL){
a= (number1==NULL) ? 0 : number1->number;
b= (number2==NULL) ? 0 : number2->number;
temp->number+=(a+b);
temp=temp->next;
n1=n1->next;
n2=n2->next;
}
correctNumber(result, base);
freeLL(number1);
freeLL(number2);
number1=result;
}
int getLength(digit* num){
digit *count=num;
int i=1;
while(count->next!=NULL){
i++;
count=count->next;
}
return i;
}
void freeLL(digit* LL){
digit* del=LL;
while(LL!=NULL){
del=del->next;
free(LL);
LL=del;
}
}
void correctNumber(digit* number, int base){
puts("correct started");
digit* ptr=number;
int c,r;
while(ptr!=NULL){
c=ptr->number/base;
r=ptr->number%base;
printf("\nnum=%d\n",r);
ptr->number=r;
if(ptr->next==NULL && c!= 0){printf("xyz");
ptr->next=createNode(c);
}
else if(ptr->next!=NULL) {(ptr->next)->number+=c; printf("\nyuh sana");}
ptr=ptr->next;
}
}