Converter em Literal

Publicado por DAVISON MARCEL PASQUALINI (última atualização em 29/11/2011)

[ Hits: 4.401 ]

Download literal




Converte números em sua forma literal (ingles e portugues). ex:

./literal -p 10
dez

./literal -e 10
ten

Foi feito para atender uma necessidade de um colega do vol, mas como foi meio corrido ele está um pouco maior do que deveria ... mas funciona. Converte arquivos também.

  



Esconder código-fonte

#!/bin/bash
# literal.sh
# --------------------------------------
# Script Viva o Linux
# --------------------------------------
#       

func_unidade_x ()
{
case $dig in 
"0" )
saida=""
;;
"1" )
saida="one"
;;
"2" )
saida="two"
;;
"3" )
saida="three"
;;
"4" )
saida="four"
;;
"5" )
saida="five"
;;
"6" )
saida="six"
;;
"7" )
saida="seven"
;;
"8" )
saida="eight"
;;
"9" )
saida="nine"
;;
esac
}


func_dez_x ()
{

dig_ax5=`expr $num + 2`
num_1="`echo $numero  |  cut -c $dig_ax5`"

case $dig in 
"0" )
saida=""
;;
"1" )
func_dez1_x
;;
"2" )
saida="twenty"
func_dez2_x
;;
"3" )
saida="thirty"
func_dez2_x
;;
"4" )
saida="fourty"
func_dez2_x
;;
"5" )
saida="fifty"
func_dez2_x
;;
"6" )
saida="sixty"
func_dez2_x
;;
"7" )
saida="seventy"
func_dez2_x
;;
"8" )
saida="eighty"
func_dez2_x
;;
"9" )
saida="ninety"
func_dez2_x
;;
esac

}


func_dez1_x ()
{

case "$num_1" in
"0" )
saida="ten"
;;
"1" )
saida="eleven"
;;
"2" )
saida="twelve"
;;
"3" )
saida="thirteen"
;;
"4" )
saida="fourteen"
;;
"5" )
saida="fifteen"
;;
"6" )
saida="sixteen"
;;
"7" )
saida="seventeen"
;;
"8" )
saida="eighteen"
;;
"9" )
saida="nineteen"
;;
esac
avanca

}

func_dez2_x ()
{


if [ "$num_1" = "0" ]; then
avanca
else
saida="$saida -"
fi


}

func_cem_x ()
{
dig_ax2=`expr $num + 2`
dig_ax3=`expr $num + 3`
num_1="`echo $numero  | cut -c $dig_ax2-$dig_ax3`"
case $dig in 
"0" )
saida=""
;;
* )
func_unidade_x
saida="$saida hundred"
;;
esac

if [ "$num_1" = "00" ]; then
avanca
avanca
else
saida="$saida"
fi

}


avanca ()
{
num=`expr $num + 1`
cont=`expr $cont - 1`
}

resolve_numero_x ()
{

case $cont in
"0" )
;;
"1" )
func_unidade_x
;;
"2" )
func_dez_x
;;
"3" )
if [ "$numero" -eq "1000" ]; then
texto="$texto thousand"
elif [ "$numero" -gt "1000" ]; then
texto="$texto thousands"
fi
func_cem_x
saida="$saida"
;;
"4" )
func_unidade_x

;;
"5" )
func_dez_x

;;
"6" )
func_cem_x
if [ "$numero" -eq "1000000" ]; then
texto="$texto million"
elif [ "$numero" -gt "1000000" ]; then
texto="$texto millions"
fi


;;
"7" )
func_unidade_x

;;

"8" )
func_dez_x

;;
"9" )
func_cem_x

;;
* )
clear 
echo "ERROR: Number $numero exceded script max number (999.999.999)!"
;;

esac
}



principal_x ()
{
cont_x=`echo $numero | wc -c`
cont=`expr $cont_x - 1`
until  [ "$cont" -le 0 ]
do
dig_ax1=`expr $num + 1`
dig="`echo $numero  |  cut -c $dig_ax1`"

resolve_numero_x
avanca
if [ "$texto" = "" ]; then
texto="$saida"
else 
texto="$texto $saida"
fi

done



}

#-------------------------fim-----




func_unidade ()
{
case $dig in 
"0" )
saida=""
if [ "$numero" -eq 0 ]; then
saida="zero"
fi 
;;
"1" )
saida="um"
;;
"2" )
saida="dois"
;;
"3" )
saida="tres"
;;
"4" )
saida="quatro"
;;
"5" )
saida="cinco"
;;
"6" )
saida="seis"
;;
"7" )
saida="sete"
;;
"8" )
saida="oito"
;;
"9" )
saida="nove"
;;
esac
}


