Chapter 2
Introduction to
2.1 Basic Concepts
(Problem Statements for Project 0)
Summary for the Steps to Take
• Analysis
– Understanding the problem – Finding the requirements
• Design
– Abstract design
• Basic functions
– Architectural design
• Control and data flow
• Structure charts
– Detail design
• Content of each function
Project 0
Appointment Book
• This project is to write a program to manage a monthly appointment book.
• The appointment book contains some slots to write in
information, so that the user knows who/what/when/where information.
• The system allows the user to enter at most 10 records.
• Each record consists of fields, such as when, what, who, where.
• The records are read from a file (if present) and saved to the file after leaving the system.
4
Project 0
Appointment Book
• This project is to write a program to manage a monthly appointment book.
• The appointment book contains some slots to write in
information, so that the user knows who/what/when/where information.
• The system allows the user to enter at most 10 records.
• Each record consists of fields, such as when, what, who, where.
• The records are read from a file (if present) and saved to the file after leaving the system.
Project 0 (cont.)
• The system provides the following services.
– Enter Record: to enter the who/what/when/where information – View Day: to show the appointments of a given day
– View week: to show the appointments of a given week – Modify: to modify a record
– Delete: to delete a record
– Search: to search a record by one of when/what/who/where – Quit: It saves the current records to the file and terminates the
program.
6
Software Prototyping
• To develop an executable model quickly – rapid prototyping
• To let the user work on the model, so that the user can give feedback
• To provide a common platform for discussion between the user and the developer
• To modify the model according to the feedback to develop the next version of the prototype quickly
Software Prototyping
8
Analysis & Design
Design Document
Implementation System Prototype
User Interaction User Feedback
How to Start?
• What are the data to be processed?
– A monthly appointment book
– who/what/when/where information – at most 10 records
How to Start?
• What are the data to be processed?
– A monthly appointment book
– who/what/when/where information – at most 10 records
10
Who:
What:
When:
Where:
× 10
type & length?
Control Structure?
What’s Next?
• Representation of data
– Data structures ready
• How to use the data?
– Control flow – Function calls
• Representation of “flow”?
– Structure chart
• Relationship between modules
– Flow chart
• Steps taken in processing data
Structure Chart
• Simple components
12
module
common module
conditional loop conditional loop
(+) exclusive or
Structure Chart for Proj 0 v0.1
Appointment Book System
Structure Chart for Proj 0 v0.1
14
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day …..
(+) (+)
Flow of Structure Chart
• Structure of modules
– somewhat similar to function calls
• Flow
– Top to bottom – Left to right
• Calling functions on top
– Layers above – abstract definitions
– Layers below – more detail definitions
2.2 Version 0.1
Project 0
Appointment Book
• This project is to write a program to manage a monthly appointment book.
• The appointment book contains some slots to write in
information, so that the user knows who/what/when/where information.
• The system allows the user to enter at most 10 records.
• Each record consists of fields, such as when, what, who, where.
• The records are read from a file (if present) and saved to the file after leaving the system.
Structure Chart for Proj 0 v0.1
18
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day …..
(+) (+)
Project 0 Version 0.1
• Define the sizes in the data structure
• Define the function prototypes
• Show a menu
– Print the menu with 7 choices according to the functions – Read an integer
– Return the integer as the choice to the calling program
Data
• What are the data to be processed?
– A monthly appointment book
– who/what/when/where information – at most 10 records
20
Who:
What:
When:
Where:
× 10
type & length?
Control Structure?
Prototype of Project 0 Version 0.1
#define WHO_LEN 15
#define WHAT_LEN 20
#define WHEN_LEN 10
#define WHERE_LEN 20
#define MAX_REC 10 typedef struct {
char who[WHO_LEN];
char what[WHAT_LEN];
char when[WHEN_LEN];
char where[WHERE_LEN];
}
Record;
Who:
What:
When:
Where:
Version 0.1 (cont.)
void ReadFromFile (Record *, int *);
int menu(void);
void EnterRecord (Record *, int *);
void ViewDay (Record [], int);
void ViewWeek (Record *, int);
void Modify (Record *, int);
void Delete (Record *, int *);
void Search (Record *, int);
void Quit (Record *, int);
22
Version 0.1 (cont.)
int main (void) {
Record AppBook[MAX_REC];
int quit = 0, choice, count=0;
ReadFromFile (AppBook, &count);
while (! quit) { ...
} }
Version 0.1 (cont.)
while (! quit) {
choice = menu(); // get a choice
switch (choice) { // process according to the choice case 1: EnterRecord(AppBook, &count); break;
case 2: ViewDay(AppBook, count); break;
case 3: ViewWeek(AppBook, count); break;
case 4: Modify(AppBook, count); break;
case 5: Delete(AppBook, &count); break;
case 6: Search(AppBook, count); break;
case 9: Quit(AppBook, count); quit = 1; break;
default: printf("Please enter a choice 1-6 or 9 to quit\n");
} }
24
Discussion on Version 0.1
• Basic architecture
• Menu input
• Choices made in a loop
– Correct choice – Correct exit
• No problem to the control structure
2.3 Version 0.2
Structure Chart for Proj 0 v0.1
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day …..
(+) (+)
Analysis for Version 0.2
• Implement two “reading” functions
– EnterRecord() : to read just one record from a user – ReadFromFile(): to read many records from a file
• Implement quit() for saving to a file
28
Analysis for Version 0.2 (cont.)
• EnterRecord()
– Read a record from the user – Update the appointment book – Update the count
• ReadFromFile()
– Open a file if the file presents
– Read records until the end of file while updating the appointment book and counter
– What about the file name?? Used only here or not?
Structure Chart for Proj 0 v0.2
30
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day …..
(+) (+)
Design of Version 0.2
• EnterRecord()
– Parameters
• Appointment book
• Number of records
– Return value
• None
Ask the user to enter the WHO field
Ask the user to enter the WHERE field Ask the user to enter
the WHEN field Ask the user to enter
the WHAT field
Increment the count
Prototype of Version 0.2
• EnterRecord(Record Book[], int * count)
– Parameters: Book and count
– Prompt the user for entering a record – Update the book
– Update the count
printf("Please enter WHOM you have an appointment with: ");
gets(Book[*count].who);
printf("Please enter WHAT the event is: ");
gets(Book[*count].what);
printf("Please enter WHEN (yymmddhhmm): ");
gets(Book[*count].when);
printf("Please enter WHERE you have an appointment at: ");
gets(Book[*count].where);
(*count) ++ ;
32
Structure Chart for Proj 0 v0.2
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day …..
(+) (+)
Design of Version 0.2
• ReadFromFile(Record * Book, int * count, char filename[])
– Parameters: Book, count, filename – Prompt the user for a filename
– Open the file if it exists – Loop
• Read a record from file
• Update Book and count
• Until the end of file
34
Design of Version 0.2 (cont.)
• ReadFromFile()
– Parameters
• Appointment book
• Number of records
• File name
– Return value
• None
36
Ask the user to enter the file to read/save
Increment the count Enter a record Open the file
Open success?
Not EOF?
Quit with error message
end
Prototype of Version 0.2
void ReadFromFile (Record * Book, int * count, char buff[]) {
FILE * filePointer; int i;
printf("Please enter a file name to open/save: ");
scanf("%s", buff);
printf("Opening file: %s ....\n", buff);
if( (filePointer = fopen(buff,"r")) == NULL) return;
else
while (fgets(Book[*count].who, WHO_LEN, filePointer) != NULL) { ...
} }
Prototype of Version 0.2 (cont.)
while (fgets(Book[*count].who, WHO_LEN, filePointer) != NULL) { fgets(Book[*count].what, WHAT_LEN, filePointer);
fgets(Book[*count].when, WHEN_LEN, filePointer);
fgets(Book[*count].where, WHERE_LEN, filePointer);
(* count) ++ ; }
}
38
Book[*count].who[strlen(Book[*count].who)-1] = '\0';
// to remove LF
Structure Chart for Proj 0 v0.2
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day …..
(+) (+)
Design of Version 0.2
• Quit(Record Book [], int count, char fileName[])
– Parameters: Book, count, fileName – Open the file
– Loop
• Write one record from Book to the file
• Decrement count
• Until count = 0
40
Prototype of Version 0.2
void Quit (Record Book [], int count, char fileName[]) {
int i;
FILE * fp;
printf("Saving to file: %s ... \n", fileName);
fp = fopen ( fileName, "w");
for (i = 0; i<count; ++i)
fprintf(fp, "%s\n%s\n%s\n%s\n", Book[i].who, Book[i].what, Book[i].when, Book[i].where );
}
Discussion on Version 0.2
• No new addition to the original structure chart
• Basic I/O for the system implemented
– Input
• Entering one record through the keyboard
• Reading multiple records from a file
– Output
• Writing all records to a file
• “Not-so-ideal” functions used
– gets() – fgets()
– Can these be improved?
42
2.4 Version 0.3
Analysis for Version 0.3
• To implement three “showing” functions
– ViewDay(): showing the records with a certain day
– ViewWeek(): showing the records for 7 days starting a given day – Search(): showing the record with a key
44
Analysis for Version 0.3 (cont.)
• Introducing a utility function for ViewDay() and ViewWeek()
– ShowDates()
• Introducing another utility function for Search()
– strContain()
Structure Chart for Proj 0 v.0.3
46
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day
View ….
Week
Show
Dates Show
Dates
Search
String Contain (+)
(+) (+)
(+)
Design for Version 0.3
• ShowDates (Record Book[], int count, char date[])
– Parameters: Book, count, date – Loop
• Pick a record to see if date is contained in the “when” field
• If so, print the content
• Until Book is empty
Prototype of Version 0.3
void ShowDates (Record Book[], int count, char date[]) { int i = 0;
while (i < count) {
while (i<count && (strncmp(date, Book[i].when,strlen(date)) != 0)) ++ i ;
if (i < count) {
printf("Who: %s\n", Book[i].who);
printf("What: %s\n", Book[i].what);
printf("When: %s\n", Book[i].when);
printf("Where: %s\n\n", Book[i].where);
} i ++ ; }
return ; }
48
Structure Chart for Proj 0 v.0.3
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day
View ….
Week
Show
Dates Show
Search
String Contain (+)
(+) (+)
(+)
Design for Version 0.3
• int strContain(char * S1, char * S2, int length)
– Parameters S1, S2, length to check – Loop
• Starting from the first character of S2
• Compare S1 and S2 for length
• If match, S1 is contained else move to the next character
• Until S2 is shorter than length
– If it leaves the loop, then S2 does NOT contain S1 – Return 1 if S2 contains S1
– Return 0 if S2 does not contain S1
50
Prototype of Version 0.3
int strContain (char * S1, char * S2, int length) {
int i;
// assume the length of S2 is greater than S1's i = strlen(S2) - length;
for (; i>=0; --i)
if(strncmp(S1, S2+i, length) == 0) return 1; // if found, then finish return 0; // not found
}
Structure Chart for Proj 0 v.0.3
52
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day
View ….
Week
Show
Dates Show
Dates
Search
String Contain (+)
(+) (+)
(+)
Design and Prototype for Version 0.3
• ViewDay (Record Book[], int count)
– Parameter Book, count
– Prompt the user for a date to check – Call ShowDates() with the date
void ViewDay (Record Book[], int count) {
char date[WHEN_LEN];
printf("Please enter the day (yymmdd) to view: ");
scanf ("%s", date);
ShowDates (Book, count, date);
return ; }
Structure Chart for Proj 0 v.0.3
54
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day
View ….
Week
Show
Dates Show
Dates
Search
String Contain (+)
(+) (+)
(+)
Design for Version 0.3
• ViewWeek (Record Book [], int count)
– Parameters: Book, count – Prompt the user for a date – Starting from the given date – Loop
• Call ShowDates() with the current date
• Update the current date with the next day
• Until 7 days covered
•
Prototype of Version 0.3
void ViewWeek (Record Book [], int count) {
char date[WHEN_LEN];
int i=0;
printf("Please enter the day (yymmdd) to view: ");
scanf ("%s", date);
do {
ShowDates (Book, count, date);
date[5] = date[5] + 1; // increment one day per loop if ((date[5] - '0') > 9) {
date [4] = date[4] + 1;
date [5] = date[5] - 10;
} ++ i;
} while (i < 7); // 7 days to increment }
56
Structure Chart for Proj 0 v.0.3
Appointment Book System
Read from
file
Menu Enter
Record View Quit
Day
View ….
Week
Show
Dates Show
Search
String Contain (+)
(+) (+)
(+)
Design for Version 0.3
• int Search (Record Book [], int count)
– Paramters: Book, count
– Prompt the user for a keyword
– Starting from the first record in Book – Loop
• Loop until a record with the keyword is found
• Ask the user to see if it is the desired record
• If so, quit
• Until all records are checked in Book
– Return record number if found; otherwise –1 is returned
58
Prototype of Version 0.3
int Search (Record Book [], int count) {
char key[KEY_LEN]; int length, i=0; char YorN='N';
printf("Please enter a keyword (less than 10 characters): ");
scanf ("%s", key);
length = strlen (key);
while (YorN == 'n' || YorN == 'N') {
…
if (i >= count && (YorN == 'N' || YorN == 'n')) return -1;
}
return (i-1);
}
Prototype of Version 0.3 (cont.)
while (YorN == 'n' || YorN == 'N') {
while (i<count && (strContain(key, Book[i].who, length) == 0) &&
(strContain(key, Book[i].what, length) == 0) &&
(strContain(key, Book[i].when, length) == 0) &&
(strContain(key, Book[i].where, length) == 0) ) ++ i ;
if (i < count) {
printf("Who: %s\n", Book[i].who); ...
printf("\nIs this the record? (Y or N): "); scanf(" %c", &YorN);
} ++ i ;
if (i >= count && (YorN == 'N' || YorN == 'n')) return -1;
}
return (i-1);
}
60
Discussion on Version 0.3
• Implementation in C used for detailed “Design”
• Modify() and Delete() not done
• Many assumption
– Correct input by user – Complete records in file – One field per line
– etc.
Summary
• Importance of analysis and design
• Structure chart
• Flowchart / pseudocode
• Software prototyping
• Iterative and incremental process
62