Advertisement
LA77

Untitled

Sep 23rd, 2022 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /// Global variables that are used in multiple functions in the code
  4. int n, d;
  5. int frq[100005], mem[100005], adp[100005];
  6.  
  7. /// Setting up the array "mem";
  8. void Set_Up()
  9. {
  10. for(int i = 1; i <= 100004; ++ i) mem[i] = -1;
  11. }
  12.  
  13. /*
  14. This function is meant to help us simulate how division is being done manually.
  15. It will be used both in the "Calculate_Before_Decimal_Point" and "Calculate_After_Decimal_Point".
  16. */
  17. int calc(int step)
  18. {
  19. int c = 0;
  20. while(n >= d)
  21. {
  22. n-=d;
  23. c++;
  24. }
  25. if(step == 1) frq[n]++;
  26. return c;
  27. }
  28.  
  29. /*
  30. Calculating the digits before the decimal point is trivial, we just have to simulate how manual division works, which was already done with the function above.
  31. All that remains is to call the function.
  32. */
  33. void Calculate_Before_Decimal_Point()
  34. {
  35. printf("%d", calc(0));
  36. }
  37.  
  38. /*
  39. By far, this is the most complex function of the entire code. First, it makes use of the function "calc" to simulate the manual division.
  40. The array adp is used to store the necessary information to give a proper output while the array "mem" is used to remember important information about calculations.
  41. */
  42. void Calculate_After_Decimal_Point()
  43. {
  44. int flag = 0, idx = 1, cnt = 0, cc = 0;
  45. while(n != 0)
  46. {
  47. if(n >= d)
  48. {
  49. int c = calc(1);
  50. if(frq[n] > 1)
  51. {
  52. flag = 1;
  53. break;
  54. }
  55. mem[idx] = n;
  56. adp[idx++] = c;
  57. cnt = 0;
  58. }
  59. else
  60. {
  61. n *= 10;
  62. cnt++;
  63. if(cnt > 1) adp[idx++] = 0;
  64. }
  65. }
  66.  
  67. // Printing the array while making sure that the mathematical notation is used correctly
  68. printf(".");
  69. for(int i = 1; i < idx; ++ i)
  70. {
  71. if(mem[i] == n)
  72. {
  73. cc++;
  74. if(cc == 2) break;
  75. if(flag == 1) printf("[");
  76. }
  77. printf("%d", adp[i]);
  78. }
  79. if(flag > 0)
  80. printf("]");
  81. }
  82.  
  83. int main()
  84. {
  85. /// Setting up one of the arrays for future use
  86. Set_Up();
  87.  
  88. /// Reading the necessary input
  89. scanf("%d %d", &n, &d);
  90.  
  91. /// Printing out the mathematical symbols
  92. printf("%d", n);
  93. printf("/");
  94. printf("%d", d);
  95. printf(" = ");
  96.  
  97. /// Calculating the digits are before the decimal point
  98. Calculate_Before_Decimal_Point();
  99.  
  100. /// Calculating the digits that are after the decimal point (if there are any at all)
  101. if(n) Calculate_After_Decimal_Point();
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement