[solve] This is a warmup exercise to get you familiar with using Linux.
Write a program that reads an integer and shows its factorial.
Here is a sample session with the program. Parts shown in black
are written by the program. Parts in red are written by the user.
Show the factorial of: 5
5! = 120
You will find the following useful. Cinnameg expression readInteger( ) reads one integer from the user and yields it as the expression's value. Typically you write a statement such as
Let n = readInteger().to read an integer and call it n.
Follow these steps.
Log into Linux. You can do that in Austin 208 by giving your pirate id and password. You should be logged in immediately after that. If you get a complaint that the system cannot install something, just click Ok to make it go away.
You can also log in from another computer with an internet connection by using the NX client. Download NX and run it. (Do an internet search for "NX Client" to find it.) In the box that pops up, select configure. Set the host to login.cs.ecu.edu, set the host operating system to Unix and the desktop manager to Gnome. Select Ok and login as in the lab.
Double-click on your home folder. Select menu item File/New Folder and create a folder for your CSCI 2310 work. Then open that folder. Right-click it and select Open in terminal. A console will open. You can type commands in the console. End each command with the Enter key.
In the console window type command
gedit&
A text editor will pop up. (The ampersand at the end
says for the terminal not to wait for gedit to finish.
That way, you can keep using the terminal without
closing the editor.)
Type your program into the text editor. See below for the program requirements. Make the program have the following form.
Package factorial Import "collect/string". ...Your material here... %PackageThe import line lets you use the readString function.
Save the program when it is ready. Call it factorial.cmg. Do not close the text editor. You will want to keep it open so that you can make modifications to your program.
When you are ready, go to the console window and type
command
ls
You will see a list of the files in this folder. You should
see factorial.cmg. If not, either you are in the wrong folder
or you have not saved your program.
Compile your program using command
cmgc factorial
If the compiler says nothing it means there are no errors.
If there are errors, fix them and try again. To fix an
error, just go to the text editor, make the change, and
save the changed file.
After the program compiles, run it using the following command.
cmgr factorial
The program should run. If there is a problem, fix it and
try it again. Be sure to compile it again after every modification.
After you believe that your program is correct, come back to this web page. Copy your program (factorial.cmg) to the clipboard, then paste it into the box below. Submit it.
This program should ask the user for two strings: the first is the name of a file to search, and the second is a string to search for. The program should then show each line of the named file that contains the given string, with its line number.
For example, suppose that file data.txt contains the following.
'Twas brillig, and the slithy toves
did gyre and gimbol in the wabe;
all mimsy were the borogoves
and the mome raths outgrabe.
Now you run the program. It interacts with the user. A sample
interaction is as follows. Parts in black are written by the program
and parts in red are written by the user.
What file shall I search? data.txt
What string shall I search for? gim
2. did gyre and gimbol in the wabe;
Here is another sample run.
What file shall I search? data.txt
What string shall I search for? and
1. 'Twas brillig, and the slithy toves
2. did gyre and gimbol in the wabe;
4. and the mome raths outgrabe.
Open gedit and type in a data file. Save it with name data.txt.
Open gedit again. Copy your definition of function indexOf from lab 5 question 2 into the editor. (Do not copy it by hand. Copy it to the clipboard and paste it.) Save it with name find.cmg. Make the program have form
Package find Import "collect/string". ...Your material here... %Package
Now modify the program so that it does what is desired for this assignment. You will find the indexOf function useful.
You will also find the following useful.
readString( ) is an expression that reads a string from the user and yields that string as its value. Use it to read the file name and search string. For example,
Let filename = readString().will get a string and call it filename.
Statement
ReadFrom(fileContents(filename)).causes future input to come from the file whose name is in variable filename. Then
Let line:String = readLine().reads one line of text from the file and calls that text line. Each time you do that, it gets the next line of the file. The line will not contain an end-of-line character. That is removed.
Expression !stdin( ) == "" yields true if there is nothing left to read in the file. Be sure to include the exclamation point.
Build a loop. Each time around the loop, read a line of text and check whether the search string occurs in that line. If it does, then show the line. You will need to keep a counter so that you know which line it is. Keep going until there is nothing left to read.
When the program works, paste it into the box below and submit it.