my $differenz = 0;

sub setdifferenz {
    my ($ergebnis) = @_;
    $differenz = $ergebnis;
}

sub setsubtraktion {
    my ($subtrahend) = @_;
    $differenz = $differenz - $subtrahend;
}

sub getdifferenz {
    return $differenz;
}

sub subtrahiereneinlesen {
    my ( $nummer, $summand, $summandexp ) = @_;

    my ($stellensummand);

    if ( $summandexp >= 0 ) {
	$stellensummand = vorkommastellen( $summand * 10**$summandexp );
    }
    else {
	$stellensummand = vorkommastellen($summand);
	if ( $stellensummand <= abs($summandexp) ) {
	    $stellensummand = abs($summandexp) + 1;
	}
    }
    setspaltenanzahl( $stellensummand + 1 );

    schreibezahlintabelle( $summand, $summandexp, $nummer, $stellensummand );
    if ( $nummer > 1 ) {
	setsubtraktion( $summand * 10**$summandexp );
	setaddition( $summand * 10**$summandexp );

    }
    else {
	schreibezahlintabelle( $summand, $summandexp, 0, $stellensummand );
	setsumme(0);
	setdifferenz( $summand * 10**$summandexp );
    }
    setspaltenanzahl( vorkommastellen( getdifferenz() ) );

}

sub subtrahierenauslesen {
    my ($anzahlzeilen) = @_;
    my ( $j, $i, $speicherzahl, $uebertrag, $teilsumme, $teildifferenz, $minuend );

    for ( $i = 0 ; $i <= $anzahlzeilen ; $i++ ) {
	for ( $j = 1 ; $j <= getspaltenanzahl() ; $j++ ) {
	    $speicherzahl = zusammensetzen( $i, 1, $j );
	}
	for ( $j = 1 ; $j <= getspaltenanzahl() ; $j++ ) {
	    schreibezelle( "", $i, $j );
	}
	schreibezahlintabelle( $speicherzahl, 0, $i, getspaltenanzahl() );

	if ( $i > 1 ) {
	    schreibezelle( "\$-\$", $i, 1 );
	}
    }
    for ( $j = 1 ; $j <= getspaltenanzahl() ; $j++ ) {
	schreibezelle( "", $anzahlzeilen + 2, $j );
    }

    for ( $j = 1 ; $j <= getspaltenanzahl() ; $j++ ) {
	schreibezahlintabelle( 0, 0, $anzahlzeilen + 1, $j );
	schreibezahlintabelle( 0, 0, $anzahlzeilen + 3, $j );
	schreibezahlintabelle( 0, 0, $anzahlzeilen + 5, $j );
    }

    schreibezahlintabelle( getsumme(), 0, $anzahlzeilen + 3, getspaltenanzahl() );

    schreibezahlintabelle( getdifferenz(), 0, $anzahlzeilen + 4, getspaltenanzahl() );

    for ( $i = 1 ; $i <= $anzahlzeilen ; $i++ ) {
	lesetabellenzeile( $i, getspaltenanzahl() );
    }

    $uebertrag = $teilsumme = 0;
    for ( $i = getspaltenanzahl() ; $i > 0 ; $i-- ) {
	for ( $j = 2 ; $j <= $anzahlzeilen ; $j++ ) {
	    if (   ( $rechne[$j][$i] eq "" )
		   || ( $rechne[$j][$i] eq "\$-\$" ) )
	    {
		$teilsumme = $teilsumme;

	    }
	    else {
		$teilsumme += $rechne[$j][$i];
	    }

	    $uebertrag = ( $teilsumme - ( $teilsumme % 10 ) ) / 10;

	    if ( $uebertrag == 0 ) {
		$rechne[ $anzahlzeilen + 1 ][ $i - 1 ] = 0;
	    }
	    else {
		$rechne[ $anzahlzeilen + 1 ][ $i - 1 ] = $uebertrag;
	    }

	    $rechne[ $anzahlzeilen + 2 ][$i] = $teilsumme % 10;
	}

	$teilsumme = $uebertrag;
    }

    $teildifferenz = $minuend = 0;
    for ( $j = 1 ; $j <= getspaltenanzahl() ; $j++ ) {
	$minuend = zusammensetzen( 0, 1, $j );
    }
    for ( $j = getspaltenanzahl() ; $j > getspaltenanzahl() - vorkommastellen($minuend) ; $j-- ) {
	$teildifferenz = $rechne[0][$j] - $rechne[ $anzahlzeilen + 3 ][$j];
	if ( $teildifferenz >= 0 ) {
	    $rechne[ $anzahlzeilen + 5 ][ $j - 1 ] = $rechne[ $anzahlzeilen + 1 ][ $j - 1 ] + 0;
	}
	else {
	    $rechne[ $anzahlzeilen + 5 ][ $j - 1 ] = $rechne[ $anzahlzeilen + 1 ][ $j - 1 ] + 1;
	}
    }

    for ( $j = 1 ; $j <= getspaltenanzahl() ; $j++ ) {
	if ( $rechne[ $anzahlzeilen + 5 ][$j] == 0 ) {
	    $rechne[ $anzahlzeilen + 5 ][$j] = "";
	}
	else {
	    $rechne[ $anzahlzeilen + 5 ][$j] = "\\tiny{" . $rechne[ $anzahlzeilen + 5 ][$j] . "}";
	}
    }

    lesetabellenzeile( $anzahlzeilen + 5, getspaltenanzahl() );
    ausgabe("\\hline \\hline");
    lesetabellenzeile( $anzahlzeilen + 4, getspaltenanzahl() );
}
