Help with assignment2

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with assignment2
# 1  
Old 12-04-2010
Data Help with assignment2

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Specifications:
The user has the option of providing the conversion requests from the command line or if this information is not entered on the command line, the user is to be requested for this information by the script.
The user will provide up to 3 pieces of information: country symbol, amount to be converted and the country symbol to convert to.
e.g.
$ exchange.bash Cad 2000 Gbp
will covert Canadian dollars into British pounds.

The country symbol can be entered in capital letters, small letters or the first letter is capital and the rest are small, any other combination is illegal. e.g. CAD, cad or Cad for Canadian currency.
If the user does not enter the converting information on the command line, the script should prompt the user to enter them all as a single line. e.g.
CAD 50 IRR
If the user entered incorrect information on the command line or at the prompt, your script should tell the user which argument is incorrect and request the whole data to be re-entered as a single line, as stated above.
You remain in this loop until all data entered is correct.

Input
Valid user input can be in the following formats:
Country name to convert from: Three letter country symbol, format is: XXX, xxx or Xxx.
Amount: Currency amount to be converted, must be an integer or a decimal number with 2 decimal places.
Country name to convert to: Three letter country symbol. (For Canadian to US and US to Canadian conversions, this argument is optional)

There are only two exceptions to the rule of supplying the symbols of the two countries for converting, if the user enters only 2 arguments and:
1. If the user enters Canada as the country to convert from, then the conversion will automatically be to US Dollars.
2. If the user enters US as the country to convert from, then the conversion will automatically be to Canadian Dollars.

Example 1:
$ exchange.bash cad 1455.25
Canadian: 1.00 Dollar
US: 0.99 Dollar

$1455.25 Canadian is equivalent to $1439.68 US

Have to create a script to convert currency rates with the use of data from a file called conver.table.
The file contains this kind of data.
Code:
Country name    Symbol   Rate
Danish Krone    (DKK)   5.4268
Dominican Republic Peso (DOP)   36.7022
East Caribbean Dollar   (XCD)   2.63782
Egyptian Pound  (EGP)   5.7727
European Euro   (EUR)   0.72178
Fiji Dollar     (FJD)   1.8166
Gold Ounce      (XAU)   0.000721864
Hong Kong Dollar        (HKD)   7.66051

Now, what i have to do is create use command so it compares my input with the data of that file and calculates the rates.
For example,
I have created a bash fil named mess.bash
Now, in command line if I use mess.bash usd 50 cad
it should give me the ouput like this,
$456.78 US is equivalent to $461.72 Canadian.
2. Relevant commands, code, scripts, algorithms:
Valid user input can be in the following formats:
Country name to convert from: Three letter country symbol, format is: XXX, xxx or Xxx.
Amount: Currency amount to be converted, must be an integer or a decimal number with 2 decimal places.
Country name to convert to: Three letter country symbol. (For Canadian to US and US to Canadian conversions, this argument is optional) There are only two exceptions to the rule of supplying the symbols of the two countries for converting, if the user enters only 2 arguments and:
1. If the user enters Canada as the country to convert from, then the conversion will automatically be to US Dollars.
2. If the user enters US as the country to convert from, then the conversion will automatically be to Canadian Dollars.

Output part:
  • Each time your output is referring to Canadian or US Dollars, the dollar number must be preceded by a $ sign, such as $1540 Canadian, or $164.55 US. (The word Dollar should not appear).
  • The resultant currency displayed and the currency exchange rate should always be rounded off to 2 decimal places.
  • The output of the script should be formatted so that the name of the country in the 1st 2 lines, always starts at the 1st character on the screen, the decimal point of the currency exchange rates should always be the 35th character, and the currency unit always starts at character 41.
Example 2:
$ exchange.bash CAD 50 Irr
Canadian: 1.00 Dollar
Iranian: 10230.19 Rial


$50 Canadian is equivalent to 511509.50 Iranian Rials.
  • If the original or resulting amount is more than 1 then you must display the currency name with an "s" at the end, e.g. Rials versus Rial.
  • The resulting output line must be in the format and text as per examples provided.
There should be a blank line between the 2nd and last line.

3. The attempts at a solution (include all code and scripts):
I have tried following commands to do this but it doesnt work.
I havent done the mathematical part yet.

Code:
awk -F"\t" '$1==NR && NR==9 {print $3}' convert.table
awk -F"\t" 'NR==9 || $1==$x {print $1,$2,$3}' convert.table
awk 'NR==FNR{arr[$1];next}$x in arr{print}' convert.table
awk -F"\t" '{if($1~ /NR==$2/) { print $3 }}' convert.table
awk -F"\t" 'y=$2 && NR==2{print $3}' convert.table

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Seneca College,Toronto,Canada,C. Jankul,Ops435
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by newbee006; 12-05-2010 at 06:37 PM..
# 2  
Old 12-05-2010
I think your problem is not the script but even before. Let us talk about the program structure prior to attempting to solve it in any language.

1. You need some kind of input validation. How should your program react when the input is unintelligible? Throw an error? Try to correct it? Leave it to the user to recognize that some output might be nonsense? Have correct input as a (silent) prerequisite?

Every answer to this is "valid" in its own right, but you will have to answer it before you can even try to create some program logic. Programming is automated problem solving - you could, using your file, convert currencies yourself (otherwise you wouldn't be able to write program at all). The program is just a generalized, fomalized description of what you would do.

It is good style to document with every possible exactitude what *is* done by the program and what *isn't*. Doing "A and B, but not C and D" is absolutely ok, but stopping for no obvious reason is not. Leaving it to the user to find out when the program output is valid and when it is non-sensical is not ok either.

2) Create a program logic before creating code. This helps in bringing your thoughts to formal conclusion. There are some formal approaches to this, but every method is as good as any other - what really helps is that you do it at all.

