Talk: Perl check program oplæg

From Teknologisk videncenter
Jump to: navigation, search

Mulig løsning (noget rodet)

#!/usr/bin/perl -w
#
use strict;
use warnings;


#program version
my $VERSION="0,1";

my @e  = qw(en to tre fire fem seks syv otte ni ti);
my @pe = qw(et to tre fire fem seks syv otte ni ti);
my @teen = qw(eleve tolv tretten fjorten femten seksten sytten atten nitten);
my @postteen = qw(tyve tredive fyrre halvtres tres halvfjers firs halvfems);
my $tal;
my $rest;
my $check;
my $decimal = 0;

my $MAKS=10000;

sub hvormange {
	# Returnere en taltekst for tal over 99.
	# Input : base
	# Input : tal
	#Output : taltekst
	
	my $base = shift;
	my $tal = shift;
	
	my $retur;
	my @t_base = qw(hundrede tusinde);
	my $post;
	
	my $rest = $tal%$base;
	my $antal = ($tal-$rest)/$base;
	
	if ( $base == 100 ) {
		$post = $t_base[0];
	} else {
		$post = $t_base[1];
	}
	
	if ($antal < 10) {
		return "$pe[$antal-1]$post";
	}
	
	if (($antal < 20) && ($base == 1000)) {
		return "$teen[$antal-11][$post]";
	}
	
	if (($antal < 100) && ($base == 1000)) {
		return "$postteen[$antal-($antal%10)/10]";
	}
	
}


print "Indtast tal (under $MAKS): ";
$tal = <>;
chomp $tal;

$decimal = $tal-int $tal;
if ($tal >= $MAKS) {
	print "Tallet skal være mindre end $MAKS\n";
}

if ( $tal > 999 ){
	$check .= hvormange(1000,$tal);
	$tal = $tal-($tal-$tal%1000);
}

if ( $tal > 99 ) {
	$check .= hvormange(100,$tal);
	$tal = $tal-($tal-$tal%100);
	}

if ( $tal > 19 ) {
	if (($rest = $tal%10) > 0){
		$check .= $e[$rest-1]; 
		#printf "Rest %d %d %d\n", $rest, $tal, ($tal-$rest)/10-2;
		$check .= "og".$postteen[($tal-$rest)/10-2];
		$tal = $tal-int $tal;
	} else {
		$check .= "og".$postteen[($tal-$rest)/10-2];
		$tal -= int $tal;
	}
}

if ( $tal > 9 ){
	$check .= $teen[($tal%10)-1];
	$tal -= int $tal;
}

if ( $tal > 0 ) {
	$check .= $e[$tal-1];
	$tal -= int $tal;
	}
	
$check .= sprintf " %2d/100", $decimal*100;
print "$check\n";




__END__