Title: CS 325
1CS 325
- Unix Programming Environment
- and
- Windows Programming
- with Microsoft Foundation Classes (MFC)
2CS Supplemental Instruction
Association for Computing Machinery
- Start the semester off right!
- Dont get behind!
- Ask questions early!
Where AIME Building Behind Ferg. Parking Lot
Across from Paty Hall Room 111/112 When CS
325 Wednesday Night 6-7pm Thursday 630-730pm
NEW TIMES! NO CHARGE! CS325 Leader Hayden Hayes
3Lecture 6
- Today
- Unix programming environment
- vi
- make
- ftp
- chmod
- Homework
- Reading Assignments
- Unix
- vi, emacs, make, ftp, chmod
- http//bama.ua.edu/cgi-bin/cgiwrap/unixinfo/chsh.
pl - Make sure you have an normal, interactive shell
(ksh/bash) - Sign up for an account on unix.eng.ua.edu
(required) - For Fun http//www.catb.org/esr/writings/taoup/h
tml/
4Unix
5vi Basics
- Two Modes
- Command Mode
- For entering commands
- i, a, o, I, A, O, r, /ltsearchstrgt, ?ltsearchstrgt
- yy, p, P, ., q!, w, wq
- cw, dw, dd, D
- And many more
- Insert Mode
- For entering text
- esc returns to Command Mode
6vi commands
- i, a, o, I, A, O, r, R
- Various methods of entering insert mode
- i, a, o insert before/after/under curser
- I, A, O insert before/after/over line
- r, R replace char, type over
- /ltsearchstrgt, ?ltsearchstrgt
- Searching forward and backwards
- Use n to repeat search (find next)
7vi commands
- yy
- Yank (copy) lines of text
- p, P
- Push yanked lines of text below/above
- . (Dot)
- Repeat last command (very useful)
- q!, w, wq
- Quit with saving (sometimes useful)
- Write without exiting
- Write and exit
8vi commands
- cw
- Change () words
- dw
- Delete words
- dd
- Delete lines
- D
- Delete rest of line
9Compilers
- CC Sun C compiler
- g GNU C compiler
- We will use the GNU C compiler
- Create a source file (pico or vi), extension of
.cpp - g filename to compile the file
- ./a.out to execute the file (executable in a.out)
- g -o executablename filename
- (if you dislike a.out)
10Class Exercises
- Write hello world using the GNU C compiler
- Write a program (using GNU C) that declares a
template class for a dynamic linked list and then
adds five integers to an integer instantiation of
the list
11Makefiles
- Need a good way to compile big projects
- Simple
- Easy-to-use
- Not require a lot of typing
- Unix world has makefiles
- A file that tells how to build executables from
code - Name of the file is Makefile (or makefile)
- To run this file, type make
- The command make finds Makefile and executes
the commands listed in it
12Understanding Makefiles
- To understand makefiles, must know how programs
are compiled into executables - Two-stage process
- Compilation
- Linking
- Compilation
- Check for syntax semantic errors
- Generate object code for your statements
- Linking
- Links your object code w/ other routines
(libraries)
13Consider the file hello.C
- Compilation
- Checks for syntax errors (missing semi-colon,
etc) - Checks for semantic errors (e.g. cout gtgt x)
- Constructs object code (something the machine can
run) for your source code - Linking
- Combines your object code with other object code
needed during execution - Object code for I/O routines, math routines, etc.
- Builds one executable program
14Compiling hello.C (continued)
- Source code (hello.C) to object code (hello.o) is
done during compilation - The g command normally compiles and links (all
in one command) - g -c hello.C produces object file, no linking
done - Can then link this file manually if desired
- Can use g command to compile together both .C
files and .o files (previously compiled)
15Makefiles and C code
- Sample Makefile
- Hellolttabgthello.o
- lttabgtg hello.o
- To build Hello (target) you need hello.o
- Makefile knows hello.o can be built from hello.C
- Once have hello.o, do the command shown to build
executable - If multiple targets given, Makefile does first one
- The make command knows how C programs are
compiled - Makefiles consist of two lines
- First line is a target list of dependencies
- Second line is commands needed to build target
- Makefiles use tab as separator (not spaces)
16Larger Makefiles
- a.out CSTree.o CSVector.o CSList.o CSQueue.o
CSStack.o CSContainer.o - g CSTree.o CSVector.o CSList.o
CSQueue.o CSStack.o CSContainer.o - CSTree.o CSTree.cpp
- g -c CSTree.cpp
- CSVector.o CSVector.cpp
- g -c CSVector.cpp
-
- CSContainer.o CSContainer.cpp
- g -c CSContainer.cpp
- clean
- rm .o
- rm a.out
17Useful Commands
- Touch filename
- make step
- make tree.o
- execute just the steps to build tree.o
- make clean
- Run the clean step
- make f someOtherMakefile
- Specify the makefile to use
- Allows you to have more than one in a directory
18Class Exercises
- Using the Unix system, write a sample Makefile.
- Non-template code from project1 is a multifile
project. My source files for these can be found
on the web at bama.ua.edu/yessi001
19Moving files to/from Unix
- Cut and Paste
- Right click on the menu bar of telnet process
- Works as if actual keystrokes sent
- Limited by buffer size
- May lose data
20Moving files to/from Unix
- Moving files between a PC and Unix
- Use ftp(file transfer protocol)
- PC must be connected to the network
- C\gtftp bama.ua.edu
- Connected to bama.ua.edu.
- 220-
- 220- Use of this system (lines suppressed here)
- 220 bama.ua.edu FTP server .
- User (bama.ua.edu(none)) cordes
- 331 Password required for cordes.
- Password ltenter password heregt
- 230 User cordes logged in.
- ftpgt put hello.C
- 200 PORT command successful.
- 150 Opening ASCII mode data connection for
hello.C. - 226 Transfer complete.
- ftp 85 bytes sent in 0.00Seconds
85000.00Kbytes/sec. - ftpgt quit
21Moving files to/from Unix (cont)
- Other ftp commands
- dir, lists all the files on the remote system
- cd xxx, changes directory on the remote
system(also have lcd to change local directory) - get xxx, gets a file from the remote system
- binary, toggles to binary mode (non-ASCII files)
- mput .cpp, copies all your cpp files to remote
sys(also have mget to retrieve files) - help, gives list of commands
22Windows version
- Most pcs have a windows version of ftp
- Start
- Programs
- WS_FTP (usually)
- WS_FPT
- Beside host name/Address type bama.ua.edu
- Enter your ID and password in appropriate spots
- Click OK
- This is a much easier way to ftp, however, be
familiar with Unix version as well
23Some Final Notes
- Two modes of transport
- binary
- ascii
- Binary
- Copies bits exactly
- Note that .exe compiled on windows won't run on
bama still .exe's are examples of files that
should be copied in binary mode - ascii
- Twiddles line ending during transfer of text
files - Some machines want \r\n
- Some machines want \n only
- Some machines may want \n\r (? Not sure about
this) - Some machines do it another way entirely (no
concept of \n) - Effects of extra missing characters
- Appear as junk
- Lose line breaks entirely
24Class Exercises (short)
- Find a C file on your local PC
- Copy it to your account on bama.ua.edu
- Copy a file from your bama account to your local
PC
25Unix File Permissions
- Each file in Unix has three levels of security
- What the user can do to it
- What people in the users group can do to it
- What everyone else can do with it
- The ls command can display security
- /fs/cordes ls -l
- total 10
- drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
cs325 - drwx------ 2 cordes facstaff 512 Aug 30 0510
foo - -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
hello.C - drwx------ 2 cordes facstaff 512 Aug 30 0937
mail - drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
networks - ls l shows files and directories and gives extra
information such as permissions, userid, group,
last modified date
26Unix File Permissions (cont)
- /fs/cordes ls -l
- total 10
- drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
cs325 - drwx------ 2 cordes facstaff 512 Aug 30 0510
foo - -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
hello.C - drwx------ 2 cordes facstaff 512 Aug 30 0937
mail - drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
networks
permissions user group last-mod-date filename
27Unix File Permissions (cont)
- -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
hello.C - - r w r - - r - -
- indicates this is a regular file, not a
directory - r w indicates the user can read write this
file - r indicates the group can read this file
- r indicates that others can read this file
28Changing Permissions
- chmod ltpermissionsgt filename
- Permissions consist of who /- what
- Who is u (user), g (group), o (others)
- What is r (read), w (write), x (execute)
- gives permission
- - takes permission
- Examples
- chmod u-w hello.C (user cannot write)
- chmod gor hello.C (group others can read)
- chmod ar hello.C (a is short for u, g, o)
- man chmod talks about changing permissions in
terms of octal numbers
29Class Exercises
- Type ls l in your home directory. Explain
what permissions are set for each file. - The file .plan in your home directory can tell
information about you (what you do). In order
for others to read this file, they need to be
able to execute your home directory (.) and also
read your .plan file. Set it up so that other
people can finger userid and see your .plan.
30Unix requirements for 325
- Basic manipulation of your files on Unix
- Create sub-directories for organization, etc
- Edit and compile C files
- Some projects are done on Unix
- Move files between PCs and Unix
31Unix Directories
- mkdir
- Creates directory
- rmdir
- Removes directory (must be empty)
32Unix apropos
- apropos keyword
- Searches on keyword for relevant info
- Useful when you cannot use man (looking for
command)
33Class Exercises
- Teams new to Unix
- Editors how do you
- Copy a line
- Delete a line
- Give the command to
- Rename hello.cpp to hello.C
- Copy (to your home dir) hello.cpp from
/st3/yessi001/cs325 - Mail hello.cpp to yourself
- Teams that know Unix
- Give a one-line command (you can use pipes) to
- Find the home directory of the user cordes
- Print your last name in hex (and octal)
- Print the first name of all the users on the
system with the last name Smith - Convert the contents of hello.C to uppercase
34The Iterator Design Pattern
- Most container classes need to be able to look
at all the elements in the class - Consider a LIST class
- Want to be able to print all the items
- Want to be able to find an item
- Want to be able to print all possible pairs of
items - Some of these could embed in LIST itself
- Others are more awkward to embed
- Dont know all possible ways to examine LIST
35Iterator Assumptions
- Basic class exists for the container
- This class creates the underlying data structure,
and handles adding/removing from the container - A friend class is responsible for allowing
access to the elements in the container - Initialize the iterator
- Get the item the iterator currently points to
- Increment the iterator to the next item in the
list - Check to see if the iterator has reached the end
of the list
36Implementing the Iterator
- include ltiostream.hgt
- class Node int dataNode next
- publicNode() data(0), next(0)
friend class Listfriend class ListIter -
- class List Node head friend class ListIter
- publicList() head(0) void add(int e)
- class ListIter Node current
- publicListIter(List theList) current
theList.head int done(void) return current
0 int get(void) return current-gtdata
void next(void) if (current)
currentcurrent-gtnext
37Using the Iterator
- This program prints out all possible pairs of
items in the list. It uses two iterators to
control the two loops.
- void main(void) List aa.add(10) a.add(20)
a.add(30)ListIter y(a)while ( ! y.done() )
ListIter z(a) int b y.get() while ( !
z.done() ) coutltltbltlt" "ltltz.get()ltltendl z.ne
xt() y.next() -
38Class Exercises
- Write the CSListIterator with templates.
- Write the CSStackIterator with templates.
39Project 2
- Templates and Iterators for BinaryTree
40Project Description
- Have a templated BinaryTree class
- Constructor
- add()
- Create templated iterators for BinaryTreeltTgt
- PreorderBinaryTreeIterator
- InorderBinaryTreeIterator
- PostorderBinaryTreeIterator
- LevelOrderBinaryTreeIterator
41Iterator methods
- Constructor
- Initialize iterator -- set current to first
element - bool hasMoreElements()
- return false if no more elements
- int getValue()
- return current data (return type wont be int)
- bool next()
- return false if cannot advance
42Binary Tree Traversals
43Iterator Logic
- Preorder (N L R)
- Initpush root node
- while top is node loop
- current pop
- push current.right
- push current.left
- push current.data
- end loop
- return pop
- Inorder (L N R)
- Initpush root node
- while top is node loop
- current pop
- push current.right
- push current.data
- push current.left
- end loop
- return pop
44More Iterator Logic
- Postorder (L R N)
- Initpush root node
- while top is node loop
- current pop
- push current.data
- push current.right
- push current.left
- end loop
- return pop
- LevelOrder
- Initnq root node
- while front is node loop
- current dq
- nq current.data
- nq current.left
- nq current.right
- end loop
- return dq
45Project Comments
- Must run on Ox with GNU g
- General format
- Template classes for Iterators (4)
- One File For Compilation
- Project2.h
46Boundry Conditions
- It new TreeIterator(bt)
- while (it.hasMoreElements()
- xit.getValue()
- DoSomething(x)
- it.next()
-
It new TreeIterator(bt) while
(it.next()) xit.getValue() DoSomething(x)
47Boundry Conditions
It new TreeIterator(bt) it.next() it.getValue(X)
It new TreeIterator(bt) it.getValue(X)
48Project Notes
- The first next() precedes getValue()
- Advance to first item
- The first next() follows getValue()
- Advance to second item
- hasMoreElements() and getValue()
- Make sure you pick up the last item
- Start by not worrying about the boundry
conditions once the Iterator is generally
working these refinements are fairly simple to
add using flags