Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- using namespace System;
- #define ALEATORIO(INI, FIN) rand() % (FIN - INI) + INI
- void crearVector(int* &v, short n) {
- v = new int[n];
- }
- void inicializarVector(int* v, short n) {
- for (short i = 0; i < n; ++i) {
- v[i] = ALEATORIO(0, 100);
- }
- }
- void mostrarVector(int* v, short n) {
- cout << "Mostrando el vector" << endl;
- for (short i = 0; i < n; ++i) {
- cout << v[i] << " - ";
- }
- cout << endl;
- system("pause>0");
- }
- //vector:
- //pop_back(); equivalente a "eliminarElemento(...)"
- //back(); obtiene el último elemento
- void eliminarElemento(int* &v, short &n) {
- if (n > 1) {
- //Creo el nuevo vector deseado
- int* aux = new int[n - 1];
- //Copio todos los elementos a rescatar
- for (short i = 0; i < n - 1; i++)
- aux[i] = v[i];
- //Elimino el antiguo vector
- delete[] v;
- //Actualizo la referencia de mi puntero al nuevo vector
- v = aux;
- //Actualizo el tamaño del vector
- n--;
- }
- else if (n == 1) {
- delete[] v;
- v = NULL;
- n--;
- }
- }
- //vector:
- //push_back(); equivalente a "agregarElemento(...)"
- void agregarElemento(int* &v, short &n, int nuevoValor) {
- //Creo un vector del tamaño deseado
- int* aux = new int[n+1];
- //Copio todos los valores del vector antiguo
- for (short i = 0; i < n; ++i)
- aux[i] = v[i];
- //Inserto el nuevo valor
- aux[n] = nuevoValor;
- //Elimino el vector antiguo
- delete[] v;
- //Actualizo la referencia del puntero al nuevo vector
- v = aux;
- //Actualizo n
- n++;
- }
- int main() {
- srand(time(NULL));
- int* v;
- short n;
- cout << "Ingrese n: "; cin >> n;
- crearVector(v, n);
- inicializarVector(v, n);
- mostrarVector(v, n);
- eliminarElemento(v, n);
- mostrarVector(v, n);
- agregarElemento(v, n, 5);
- mostrarVector(v, n);
- agregarElemento(v, n, 8);
- mostrarVector(v, n);
- agregarElemento(v, n, 10);
- mostrarVector(v, n);
- delete[] v;
- return 0;
- }
- -----------------------------------------------------------------
- #include <iostream>
- #include <ctime>
- #include <vector>
- using namespace std;
- using namespace System;
- #define ALEATORIO(INI, FIN) rand() % (FIN - INI) + INI
- void inicializarVector(vector<int> &v, short n) {
- for (short i = 0; i < n; i++)
- v.push_back(ALEATORIO(1,100));
- }
- void mostrarVector(vector<int> &v) {
- cout << "Mostrando el vector" << endl;
- for(int x : v)
- cout << x << " - ";
- cout << endl;
- system("pause>0");
- /*cout << "Mostrando el vector" << endl;
- for (short i = 0; i < v.size(); ++i) {
- cout << v[i] << " - ";
- }
- cout << endl;
- system("pause>0");*/
- }
- int main() {
- srand(time(NULL));
- short n;
- cout << "Ingrese n: "; cin >> n;
- vector<int> v;
- inicializarVector(v, n);
- mostrarVector(v);
- v.pop_back();
- mostrarVector(v);
- v.push_back(5);
- mostrarVector(v);
- v.push_back(8);
- mostrarVector(v);
- v.push_back(19);
- mostrarVector(v);
- return 0;
- }
- /********************************************************/
- #include <iostream>
- #include <ctime>
- #include <vector>
- #include <conio.h>
- using namespace std;
- using namespace System;
- #define ALEATORIO(INI, FIN) rand() % (FIN - INI) + INI
- struct Piedra {
- short _x, _y;
- Piedra(short x = 10, short y = 5) {
- _x = x;
- _y = y;
- }
- ~Piedra() {
- Console::SetCursorPosition(_x, _y);
- cout << " ";
- }
- void animar() {
- //Borra
- Console::SetCursorPosition(_x, _y);
- cout << " ";
- //Avanza
- if (_x > 0) _x--;
- //Dibuja
- Console::SetCursorPosition(_x, _y);
- cout << "o";
- }
- };
- int main() {
- srand(time(NULL));
- vector<Piedra*> piedras;
- while (true) {
- if (kbhit()) {
- if(getch()==32)
- piedras.push_back(new Piedra(ALEATORIO(6,15), ALEATORIO(0, 8)));
- }
- for (short i = 0; i < piedras.size(); ++i) {
- piedras[i]->animar();
- if (piedras[i]->_x == 0) {
- delete piedras[i];
- piedras.erase(piedras.begin() + i);
- --i;
- }
- }
- _sleep(100);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement