Advertisement
Schnuk

Untitled

Feb 23rd, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Fraction
  5. {
  6. int numerator;
  7. int denominator;
  8. Fraction* next;
  9.  
  10. Fraction(int numerator, int denominator, Fraction* next)
  11. {
  12. this->numerator = numerator;
  13. this->denominator = denominator;
  14. this->next = next;
  15. }
  16. };
  17.  
  18. void AddFractionToArray(Fraction fraction, int* array)
  19. {
  20.  
  21. }
  22.  
  23. int main()
  24. {
  25. Fraction last = Fraction(1, 1, NULL);
  26. Fraction first = Fraction(0, 1, &last);
  27. Fraction* array[100];
  28. array[0] = &first;
  29. array[1] = &last;
  30. for (int i = 2; i < 100; i++)
  31. array[i] = 0;
  32. int n;
  33. cin >> n;
  34. int newFractionPosition = 2;
  35. for (int i = 2; i <= n; i++)
  36. {
  37. Fraction* current = &first;
  38. while (current->next != NULL)
  39. {
  40. if (current->denominator + current->next->denominator == i)
  41. {
  42. int newFractionNumerator = current->numerator + current->next->numerator;
  43. int newFracTionDenominator = current->denominator + current->next->denominator;
  44. Fraction newFraction = Fraction(newFractionNumerator, newFracTionDenominator, current->next);
  45. current = &newFraction;
  46. array[newFractionPosition++] = &newFraction;
  47. }
  48. else
  49. current = current->next;
  50. }
  51. }
  52. Fraction* current = &first;
  53. while (current->next != NULL)
  54. {
  55. cout << current->numerator << " / " << current->denominator;
  56. current = current->next;
  57. }
  58.  
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement