fork(2) download
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #define MAX 10000
  6.  
  7. char * addition(char a[],char b[]){
  8.  
  9. static char add[MAX];
  10. char c[MAX];
  11. int temp;
  12. int la,lb;
  13. int i,j,k=0,x=0,y;
  14. long int car=0;
  15. long sum = 0;
  16. la=strlen(a)-1;
  17. lb=strlen(b)-1;
  18. //printf("\n la=%d lb=%d ",la,lb);
  19. for(i=0;i<=la;i++){ a[i] = a[i] - 48; }
  20. for(i=0;i<=lb;i++){ b[i] = b[i] - 48; }
  21.  
  22. if(la>lb){
  23. y=0;
  24. k=la;
  25.  
  26. }
  27. else{
  28. k=lb;
  29. y=1;
  30. }
  31. //printf("\nAddition called ");
  32. for(i=la,j=lb;i>=0,j>=0;i--,j--){
  33. //printf("Addition called ");
  34. c[k--]=(car+a[i]+b[j])%10;
  35. car=(car+a[i]+b[j])/10;
  36. }//for
  37. printf("\nWithout carry %s",c);
  38.  
  39. if(y=0){
  40. j=0;
  41. for(i=0;i<la+1;i++){
  42. add[j++]=c[i] + 48;
  43. }
  44. add[j]='\0';
  45. }
  46. else{
  47. j=0;
  48. for(i=0;i<lb+1;i++){
  49. add[j++]=c[i] + 48;
  50. }
  51. add[j]='\0';
  52. }
  53. return add;
  54.  
  55. }
  56.  
  57.  
  58. char * multiply(char a[],char b[]){
  59. static char mul[MAX];
  60. char c[MAX];
  61. char temp[MAX];
  62. int la,lb;
  63. int i,j,k=0,x=0,y;
  64. long int r=0;
  65. long sum = 0;
  66. la=strlen(a)-1;
  67. lb=strlen(b)-1;
  68.  
  69. for(i=0;i<=la;i++){
  70. a[i] = a[i] - 48;
  71. }
  72.  
  73. for(i=0;i<=lb;i++){
  74. b[i] = b[i] - 48;
  75. }
  76.  
  77. for(i=lb;i>=0;i--){
  78. r=0;
  79. for(j=la;j>=0;j--){
  80. temp[k++] = (b[i]*a[j] + r)%10;
  81. r = (b[i]*a[j]+r)/10;
  82. }
  83. temp[k++] = r;
  84. x++;
  85. for(y = 0;y<x;y++){
  86. temp[k++] = 0;
  87. }
  88. }
  89.  
  90. k=0;
  91. r=0;
  92. for(i=0;i<la+lb+2;i++){
  93. sum =0;
  94. y=0;
  95. for(j=1;j<=lb+1;j++){
  96. if(i <= la+j){
  97. sum = sum + temp[y+i];
  98. }
  99. y += j + la + 1;
  100. }
  101. c[k++] = (sum+r) %10;
  102. r = (sum+r)/10;
  103. }
  104. c[k] = r;
  105. j=0;
  106. for(i=k-1;i>=0;i--){
  107. mul[j++]=c[i] + 48;
  108. }
  109. mul[j]='\0';
  110. return mul;
  111. }
  112.  
  113. int main(){
  114. char a[MAX]={'9','5','5','5','5' };
  115. char b[MAX]={'3','3','3','3','3','3','3','3','3','3'};
  116. char *c;
  117. int la,lb;
  118. //int i = 0;
  119. int i = 1;
  120. //int i = 2;
  121.  
  122. printf("Answer of two numbers : ");
  123. switch(i){
  124. case 0: c = multiply(a,b); break;
  125. case 1: c = addition(a,b); break;
  126. //case 2: c = sub(a,b); break;
  127. default: break;
  128. }
  129. //c = multiply(a,b);
  130. printf("%s",c);
  131. return 0;
  132. }
  133.  
  134.  
Success #stdin #stdout 0s 2264KB
stdin
Standard input is empty
stdout
Answer of two numbers : 
Without carry 3333428888