func_dez ()
{

dig_ax2=`expr $num + 2` 
num_1="`echo $numero  |  cut -c $dig_ax2`"

case $dig in 
"0" )
saida=""
;;
"1" )
func_dez1
;;
"2" )
saida="vinte"
func_dez2
;;
"3" )
saida="trinta"
func_dez2
;;
"4" )
saida="quarenta"
func_dez2
;;
"5" )
saida="cinquenta"
func_dez2
;;
"6" )
saida="sessenta"
func_dez2
;;
"7" )
saida="setenta"
func_dez2
;;
"8" )
saida="oitenta"
func_dez2
;;
"9" )
saida="noventa"
func_dez2
;;
esac

}


func_dez1 ()
{

case "$num_1" in
"0" )
saida="dez"
;;
"1" )
saida="onze"
;;
"2" )
saida="doze"
;;
"3" )
saida="treze"
;;
"4" )
saida="quatorze"
;;
"5" )
saida="quinze"
;;
"6" )
saida="dezesseis"
;;
"7" )
saida="dezessete"
;;
"8" )
saida="dezoito"
;;
"9" )
saida="dezenove"
;;
esac
avanca

}

func_dez2 ()
{


if [ "$num_1" = "0" ]; then
avanca
else
saida="$saida e"
fi


}

func_cem ()
{
dig_ax6=`expr $num + 1`
dig_ax3=`expr $num + 2`
dig_ax4=`expr $num + 3`
num_1="`echo $numero  | cut -c $dig_ax3-$dig_ax4`"
num_1x="`echo $numero  | cut -c $dig_ax6-$dig_ax4`"
cont1_x=`echo $numero | wc -c`
cont1=`expr $cont1_x - 1`
case $dig in 
"0" )
saida=""
;;
"1" )

if [ "$num_1" = "00" ]; then
saida="cem"
else
saida="cento"
fi
;;
"2" )
saida="duzentos"
;;
"3" )
saida="trezentos"
;;
"4" )
saida="quatrocentos"
;;
"5" )
saida="quinhentos"
;;
"6" )
saida="seiscentos"
;;
"7" )
saida="setecentos"
;;
"8" )
saida="oitocentos"
;;
"9" )
saida="novecentos"
;;
esac

if [ "$num_1" = "00" ]; then
avanca
avanca
else
saida="$saida e"
fi

}


avanca ()
{
num=`expr $num + 1`
cont=`expr $cont - 1`
}

resolve_numero ()
{

case $cont in
"0" )
;;
"1" )
func_unidade
;;
"2" )
func_dez
;;
"3" )
func_cem
if [ "$num_1x" -gt "1" -a "$cont1" -gt "3" ]; then
saida="e $saida"
fi
;;
"4" )
func_unidade
if [ "$cont" = "4" -a "$dig" -gt 0 ]; then
saida="$saida mil"
fi


;;
"5" )
func_dez
if [ "$num_1" = "0" -a "$dig" -gt "0" ]; then
saida="$saida mil"
elif [ "$dig" = "1" ]; then
saida="$saida mil"
fi
;;
"6" )
func_cem
if [ "$num_1" = "00" -a "$dig" -gt "0" ]; then
saida="$saida mil"
fi
if [ "$num_1x" -gt "1" -a "$cont1" -gt "6" ]; then
saida="e $saida"
fi
;;
"7" )
func_unidade
if [ "$cont" = "7" -a "$dig" = 1 -a "$ml" != 1 ]; then
saida="$saida milhco"
else 
saida="$saida milhoes"
fi

;;

"8" )
func_dez
if [ "$num_1" = "0" ]; then
saida="$saida milhoes"
elif [ "$dig" = "1" ]; then
saida="$saida milhoes"
fi
if [ "$dig" -gt "0" ]; then
ml=1
fi
;;
"9" )
func_cem
if [ "$num_1" = "00" ]; then
saida="$saida milhoes"
fi
if [ "$dig" -gt "0" ]; then
ml=1
fi
;;
* )
clear 
echo "ERROR: Numero $numero excedeu o valor maximo do script (999.999.999)!"
exit 0
;;

esac
}


principal ()
{
cont_ax=`echo $numero | wc -c`
#echo "DEBUG: cont_ax"
#echo "DEBUG: $cont_ax"
cont=`expr $cont_ax - 1`
#echo "DEBUG: cont"
#echo "DEBUG: $cont"
until  [ "$cont" -le 0 ]
do
#echo "DEBUG: num"
#echo "DEBUG: $num"
dig_ax=`expr $num + 1` 
#echo "DEBUG: dig_ax"
#echo "DEBUG: $dig_ax"
dig="`echo $numero  |  cut -c $dig_ax`"

resolve_numero
avanca
if [ "$texto" = "" ]; then
texto="$saida"
else 
texto="$texto $saida"
fi

done



}




#-------------------------fim-----


help ()
{

clear
echo "
User Commands                                           literal(1)

`tput bold`NAME`tput sgr0`
     literal - traduz numeros para sua representacao por extenso.

`tput bold`SYNOPSIS`tput sgr0`
     literal  [opcao][numero] [-f] [nome_do_arquivo]


`tput bold`DESCRIPTION`tput sgr0`
     Converte numeros em sua representacao por extenso.
     Valor limitado a 999999999.

`tput bold`OPTIONS`tput sgr0`
     The following options are supported:

     -p    -  numeros em portugues.

     -e    -  numeros em ingles.

     -v    -  imprime o numero entre parenteses apos a sua forma por extenso.

     -f    -  define arquivo de entrada.


     Examples:
       literal -p 1000
       literal -e 200
       literal -pv 100
       literal -p -f arquivo_entrada
       literal -ev -f arquivo_entrada > arquivo_de_saida

`tput bold`ALERT`tput sgr0`
       A conversco podera alterar a formatacao do arquivo.


`tput bold`VERSION`tput sgr0`
       Version is 1.01

`tput bold`BUG REPORT`tput sgr0`
       dmarp@ig.com.br

"
exit 0

}




le_arquivo ()
{
frase=""
cat $arquivo | sed 's/\*/#_asterisco_flag/'g | while read line 
do

num=0
le2 


echo "$frase" | sed 's/#_asterisco_flag/\*/'g
frase=""

done 

}

le2 ()

{
for i in `echo $line | xargs -n 1`; do 
expr $i / 1 2>>/dev/null 1>>/dev/null
if [ $? = 0 ]; then
numero="$i"
le_arquivo_1
# mudei
if [ "$opcao" = '-vp' ]; then
i="$numero ($texto)"
elif [ "$opcao" = '-pv' ]; then
i="$texto ($numero)"
elif [ "$opcao" = '-ve' ]; then
i="$numero ($texto)"
elif [ "$opcao" = '-ev' ]; then
i="$texto ($numero)"
else 
i="$texto"
fi
texto=""
num=0
fi

if [ "$frase" = "" ]; then
frase="$i"
else
frase="$frase $i"
fi;
done

}


le_arquivo_1 ()
{
if [ "$opcao" = '-p' ]; then
principal

elif [ "$opcao" = '-e'  ]; then
principal_x

elif [ "$opcao" = '-pv'  ]; then
principal

elif [ "$opcao" = '-vp' ]; then
principal

elif [ "$opcao" = '-ev' ]; then
principal_x

elif [ "$opcao" = '-ve'  ]; then
principal_x

else
help
exit 0
fi
}

num=0
cont_ax=0
dig_ax=0
dig_ax1=0
dig_ax2=0
dig_ax3=0
dig_ax4=0
dig_ax5=0

opcao=$1
numero="$2"
if [ "$2" = '-f'  ]; then
arquivo="$3"
le_arquivo
elif [ "$1" = '-p' ]; then
principal
echo $texto
elif [ "$1" = '-e'  ]; then
principal_x
echo $texto
elif [ "$1" = '-pv'  ]; then
principal
echo "$texto ($numero)"
elif [ "$1" = '-vp' ]; then
principal
echo "$numero ($texto)"
elif [ "$1" = '-ev' ]; then
principal_x
echo "$texto ($numero)"
elif [ "$1" = '-ve'  ]; then
principal_x
echo "$numero ($texto)"
else
help
exit 0
fi

Scripts recomendados

Automatizar scan e atualização com o ClamAV

mp3towav

Configuração do horário de verão 2006/2007 (Red Hat, Fedora)

Cálculo de número primo

verifica net e reconecta


  

Comentários
[1] Comentário enviado por alexandregms em 20/03/2012 - 11:34h

Script show de bola!
Parabéns pela lógica, ficou bem bacana!


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts