elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / Ayuda perfeccionar programa en: 15 Diciembre 2014, 19:02 pm
Buenas necesito un empujón para perfeccionar programa, saco las letras en horizontal y vertical en la matriz pero lo tengo un poco guarrete, necesito que no me pise las palabras mostradas y que muestre todas bien mezcladas en horizontal y vertical.
Gracias de antemano, saludos!!
Código
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <random>
  9.  
  10. using namespace std;
  11.  
  12. string filename = "words.txt";
  13. ifstream ifile {filename};
  14. void error(const string&);
  15.  
  16. vector<string> load_vector(const string& filename) {
  17.  
  18.    vector<string> vi;
  19.  
  20.    if (!ifile)
  21.        error("load_vector cannot open the file " + filename);
  22.    copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
  23.  
  24.    return vi;
  25. }
  26.  
  27. void error(const string& s) {
  28.    cerr << "Error: " << s << endl;
  29.    exit(EXIT_FAILURE);
  30. }
  31.  
  32. void aleatorio () {
  33.    int HEIGHT = 15; //Numero de filas
  34.    int WIDTH = 15; //Numero de columnas
  35.    int wordsize = 0;
  36.    int wordPos = 0;
  37.    char puzzle[HEIGHT][WIDTH]; //Matriz de caracteres
  38.    bool okWord;
  39.    bool horizontal;
  40.    int randColumn;
  41.  
  42.    srand(time(NULL)); //inicializamos semilla
  43.  
  44.    char random; //Carácter aleatorio
  45.    int filarand = 0; //Variable para escoger una fila aleatoria
  46.  
  47.    vector<string> load_vector(const string&);
  48.    vector<string> words = load_vector(filename);
  49.    vector<string> auxWords;
  50.    string enteredWord;
  51.    string aux;
  52.  
  53.    //Inicializamos matriz
  54.    for (int i = 0; i < HEIGHT; i++) {
  55.        for (int j = 0; j < WIDTH; j++) {
  56.            puzzle[i][j] = ' '; //Al inicializar la matriz la rellenamos con espacios
  57.        }
  58.    }
  59.  
  60.    for (int i = 0; i < words.size(); i++) {
  61.        switch (rand() % 2) {
  62.        case 1:
  63.            filarand = rand() % 15; //Escogemos una fila aleatoria
  64.            wordsize = WIDTH - words[i].size() - 1; //Posiciones que nos quedan para escoger columna aleatoria
  65.            wordPos = rand() % wordsize; //Escogemos columna aleatoria para empezar a escribir la palabra
  66.            for (int j = 0; j < words[i].size(); j++) {
  67.                if (words[i].size() < 10) { //Si la palabra es menor de 10 caracteres
  68.                    aux = ""; // Inicializamos la variable con ningún caracter
  69.                    aux = words[i].substr(j,1); //Sustraemos una letra cada vez de la palabra
  70.                    puzzle[filarand][wordPos + j] = aux[0]; //Rellenamos la fila de la matriz con la palabra
  71.                }
  72.            }
  73.            break;
  74.        case 0:
  75.            randColumn = rand() % 15;
  76.            wordsize = HEIGHT - words[i].size() - 1;
  77.            wordPos = rand() % wordsize;
  78.            for (int j = 0; j < words[i].size(); j++) {
  79.                if (words[i].size() < 10) {
  80.                    aux = "";
  81.                    aux = words[i].substr(j,1);
  82.                    puzzle[wordPos + j][randColumn] = aux[0];
  83.                }
  84.            }
  85.            break;
  86.        }
  87.    }
  88.  
  89.    auxWords = words;
  90.  
  91.    for (int i = 0; i < HEIGHT; i++) {          //la llenamos aleatoriamente de letras
  92.        for (int j = 0; j < WIDTH; j++) {
  93.            if (puzzle[i][j] == ' ') {
  94.                random = 'A' + (rand() % 26); //Escogemos un carácter aleatorio
  95.                puzzle[i][j] = random; //Rellenamos la posición con un carácter aleatorio
  96.            }
  97.        }
  98.    }
  99.  
  100.  
  101.    for (int i = 0; i < HEIGHT; i++) { //Mostramos la matriz final
  102.        for (int j = 0; j < WIDTH; j++) {
  103.            cout << puzzle[i][j] ;
  104.            cout << "  ";
  105.        }
  106.        cout << endl;
  107.    }
  108.  
  109.    do {
  110.        cout << "Introduce una palabra: ";
  111.        cin >> enteredWord;
  112.  
  113.        for (int i = 0; i < auxWords.size(); i++) {
  114.            if (enteredWord == auxWords[i]) { //Miramos si la palabra introducida existe en el vector de palabras
  115.                okWord = true;                  // SI LA PALABRA existe borrarla del vector, cuando la borre sale
  116.                auxWords.erase(auxWords.begin() + i);
  117.                break;
  118.            }
  119.            else {                          // si no la encuentra sigue buscando
  120.                okWord = false;
  121.            }
  122.        }
  123.  
  124.        if (okWord == true) {                  // si la a encontrado te dice la palabra
  125.            cout << "Palabra " << enteredWord << " correcta" << endl;
  126.        }
  127.        else if (enteredWord != "FIN") {        //si no la encuetra te dice incorrecta
  128.            cout << "Palabra " << enteredWord << " incorrecta" << endl;
  129.        }
  130.    } while (enteredWord != "FIN");             //Mientras que no escribas FIN no termina programa
  131. }
  132.  
  133.  
  134.  
  135. int main() {
  136.  
  137.    int HEIGHT = 10;
  138.    int WIDTH = 10;
  139.  
  140.    char puzzle[HEIGHT][WIDTH];
  141.  
  142.    srand(time(NULL));
  143.    aleatorio();
  144.    //wordToMatrix(10, 10, puzzle[HEIGHT][WIDTH]);
  145.  
  146.    /*for (int i = 0; i < HEIGHT; i++) {
  147.      for (int j = 0; j < WIDTH; j++) {
  148.      cout << puzzle[i][j] << endl;
  149.      }
  150.      }*/
  151.  
  152.  
  153.  
  154.    return 0;
  155. }
  156.  
  157.  
2  Programación / Programación C/C++ / Problema con matriz char en: 12 Diciembre 2014, 21:51 pm
Muy buenas no consigo acabar este programa:
que me saque la matriz de 10*10 con palabras de un archivo y en los espacios me ponga una letra aleatoria me saca las palabras tal cual, llevo un rato volviéndome loco y no doy con el problema.

Código
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <random>
  9.  
  10. using namespace std;
  11.  
  12. string filename = "words.txt";
  13. void error(const string&);
  14.  
  15. vector<string> load_vector(const string& filename) {
  16.  
  17.    vector<string> vi;
  18.  
  19.    ifstream ifile {filename};
  20.    if (!ifile)
  21.        error("load_vector cannot open the file " + filename);
  22.    copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
  23.  
  24.    return vi;
  25. }
  26.  
  27. void error(const string& s) {
  28.    cerr << "Error: " << s << endl;
  29.    exit(EXIT_FAILURE);
  30. }
  31.  
  32. void aleatorio (int HEIGHT, int WIDTH,const char* puzzle[HEIGHT][WIDTH]) {
  33.  
  34.    srand(time(NULL)); //inicializamos semilla
  35.    string random;
  36.    for (int i = 0; i < HEIGHT; i++) {
  37.        for (int j = 0; j < WIDTH; j++) {
  38.            random = 'A' + (rand() % 26);
  39.            if (puzzle[i][j] == " ") {
  40.                puzzle[i][j] = random.c_str();
  41.            }
  42.        }
  43.  
  44.    }
  45. }
  46.  
  47. char wordToMatrix(int HEIGHT, int WIDTH) {
  48.  
  49.    char puzzle[HEIGHT][WIDTH];
  50.    int filarand = 0;
  51.    vector<string> load_vector(const string&);
  52.    vector<string> words = load_vector(filename);
  53.    string aux;
  54.    char empty = ' ';
  55.    const char* aux2;
  56.    char random;
  57.    char espacio = ' ';
  58.  
  59.    //Inicializamos matriz
  60.    for (int i = 0; i < HEIGHT; i++) {
  61.        cout << endl;
  62.        for (int j = 0; j < WIDTH; j++) {
  63.            puzzle[i][j] = '1';
  64.            cout << puzzle[i][j];
  65.        }
  66.    }
  67.  
  68.  
  69.    filarand = rand() % 10;
  70.    //cout << filarand << endl;
  71.  
  72.    for (int i = 0; i < 10; i++) {
  73.        filarand = rand() % 10;
  74.        cout << "" << endl;
  75.        for (int j = 0; j < words[i].size(); j++) {
  76.            if (words[i].size() < 10) {
  77.                    //aux2 = words[i].substr(j,1);
  78.                    aux = words[i].substr(j,1);
  79.                    puzzle[i][j] = aux[0];
  80.                    cout << puzzle[i][j];
  81.            }
  82.        }
  83.    }
  84.  
  85.    for (int i = 0; i < 10; i++) {
  86.        //cout << endl;
  87.        for (int j = 0; j < 10; j++ ) {
  88.            if (puzzle[i][j] == '1') {
  89.                random = 'A' + (rand() % 26);
  90.                puzzle[i][j] = random;
  91.                //cout << puzzle[i][j];
  92.            }
  93.        }
  94.    }
  95.  
  96.    /*for (int i = 0; i < HEIGHT; i++) {
  97.         cout << endl;
  98.         for (int j = 0; j < WIDTH; j++) {
  99.             random = 'A' + (rand() % 26);
  100.             if (puzzle[i][j] == espacio) {
  101.                 puzzle[i][j] = random;
  102.                 cout << puzzle[i][j];
  103.             }
  104.         }
  105.        
  106.     }*/
  107.  
  108.    return puzzle[HEIGHT][WIDTH];
  109.  
  110.  
  111. }
  112.  
  113.  
  114. int main() {
  115.  
  116.    int HEIGHT = 10;
  117.    int WIDTH = 10;
  118.  
  119.    char puzzle[HEIGHT][WIDTH];
  120.  
  121.    srand(time(NULL));
  122.    wordToMatrix(10,10);
  123.  
  124.    /*for (int i = 0; i < HEIGHT; i++) {
  125.         for (int j = 0; j < WIDTH; j++) {
  126.             cout << puzzle[i][j] << endl;
  127.         }
  128.     }*/
  129.  
  130.  
  131.  
  132.    return 0;
  133. }
