|
Mostrar Mensajes
|
Páginas: [1] 2
|
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!! #include <vector> #include <iostream> #include <fstream> #include <string> #include <cstdio> #include <cstdlib> #include <ctime> #include <random> using namespace std; string filename = "words.txt"; ifstream ifile {filename}; void error(const string&); vector<string> load_vector(const string& filename) { vector<string> vi; if (!ifile) error("load_vector cannot open the file " + filename); copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi)); return vi; } void error(const string& s) { cerr << "Error: " << s << endl; exit(EXIT_FAILURE); } void aleatorio () { int HEIGHT = 15; //Numero de filas int WIDTH = 15; //Numero de columnas int wordsize = 0; int wordPos = 0; char puzzle[HEIGHT][WIDTH]; //Matriz de caracteres bool okWord; bool horizontal; int randColumn; srand(time(NULL)); //inicializamos semilla char random; //Carácter aleatorio int filarand = 0; //Variable para escoger una fila aleatoria vector<string> load_vector(const string&); vector<string> words = load_vector(filename); vector<string> auxWords; string enteredWord; string aux; //Inicializamos matriz for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { puzzle[i][j] = ' '; //Al inicializar la matriz la rellenamos con espacios } } for (int i = 0; i < words.size(); i++) { switch (rand() % 2) { case 1: filarand = rand() % 15; //Escogemos una fila aleatoria wordsize = WIDTH - words[i].size() - 1; //Posiciones que nos quedan para escoger columna aleatoria wordPos = rand() % wordsize; //Escogemos columna aleatoria para empezar a escribir la palabra for (int j = 0; j < words[i].size(); j++) { if (words[i].size() < 10) { //Si la palabra es menor de 10 caracteres aux = ""; // Inicializamos la variable con ningún caracter aux = words[i].substr(j,1); //Sustraemos una letra cada vez de la palabra puzzle[filarand][wordPos + j] = aux[0]; //Rellenamos la fila de la matriz con la palabra } } break; case 0: randColumn = rand() % 15; wordsize = HEIGHT - words[i].size() - 1; wordPos = rand() % wordsize; for (int j = 0; j < words[i].size(); j++) { if (words[i].size() < 10) { aux = ""; aux = words[i].substr(j,1); puzzle[wordPos + j][randColumn] = aux[0]; } } break; } } auxWords = words; for (int i = 0; i < HEIGHT; i++) { //la llenamos aleatoriamente de letras for (int j = 0; j < WIDTH; j++) { if (puzzle[i][j] == ' ') { random = 'A' + (rand() % 26); //Escogemos un carácter aleatorio puzzle[i][j] = random; //Rellenamos la posición con un carácter aleatorio } } } for (int i = 0; i < HEIGHT; i++) { //Mostramos la matriz final for (int j = 0; j < WIDTH; j++) { cout << puzzle[i][j] ; cout << " "; } cout << endl; } do { cout << "Introduce una palabra: "; cin >> enteredWord; for (int i = 0; i < auxWords.size(); i++) { if (enteredWord == auxWords[i]) { //Miramos si la palabra introducida existe en el vector de palabras okWord = true; // SI LA PALABRA existe borrarla del vector, cuando la borre sale auxWords.erase(auxWords.begin() + i); break; } else { // si no la encuentra sigue buscando okWord = false; } } if (okWord == true) { // si la a encontrado te dice la palabra cout << "Palabra " << enteredWord << " correcta" << endl; } else if (enteredWord != "FIN") { //si no la encuetra te dice incorrecta cout << "Palabra " << enteredWord << " incorrecta" << endl; } } while (enteredWord != "FIN"); //Mientras que no escribas FIN no termina programa } int main() { int HEIGHT = 10; int WIDTH = 10; char puzzle[HEIGHT][WIDTH]; srand(time(NULL)); aleatorio(); //wordToMatrix(10, 10, puzzle[HEIGHT][WIDTH]); /*for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { cout << puzzle[i][j] << endl; } }*/ return 0; }
|
|
|
2
|
Programación / Programación C/C++ / Re: Problema con matriz char
|
en: 14 Diciembre 2014, 23:11 pm
|
ya lo tengo jejejejej, ahora me faltarían mejorar algún cosilla, meter las palabras en diagonal o en vertical, y mas jodido que seria una función para mostrar u ocultar las palabras usadas para el relleno de tal forma que pueda imprimír la solución sin las palabras aleatorias o con ellas tambien, estoy perdido no doy mas de mi jejejejeje #include <vector> #include <iostream> #include <fstream> #include <string> #include <cstdio> #include <cstdlib> #include <ctime> #include <random> using namespace std; string filename = "words.txt"; ifstream ifile {filename}; void error(const string&); vector<string> load_vector(const string& filename) { vector<string> vi; if (!ifile) error("load_vector cannot open the file " + filename); copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi)); return vi; } void error(const string& s) { cerr << "Error: " << s << endl; exit(EXIT_FAILURE); } void aleatorio () { int HEIGHT = 15; int WIDTH = 15; int wordsize = 0; int wordPos = 0; char puzzle[HEIGHT][WIDTH]; bool okWord; srand(time(NULL)); //inicializamos semilla char random; int filarand = 0; vector<string> load_vector(const string&); vector<string> words = load_vector(filename); vector<string> auxWords; string enteredWord; string aux; //Inicializamos matriz for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { puzzle[i][j] = ' '; } } for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { random = 'A' + (rand() % 26); puzzle[i][j] = random; } } for (int i = 0; i < HEIGHT; i++) { filarand = rand() % 15; wordsize = WIDTH - words[i].size() - 1; wordPos = rand() % wordsize; for (int j = 0; j < words[i].size(); j++) { if (words[i].size() < 10) { aux = ""; aux = words[i].substr(j,1); puzzle[filarand][wordPos + j] = aux[0]; } } } for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { cout << puzzle[i][j]; cout << " "; } cout << endl; } auxWords = words; do { cout << "Introduce una palabra: "; cin >> enteredWord; for (int i = 0; i < auxWords.size(); i++) { if (enteredWord == auxWords[i]) { okWord = true; auxWords.erase(auxWords.begin() + i); break; } else { okWord = false; } } if (okWord == true) { cout << "Palabra " << enteredWord << " correcta" << endl; } else if (enteredWord != "FIN") { cout << "Palabra " << enteredWord << " incorrecta" << endl; } } while (enteredWord != "FIN"); } /*char wordToMatrix(int HEIGHT, int WIDTH, char puzzle) { int filarand = 0; vector<string> load_vector(const string&); vector<string> words = load_vector(filename); string aux; char empty = ' '; const char* aux2; char random; char espacio = ' '; char puzzle[HEIGHT][WIDTH]; filarand = rand() % 10; //cout << filarand << endl; for (int i = 0; i < 10; i++) { filarand = rand() % 10; cout << "" << endl; for (int j = 0; j < words[i].size(); j++) { if (words[i].size() < 10) { //aux2 = words[i].substr(j,1); //aux = words[i].substr(j,1); //puzzle[i][j] = aux[0]; puzzle[i][j] = words[i].substr(j,1).c_str(); cout << puzzle[i][j]; } } } for (int i = 0; i < 10; i++) { //cout << endl; for (int j = 0; j < 10; j++ ) { if (puzzle[i][j] == '1') { random = 'A' + (rand() % 26); puzzle[i][j] = random; //cout << puzzle[i][j]; } } } for (int i = 0; i < HEIGHT; i++) { cout << endl; for (int j = 0; j < WIDTH; j++) { random = 'A' + (rand() % 26); if (puzzle[i][j] == espacio) { puzzle[i][j] = random; cout << puzzle[i][j]; } } } return puzzle[HEIGHT][WIDTH]; }*/ int main() { int HEIGHT = 10; int WIDTH = 10; char puzzle[HEIGHT][WIDTH]; srand(time(NULL)); aleatorio(); //wordToMatrix(10, 10, puzzle[HEIGHT][WIDTH]); /*for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { cout << puzzle[i][j] << endl; } }*/ return 0; }
|
|
|
3
|
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. #include <vector> #include <iostream> #include <fstream> #include <string> #include <cstdio> #include <cstdlib> #include <ctime> #include <random> using namespace std; string filename = "words.txt"; void error(const string&); vector<string> load_vector(const string& filename) { vector<string> vi; ifstream ifile {filename}; if (!ifile) error("load_vector cannot open the file " + filename); copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi)); return vi; } void error(const string& s) { cerr << "Error: " << s << endl; exit(EXIT_FAILURE); } void aleatorio (int HEIGHT, int WIDTH,const char* puzzle[HEIGHT][WIDTH]) { srand(time(NULL)); //inicializamos semilla string random; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { random = 'A' + (rand() % 26); if (puzzle[i][j] == " ") { puzzle[i][j] = random.c_str(); } } } } char wordToMatrix(int HEIGHT, int WIDTH) { char puzzle[HEIGHT][WIDTH]; int filarand = 0; vector<string> load_vector(const string&); vector<string> words = load_vector(filename); string aux; char empty = ' '; const char* aux2; char random; char espacio = ' '; //Inicializamos matriz for (int i = 0; i < HEIGHT; i++) { cout << endl; for (int j = 0; j < WIDTH; j++) { puzzle[i][j] = '1'; cout << puzzle[i][j]; } } filarand = rand() % 10; //cout << filarand << endl; for (int i = 0; i < 10; i++) { filarand = rand() % 10; cout << "" << endl; for (int j = 0; j < words[i].size(); j++) { if (words[i].size() < 10) { //aux2 = words[i].substr(j,1); aux = words[i].substr(j,1); puzzle[i][j] = aux[0]; cout << puzzle[i][j]; } } } for (int i = 0; i < 10; i++) { //cout << endl; for (int j = 0; j < 10; j++ ) { if (puzzle[i][j] == '1') { random = 'A' + (rand() % 26); puzzle[i][j] = random; //cout << puzzle[i][j]; } } } /*for (int i = 0; i < HEIGHT; i++) { cout << endl; for (int j = 0; j < WIDTH; j++) { random = 'A' + (rand() % 26); if (puzzle[i][j] == espacio) { puzzle[i][j] = random; cout << puzzle[i][j]; } } }*/ return puzzle[HEIGHT][WIDTH]; } int main() { int HEIGHT = 10; int WIDTH = 10; char puzzle[HEIGHT][WIDTH]; srand(time(NULL)); wordToMatrix(10,10); /*for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { cout << puzzle[i][j] << endl; } }*/ return 0; }
|
|
|
4
|
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. #include<string> // std::string #include<vector> // std::vector<> #include<iostream> // std::cout | std::cin | std::cerr #include<fstream> // std::ifstream | std::ofstream #include<cstdlib> // std::exit() #include<algorithm> // std:: sort() #include <locale> // std::tolower | st::locale #include <cctype> //tolower using namespace std; vector<string> load_vector(const string&); void save_vector(const vector<string>&, const string&); void error(const string&); int main() { string filename = "data.txt"; vector<string> text = load_vector(filename); string filename2 = "data2.txt"; fstream file; // start to see if we want to overwrite the file if the file already exist file.open("data2.txt", ios_base::out | ios_base::in); // will not create file if (file.is_open()) { cout << "Warning, file already exists, proceed?"; if (!file.is_open()) { file.close(); } } else { file.clear(); file.open("data2.txt", ios_base::out); // will create if necessary } sort(text.begin(), text.end()); text.erase(unique(text.begin(), text.end()), text.end()); /*for (unsigned int i=0; i<text.size(); i++) { text[i] = tolower (); } // for (auto word : text) { // string lowcase; // for (auto ch : word) // if (ch>='A' and ch<='Z') lowcase += ch +'a'-'A' ; else lowcase = ch; // word = lowcase; } */ save_vector(text, filename2); return 0; } vector<string> load_vector(const string& filename) { vector<string> vi; ifstream ifile {filename}; if (!ifile) error("load_vector cannot open the file " + filename); copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi)); return vi; } void save_vector(const vector<string>& vi, const string& filename2) { ofstream ofile ("data2.txt", ofstream::out); if (!ofile) error("save_vector cannot open the file " + filename2); for (auto n : vi) ofile << n << ' '; ofile.close(); cout << "\nVector written in file \"" << filename2 << "\"" << endl; } /* return true if two vector<string> contain exactly the same members bool compare(const vector<string>& v1, const vector<string>& v2) { if (v1.size()!=v2.size()) return false; // vectors have different sizes for (int i=0; i<v1.size(); ++i) if (v1[i]!=v2[i]) return false; // i-th members of v1 and v2 are different return true; } */ void error(const string& s) { cerr << "Error: " << s << endl; exit(EXIT_FAILURE); }
|
|
|
9
|
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. #include<string> // std::string #include<vector> // std::vector<> #include<iostream> // std::cout | std::cin | std::cerr #include<fstream> // std::ifstream | std::ofstream #include<cstdlib> // std::exit() #include<algorithm> // std:: sort() #include <locale> #include <cctype> using namespace std; vector<string> load_vector(const string&); void save_vector(const vector<string>&, const string&); void error(const string&); int main() { string filename2 = "data2.txt"; //locale loc; string filename = "data.txt"; vector<string> text = load_vector(filename); sort(text.begin(), text.end()); text.erase(unique(text.begin(), text.end()), text.end()); //for (unsigned int i=0; i<text.size(); i++) { // text [i] = tolower (text[i], loc); // } save_vector(text, filename2); return 0; } vector<string> load_vector(const string& filename) { vector<string> vi; ifstream ifile {filename}; if (!ifile) error("load_vector cannot open the file " + filename); copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi)); return vi; } void save_vector(const vector<string>& vi, const string& filename2) { ofstream ofile ("data2.txt"); if (!ofile) error("save_vector cannot open the file " + filename2); for (auto n : vi) ofile << n << ' '; ofile.close(); cout << "\nVector written in file \"" << filename2 << "\"" << endl; } void error(const string& s) { cerr << "Error: " << s << endl; exit(EXIT_FAILURE); }
|
|
|
|
|
|
|