Solução do laboratório 1 #1

Open
opened 2026-03-21 15:38:59 -03:00 by blau_araujo · 1 comment
Owner

Deixe aqui a sua implementação para o problema da conversão de temperaturas.

Deixe aqui a sua implementação para o [problema da conversão de temperaturas](https://bolha.dev/blau_araujo/l3c/src/branch/main/lab/01).
#!/usr/bin/env bash

#----------------------------------------------------------
# Função para verificar se o argumento é válido
#----------------------------------------------------------

verifica_valor(){
  [[ ${#1} > 1 && $1 =~ ^[-+]?[0-9]+([.][0-9]+)?[a-zA-Z]$ ]]
}

#----------------------------------------------------------
# funções para conversão
#----------------------------------------------------------

farenheit(){
 local C=$(echo "scale=2; ($1 - 32) * 1.8" | bc)
 local K=$(echo "scale=2; ($1 - 32) * 1.8 + 273.15" | bc)
 echo "C:$C F:$1* K:$K"
}

kelvin(){
 C=$(echo "scale=2; $1 - 273.15" | bc)
 F=$(echo "scale=2; 1.8 * ($1 - 273.15) + 32" | bc)
 echo "C:$C F:$F K:$1*"
}

celsius(){
 F=$(echo "scale=2; $1 * 1.8 + 32" | bc)
 K=$(echo "scale=2; $1 + 273.15" | bc)
 echo "C:$1* F:$F K:$K"
}


#----------------------------------------------------------
# função para tratar escolha
#----------------------------------------------------------

handle(){
 numero=${1:0:((${#1} - 1))}
 unidade=${1:((${#1} - 1)):1}

 if [[ $1 == *,* ]]; then
   echo "Use ponto (.) como separador decimal: $1"
   return
 fi

 if ! verifica_valor "$1"; then
   echo "Argumento inválido: $1"
   return
 fi

 if [[ ${unidade^} == "K" && $(echo "$numero < 0" | bc) -eq 1 ]]; then
  echo "Kelvin não pode ser negativo"
  return
 fi

 case ${unidade^} in
  "F") farenheit "$numero" ;;
  "K") kelvin "$numero"    ;;
  "C") celsius "$numero"   ;;
    *) echo "Unidade inválida" ;;
 esac
}


#----------------------------------------------------------
# função para conferir origem do dado 
#----------------------------------------------------------

handle_input() {
  if [ -t 0 ]; then

   if [ $# -eq 0  ]; then
    echo "Modo interativo"
    while true; do
     read -p "Valor: " -a valor
     
     if [ "${#valor[@]}" -gt 1  ]; then
       for val in "${valor[@]}"; do
         handle "$val"
       done
       break
     else
       handle $valor
       break
     fi
    done

   elif [ $# -gt 1  ]; then
    for arg in $@; do
     handle $arg
    done
   else
     handle $1
   fi

  else
   echo "Argumentos por pip/redirecionamento"
   while read -r valor; do
     handle "$valor"
   done
  fi
}

handle_input $@

exit 0
```bash #!/usr/bin/env bash #---------------------------------------------------------- # Função para verificar se o argumento é válido #---------------------------------------------------------- verifica_valor(){ [[ ${#1} > 1 && $1 =~ ^[-+]?[0-9]+([.][0-9]+)?[a-zA-Z]$ ]] } #---------------------------------------------------------- # funções para conversão #---------------------------------------------------------- farenheit(){ local C=$(echo "scale=2; ($1 - 32) * 1.8" | bc) local K=$(echo "scale=2; ($1 - 32) * 1.8 + 273.15" | bc) echo "C:$C F:$1* K:$K" } kelvin(){ C=$(echo "scale=2; $1 - 273.15" | bc) F=$(echo "scale=2; 1.8 * ($1 - 273.15) + 32" | bc) echo "C:$C F:$F K:$1*" } celsius(){ F=$(echo "scale=2; $1 * 1.8 + 32" | bc) K=$(echo "scale=2; $1 + 273.15" | bc) echo "C:$1* F:$F K:$K" } #---------------------------------------------------------- # função para tratar escolha #---------------------------------------------------------- handle(){ numero=${1:0:((${#1} - 1))} unidade=${1:((${#1} - 1)):1} if [[ $1 == *,* ]]; then echo "Use ponto (.) como separador decimal: $1" return fi if ! verifica_valor "$1"; then echo "Argumento inválido: $1" return fi if [[ ${unidade^} == "K" && $(echo "$numero < 0" | bc) -eq 1 ]]; then echo "Kelvin não pode ser negativo" return fi case ${unidade^} in "F") farenheit "$numero" ;; "K") kelvin "$numero" ;; "C") celsius "$numero" ;; *) echo "Unidade inválida" ;; esac } #---------------------------------------------------------- # função para conferir origem do dado #---------------------------------------------------------- handle_input() { if [ -t 0 ]; then if [ $# -eq 0 ]; then echo "Modo interativo" while true; do read -p "Valor: " -a valor if [ "${#valor[@]}" -gt 1 ]; then for val in "${valor[@]}"; do handle "$val" done break else handle $valor break fi done elif [ $# -gt 1 ]; then for arg in $@; do handle $arg done else handle $1 fi else echo "Argumentos por pip/redirecionamento" while read -r valor; do handle "$valor" done fi } handle_input $@ exit 0 ```
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
blau_araujo/l3c#1
No description provided.