You probably have seen people - programmers - hearing a problem description and start right away typing code. They do in fact the same - create some plan first - but they are that much trained that they can do it in mind and instantly. Very likely they go back to coming up with such a "program plan" first before writing the code once the problem gets complex enough.

Write your plan here, we will discuss it and only then implement it.

I hope this helps.

bakunin
# 3  
Old 12-05-2010
Hi,
Sorry for not explaining properly.
But I am done the verification part.
Here is my verifcation part,
Code:
x=$1
y=$2
z=$3

#This if statement checks if there ar any arguments is or not.

if [ "$#" = 0 ]; then
echo "Please enter data in one line seperated by a space. ex; CAD 2000 INR" ; read x y z
set $x $y $z
fi

#This if statement checks if there are 2 or 3 arguments or not.

if [ "$#" -lt 2 ] ; then
echo "          You entered "$*
echo $# "arguments were entered, please enter 2 or 3 arguments" ; read x y z


#This if statement checks if argument 1 is CAD or USA or not.

elif [ "$#" -eq 2 ]; then
echo ""
elif ! echo $1| grep -Ewq '[A-Z][A-Z][A-Z]|[A-z][a-z][a-z]' ; then
echo "          You entered "$1 $2
echo $1 "is an invalid entry, please re-enter new data."i ; read x y z

#This if statement checks if argument 2 is numeric or not.

elif  echo $2| grep -vq "^-\?[0-9]\+$" &&  echo $2| grep -vq "^-\?[0-9]*[0-9]\.[0-9]$" && echo $2| grep -vq "^-\?[0-9]*[0-9]\.[0-9][0-9]$"; then
echo "          You entered "$1 $2
echo $2 "is an invalid entry, please re-enter new data." ; read x y z

else

#This if statement checks if there are 3 arguments entered and they all are correct.

        if ! echo $1| grep -Ewq '[A-Z][A-Z][A-Z]|[A-z][a-z][a-z]'; then
                echo "          You entered "$1 $2 $3
                echo $1 "is an invalid entry, please re-enter new data." ; read x y z

        elif  echo  $2| grep -vq "^-\?[0-9]\+$" &&  echo  $2| grep -vq "^-\?[0-9]*[0-9]\.[0-9]$" && echo  $2| grep -vq "^-\?[0-9]*[0-9]\.[0-9][0-9]$"; then
                echo "          You entered "$1 $2 $3
                echo $2 "is an invalid entry, please re-enter new data." ; read x y z

        elif ! echo $3| grep -Ewq '[A-Z][A-Z][A-Z]|[A-z][a-z][a-z]'; then
                echo "          You entered "$1 $2 $3
                echo $3 "is an invalid entry, please re-enter new data." ; read x y z

        elif [ "$#" -gt 3 ] ; then
        echo "          You entered "$*
        echo $# "arguments were entered, please enter 2 or 3 arguments" ; read x y z

        fi
fi


All want to do is, get this output.
Output part:
  • Each time your output is referring to Canadian or US Dollars, the dollar number must be preceded by a $ sign, such as $1540 Canadian, or $164.55 US. (The word Dollar should not appear).
  • The resultant currency displayed and the currency exchange rate should always be rounded off to 2 decimal places.
  • The output of the script should be formatted so that the name of the country in the 1st 2 lines, always starts at the 1st character on the screen, the decimal point of the currency exchange rates should always be the 35th character, and the currency unit always starts at character 41.
Example 2:
-> as per this example i have to get the decimal point on 35th char, and "D" of Dollar on 41st char.
$ exchange.bash CAD 50 Irr
Canadian: 1.00 Dollar
Iranian: 10230.19 Rial

-> For this line, I need to get "s" after Dollar or Rial if its more than 1.
$50 Canadian is equivalent to 511509.50 Iranian Rials.
  • If the original or resulting amount is more than 1 then you must display the currency name with an "s" at the end, e.g. Rials versus Rial.
  • The resulting output line must be in the format and text as per examples provided.
-> to get this kind of example i have done this,
->the echo line is checking if the decimal point is on 35th char and the "D" of Dollar on 41st char on both line generated by 1st two awk line.
but its not correct it doesnt work properly.
Code:
echo "12345678901234567890123456789012345678901234567890"
awk '$3~/'$x'/ {printf "%s: %35s %41s ",$1, $4, $2}' convert.table.103
awk '$3~/'$z'/ {printf "\n%s: %27.2f %41c \n",$1, $4, $2}' convert.table.103

->this awk fetches data from the file and does the math,
Code:
 
awk -v original="$x" -v new="$z" '$3~/'$z'/ {value='$2'*$4;printf  "\n$%.2f %s is equal to  %.2f %s\n", '$y',$1,value, $1 ,$2}'  convert.table


hope you get what i want to say.


---------- Post updated at 10:43 PM ---------- Previous update was at 01:08 PM ----------

THis thread is closed now.
THank you for ur help.

Last edited by newbee006; 12-05-2010 at 02:26 PM..
# 4  
Old 12-06-2010
: Packaging in Python

Dear All,

I python program which print "Hello World".

I want to run this program in 100 system.

I dont want to copy and run this script. I want to make .deb package which is easy to install by gui double click itself.

Is this possible ?, If yes, how to do.

PS: I am not using glade and quickly.

It's plain script python file, which will do some automation.say like text processing.

So I no need gui. but the python script should be installable one as like deb/exe.

Thanks in advance.
# 5  
Old 12-06-2010
Please do not inject your own homework questions into someone else's completely unrelated homework thread!
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question