3  Programación / Programación C/C++ / Ayuda tolower & overwrite c++ en: 2 Diciembre 2014, 19:17 pm
Buenas tengo casi terminado el programa aunque me falta rematarlo. Necesito que el usuario decida por teclado si quiere sobrescribir el data2.txt que ya existe creado o no , esta parte la tengo casi casi. Por otra parte no consigo hacer funcionar la función tolower en un vector string ahi tengo varias pruebas que no consigo que funcionen muchas gracias de antemano.
Código
  1. #include<string>    // std::string
  2. #include<vector>    // std::vector<>
  3. #include<iostream>  // std::cout | std::cin | std::cerr
  4. #include<fstream>   // std::ifstream | std::ofstream
  5. #include<cstdlib>   // std::exit()
  6. #include<algorithm>  // std:: sort()
  7. #include <locale>  // std::tolower | st::locale
  8. #include <cctype>   //tolower
  9.  
  10. using namespace std;
  11.  
  12. vector<string> load_vector(const string&);
  13. void save_vector(const vector<string>&, const string&);
  14. void error(const string&);
  15.  
  16. int main() {
  17.  
  18.    string filename = "data.txt";
  19.    vector<string> text = load_vector(filename);
  20.  
  21.    string filename2 = "data2.txt";
  22.  
  23.    fstream file;    // start to see if we want to overwrite the file if the file already exist
  24.    file.open("data2.txt", ios_base::out | ios_base::in);  // will not create file
  25.    if (file.is_open())
  26.    {
  27.        cout << "Warning, file already exists, proceed?";
  28.        if (!file.is_open())
  29.        {
  30.            file.close();
  31.  
  32.        }
  33.    }
  34.    else
  35.    {
  36.        file.clear();
  37.        file.open("data2.txt", ios_base::out);  // will create if necessary
  38.    }
  39.  
  40.  
  41.  
  42.    sort(text.begin(), text.end());
  43.    text.erase(unique(text.begin(), text.end()), text.end());
  44.  
  45.  
  46.    /*for (unsigned int i=0; i<text.size(); i++) {
  47.       text[i] = tolower ();
  48.     }
  49.    
  50.    // for (auto word : text) {
  51.      //   string lowcase;
  52.        // for (auto ch : word)
  53.          //   if (ch>='A' and ch<='Z') lowcase += ch +'a'-'A' ; else lowcase = ch;
  54.        // word = lowcase;
  55.   } */
  56.  
  57.    save_vector(text, filename2);
  58.    return 0;
  59.  
  60.  
  61.  
  62. }
  63.  
  64. vector<string> load_vector(const string& filename) {
  65.    vector<string> vi;
  66.  
  67.    ifstream ifile {filename};
  68.    if (!ifile)
  69.        error("load_vector cannot open the file " + filename);
  70.    copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
  71.  
  72.    return vi;
  73. }
  74.  
  75.  
  76. void save_vector(const vector<string>& vi, const string& filename2) {
  77.  
  78.    ofstream ofile ("data2.txt", ofstream::out);
  79.    if (!ofile)
  80.        error("save_vector cannot open the file " + filename2);
  81.    for (auto n : vi)
  82.        ofile << n << ' ';
  83.    ofile.close();
  84.    cout << "\nVector written in file \"" << filename2 << "\"" << endl;
  85. }
  86. /* return true if two vector<string> contain exactly the same members
  87. bool compare(const vector<string>& v1, const vector<string>& v2) {
  88.     if (v1.size()!=v2.size())
  89.         return false;       // vectors have different sizes
  90.    
  91.     for (int i=0; i<v1.size(); ++i)
  92.         if (v1[i]!=v2[i])
  93.             return false;   // i-th members of v1 and v2 are different
  94.    
  95.     return true;
  96. }
  97. */
  98. void error(const string& s) {
  99.    cerr << "Error: " << s << endl;
  100.    exit(EXIT_FAILURE);
  101. }
