[LON-CAPA-cvs] cvs: loncom /homework/caparesponse caparesponse.pm

sakharuk lon-capa-cvs@mail.lon-capa.org
Tue, 01 Oct 2002 14:10:44 -0000


sakharuk		Tue Oct  1 10:10:44 2002 EDT

  Modified files:              
    /loncom/homework/caparesponse	caparesponse.pm 
  Log:
  Added part to define the type of numerical answer - this is neccessary 
  for formated output.
  
  
Index: loncom/homework/caparesponse/caparesponse.pm
diff -u loncom/homework/caparesponse/caparesponse.pm:1.61 loncom/homework/caparesponse/caparesponse.pm:1.62
--- loncom/homework/caparesponse/caparesponse.pm:1.61	Mon Sep 30 14:32:45 2002
+++ loncom/homework/caparesponse/caparesponse.pm	Tue Oct  1 10:10:44 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # caparesponse definition
 #
-# $Id: caparesponse.pm,v 1.61 2002/09/30 18:32:45 sakharuk Exp $
+# $Id: caparesponse.pm,v 1.62 2002/10/01 14:10:44 sakharuk Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -225,13 +225,23 @@
         if ($target eq 'web') {
 	    $result.= '<table border="1"><tr>';
 	    for ($ind=0;$ind<$number_of_bubbles;$ind++) {
+		my $ans;
 		if ($formats[0] ne '') {
-		    my $ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
-		    $result.='<td>'.$alphabet[$ind].': '.$ans.'</td>';
+		    $ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
 		} else {
-		    my $ans = sprintf('%.'.'d',$bubbles_values[$ind]);
-		    $result.='<td>'.$alphabet[$ind].': '.$ans.'</td>';
+		    my $badans = $bubbles_values[$ind];
+		    my $format = ''; 
+                    #What is the number? (integer,decimal,floating point)
+                    if ($badans=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
+			$format = 'e'.$2;
+		    } elsif ($badans=~/^(\d*)\.(\d*)$/) {
+			$format = '4f';
+		    } elsif ($badans=~/^(\d*)$/) {
+			$format = 'd';
+		    }
+		    $ans = sprintf('%.'.$format,$bubbles_values[$ind]);
 		}
+		    $result.='<td>'.$alphabet[$ind].': '.$ans.'</td>';
 	    }
 	    $result.='</tr></table>';
 	} elsif ($target eq 'tex') {
@@ -260,13 +270,23 @@
 		}
 		$result.='}\hline';
 		for ($ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
+		    my $ans;
 		    if ($formats[0] ne '') {
-			my $ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
-			$result.=' '.$alphabet[$ind].': & '.$ans.' ';
+			$ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
 		    } else {
-			my $ans = sprintf('%.'.'d',$bubbles_values[$ind]);
-			$result.=' '.$alphabet[$ind].': & '.$ans.' ';
+			my $badans = $bubbles_values[$ind];
+			my $format = ''; 
+			#What is the number? (integer,decimal,floating point)
+			if ($badans=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
+			    $format = 'e'.$2;
+			} elsif ($badans=~/^(\d*)\.(\d*)$/) {
+			    $format = '4f';
+			} elsif ($badans=~/^(\d*)$/) {
+			    $format = 'd';
+			}
+			$ans = sprintf('%.'.$format,$bubbles_values[$ind]);
 		    }
+		    $result.=' '.$alphabet[$ind].': & '.$ans.' ';
 		    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
 		}
 		$j++;