Dev - C++ - problema con un string

 
Vista:
sin imagen de perfil
Val: 12
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por rodrigo (7 intervenciones) el 08/06/2019 08:05:14
el problema esta en el caso 5 ya que al ejecutar y posteriormente mostrar me marca los numeros que elimino con 0, y lo que yo necesito es que elimine el espacio.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*Se tiene una serie de numero que son capturados, se requiere un algoritmo que realize las siguientes funciones
1 elimine duplicados
2 Muestra la serie
3 ordene los numeros (menor a mayor).
4 Busque un valor determinado e indicar cuantas veces se repite en el arreglo.
5 Eliminar numeros impares, peor nolos numeros que teminan en 4.
6 Convertir un numero a letra
7 Terminar
Todas las funciones pueden ser ejecutadas hasta que se le indique la funcion terminar
La funcion capturar solo se ejecuta una vez */
 
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
 
int n;
int numeros;
int a[100];
int i=0;
int c=0;
int d=0;
int x, pos, aux;
int j;
int k;
int y, opcion;
int contador=0;
int b[100];
int enc=0;
int res;
 
 
using namespace std;
 
int main(int argc, char * argv[])
{
    printf ("dame el tamano del arreglo: ");
    scanf ("%i", &n);
    do
    {
        printf ("dame el numero: ");
        scanf ("%i", &numeros);
        a[i]=numeros;
        i++;
    }while (i<n);
 
 
 
    do
    {
        printf( "\n   1. Elimimar duplicados.", 163 );
        printf( "\n   2. Mostrar la serie.", 163 );
        printf( "\n   3. Ordenar numeros.", 163 );
        printf( "\n   4. Buscar un numero.", 163);
        printf( "\n   5. Mostrar impares.", 163 );
        printf( "\n   6. Buscar numero y convertirlo a letra", 163 );
        printf( "\n   7. Terminar", 163);
        printf( "\n\n   Introduzca opci%cn (1-7): ", 162 );
 
        scanf( "%d", &opcion );
 
        /* Inicio del anidamiento */
 
        switch ( opcion )
        {
            case 1: for(i=0;i<n;i++)
                for(j=i+1;j<n;j++)
                if(a[i]==a[j])
                {
                    for(k=j;k<n-1;k++)
                    a[k]=a[k+1];
                    n=n-1;
                    j=i;
                }
                break;
 
            case 2:     printf("\n Tu serie es: \n");
                for(i=0; i<n; i++)
                {
                    printf("%i \n", a[i] );
                }
 
                break;
            case 3: for (i=0; i<n; i++)
                {
                    pos = i;
                    aux = a[i];
                    while((pos>0)&&(aux<a[pos-1]))
                    {
                        a[pos] = a[pos-1];
                        pos--;
                    }
                        a[pos] = aux;
                }
 
                break;
 
            case 4:    /*printf ("Que numero quieres buscar: ");
                scanf ("%i", &x);
                i=0;
                do
                {
                    if (a[i]==x)
                        {
                            printf ("encontre el numero %i en el arreglo.\n", x);
                        }
                    else (a[i]!=x); printf ("No encontre el numero en el arreglo. \n");
                    i++;

                }while (i<n); */
                printf ("Que numero quieres buscar: " );
                scanf ("%i", &x);
                i=0;
                do
                {
                    if(a[i]==x)
                    {
                        for (i=0;i<n;i++)
                            {
                            for (i=0;i<n;i++)
                                {
                                    if (x==a[i])
                                    {
                                    contador++;
                                    }
                                }
                             printf("El numero se repite %i veces en tu arreglo\n",contador);
                            }
                    }
                    if(enc==0)
                    {
                    printf("\n No esta en el arreglo \n ");
                    }
                    i++;
                }while(i<n);
                break;
 
            case 5: for( c=0; c<n; c++ ) //Limpia arreglo tmp
                    {
                    b[c]= '\0';
                    }
                for (i=0; i<n; i++)
                    {
                    res = (a[i]%2);
                    if (res != 0)
                        {
                            b[d]=a[i];
                            d++;
                        }
                    res = (a[i]%10);
                    if (res == 4)
                        {
                            b[d]=a[i];
                            d++;
                        }
                    }               //Limpia arreglo original
                 for( c=0; c<n; c++)
                    {
                    a[c]= '\0';
                    }               //Envía datos de tmp a original
                 for (i=0; i<d; i++)
                    {
                        a[i]=b[i];
                    }
                    i++;
            break;
            case 6:
    cout<<"Ingresa el numero"<<endl;
    cin>>x;
if((x<1)||(x>999)) cout<<"INGRESA UN NUMERO DEL 1 AL 999\n";
else
 
    {
            if(x>=900)   {cout<<"NOVECIENTOS " ;x=x-900;}
       else if(x>=800)   {cout<<"OCHOCIENTOS " ;x=x-800;}
       else if(x>=700)   {cout<<"SETECIENTOS " ;x=x-700;}
       else if(x>=600)   {cout<<"SEISCIENTOS " ;x=x-600;}
       else if(x>=500)   {cout<<"QUINIENTOS "  ;x=x-500;}
       else if(x>=400)   {cout<<"CUATROCIENTOS "   ;x=x-400;}
       else if(x>=300)   {cout<<"TRESCIENTOS " ;x=x-300;}
       else if(x>=200)   {cout<<"DOSCIENTOS "  ;x=x-200;}
       else if(x>100)    {cout<<"CIENTO "  ;x=x-100;}
       else if(x==100)  {cout<<"CIEN"     ;x=x-100;}
            if(x>90) {cout<<"NOVENTA Y "   ;x=x-90; }
            if(x==90)   {cout<<"NOVENTA"  ;x=x-90; }
            if(x>80) {cout<<"OCHENTA Y "   ;x=x-80; }
            if(x==80)   {cout<<"OCHENTA"  ;x=x-80; }
            if(x>70) {cout<<"SETENTA Y "   ;x=x-70; }
            if(x==70)   {cout<<"SETENTA"  ;x=x-70; }
            if(x>60) {cout<<"SESENTA Y "   ;x=x-60; }
            if(x==60)   {cout<<"SESENTA"  ;x=x-60; }
            if(x>50) {cout<<"CINCUENTA Y " ;x=x-50; }
            if(x==50)   {cout<<"CINCUENTA"    ;x=x-50; }
            if(x>40) {cout<<"CUARENTA Y "  ;x=x-40; }
            if(x==40)   {cout<<"CUARENTA" ;x=x-40; }
            if(x>30) {cout<<"TREINTA Y "   ;x=x-30; }
            if(x==30)   {cout<<"TREINTA"  ;x=x-30; }
            if(x>20) {cout<<"VEINTI "       ;x=x-20; }
            if(x==20)   {cout<<"VEINTE"       ;x=x-20; }
            if(x>=16)    {cout<<"DIECI "        ;x=x-10; }
       else if(x==15)   {cout<<"QUINCE"       ;x=x-15; }
       else if(x==14)   {cout<<"CATORCE"  ;x=x-14; }
       else if(x==13)   {cout<<"TRECE"        ;x=x-13; }
       else if(x==12)   {cout<<"DOCE"     ;x=x-12; }
       else if(x==11)   {cout<<"ONCE"     ;x=x-11; }
       else if(x==10)   {cout<<"DIEZ"     ;x=x-10; }
            if(x==9)    {cout<<"NUEVE"        ;x=x-9;  }
            if(x==8)    {cout<<"OCHO"     ;x=x-8;  }
            if(x==7)    {cout<<"SIETE"        ;x=x-7;  }
            if(x==6)    {cout<<"SEIS"     ;x=x-6;  }
       else if(x==5)    {cout<<"CINCO"        ;x=x-5;  }
       else if(x==4)    {cout<<"CUATRO"       ;x=x-4;  }
       else if(x==3)    {cout<<"TRES"     ;x=x-3;  }
       else if(x==2)    {cout<<"DOS"      ;x=x-2;  }
       else if(x==1)    {cout<<"UNO"      ;x=x-1;  }
       }
            cout<<endl;
            cin.ignore();
 
                break;
         }
 
         /* Fin del anidamiento */
 
    } while ( opcion != 7 );
 
    return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Rodrigo
Val: 1.755
Plata
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (539 intervenciones) el 08/06/2019 19:14:17
Es rara la peticion "Eliminar numeros impares, peor nolos numeros que teminan en 4."
Si "eliminas los numeros impares" te quedas con los pares, en particular los numeros que terminan en 4, que son pares.

Por el codigo que hiciste, supongo que quisiste implementar:

"Eliminar numeros pares, pero no los numeros que teminan en 4."

Sugiero inicializar d en 0 al inicio del caso 5, y no al inicio del programa, por si alguien elige mas de 1 vez la opcion 5, pues cada vez que se elija, d va a partir en la posicion en que quedo' la ultima vez que se ejecuto.

Convendria que incluyeras que' entrada le das a tu programa y cual es la salida que el programa te da, para entender mejor.

Otra sugerencia: puedes eliminar la variable res, que lo unico que hace es guardar el resultado de una operacion matematica, en vez de

1
2
3
4
res = operacion
if( res == 0 ) {
    blabla
}
hacer

1
2
3
if( operacion == 0 ) {
   blabla
}

Si la operacion es compleja, mejor parentizarla
1
2
3
if( (operacion) == 0 ) {
   blabla
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 12
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (7 intervenciones) el 09/06/2019 01:35:01
gracis te lo agradesco
mi otro problema es que ya al ejecuta el caso 5 y mostrar la serie no elimina el espacio en el arreglo equivalente al par si no que lo marca como un 0 y quiero que elimine el espacio del arreglo

error
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Rodrigo
Val: 1.755
Plata
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (539 intervenciones) el 09/06/2019 03:36:20
No se ve bien la imagen, o no veo donde esta el 0 que dices. Donde esta el 0 que indicas?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 12
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (7 intervenciones) el 09/06/2019 03:41:09
por ejemplo meto un arrglo de 5 numeros
1
2
3
4
5
ejecuto la opcion 5 que debe de eliminar los numeros pares del arreglo pero no los que terminan en 4, entonces el unico numero que eliminara sera el 2 que es par.

pero al volver a mostrar el arreglo que es el caso 2 del codigo lo marca como un 0
1
3
4
5
0
de esta forma
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Rodrigo
Val: 1.755
Plata
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (539 intervenciones) el 09/06/2019 06:32:18
actualiza n para que valga lo que d contiene al final de la opcion 5.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 12
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (7 intervenciones) el 09/06/2019 10:14:57
Nose si pudieras darme un ejemplo de como representar lo que me dices en codigo, por favor
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Rodrigo
Val: 1.755
Plata
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (539 intervenciones) el 09/06/2019 12:54:40
1
n = d;

Como ultima instruccion del caso 5, antes del break
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 12
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Rodrigo (7 intervenciones) el 09/06/2019 20:41:32
les agradesco ya funiciona
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 417
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

problema con un string

Publicado por Thanatos (199 intervenciones) el 09/06/2019 15:27:29
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <cstddef>   // size_t
#include <iostream>
 
using namespace std;
 
int main()
{
    const size_t kSize = 100;
    int numeros[kSize] = { };
    size_t nNumeros;
    int opcion = 0;
 
    cout << "Ingrese la cantidad de numeros: ";
    cin >> nNumeros;
 
    if (nNumeros > kSize)
    {
        cerr << "\n* No se admiten mas de " << kSize << " numeros *\n";
        return -1;
    }
 
    cout << "\nIngrese los " << nNumeros << " numeros:\n";
    for (size_t i = 0; i < nNumeros; ++i)
    {
        cout << i + 1 << ": ";
        cin >> numeros[i];
    }
    cout << '\n';
 
    while (opcion != 7)
    {
        size_t nNumsAux;
        int num;
 
        cout << "============================= \n"
             << " 1. Eliminar los duplicados   \n"
             << " 2. Mostrar los numeros       \n"
             << " 3. Ordenamiento ascendente   \n"
             << " 4. Contar los repetidos      \n"
             << " 5. Eliminar pares (- fin 4)  \n"
             << " 6. Convertir numero a texto  \n"
             << " 7. Salir del programa        \n"
             << "============================= \n"
             << " opcion: ";
        cin >> opcion;
        cout << '\n';
 
        switch (opcion)
        {
            case 1:
                nNumsAux = 0;
 
                for (size_t i = 0; i < nNumeros; ++i)
                {
                    size_t j = 0;
 
                    while (j < i && numeros[i] != numeros[j])
                    {
                        ++j;
                    }
 
                    if (i == j)
                    {
                        if (nNumsAux != i)
                        {
                            numeros[nNumsAux] = numeros[i];
                        }
                        ++nNumsAux;
                    }
                }
                nNumeros = nNumsAux;
                break;
            case 2:
                for (size_t i = 0; i < nNumeros; ++i)
                {
                    cout << numeros[i] << "  ";
                }
                cout << "\n\n";
                break;
            case 3:
                for (size_t i = 0; i < nNumeros; ++i)
                {
                    int numAux = numeros[i];
                    size_t iAux = i;
 
                    while (iAux > 0 && numAux < numeros[iAux - 1])
                    {
                        numeros[iAux] = numeros[iAux - 1];
                        --iAux;
                    }
                    numeros[iAux] = numAux;
                }
                break;
            case 4:
                size_t nRepetidos; nRepetidos = 0;
 
                cout << "Ingrese el numero: ";
                cin >> num;
 
                for (size_t i = 0; i < nNumeros; ++i)
                {
                    if (numeros[i] == num)
                    {
                        ++nRepetidos;
                    }
                }
 
                if (nRepetidos > 0)
                {
                    cout << "El numero " << num << " aparece " << nRepetidos
                         << ((nRepetidos == 1) ? " vez" : " veces") << "\n\n";
                }
                else
                {
                    cout << "El numero no esta en el array\n\n";
                }
                break;
            case 5:
                nNumsAux = 0;
 
                for (size_t i = 0; i < nNumeros; i++)
                {
                    if (numeros[i] % 10 == 4 || numeros[i] % 2 != 0)
                    {
                        if (i != nNumsAux)
                        {
                            numeros[nNumsAux] = numeros[i];
                        }
                        ++nNumsAux;
                    }
                }
                nNumeros = nNumsAux;
                break;
            case 6:
                cout << "Ingresa el numero: ";
                cin >> num;
 
                if (num < 1 || num > 999)
                {
                    cerr << "* Numero fuera de rango (1 a 999) *\n\n";
                    continue;
                }
 
                if (num >= 900)      { cout << "NOVECIENTOS ";   num -= 900; }
                else if (num >= 800) { cout << "OCHOCIENTOS ";   num -= 800; }
                else if (num >= 700) { cout << "SETECIENTOS ";   num -= 700; }
                else if (num >= 600) { cout << "SEISCIENTOS ";   num -= 600; }
                else if (num >= 500) { cout << "QUINIENTOS ";    num -= 500; }
                else if (num >= 400) { cout << "CUATROCIENTOS "; num -= 400; }
                else if (num >= 300) { cout << "TRESCIENTOS ";   num -= 300; }
                else if (num >= 200) { cout << "DOSCIENTOS ";    num -= 200; }
                else if (num > 100)  { cout << "CIENTO ";        num -= 100; }
                else if (num == 100) { cout << "CIEN";           num -= 100; }
 
                if (num > 90)       { cout << "NOVENTA Y ";   num -= 90; }
                else if (num == 90) { cout << "NOVENTA";      num -= 90; }
                else if (num > 80)  { cout << "OCHENTA Y ";   num -= 80; }
                else if (num == 80) { cout << "OCHENTA";      num -= 80; }
                else if (num > 70)  { cout << "SETENTA Y ";   num -= 70; }
                else if (num == 70) { cout << "SETENTA";      num -= 70; }
                else if (num > 60)  { cout << "SESENTA Y ";   num -= 60; }
                else if (num == 60) { cout << "SESENTA";      num -= 60; }
                else if (num > 50)  { cout << "CINCUENTA Y "; num -= 50; }
                else if (num == 50) { cout << "CINCUENTA";    num -= 50; }
                else if (num > 40)  { cout << "CUARENTA Y ";  num -= 40; }
                else if (num == 40) { cout << "CUARENTA";     num -= 40; }
                else if (num > 30)  { cout << "TREINTA Y ";   num -= 30; }
                else if (num == 30) { cout << "TREINTA";      num -= 30; }
                else if (num > 20)  { cout << "VEINTI";       num -= 20; }
                else if (num == 20) { cout << "VEINTE";       num -= 20; }
 
                if (num >= 16)      { cout << "DIECI";   num -= 10; }
                else if (num == 15) { cout << "QUINCE";  num -= 15; }
                else if (num == 14) { cout << "CATORCE"; num -= 14; }
                else if (num == 13) { cout << "TRECE";   num -= 13; }
                else if (num == 12) { cout << "DOCE";    num -= 12; }
                else if (num == 11) { cout << "ONCE";    num -= 11; }
                else if (num == 10) { cout << "DIEZ";    num -= 10; }
 
                if (num == 9)      { cout << "NUEVE";  num -= 9; }
                else if (num == 8) { cout << "OCHO";   num -= 8; }
                else if (num == 7) { cout << "SIETE";  num -= 7; }
                else if (num == 6) { cout << "SEIS";   num -= 6; }
                else if (num == 5) { cout << "CINCO";  num -= 5; }
                else if (num == 4) { cout << "CUATRO"; num -= 4; }
                else if (num == 3) { cout << "TRES";   num -= 3; }
                else if (num == 2) { cout << "DOS";    num -= 2; }
                else if (num == 1) { cout << "UNO";    num -= 1; }
 
                cout << "\n\n";
                break;
            case 7:
                cout << "Fin del programa\n";
                break;
            default:
                cerr << "* Opcion desconocida *\n\n";
                break;
        }
    }
 
    return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar