Hola Leo Gutiérrez, gracias por responder. También pensé en utilizar listas enlazadas, pero son más lentas, ya que son de acceso secuencial.
Saludos.
Saludos.
Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes Menú
#include <iostream>
#include "stringlist.h"
using namespace std;
const int CHAR_MAX_LENGTH = 20;
int showmenu()
{
int option = 0;
do
{
cout << "1.- Añadir string" << endl;
cout << "2.- Mostrar strings" << endl;
cout << "3.- Salir" << endl;
cout << "Introduce una opcion: ";
cin >> option;
}while(option < 1 && option > 3);
return option;
}
int main()
{
char buff[CHAR_MAX_LENGTH];
int option = 0;
TStringList *List = new TStringList();
do
{
option = showmenu();
switch(option)
{
case 1: cout << "Introduce una cadena: ";
cout.flush();
cin >> buff;
List->Add(buff);
break;
case 2: for (int i = 0; i < List->GetCount(); i++)
cout << "Item " << i + 1 << ": " << List->GetItem(i) << endl;
break;
}
}while(option != 3);
delete(List);
return 0;
}
#ifndef _H_STRINGLIST
#define _H_STRINGLIST
struct SL_ITEM{
char *string;
};
class TStringList{
private:
SL_ITEM *Items;
long numItems;
public:
TStringList();
~TStringList();
void Add(char *cadena);
long GetCount();
char * GetItem(long Index);
};
#endif
#include "stringlist.h"
#include <memory>
#include <malloc.h>
#include <string.h>
TStringList::TStringList()
{
numItems = 0;
}
TStringList::~TStringList()
{
if (numItems > 0)
{
for (int i = 0; i < numItems; i++)
Items[i].string = NULL;
Items = NULL;
/*
Aquí también tengo alguna duda.
Probé con delete [] Items; pero compilado en Linux me suelta un stack error, en Windows, simplemente se cierra la aplicación :huh:
*/
}
}
void TStringList::Add(char *cadena)
{
if (numItems > 0){
SL_ITEM * NewItems = (SL_ITEM*) malloc((numItems + 1) * sizeof(SL_ITEM));
/*for (int i = 0; i < numItems; i++)
{
NewItems[i].string = (char *) malloc(strlen(Items[i].string) + 1);
strcpy(NewItems[i].string, Items[i].string);
}*/
memcpy(NewItems, Items, sizeof(*Items));
memcpy(Items, NewItems, sizeof(*NewItems));
NewItems = NULL; //Creo que no hace falta
}
else Items = (SL_ITEM*) malloc(sizeof(SL_ITEM));
Items[numItems].string = (char *) malloc(strlen(cadena) + 1);
strcpy(Items[numItems].string, cadena);
numItems++;
}
long TStringList::GetCount()
{
return numItems;
}
char * TStringList::GetItem(long Index)
{
if (Index < numItems && Index >= 0)
return Items[Index].string;
else return NULL;
}
Cita de: Ferno en 31 Diciembre 2010, 03:07 AM
Me supongo que en la condición, en vez de ":=" iría un "=". verdad?
if a[i] := p[j] then begin //sería =
until (j > M) or (i > N);
Cita de: XXX-ZERO-XXX en 6 Diciembre 2010, 22:58 PM
y sin el 0 tamb xD porfavor.. esq creo q al combinarlos es q da el error :S
Lista[i] = Rnd.Next(8);
Lista[i] = Rnd.Next(1, 8);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static int[] Lista = new int[7];
public static bool NumExists(int Num, int MaxPos)
{
bool resultado = false;
for (int i = 0; i < MaxPos; i++)
if (Lista[i] == Num)
{
resultado = true;
break;
}
return resultado;
}
public static void RandomNumbers()
{
Random Rnd = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Lista.Length; i++)
do{
Lista[i] = Rnd.Next(8);
}while(NumExists(Lista[i], i) == true);
Rnd = null;
}
static void Main(string[] args)
{
RandomNumbers();
for (int i = 0; i < Lista.Length; i++)
Console.WriteLine("Número: {0}", Lista[i]);
Console.ReadLine();
}
}
}
Número: 7
Número: 5
Número: 0
Número: 1
Número: 6
Número: 4
Número: 2
Cita de: Leo Gutiérrez. en 13 Noviembre 2010, 01:25 AMCita de: Khronos14 en 12 Noviembre 2010, 21:49 PM
Para administrar servidores, lo mejor es: perl, bash y awk.
Saludos.
¿por qué?