Below is the table for currency exchange from USD to 7 other currencies: D INR J
ID: 3734708 • Letter: B
Question
Below is the table for currency exchange from USD to 7 other currencies: D INR JP ND B1 USD 0.8 1.29 65.2 105.7522750 1079.13 0.000088 the curencies provided above and calculate the corresponding amount in the target currency. The program must use a hash of currency - Write a program that allows the user to input an amount of money in oneo ratio pairs and be able to handle invalid currency input. 10 pts Sample input and output: $ perl q3 p2.pl Exchangeable currency: usd, eur, cad, inr, jpy, vnd, krw, btc Enter the current currency: btc Enter the target currency: jpy Enter the amount of money: 1 1 btc is 1201704.55 jpy $ perl q3 p2.pl jpy, vnd, kru, btc Exchangeable currency: usd, eur, cad, inr, Enter the current currency: zwl We do not trade zwl! Re-enter the current currency: usd Enter your target currency: xyz We do not trade xyz! Re-enter the target currency: usd Enter the amount of money: 1000 1000 usd is 1000 usdExplanation / Answer
Steps involved in framing the program:
Step-1: We know that the perl script start with:
#!/usr/bin/perl
use warnings;
use strict;
Step-2: Now we are creating the 4 scalar variables ($from, $to, $value & $rate) & one hash variable(%rates) to store conversion rate and currency name(also known as Hash keys).
my ($from, $to, $value, $rate, %rates);
Step-3: Here we creathe the hash variable (%rates) and store the currency values given in the table,
Keep in mind that, in hash variable left side column contains hash keys by which we will
get conversion rate located in right side column.
%rates=(
dollars=>1,
eur=>0.81, # As we have used ''=>'' (Hash inplementation)
cad=>1.29,
inr=> 65.2, #Currency values on the basis of 1 Dollars
jpy=>105.75,
vnd=>22750, # Value is taken as per given table values
krw=>1079.43,
btc=>0.000088
);
Step-4: Now Create the standard input <STDIN> and we store the input of current currency
as $from and target currency as $to and the amount as $value
print"Exchangeable currency: usd , eur, cad, inr, jpy, vnd, krw, btc"
print " Enter the current currency: ";
$from= <STDIN>; #Input of current currency
print " Enter the target currency: ";
$to= <STDIN>; #Input of target currency
print "Enter the amount of money: ";
$value=<STDIN>; #Amount of money to be converted from current to target currency
chomp ($from, $to, $value); # chomp is used to remove the $/ variable which is set to mostly (new line)
Step-5 : We need to define a validation rule so that it shows some error occurence as #We dont trade in $from / $to! and we stop the process then and there only
if (not exists $rates{$from})
{
die "We do not trade $from ! "; # Invalid conversion currency input
}
if (not exists $rates{$to})
{
die "We do not trade $to ! "; #Invalid currency input
}
Step 6: Coming on the final step of calculation and printing of converted rates of currency
from current to target currency using the logic:
$rate = $rates{$to}/$rates{$from}; # Calculation on the basis of values and rates from current to target currency
print "$value $from is ", $value*$rate, "$to.n";
Final PROGRAM
#!/usr/bin/perl
use warnings;
use strict;
my ($from, $to, $value, $rate, %rates); # Initial variables defined to be used in the program
%rates=(
dollars=>1,
eur=>0.81, # As we have used ''=>'' (Hash inplementation)
cad=>1.29,
inr=> 65.2, #Currency values on the basis of 1 Dollars
jpy=>105.75,
vnd=>22750, # Value is taken as per given table values
krw=>1079.43,
btc=>0.000088
);
print"Exchangeable currency: usd , eur, cad, inr, jpy, vnd, krw, btc"
print " Enter the current currency: ";
$from= <STDIN>; #Input of current currency
print " Enter the target currency: ";
$to= <STDIN>; #Input of target currency
print "Enter the amount of money: ";
$value=<STDIN>; #Amount of money to be converted from current to target currency
chomp ($from, $to, $value); # chomp is used to remove the $/ variable # which is set to mostly (new line)
if (not exists $rates{$from})
{
die "We do not trade $from ! "; # Invalid conversion currency input
}
if (not exists $rates{$to})
{
die "We do not trade $to ! "; #Invalid currency input
}
$rate = $rates{$to}/$rates{$from}; # Calculation on the basis of values and rates # from current to target currency
print "$value $from is ", $value*$rate, "$to.n";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.