'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                   TOMADA DE DESISES
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' DESISO SIMPLES
'
'Portugues estruturado
'
'Se (<condio>) ento If (<condio>) then
'<intrues a executar caso condio seja verdadeira>
'Fim_se                                        end If
'
'Visual Basic
'
'If (<condio>) then
'<intrues a executar caso condio seja verdadeira>
'end if
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Function diferena(a, b)
diferena = 0
    If a > b Then               'Se A > B ento
        diferena = a - b       'novo valor para diferena
    End If
End Function


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'   2.2 DESISO COMPOSTA
'
'Portugues estruturado
'
'Se (<condio>) ento
'<intrues a executar caso condio seja verdadeira>
'seno
'<intrues a executar caso condio seja falsa>
'Fim_se
'
'Visual Basic
'
'If (<condio>) then
'<intrues a executar caso condio seja verdadeira>
'else
'<intrues a executar caso condio seja falsa>
'end if
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Function diferena_composta(a, b)
    If a > b Then
        diferena_composta = a - b
    Else
        diferena_composta = b - a
    End If
End Function

Function maior_(a, b As Variant)
    If a >= b Then
        maior_ = a
    Else
        maior_ = b
    End If
End Function

Function _PAR(n) As Boolean
Dim m
    m = n Mod 2
    If m = 0 Then
        _PAR = True
    Else
        _PAR = False
    End If
End Function

Function _divisivel(numero, divisor As Integer) As Boolean
Dim m
    m = numero Mod divisor
    If m = 0 Then
        _divisivel = True
    Else
        _divisivel = False
    End If
End Function


Function Aprovado(nota1, nota2, nota3, nota4 As Variant)
    If ((nota1 + nota2 + nota3 + nota4) / 4) >= 5 Then
        Aprovado = "sim"
    Else
        Aprovado = "no"
    End If
End Function


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'DESISO em cadeia
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Function salario_novo(salario As Variant)
    If salario < 500 Then
        salario_novo = salario * 1.1
    Else
        If salario <= 1000 Then
            salario_novo = salario * 1.075
        Else
            salario_novo = salario * 1.05
        End If
    End If
End Function

Sub Ordenar()
Dim a, b, C, x As Integer
Dim ln, cl As Integer
ln = 1
cl = 1
a = Cells(ln + 1, cl)
b = Cells(ln + 2, cl)
C = Cells(ln + 3, cl)
If a < b Then
x = a
a = b
b = x
End If
If a < C Then
x = a
a = C
C = x
End If
If b < C Then
x = b
b = C
C = x
End If
Cells(ln + 1, cl) = a
Cells(ln + 2, cl) = b
Cells(ln + 3, cl) = C
End Sub

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Operadores Relacionais & Logicos
'
'TABELA VERDADE E
'
'CONDIO 1     CONDIO 2      RESULTADO
'falsa          falsa           falso
'verdadeira     falsa           falso
'falsa          verdadeira      falso
'verdadeira     verdadeira      verdadeiro
'
'TABELA VERDADE OU
'
'CONDIO 1     CONDIO 2      RESULTADO
'falsa          falsa           falso
'verdadeira     falsa           verdadeiro
'falsa          verdadeira      verdadeiro
'verdadeira     verdadeira      verdadeiro
'
'TABELA VERDADE NO
'
'CONDIO       RESULTADO
'verdadeira     falso
'falso          verdadeiro
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Function Relacional_igual(a, b) As Boolean
Relacional_igual = True
    If Not a = b Then
        Relacional_igual = False
    End If
End Function
Function Relacional_diferente(a, b) As Boolean
Relacional_diferente = False
    If a <> b Then
        Relacional_diferente = True
    End If
End Function
Function Relacional_igual_menor(a, b) As Boolean
    If a >= b Then
        Relacional_igual_menor = True
    Else
        Relacional_igual_menor = False
    End If
End Function
Function Relacional_igual_maior(a, b) As Boolean
    If Not a <= b Then
        Relacional_igual_maior = False
    Else
        Relacional_igual_maior = True
    End If
End Function
Function Relacional_maior(a, b) As Boolean
Relacional_maior = True
    If Not a > b Then
        Relacional_maior = False
    End If
End Function
Function Relacional_menor(a, b) As Boolean
Relacional_menor = False
    If Not a > b Then
        Relacional_menor = True
    End If
End Function

Function logica_e(valor, menor, maior)
    If valor >= menor And valor <= maior Then
        logica_e = "correto"
    Else
        logica_e = "errado"
    End If
End Function

Function logica_ou(valor, menor, maior)
    If valor <= menor Or valor >= maior Then
        logica_ou = "correto"
    Else
        logica_ou = "errado"
    End If
End Function

Function logica_nao(valor, menor, maior)
    If Not logica_e(valor, menor, maior) = "correto" Then
        logica_nao = "correto"
    Else
        logica_nao = "errado"
    End If
End Function


Function tipo_Triangulo(lado_a, lado_b, lado_c)
'Dicide se o valores formam um tipo_Triangulo
'sendo triangulo diz qual tipo Equilatero,Isosceles ou Escaleno
    If lado_a < lado_b + lado_c And lado_b < lado_a + lado_c And lado_c < lado_a + lado_b Then
        If lado_a = lado_b And lado_b = lado_c Then
            tipo_Triangulo = "Equilatero"
        Else
            If lado_a = lado_b Or lado_a = lado_c Or lado_b = lado_c Then
                tipo_Triangulo = "Isosceles"
            Else
                tipo_Triangulo = "Escaleno"
            End If
        End If
    Else
        tipo_Triangulo = "falso"
    End If
End Function


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                           LOOPINGs
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Variavel de controle
'
'looping para
'
'Portugues Estruturado
'
'para <variavel> de <inicio> at <fim> passo <incremento> faa
'<instruo para condio verdadeira>
'fim_para
'
'Visual Basic
'
'for <variavel> = <inicio> to <fim> step <incremento>
'<instruo para condio verdadeira>
'next
Sub Fatorial(numero)
Dim Cont
    For Cont = 1 To numero  'PARA cont DE 1 AT numero
        Fatorial = Fatorial * Cont
    Next                    'PROXIMO
End Sub

Function Fibonacci(termo)
Dim Cont, Post, Ante As Integer
Ante = 0
Post = 1
    For Cont = 2 To termo
        Fibonacci = Post + Ante
        Ante = Post
        Post = Fibonacci
    Next
End Function

Sub tabuada()
Dim total, Cont, num As Integer
num = InputBox("tabuada de qual numero", "Tabuada")
For Cont = 1 To 10
total = Cont * num
Cells(Cont, 1) = num & " X " & Cont & " = " & total
Next
End Sub

'Teste Logico no INICIO do LOOPING
'
'looping enquanto
'
'Portugues Estruturado
'
'enquanto (<condiao>) faa
'<instruo para condio verdadeira>
'fim_enquanto
'
'Visual Basic
'
'Do While (<condio>)
'<instruo para condio verdadeira>
'Loop
'
Function Fatorial_enquanto(numero) As Double
Dim Cont As Integer
Cont = 1
Fatorial_enquanto = 1
    Do While Cont <= numero
        Fatorial_enquanto = Fatorial_enquanto * Cont
        Cont = Cont + 1
    Loop
End Function

Function Fibonacci_enquanto(termo)
Dim Cont, Post, Ante As Integer
Ante = 0
Post = 1
Cont = 2
    Do While Cont <= termo
        Fibonacci_enquanto = Post + Ante
        Ante = Post
        Post = Fibonacci_enquanto
        Cont = Cont + 1
    Loop
End Function
Function Logaritmo(Base, numero)
Dim total
total = Base
Logaritmo = 1
    Do While total < numero
        total = total * Base
        Logaritmo = Logaritmo + 1
    Loop
End Function

Function _PRIMO(n) As Boolean
Dim fat As Integer
Dim ult As Double
_PRIMO = True
If n > 3 Then 'VERIFICA SE N  MAIOR QUE 3
If _PAR(n) = False Then 'VERIFICA SE N  PAR
 fat = 3
 ult = RAIZ(n, 2)
  Do While _PRIMO = True And fat <= ult
   If n Mod fat = 0 Then 'SE N FOR DIVISIVEL PELO FATOR ...
   _PRIMO = False 'NO  PRIMO POIS FOI DIVIDO PELO FATOR
   Else
   fat = fat + 2 'FATOR VAI PARA O PROXIMO NUMERO IMPAR
   End If
  Loop
Else
_PRIMO = False 'SE PAR VERDADEIRO PRIMO  FALSO
End If
End If
End Function

Sub tabuada_enquanto()
Dim total, Cont, num As Integer
Cont = 1
num = InputBox("tabuada de qual numero", "Tabuada")
Do While Cont <= 10
total = Cont * num
Cells(Cont, 1) = num & " X " & Cont & " = " & total
Cont = Cont + 1
Loop
End Sub

'Teste Logico no FINAL do LOOPING
'
'looping repita
'
'Portugues Estruturado
'
'repita
'<instruo para condio verdadeira>
'ate_que (<condio>)
'
'Visual Basic
'
'Do
'<instruo para condio verdadeira>
'Loop While (<condio>)
'
Function Fatorial_repita(numero)
Dim Cont As Single
Cont = 1
Fatorial_repita = 1
    Do
        Fatorial_repita = Fatorial_repita * Cont
        Cont = Cont + 1
    Loop While Cont <= numero
End Function
Function Fibonacci_repita(termo)
Dim Cont, Post, Ante As Integer
Ante = 0
Post = 1
Cont = 2
    Do
        Fibonacci_repita = Post + Ante
        Ante = Post
        Post = Fibonacci_repita
        Cont = Cont + 1
    Loop While Cont <= termo
End Function

Function MDC(maior, menor)
Dim Resto
    Do
        Resto = menor Mod maior 'operador mod retorna o resto da diviso
        If Resto = 0 Then
            MDC = maior
        Else
            menor = maior
            maior = Resto
        End If
    Loop While MDC <> maior
End Function


Sub tabuada_repita()
Dim total, Cont, num As Integer
    Cont = 1
    num = InputBox("tabuada de qual numero", "Tabuada")
    Do
        total = Cont * num
        Cells(Cont, 1) = num & " X " & Cont & " = " & total
        Cont = Cont + 1
    Loop While Cont <= 10
End Sub
