Uploaded by Kyle James

Assignment

advertisement
COMP 1602 – Computer Programming II
Assignment 2
Date Due: February 28, 2021 at 11:55 pm
Description
This assignment requires you to write various functions for a simple Windows-based text editor. The
user interface of the editor is shown below:
The functions you are required to write for this assignment are called when the buttons are clicked
on the user interface. They are described under the following sections, File Operations, Check
Spelling, Search, and Statistics.
File Operations
You must write two functions with the following headings:
void encrypt (char document[], char filename[])
void decrypt (char document[], char filename[])
The user creates a document by typing characters in the editor. When the user is ready to save the
document to a file, the file name must be entered in the field to the right of the Save File As button.
The Save File As button is then clicked. The encrypt function is called by the user interface. When
called, the characters in the document are passed as a C-string parameter, document, to the encrypt
function. The name of the file is also passed as a C-string parameter. The encrypt function opens the
file. Each character in the document C-string is then encrypted and stored in the file.
1
The following encryption algorithm should be used to encrypt each character before it is stored in
the file:
(1)
Let length = the amount of characters in the document C-string.
(2)
Let shift = length % 4 + 1.
(3)
For each character in the document:
.
(3.1)
add shift to that character.
(3.2)
interchange bit 3 with bit 4 in the character.
(3.3)
Store the character in the file.
When the user wants to open a file, the file name must be entered in the field to the right of the
Open File button. The Open File button is then clicked. The decrypt function is called by the user
interface. When called, a C-string, document, is passed as a parameter to the decrypt function. A Cstring containing the name of the file is also passed as a parameter. The decrypt function opens the
file. Each character from the file is then read, decrypted, and inserted in the document C-string
which was passed as a parameter. The decryption algorithm should reverse the steps of the
encryption algorithm.
When the user clicks on the Clear Editor button, the contents of the editor and file name/s are
erased. You do not have to write code for this behaviour.
Check Spelling
You must write three functions with the following headings:
int readWordsDictionary(string dictionary[]);
int checkSpelling (string dictionary[], int numWords,
char document[], int start, char errorWord[]);
int checkSpellingAll (string dictionary[], int numWords,
char document[], string errorWords[]);
The first function reads all the words from the dictionary file, dictionary.txt, and stores them in
dictionary, an array of string passed as a parameter. It returns the amount of words in the dictionary.
All the words in the dictionary are in lowercase. The function is called as soon as the program starts.
The checkSpelling function accepts the dictionary as a parameter as well as a C-string containing the
characters of the document. It checks the spelling of all the words that come after location start in
the document C-string. If an incorrectly spelt word is encountered, it is copied to the C-string
errorWord and the function terminates by returning the starting location of the word that has been
spelt incorrectly. If there are no spelling errors, the function must return -1.
The checkSpellingAll function is similar to the checkSpelling function. However, it searches the entire
document C-string for incorrectly spelt words. Whenever an incorrectly spelt word is encountered, it
is inserted in the array of errorWords which was passed as a parameter. The function returns the
amount of words that were incorrectly spelt in the document.
It should be noted that the user may type uppercase and lowercase letters in the editor. So, “The”
and “the” are correctly spelt even though the dictionary only contains the word “the”. Also, the
2
getWord and getWords functions discussed in the lectures are good starting points for writing the
checkSpelling and checkSpellingAll functions.
Search
The search feature allows the user to search for a particular word in the document. You must write a
function with the following heading to perform the search:
int searchWord (char document[], int start, char keyWord[])
The function must search the document C-string to find the keyWord C-string, starting from location
start. If the keyWord substring is found in the document C-string, you must return the starting
location where it was found. Otherwise, return -1.
Statistics
A Statistics struct is used to store statistics about the document in the editor. It is declared as
follows:
struct Statistics {
int wordCount;
int longestWord;
int shortestWord;
int numChars;
};
//
//
//
//
amount of words in the document
length of the longest word in the document
length of the shortest word in the document
total number of characters in the document
You must write the following function to generate the statistics and store it in a variable of type
Statistics:
Statistics getStatistics (char document[]);
The function accepts the document C-string, analyses it, and stores the result in a Statistics struct
which is returned to the caller.
What You are Given
You are given the following files:
Assignment2.dev
GivenFunctions.cpp
Graphics.cpp
Assignment2.cpp
dictionary.txt
//
//
//
//
//
a special file called a project file
contains useful functions from the lectures
contains functions which display the user interface
write the functions for Assignment 2 in this file
dictionary with 69,903 English words
You must open the project file, Assignment2.dev, not the individual .cpp files. When the project file
is opened, the three files .cpp files are shown in Dev-C++: Assignment2.cpp, GivenFunctions.cpp, and
Graphics.cpp. You can view the code in GivenFunctions.cpp and Graphics.cpp. However, you should
not modify the code in both files.
You must write the functions required for this assignment in Assignment2.cpp. Since the functions
are being called from the user interface code in Graphics.cpp, if any of the function headings in
Assignment2.cpp are modified, the program will not compile.
The file GivenFunctions.cpp contains several functions that were discussed during the lectures (e.g.,
isLetter, toUpperCase, byteToChar, etc.). You are free to call these functions when writing your own.
3
Marking
Only the code for the following functions will be marked:
void encrypt (char document[], char filename[])
void decrypt (char document[], char filename[])
int readWordsDictionary(string dictionary[])
int checkSpelling (string dictionary[], int numWords,
char document[], int start, char errorWord[])
int checkSpellingAll (string dictionary[], int numWords,
char document[], string errorWords[])
int searchWord (char document[], int start, char keyWord[])
Statistics getStatistics (char document[])
Functions headings for each of these functions are provided in Assignment2.cpp.
What to Submit
Submit a zipped file containing the following five (5) files:
Assignment2.dev
GivenFunctions.cpp
Graphics.cpp
Assignment2.cpp
dictionary.txt
Your zipped file should not include any .exe or .o files.
Time to Compile and Run
You will notice that the compilation process takes longer than you are accustomed to. The program
also takes longer to launch. This is normal and you should not attempt to hasten the process by
pressing Enter or other keys.
For non-Windows or non-Dev-C++ Users
If you are not using a computer running the Microsoft Windows operating system or if you do not
use Dev-C++, you must still write all the functions specified in the file, Assignment2.cpp. Information
on testing your functions and submitting your assignment will be provided at a later date.
For Windows and Dev-C++ Users
If you normally insert cout statements in your program to assist with debugging (finding errors), you
will not be able to use these statements in the Windows program. We have provided two functions
you can call (in GivenFunctions.cpp) to display debugging information in a popup message box:
void displayText (string message, string title);
void displayText2 (char message[], char title[]);
The only difference between the two functions is that the first function uses messages of type string
while the second function uses messages that are C-strings.
4
Download