4  Programación / Programación C/C++ / Ayuda guardado de archivo en: 27 Noviembre 2014, 13:25 pm
No consigo que me cree el data2.txt.

Código
  1. #include<string>    // std::string
  2. #include<vector>    // std::vector<>
  3. #include<iostream>  // std::cout | std::cin | std::cerr
  4. #include<fstream>   // std::ifstream | std::ofstream
  5. #include<cstdlib>   // std::exit()
  6. #include<algorithm>  // std:: sort()
  7. #include <locale>
  8. #include <cctype>
  9. using namespace std;
  10.  
  11. vector<string> load_vector(const string&);
  12. void save_vector(const vector<string>&, const string&);
  13. void error(const string&);
  14.  
  15. int main() {
  16.    string filename2 = "data2.txt";
  17.    //locale loc;
  18.    string filename = "data.txt";
  19.    vector<string> text = load_vector(filename);
  20.  
  21.    sort(text.begin(), text.end());
  22.    text.erase(unique(text.begin(), text.end()), text.end());
  23.  
  24.    //for (unsigned int i=0; i<text.size(); i++) {
  25.      // text [i] = tolower (text[i], loc);
  26.   // }
  27.  
  28.    save_vector(text, filename2);
  29.    return 0;
  30.  
  31.  
  32.  
  33. }
  34.  
  35. vector<string> load_vector(const string& filename) {
  36.    vector<string> vi;
  37.  
  38.    ifstream ifile {filename};  
  39.  
  40.    if (!ifile)
  41.        error("load_vector cannot open the file " + filename);
  42.    copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
  43.  
  44.    return vi;
  45. }
  46.  
  47.  
  48.  
  49. void save_vector(const vector<string>& vi, const string& filename2) {
  50.  
  51.    ofstream ofile ("data2.txt");  
  52.  
  53.    if (!ofile)
  54.        error("save_vector cannot open the file " + filename2);
  55.    for (auto n : vi)
  56.        ofile << n << ' ';
  57.    ofile.close();
  58.    cout << "\nVector written in file \"" << filename2 << "\"" << endl;
  59. }
  60. void error(const string& s) {
  61.    cerr << "Error: " << s << endl;
  62.    exit(EXIT_FAILURE);
  63. }
  64.  
5  Programación / Programación C/C++ / problemillas varios en: 26 Noviembre 2014, 18:22 pm
resuelto
6  Programación / Programación C/C++ / Dudas varias programa en: 24 Noviembre 2014, 19:20 pm
Muy buenas estoy haciendo una practica de fibonacci tengo lo siguiente después de mucha comedura de cerebro...
Código
  1. #include <vector>
  2. #include <math.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. void fibonacci (int x, int y , vector<int>& v, int n){
  8.    int num1 = x;
  9.    int num2 = y;
  10.    int aux;
  11.    v.push_back(x);
  12.    v.push_back(y);
  13.    for (int d = n-2; d > 0; --d){
  14.        aux = num1 + num2;
  15.        num1 = num2;
  16.        num2 = aux;
  17.        v.push_back(aux);
  18.    }
  19. }
  20.  
  21. int main(){
  22.    int x;
  23.    int y;
  24.    int n;
  25.    cout << "Enter the first term: \n";
  26.    cin >> x;
  27.    cout << "Enter the second term: \n";
  28.    cin >> y;
  29.    cout << "Enter the number of terms: \n";
  30.    cin >> n;
  31.    vector<int> v;
  32.    fibonacci(x, y, v , n);
  33.    cout << endl << "The numbers for the sequence asked are: " << endl;
  34.    for (int i = 0; i <v.size(); ++i){
  35.        cout << v[i] << endl;
  36.    }
  37. }
  38.  

¿como puedo sacar del main la lectura de los datos?
¿como hacer que no se pueda introducir letras, que solo lea datos?
en fin mejorarlo que llevo tiempo con ello y no me apaño no hacen mas que salirme errores, Gracias de antemano, saludos
7  Programación / Programación C/C++ / consejos sobre programa en: 26 Octubre 2014, 13:31 pm
buenas tengo realizado este programa:

Código
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. int main(){
  5.    double a,b,c;
  6.    cout<<"Ingresa a"<<endl;
  7.    cin>>a;
  8.    cout<<"Ingresa b"<<endl;
  9.    cin>>b;
  10.    cout<<"Ingresa c"<<endl;
  11.    cin>>c;
  12.    double disc=pow(b,2)-4*a*c;
  13.    if(a!=0){
  14.        if(disc<0){
  15.            cout<<"Tiene raices imaginarias";
  16.        }else{
  17.            double x1=(-b+sqrt(disc))/(2*a);
  18.            double x2=(-b-sqrt(disc))/(2*a);
  19.            cout<<"X1 = "<<x1<<" X2 = "<<x2;
  20.        }
  21.    }else{
  22.        cout<<"El coeficiente a debe ser diferente a 0";
  23.    }
  24.    return 0;
  25. }

Mis preguntitas son las siguientes:
Como hacer un infinite main loop para que al acabar pregunte al usuario si quiere hacer mas ecuaciones o salir?
Y como puedo conseguir que el usuario no pueda meter letras para que de un error, gracias de antemano saludos
8  Programación / Programación C/C++ / Programa operaciones no muy bien acabado en: 16 Octubre 2014, 15:42 pm
Buenas solo llevo 2 días con c ++ y necesito consejos ya que estoy un poco bloqueado, tengo lo siguiente:
#include <iostream>

using namespace std;

int main() {
    double firstnum; double seconnum; double thirnum; double fournum;
    string operation; string operation2;
    double plus ;double minus; double mul; double div;
   
   
    cout << "Please enter 2 operations: ";
    cin >> firstnum >> operation >> seconnum; // numbers for first operation
    cin >> thirnum >> operation2 >> fournum; // numbers for second numbers
   
    if (operation=="+" || operation=="plus" ) {
        plus = firstnum + seconnum;
        cout << "The first sum is: " << plus << endl;}
   
    if  (operation=="-" || operation=="minus") {
        minus = firstnum - seconnum;
        cout << "The first minus is: " << minus << endl;}
   
    if (operation=="*" || operation=="mul") {
        mul = firstnum * seconnum;
        cout << "The first multiplication is: " << mul << endl;}
    if (operation=="/" || operation=="div"){
        if (seconnum==0) {
            cout << "cero is impossible to operate" << endl;  }
       
        div = firstnum / seconnum ;
        cout << "The first div is: " << div << endl; }
   
   
    if (operation2=="+" || operation2=="plus" ) {
        plus = thirnum + fournum;
        cout << "The second sum is: " << plus << endl;}
   
    if  (operation2=="-" || operation2=="minus") {
        minus = thirnum - fournum;
        cout << "The second minus is: " << minus << endl;}
   
    if (operation2=="*" || operation2=="mul") {
        mul = thirnum * fournum;
        cout << "The second multiplication is: " << mul << endl;}
    if (operation2=="/" || operation2=="div"){
        if (fournum==0) {
            cout << "cero is impossible to operate" << endl; }
       
        div = thirnum / fournum;
        cout << "The second div is: " << div << endl; ;
    }
}

El problema que meto la primera operación al poner una division entre 0 quiero que siga con la segunda operación y no que me diga que es infinito, esta un poco guarrete pero para 2 días que llevo me doy por satisfecho al día de hoy, consejos ayudas? gracias de antemano
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines