The total grade for the assignment is 100 points.
You must follow the programming and documentation guidelines (see file Programming Assignments Requirements and Recommendations.docx).
This is a team project (except for those students who have opted to work on their own). Group of two students may work on this project together.
Due date: EOD before Lab Class #7.
Description:
You are required to write an interactive C program that prompts the user for commands, accepts commands from the keyboard (stdin) and executes those commands. When a command requires output, it must be written to stdout. The program must continue to accept and process commands until the user types the end command.
The program deals with linked lists. Each node of such a list contains a string of length at most 10, a positive integer (i.e., an integer value ≥ 1) and a pointer to the next node of the list. For any node, the string and the integer stored in that node will be referred to as the symbol and count for that node respectively. At all times, the list must satisfy the following two important properties.
1.The symbols appearing in the list are all distinct; that is, no two nodes have the same symbol.
2.When the list is scanned from left to right, the counts must be in non-increasing order. An example of such a linked list is shown below.
HEAD
Initially, we have an empty list. Some commands require your program to modify the list while others involve traversing the list to gather and print information about the list. The commands and their interpretations are as follows. (You should bear in mind that different parts of a command are separated by one or more spaces.)
A.Insert Command: The syntax for this command is as follows:
ins str
Here, ins represents the name of the command and str represents a string. The interpretation of this command is as follows.
(a)If the list contains a node whose symbol is identical to the string specified in the command, then the count stored in the node must be incremented by 1. After this increment, if necessary, the node must be moved to an appropriate position in the list to ensure that the counts are in non-increasing order.
(b)If the list does not contain a node whose symbol is identical to the string specified in the command, then a new node must be created. The symbol stored in the new node is the string specified in the command and the count stored in the new node must be 1. The new node must be inserted at the end of the list.
Note that the ins command does not produce any output.
B.Delete Command: The syntax for this command is as follows:
del str
Here, del represents the name of the command and str represents a string. The interpretation of this command is as follows.
(a)If the list contains a node whose symbol is identical to the string specified in the command, then the count stored in the node must be decremented by 1. If the new count becomes 0, then the node must be removed from the list. If the new count is at least 1, the node must be moved, if necessary, to an appropriate position in the list to ensure that the counts are in non-increasing order.
(b)If the list does not contain a node whose symbol is identical to the string specified in the command, then your program must leave the list unchanged.
Note that the del command does not produce any output.
C.Forced Delete Command: The syntax for this command is as follows:
fde val
Here, fde represents the name of the command and val represents a positive integer value. The command must remove from the list, each node whose count is less than or equal to the integer value specified by val. If the list is empty or the count stored in each node is greater than the value specified by val, then your program must leave the list unchanged.
Note that the fde command does not produce any output.
D.Print Statistics Command: The syntax for this command is as follows:
pst
Here, pst represents the name of the command. If the list is empty, your program should simply print the message “The list is empty.”. Otherwise (i.e., the list is non-empty), your program must compute and print the following quantities.
(a)The number of nodes in the list.
(b)The maximum count in the list.
(c)The minimum count in the list.
(d)The average count in the list. (Bear in mind that, in general, the average count is a real number.)
E.Print List Command: The syntax for this command is as follows:
prl
Here, prl represents the name of the command. If the list is empty, your program should print the message “The list is empty.”. Otherwise, your program should traverse the list (from left to right) and print each symbol and the corresponding count on a line by itself. (Thus, when the list is non-empty, the number of lines printed is the number of nodes in the list.)
F.Print using Count Range Command: The syntax for this command is as follows:
pcr v1 v2
Here, pcr represents the name of the command and v1 and v2 are non-negative integer values such that the value specified by v1 is less than or equal to that specified by v2. If the list is empty, your program should simply print the message “The list is empty.”. Otherwise, your program should traverse the list (from left to right); for each node, whose count is in the integer range specified by v1 and v2 (i.e., the count is greater than or equal to the value specified by v1 and less than or equal to the value specified by v2), the program must print the symbol and the count for that node on a line by itself. (Thus, when the list is non-empty, the number of lines printed is the number of nodes in the list whose counts are in the integer range specified by v1 and v2.)
G.Print Prefix Command: The syntax for the print prefix command is as follows:
ppr str
To discuss this command, we first note that a prefix is a substring that occurs at the beginning of a string. For example, the string “val” is a prefix of the symbol “value” and the string “On” is a prefix of the symbol “Only”. (Note also that each string is a prefix of itself.) However, the string “alu” is not a prefix of the symbol “value” and the string “ly” is not a prefix of the symbol “Only”.
Here, ppr represents the name of the command and str represents a string. If the list is empty, your program should simply print the message “The list is empty.”. Otherwise, your program should traverse the list (from left to right); if the given string str is a prefix of the symbol stored in a node, then the command must print the symbol and the corresponding count on a line by itself. (Thus, when the list is non-empty, the number of lines printed is the number of symbols which have the string specified by str as a prefix.)
H.Print Suffix Command: The syntax for the command is as follows:
psu str
To discuss this command, we first note that a suffix is a substring that occurs at the end of a string. For example, the string “ue” is a suffix of the symbol “value” and the string “y” is a suffix of the symbol “Only”. (Note also that each string is a suffix of itself.) However, the string “va” is not a suffix of the symbol “value” and the string “nl” is not a suffix of the symbol “Only”.
Here, psu represents the name of the command and str represents a string. If the list is empty, your program should print the message “The list is empty.”. Otherwise, your program should traverse the list (from left to right); if the given string str is a suffix of the symbol stored in a node, then the command must print the symbol and the corresponding count on a line by itself. (Thus, when the list is non-empty, the number of lines printed is the number of symbols which have the string specified by str as a suffix.)
I.End Command: The syntax for this command is as follows:
end
In response to this command, your program must stop.
Assumptions: In writing this program, you may assume the following.
(a)Symbols, prefixes, suffixes are all case sensitive. (Thus, the symbols “value” and “Value” are distinct. The string “val” is a prefix of the symbol “value” but not a prefix of the symbol “Value”. Similar considerations apply to suffixes.)
(b)The command given by the user will be one of ins, del, fde, pst, prl, pcr, ppr, psu or end. (The command names are also case sensitive.)
(c)Each command will contain all and only the necessary arguments. (Thus, commands won’t have missing or extraneous arguments.) Further, when a command has one or more arguments, the command name and the successive arguments will be separated by one or more spaces.
(d)Each string specified in a command will have a length of at least 1 and at most 10; further, the string won’t include any whitespace characters.
(e)Integer values specified in commands will be non-negative; further, in the pcr command, the value specified by v1 will be less than or equal to that specified by v2.
Thus, there is no need to deal with any erroneous commands. Your program should continue to prompt the user and process commands until the user types the end command.
Program Outline:
1.Prompt the user for a command.
2.Read the command.
3.While command is not "end":
a.Read the value(s) for the command, if necessary.
b.Process the command.
c.Prompt the user for the next command.
d.Read the next command.
Structural Requirements:
In addition to main, you must have a separate function to implement each of the commands ins, del, fde, pst, prl, pcr, ppr and psu described above. (You may have other functions in addition to these.)
Suggestions:
(a)Use the "%s" format to read the command as a string into a char array of size 4. (Since each command is exactly three characters long and each string must be properly terminated using the ’\0’ character, the size of the character array must be 4.)
(b)Use the "%s" format to read the string specified as an argument to the commands ins, del, ppr and psu. (You may use a char array of size 11 to store such a string.)
(c)Use the "%d" format to read the integer value(s) specified as argument(s) to the commands fde and prc.
(d)Use the strcmp function in the string library (<string.h>) to identify which command is specified.
(e)Use I/O redirection facility while testing your program.
(f)Use fflush(stdout) after each call to printf.
Submission:
You must perform submissions as directed by your instructor. Submission should include:
source code for the evaluation – the procedure will be explained in your lab classes,
screenshots with program output.
For the team project each team must make only one submission. That is, in each team, ONLY ONE member must do this. Team submissions must include additional documentation in the source file as explained below.
Important Notes: ignoring any of the following rules will result in penalty or even ZERO grade for the project.
1.For Project 2 you must turn in the file named “p2.c”.
2.At the top of your C source file the following information must appear in the form of comments:
(a)Students working by themselves must have the following information at the beginning of your source file (p2.c) in the form of comments:
i.course code and title,
ii.semester,
iii.your class ID (e.g., ZR160102),
iv.your name,
v.your student ID,
vi.the name of your lab classes supervisor.
(a)Students working in a team must have the following information at the beginning of your source file (p2.c) in the form of comments:
i.course code and title,
ii.semester,
iii.your class ID (e.g., ZR160102),
iv.the names of the two team members,
v.the student IDs of the two team members,
vi.the name of your lab classes supervisor.
vii.A clear explanation of how the work for the project was divided among the two team members. Indicate clearly who developed each function and how the testing work was divided between the team members.
3.Make sure that your programs compile and produce correct results on the lab machines. Programs that cause compiler or linker errors on these machines will NOT receive any credit.
Some sample data to test your program:
Important Note: Some sample inputs that can be used to test your programs are given below. However, you should remember that when we compile and run your source files, we will use other data. Just because your programs work for the sample inputs given below, you shouldn't assume that they will work for all inputs. Therefore, you should test your programs thoroughly with other input values.
Attached there is sample test input for this program in the file t1.txt. The file contains a sequence of commands to be executed by your program.
The file t1_with_output.txt shows respectively the commands in t1.txt along with the correct output for each command that produces output. (Recall that some of the commands don't produce any output.) In the file that shows the outputs, a blank line has been inserted between successive commands to make it easier to see the outputs for each command. Your programs need not include such blank lines.
Important Note:
(a)You should use command line and have the sample test inputs and outputs mentioned above available in your working directory.
(b)Remember that your program MUST read its input from stdin and produce its output to stdout. Use the I/O redirection facility while testing your program.
(c)It is very important to construct many sequences of commands and thoroughly test your programs. Be sure to test your program with sequences of commands so that the list becomes empty in the middle of a command sequence and the remaining commands in the sequence allow the list to grow.
Program Grading:
If the ins, del and fde commands (which can modify the list) don’t work correctly, the answers for all subsequent commands are likely to be incorrect. Test your code thoroughly. If your program crashes during the middle of a sequence of commands, you won’t get credit for any of the subsequent commands in that sequence.
For students working individually:
(a)Correctness: 85 points
(b)Structure and documentation: 15 points
For students working in a team:
(a)Correctness: 65 points
(b)Structure and documentation: 15 points
(c)Team work: 20 points
Each team member must participate in developing, documenting and testing the program. Each team should include additional documentation at the beginning of the source file indicating how the work for the project was divided between the two team members. (Indicate clearly who developed each function and how the testing work was divided between the team members.) After the submission deadline, each team must meet with their instructor who supervises the lab classes. During the meeting, the instructor will ask questions about the team’s program and determine the points for team work. (The two team members may receive different scores for team work.)
Example of program execution:
> p3.out
Command? ins Loop
Command? ins Search
Command? ins begin
Command? prl
Loop 1
Search 1
begin 1
Command? ins begin
Command? pcr 2 3
begin 2
Command? ins Loop
Command? ins Long
Command? ppr Lo
Loop 2
Long 1
Command? ins Starch
Command? psu arch
Search 1
Starch 1
Command? pst
No. of nodes = 5
Max. count = 2
Min. count = 1
Avg. count = 1.4
Command? fde 1
Command? del begin
Command? prl
Loop 2
begin 1
Command? end
>
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。