fork download
  1. //
  2. // Assignment 2 Task 4 template
  3. // Copyright © 2019 HKU ENGG1340. All rights reserved.
  4. //
  5.  
  6. #include <stdio.h>
  7.  
  8. const int MAX_INT_LENGTH = 100;
  9.  
  10. ///////// DO NOT MODIFY ANYTHING ABOVE THIS LINE /////////
  11.  
  12. // IMPORTANT: Do NOT change any of the function headers already provided to you
  13. // It means that you will need to use the function headers as is
  14.  
  15.  
  16. // You may implement additional functions here
  17. void printArray(char arr[], int size)
  18. {
  19. int i;
  20. for (i=0; i < size; i++)
  21. printf("%c ", arr[i]);
  22.  
  23. printf("\n");
  24. }
  25.  
  26. void rvereseArray(char arr[], int start, int end)
  27. {
  28. char temp;
  29. while (start < end)
  30. {
  31. temp = arr[start];
  32. arr[start] = arr[end];
  33. arr[end] = temp;
  34. start++;
  35. end--;
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42. //
  43. // AddTwoBigNumbers: to sum up two big numbers represented as digits in a char clear_char_array
  44. //
  45. // input: char bigN[], char bigM[]: two large numbers
  46. // output the sum as a big number in the input array char sum[]
  47. void AddTwoBigNumbers(char bigN[], char bigM[], char sum[])
  48. {
  49. rvereseArray(bigN,0,100);
  50. rvereseArray(bigM,0,100);
  51.  
  52. //calculation
  53. int carry =0;
  54. for(int i=100;i>=0;i--)
  55. {
  56. int sum_two = ((bigN[i]-'0') + (bigM[i]-'0') + carry);
  57. sum[i]=(sum_two%10 + '0');
  58. carry = sum_two/10;
  59. }
  60. rvereseArray(sum,0,100);
  61. }
  62.  
  63.  
  64. ///////// DO NOT MODIFY ANYTHING BELOW THIS LINE /////////
  65.  
  66. // Function: main function
  67. // ==============================================================
  68. int main()
  69. {
  70. char bignum[2][MAX_INT_LENGTH]; // bignum[0] and bignum[1] are to store the digits of the two input number
  71. char sum[MAX_INT_LENGTH+1]; // to store the sum of the two big numbers
  72.  
  73. // read in two numbers
  74. scanf("%s", bignum[0]);
  75. scanf("%s", bignum[1]);
  76. // calculate sum of the two numbers
  77. AddTwoBigNumbers(bignum[0], bignum[1], sum);
  78.  
  79. // display the sum on screen
  80. printf("%s\n", sum);
  81.  
  82.  
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0s 9424KB
stdin
9
9999
stdout
+,-//***-/+--)'.*-,/))))*-+-*-.*('/+,2..*7-+()*..*),09-.1/.--)/*,0*-'90(/5'/-)/*))))****,1'*+10(,*)+++