fork(2) download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. class punkt
  7. {
  8. public:
  9. void zapamietaj(char *nap, int a, int b);
  10. void wypisz();
  11. void zamien(punkt *ptr);
  12. void sortuj(punkt *ptr, int ile, punkt *pierw);
  13.  
  14. private:
  15. char nazwa[10];
  16. int x;
  17. int y;
  18. float przeciw;
  19. };
  20.  
  21.  
  22. int n; //Ile punktow
  23. int p; //Ile testow
  24.  
  25. int c, d;
  26. char nazwa[10];
  27.  
  28. int main() {
  29.  
  30. cin>>p;
  31. for(int m=0; m<p; m++)
  32. {
  33.  
  34. cin>>n;
  35.  
  36.  
  37. punkt *poczatek;
  38. punkt *tabpunk;
  39. tabpunk = new punkt[n];
  40. poczatek = tabpunk;
  41.  
  42. for(int i=0; i<n; i++)
  43. {
  44. cin>>nazwa>>c>>d;
  45. tabpunk->zapamietaj(nazwa, c, d);
  46. if(i<=n-2)
  47. {
  48. tabpunk ++;
  49. }
  50. }
  51.  
  52. tabpunk = poczatek;
  53. tabpunk->sortuj(tabpunk, n, poczatek);
  54. tabpunk = poczatek;
  55.  
  56. for(int i=0; i<n; i++)
  57. {
  58. tabpunk->wypisz();
  59. if(i<=n-2)
  60. {
  61. tabpunk ++;
  62. }
  63. }
  64. delete [] tabpunk;
  65. }
  66.  
  67.  
  68. return 0;
  69. }
  70.  
  71. void punkt::zapamietaj(char *nap, int a, int b)
  72. {
  73. x = a;
  74. y = b;
  75. strcpy(nazwa, (nap ? nap: "X"));
  76. przeciw = sqrt(pow(x,2)+pow(y,2));
  77. }
  78.  
  79. void punkt::zamien(punkt *ptr)
  80. {
  81. int temp1x;
  82. int temp1y;
  83. float temp1przecis;
  84. char temp1nap[10];
  85.  
  86. int temp2x;
  87. int temp2y;
  88. float temp2przecis;
  89. char temp2nap[10];
  90.  
  91. temp1x = ptr->x;
  92. temp1y = ptr->y;
  93. temp1przecis = ptr->przeciw;
  94. strcpy(temp1nap, (ptr->nazwa ? ptr->nazwa: "X"));
  95.  
  96. ptr++;
  97.  
  98. temp2x = ptr->x;
  99. temp2y = ptr->y;
  100. temp2przecis = ptr->przeciw;
  101. strcpy(temp2nap, (ptr->nazwa ? ptr->nazwa: "X"));
  102.  
  103. ptr->x = temp1x;
  104. ptr->y = temp1y;
  105. ptr->przeciw = temp1przecis;
  106. strcpy(ptr->nazwa, (temp1nap ? temp1nap: "X"));
  107.  
  108. ptr--;
  109.  
  110. ptr->x = temp2x;
  111. ptr->y = temp2y;
  112. ptr->przeciw = temp2przecis;
  113. strcpy(ptr->nazwa, (temp2nap ? temp2nap: "X"));
  114.  
  115. }
  116.  
  117. void punkt::sortuj(punkt *ptr, int ile, punkt *pierw)
  118. {
  119. float temp;
  120.  
  121. for(int i=0; i<ile; i++)
  122. {
  123. for(int j=0; j<(ile-1); j++)
  124. {
  125. temp = ptr->przeciw;
  126. ptr++;
  127. if(temp > ptr->przeciw)
  128. {
  129. ptr--;
  130. zamien(ptr);
  131. ptr++;
  132. }
  133. }
  134. ptr = pierw;
  135. }
  136. }
  137.  
  138. void punkt::wypisz()
  139. {
  140. cout<<nazwa<<' '<<x<<' '<<y<<endl;
  141. }
Runtime error #stdin #stdout #stderr 0s 4564KB
stdin
2
2
a 3 4
b 1 1
1
x 1 2
stdout
b 1 1
a 3 4
stderr
free(): invalid pointer