Dev - C++ - numero positivo y par o inpar

 
Vista:

numero positivo y par o inpar

Publicado por alexu (5 intervenciones) el 31/10/2020 13:44:08
De un número ingresado por teclado identificar si es positivo, negativo, par o impar
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
-1
Responder

numero positivo y par o inpar

Publicado por Nacho (181 intervenciones) el 31/10/2020 14:15:14
Fácil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main()
{
    int n;
    bool positivo = false;
    bool par = false;
 
    cin >> n;
    __asm
    {
        mov eax, n
        bt eax, 31
        jc s1
        inc [positivo]
        s1:
        bt eax, 0
        jc s2
        inc [par]
        s2 :
    }
}
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

numero positivo y par o inpar

Publicado por Nacho (181 intervenciones) el 31/10/2020 18:49:39
Otra versión

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main()
{
    int n;
    bool negativo = false;
    bool impar = false;
 
    cin >> n;
    __asm
    {
        xor edx, edx
        mov eax, n
        bt eax, 31
        adc edx, edx
        mov byte ptr[negativo], dl
        xor edx, edx
        bt eax, 0
        adc edx, edx
        mov byte ptr[impar], dl
  }
}
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 Alfil
Val: 4.344
Oro
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

numero positivo y par o inpar

Publicado por Alfil (1444 intervenciones) el 31/10/2020 18:59:48
Versión más reducida sin ensamblador

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 
using namespace std;
 
int main()
{
    int numero;
 
    cout << "\nNumero: ";
    cin >> numero;
 
    cout << "\nEl numero es ";
    numero >= 0 ? cout << "Positivo" : cout << "Negatigo";
    numero % 2 == 0 ? cout << ", Par" : cout << ", Impar";
 
    cout << endl;
 
    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

numero positivo y par o inpar

Publicado por Nacho (181 intervenciones) el 31/10/2020 21:30:23
Bueno, más reducida...

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
void main()
{
 push        ebp
 mov         ebp,esp
 sub         esp,8
    int n;
    bool negativo = false;
    bool impar = false;
 
    cin >> n;
 mov         ecx,dword ptr [_imp_?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A (0A510A8h)]
 lea         eax,[n]
 push        eax
 mov         byte ptr [negativo],0
 mov         byte ptr [impar],0
 call        dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (0A5111Ch)]
    __asm
    {
        xor edx, edx
 xor         edx,edx
        mov eax, n
 mov         eax,dword ptr [n]
        bt eax, 31
 bt          eax,1Fh
        adc edx, edx
 adc         edx,edx
        mov byte ptr[negativo], dl
 mov         byte ptr [negativo],dl
        xor edx, edx
 xor         edx,edx
        bt eax, 0
 bt          eax,0
        adc edx, edx
 adc         edx,edx
        mov byte ptr[impar], dl
 mov         byte ptr [impar],dl
  }
}
 mov         esp,ebp
 pop         ebp
 ret

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
int main()
{
 mov         ecx,offset _88D22677_LosPillaos@cpp (09300Bh)
 call        @__CheckForDebuggerJustMyCode@4 (081307h)
 push        offset string " " (08DC30h)
 push        dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0910ACh)]
 call        std::operator<<<std::char_traits<char> > (0815CDh)
 mov         ecx,dword ptr [_imp_?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A (0910A8h)]
 lea         eax,[numero]
 push        eax
 call        dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (09111Ch)]
 push        offset string "\nEl numero es " (08DC3Ch)
 push        dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0910ACh)]
 call        std::operator<<<std::char_traits<char> > (0815CDh)
 cmp         dword ptr [numero],0
 jl          std::operator<<<std::char_traits<char> >+3Bh (082F3Bh)
    int numero;
    cout << "\nNumero: ";
    cin >> numero;
    cout << "\nEl numero es ";
    numero >= 0 ? cout << "Positivo" : cout << "Negatigo";
 push        offset string "Positivo" (08DC4Ch)
 jmp         std::operator<<<std::char_traits<char> >+40h (082F40h)
 push        offset string "," (08DC58h)
    numero % 2 == 0 ? cout << ", Par" : cout << ", Impar";
 push        dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0910ACh)]
 call        std::operator<<<std::char_traits<char> > (0815CDh)
 mov         eax,dword ptr [numero]
 and         eax,80000001h
 jns         std::operator<<<std::char_traits<char> >+5Ch (082F5Ch)
 dec         eax
 or          eax,0FFFFFFFEh
 add         eax,1
 jne         std::operator<<<std::char_traits<char> >+65h (082F65h)
 push        offset string ", Par" (08DC64h)
 jmp         std::operator<<<std::char_traits<char> >+6Ah (082F6Ah)
 push        offset string ", Impar" (08DC6Ch)
    cout << endl;
 push        dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0910ACh)]
 call        std::operator<<<std::char_traits<char> > (0815CDh)
 mov         ecx,dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0910ACh)]
 push        offset std::endl<char,std::char_traits<char> > (0815C8h)
 call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0910B4h)]
    return 0;
 xor         eax,eax
}
 mov         esp,ebp
 pop         ebp
 ret
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++

numero positivo y par o inpar

Publicado por Rodrigo (539 intervenciones) el 01/11/2020 01:56:04
Sugiero cambiar esto

1
numero >= 0 ? cout << "Positivo" : cout << "Negatigo";

por ...

1
numero >= 0 ? cout << "Positivo" : cout << "Negativo";

y luego por

1
cout << ((numero >= 0) ? "Positivo" : "Negativo");
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