Tree

How I can to change the cases for vector or other thing. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "arvore.h"

//using namespace std;

Arvore::Arvore() {
    raiz = new Elemento();
}

void Arvore::ConstrutorArvore() {
    string palavra;
    ifstream palavrast9("termos1_full_ordenado.txt");
    if (!palavrast9.is_open()) {
        system("cls");
        cout << "Erro ao abrir arquivo" << endl;
    } else
        while (!palavrast9.eof()) {
            palavra.clear();
            palavrast9 >> palavra;
            Elemento *elemento = raiz;

            for (int i = 0; i < palavra.size(); i++) {
                switch (palavra[i]) {
                    case'a': case'b': case'c': case'A': case'B': case'C':
                        if (!elemento->p2)
                            elemento->p2 = new Elemento();
                        elemento = elemento->p2;
                        break;

                    case'd': case'e': case'f': case'D': case'E': case'F':
                        if (!elemento->p3)
                            elemento->p3 = new Elemento();
                       elemento = elemento->p3;
                        break;

                    case'g': case'h': case'i': case'G': case'H': case'I':
                        if (!elemento->p4)
                            elemento->p4 = new Elemento();
                        elemento = elemento->p4;
                        break;

                    case'j': case'k': case'l': case'J': case'K': case'L':
                        if (!elemento->p5)
                            elemento->p5 = new Elemento();
                        elemento = elemento->p5;
                        break;

                    case'm': case'n': case'o': case'M': case'N': case'O':
                        if (!elemento->p6)
                            elemento->p6 = new Elemento();
                        elemento = elemento->p6;
                        break;

                    case'p': case'q': case'r': case's': case'P': case'Q': case'R': case'S':
                        if (!elemento->p7)
                            elemento->p7 = new Elemento();
                        elemento = elemento->p7;
                        break;

                    case't': case'u': case'v': case'T': case'U': case'V':
                        if (!elemento->p8)
                            elemento->p8 = new Elemento();
                        elemento = elemento->p8;
                        break;

                    case'w': case'x': case'y': case'z': case'W': case'X': case'Y': case'Z':
                        if (!elemento->p9)
                            elemento->p9 = new Elemento();
                        elemento = elemento->p9;
                        break;

                    default:
                        if (!elemento->p1)
                            elemento->p1 = new Elemento();
                        elemento = elemento->p1;
                        break;
                }
                elemento->palavras.push_back(palavra);
            }
        }
    palavrast9.close();
}

Can you explain your question a little more? I assume you're talking about the cases in the switch statement, but what do you mean by "for vector or other thing?"

A few general comments about the code so far:
- You can cut down on the number of cases by doing switch(tolower(palavra[i])). Then you only have to worry about lower case letters.

But really, you should be using an array or vector here. Maybe that's what you're talking about. HINT #1: any time you find yourself writing variable names like p1,p2,p3... you should consider replacing them with an array or vector.

So Elemento should probably have:
Elemento *p[10];
instead of all the p variables.

Now it appears that you're translating letters to the corresponding digits on a telephone keypad. It would be smaller and faster to do this with data.

HINT #2: Data is usually smaller and faster than code.

In this case, just create an array of 26 chars (we'll use the chars as "small integers"). Where each entry maps a letter to a corresponding digits:
1
2
//                          a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
char lettersToDigits[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};


Then you get the digit with:
1
2
3
4
5
int getDigit(char letter) {
    letter = tolower(letter);
    if (letter < 'a' || letter > 'z') return 1;  // default
    else return lettersToDigits[letter-'a'];
}

Yes, I am working with a telephone keypad. Where a put the function get Digit?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "arvore.h"

//using namespace std;

Arvore::Arvore() {
    raiz = new Elemento();
}

void Arvore::ConstrutorArvore() {
    string palavra;
    ifstream palavrast9("termos1_full_ordenado.txt");
    if (!palavrast9.is_open()) {
        system("cls");
        cout << "Erro ao abrir arquivo" << endl;
    } else
        while (!palavrast9.eof()) {
            palavra.clear();
            palavrast9 >> palavra;
            Elemento *elemento = raiz;

            for (int i = 0; i < palavra.size(); i++) {
                switch (tolower(palavra[i])) {                
                    case'a': case'b': case'c':
                        if (!elemento->p2)
                            elemento->p2 = new Elemento();
                        elemento = elemento->p2;
                        break;

                    case'd': case'e': case'f': 
                    	if (!elemento->p3)
                            elemento->p3 = new Elemento();
                       elemento = elemento->p3;
                        break;

                    case'g': case'h': case'i':
                        if (!elemento->p4)
                            elemento->p4 = new Elemento();
                        elemento = elemento->p4;
                        break;

                    case'j': case'k': case'l':
                    	if (!elemento->p5)
                            elemento->p5 = new Elemento();
                        elemento = elemento->p5;
                        break;

                    case'm': case'n': case'o':
                        if (!elemento->p6)
                            elemento->p6 = new Elemento();
                        elemento = elemento->p6;
                        break;

                    case'p': case'q': case'r': case's':
                        if (!elemento->p7)
                            elemento->p7 = new Elemento();
                        elemento = elemento->p7;
                        break;

                    case't': case'u': case'v': 
                        if (!elemento->p8)
                            elemento->p8 = new Elemento();
                        elemento = elemento->p8;
                        break;

                    case'w': case'x': case'y': case'z':
                        if (!elemento->p9)
                            elemento->p9 = new Elemento();
                        elemento = elemento->p9;
                        break;

                    default:
                        if (!elemento->p1)
                            elemento->p1 = new Elemento();
                        elemento = elemento->p1;
                        break;
                }
                elemento->palavras.push_back(palavra);
            }
        }
    palavrast9.close();
}
functions can't be inside other functions.
if you make prototypes, anywhere, it does not matter.
if you don't bother with them, it needs to be above the user of the function.
Topic archived. No new replies allowed.