hello! I have tried to do a big integer calculator. as you can see from my code, I'm not an expert... (this is made to unix).
I've made so far addition algorithm and it works fine for positive integers BUT how to implement subtraction algorithm?
I take the numbers from console as strings and handle them as strings as well until the calculations are done with integer values and stored to integer type array.
I didn't want to put the whole source code here, but here is the function which makes addition. Before this function is called, the strings (numbers) are reversed vice versa. Is this algorithm insane? How can I handle negative numbers as well and how to make subtraction algorithm.
insertCode:void add(int argc,char *argv[]){ int c1=0,c2=0,c3=0,i=0,ii=0,k=0,carry=0; unsigned int result[1000],luku1[1000],luku2[1000]; c1=strlen(argv[1]); c2=strlen(argv[2]); //check which number is longer if(c1<c2){ c3=c2; } else if(c1>c2){ c3=c1; } else{ c3=c1; } //store strings to int tables as ints for(i=0;i<c1;i++){ luku1[i]=argv[1][i]-48; } for(ii=0;ii<c2;ii++){ luku2[ii]=argv[2][ii]-48; } //addition algorithm for(k=0;k<=c3;k++){ result[k]=luku1[k]+luku2[k]+carry; if(result[k]>=10){ result[k]=result[k]-10; carry=1; }//if else{ result[k]=result[k]; carry=0; }//else }//for //if the last addition result was <10, don't print 0 if(result[c3]==0){ c3=c3-1; } //result for(k=c3;k>=0;k--){ printf("%d",result[k]); } printf("\n"); }//add



LinkBack URL
About LinkBacks


