Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Operating Systems 2230
Computer Science & Software Engineering
Labsheet 1
2230 will demand a reasonable understanding of C and its use of dynamic memory. You are encouraged
to become familiar with these techniques, even if you have encountered C in other units. We will use C
under Linux extensively in the first half of 2230.
The exercises on this labsheet, although not strictly on operating system issues, will be of great benefit to
your understanding. You are strongly encouraged to attempt every exercise on this sheet — their coding
may not be as easy as you think on a first reading. It is not expected that you can complete all of
these exercises in one week.
As C is probably not your current programming language of choice, please be proactive in seeking assistance
from 2230 lab demonstrators in the first few weeks. You are expected to have a sound knowledge of another
programming language, such as Java or Haskell, and so you may like to sketch out a solution in that
language first (no need to implement a full solution in that language).
1. If you are new to C programming, it will be a good idea to start with the example programs we
discussed in this week’s lecture. I would suggest that you look up the programs from the lecture file,
type them and compile and execute them. The link to this week’s lecture is :
http://undergraduate.csse.uwa.edu.au/units/CITS2230/handouts/LecturesOnC/lect.pdf
2. A Pythagorean triplet contains three integers that can be the lengths of the sides of a right triangle.
Write a short C program to print all Pythagorean triplets whose largest member is 1000. Write this
program using only integer variables and calling no external functions.
3. Recode your solution to the previous question so that it uses the sqrt() function from C’s standard
maths library. You will need to link with C’s maths library.
4. Write a C program which simulates the rolling of two dice, several thousand times, and maintains
and prints a record of the number of 2s, 3s, . . ., 12s seen. See man random under Linux.
srandom( time(NULL) );
initializes a random seed. A call to random() gives you a random number. However, you have to get
a number between 1 and 6. That can be done by the % operator.
5. A computer password is often considered “safe” (i.e. hard to guess) if it contains a mixture of upper-
and lower-case characters and digits. Write a C program which determines if a password, stored in
a character array, has at least two upper-case characters, two lower-case characters, and two digits.
See man isalpha under Linux.
1
6. Write a C function to convert the characters in a NULL-byte-terminated string to their numeric
“meaning”. For example, myconvert(“1234”) will return the integer 1234.
7. Consider the sequence 4D, KC, JS, 10S, . . ., which represents the playing cards Four of Diamonds,
King of Clubs, Jack of Spades, Ten of Spades, . . ., once they have been randomly shuffled. Write a
program to shuffle cards in a naive fashion and to print such a sequence. See man random under
Linux.
8. Rewrite your program from the previous question to be as efficient as possible.
9. Write a program in C named replace, which replaces all instances of one string for another in a third
string. The strings used by the program are received as command line arguments. For example,
when invoked as replace cat bison catocathartic, the program will print bisonobisonhartic.
10. For your own reasonable definition of an email address, write a program to locate and print all email
addresses in a text file. Do not sell your program to spammers.
11. Under Linux, the file /usr/X11R6/lib/X11/rgb.txt contains lines of the form:
106 90 205 SlateBlue
providing the red, green, and blue (RGB) values required to display the indicated colour (here,
SlateBlue) under X-windows.
Write a simple program named rgbvalues to print the integral red, green, and blue values for any
colour passed as a command line parameter. How will you handle colours with spaces in their name,
e.g. “dodger blue”?
12. Modify your program from the previous question to develop a new program named rgbclosest, which
prints the name of the colour with the closest Euclidean distance to the colour indicated by three
integral values passed on the command line. You will need to link with C’s maths library to use the
sqrt() function.
13. A Uniform Resource Locator (URL) is a scheme for locating information on the Internet. A general
URL is defined (for this question) to include a protocol, an Internet site name, and an optional
pathname understood at that site, and is written on a single line as
protocol://sitename/pathname
Assuming that the only valid protocols are http, https, ftp, and file, write a simple program named
findurls, to print out all the URLs found in an indicated text file. Use a number of different saved
Web pages or Usenet news articles as your test input data.
14. Extend your solution to the previous question with a command-line switch to your program, say -s,
which when provided results in the output being sorted by sitename. See man qsort under Linux.
15. Write a C program named listints, which will print the integers requested on its command-line. The
output is to appear in strictly increasing order, each requested integer appearing once and only once.
Typical examples of its use include:
listints 3,5,9
listints 1-10
listints 1-10,6
listints 2000-2020,40-50
listints 1-10,2010-2020,3000000-3000010
2
16. (An almost impossible challenge — don’t waste your time) Consider the following self describing
sentence:
This sentence has forty three As, sixty eight Bs, . . ., and seventeen Zs.
Write a program to print out the whole sentence (alphabetic case is ignored when counting the
letters).
3