Advertisement
Guest User

Clase 12/11/19

a guest
Nov 12th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. using namespace System;
  5.  
  6. #define ALEATORIO(INI, FIN) rand() % (FIN - INI) + INI
  7.  
  8. void crearVector(int* &v, short n) {
  9. v = new int[n];
  10. }
  11. void inicializarVector(int* v, short n) {
  12. for (short i = 0; i < n; ++i) {
  13. v[i] = ALEATORIO(0, 100);
  14. }
  15. }
  16. void mostrarVector(int* v, short n) {
  17. cout << "Mostrando el vector" << endl;
  18. for (short i = 0; i < n; ++i) {
  19. cout << v[i] << " - ";
  20. }
  21. cout << endl;
  22. system("pause>0");
  23. }
  24. //vector:
  25. //pop_back(); equivalente a "eliminarElemento(...)"
  26. //back(); obtiene el último elemento
  27. void eliminarElemento(int* &v, short &n) {
  28. if (n > 1) {
  29. //Creo el nuevo vector deseado
  30. int* aux = new int[n - 1];
  31. //Copio todos los elementos a rescatar
  32. for (short i = 0; i < n - 1; i++)
  33. aux[i] = v[i];
  34. //Elimino el antiguo vector
  35. delete[] v;
  36. //Actualizo la referencia de mi puntero al nuevo vector
  37. v = aux;
  38. //Actualizo el tamaño del vector
  39. n--;
  40. }
  41. else if (n == 1) {
  42. delete[] v;
  43. v = NULL;
  44. n--;
  45. }
  46. }
  47. //vector:
  48. //push_back(); equivalente a "agregarElemento(...)"
  49. void agregarElemento(int* &v, short &n, int nuevoValor) {
  50. //Creo un vector del tamaño deseado
  51. int* aux = new int[n+1];
  52. //Copio todos los valores del vector antiguo
  53. for (short i = 0; i < n; ++i)
  54. aux[i] = v[i];
  55. //Inserto el nuevo valor
  56. aux[n] = nuevoValor;
  57. //Elimino el vector antiguo
  58. delete[] v;
  59. //Actualizo la referencia del puntero al nuevo vector
  60. v = aux;
  61. //Actualizo n
  62. n++;
  63. }
  64. int main() {
  65. srand(time(NULL));
  66. int* v;
  67. short n;
  68. cout << "Ingrese n: "; cin >> n;
  69. crearVector(v, n);
  70. inicializarVector(v, n);
  71. mostrarVector(v, n);
  72. eliminarElemento(v, n);
  73. mostrarVector(v, n);
  74. agregarElemento(v, n, 5);
  75. mostrarVector(v, n);
  76. agregarElemento(v, n, 8);
  77. mostrarVector(v, n);
  78. agregarElemento(v, n, 10);
  79. mostrarVector(v, n);
  80. delete[] v;
  81. return 0;
  82. }
  83.  
  84. -----------------------------------------------------------------
  85.  
  86. #include <iostream>
  87. #include <ctime>
  88. #include <vector>
  89. using namespace std;
  90. using namespace System;
  91.  
  92. #define ALEATORIO(INI, FIN) rand() % (FIN - INI) + INI
  93.  
  94. void inicializarVector(vector<int> &v, short n) {
  95. for (short i = 0; i < n; i++)
  96. v.push_back(ALEATORIO(1,100));
  97. }
  98. void mostrarVector(vector<int> &v) {
  99. cout << "Mostrando el vector" << endl;
  100. for(int x : v)
  101. cout << x << " - ";
  102. cout << endl;
  103. system("pause>0");
  104. /*cout << "Mostrando el vector" << endl;
  105. for (short i = 0; i < v.size(); ++i) {
  106. cout << v[i] << " - ";
  107. }
  108. cout << endl;
  109. system("pause>0");*/
  110. }
  111. int main() {
  112. srand(time(NULL));
  113. short n;
  114. cout << "Ingrese n: "; cin >> n;
  115. vector<int> v;
  116. inicializarVector(v, n);
  117. mostrarVector(v);
  118. v.pop_back();
  119. mostrarVector(v);
  120. v.push_back(5);
  121. mostrarVector(v);
  122. v.push_back(8);
  123. mostrarVector(v);
  124. v.push_back(19);
  125. mostrarVector(v);
  126. return 0;
  127. }
  128.  
  129. /********************************************************/
  130.  
  131. #include <iostream>
  132. #include <ctime>
  133. #include <vector>
  134. #include <conio.h>
  135. using namespace std;
  136. using namespace System;
  137.  
  138. #define ALEATORIO(INI, FIN) rand() % (FIN - INI) + INI
  139.  
  140. struct Piedra {
  141. short _x, _y;
  142. Piedra(short x = 10, short y = 5) {
  143. _x = x;
  144. _y = y;
  145. }
  146. ~Piedra() {
  147. Console::SetCursorPosition(_x, _y);
  148. cout << " ";
  149. }
  150. void animar() {
  151. //Borra
  152. Console::SetCursorPosition(_x, _y);
  153. cout << " ";
  154. //Avanza
  155. if (_x > 0) _x--;
  156. //Dibuja
  157. Console::SetCursorPosition(_x, _y);
  158. cout << "o";
  159. }
  160. };
  161.  
  162. int main() {
  163. srand(time(NULL));
  164. vector<Piedra*> piedras;
  165. while (true) {
  166. if (kbhit()) {
  167. if(getch()==32)
  168. piedras.push_back(new Piedra(ALEATORIO(6,15), ALEATORIO(0, 8)));
  169. }
  170. for (short i = 0; i < piedras.size(); ++i) {
  171. piedras[i]->animar();
  172. if (piedras[i]->_x == 0) {
  173. delete piedras[i];
  174. piedras.erase(piedras.begin() + i);
  175. --i;
  176. }
  177. }
  178. _sleep(100);
  179. }
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement