[EMAIL PROTECTED] wrote on 05/01/2005 10:50:38 AM:
> May anyone tell me if this program is wrong or if there is a
> implementation problem in perl 5.8.6, 5.8.5, 5.8.2
> I will show the perl version and a c++ version. The c++ version
> worked. I will also show the version of the perl version that
> worked with a number changed.
>
The "correct" version works for me with the following change. Please
provide examples of failing input and output.
>
> The correct version of perl:
>
> #!/usr/bin/perl
> # Input an integer containing only 0s and 1s and print its decimal
> equivalent
>
> $counter = 1;
> $counter2 = 1;
> $total = 0;
>
> print "Enter a binary number\n";
> $number = ;
> chomp $number;
>
> while( ($number / $counter) != 0 )
while( int($number / $counter) != 0 ) {
> {
> $number1 = ($number / $counter) % 10;
> $total = $total + ($number1 * $counter2);
> $counter = $counter * 10;
> $counter2 = $counter2 * 2;
> }
>
> print "The decimal equivalent of ", $number, " is ", $total, "\n";
>
> perl wrong version:
>
> #!/usr/bin/perl
> # Input an integer containing only 0s and 1s and print its decimal
> equivalent
>
> $counter = 1;
> $counter2 = 1;
> $total = 0;
>
> print "Enter a binary number\n";
> $number = ;
> chomp $number;
>
> while( ($number / $counter) != 0 )
> {
> $number1 = ($number / $counter) % 10;
> $total = $total + ($number1 * $counter2);
> $counter = $counter * 10;
> $counter2 = $counter2 * 16; // changed 2 to 16
> }
>
> print "The decimal equivalent of ", $number, " is ", $total, "\n";
>
>
> The c++ version of the wrong perl version:
>
> #include
> using namespace std;
>
> int main(int argc, char *argv[])
> {
> int counter = 1;
> int counter2 = 1;
> long int total = 0;
> int number;
> int number1;
>
> cout << "Enter a binary number" << endl;
> cin >> number;
>
> while( (number / counter) != 0 )
> {
> number1 = (number / counter) % 10;
> total = total + (number1 * counter2);
> counter = counter * 10;
> counter2 = counter2 * 16;
> }
>
> cout << "The decimal equivalent of " << number << " is " <<
> total << endl;
>
> return 0;
> }
>
>
> _______________________________________________
> ActivePerl mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs