C program辅导、Data records CS 程序代作代辅导、辅导C语言
- 首页 >> C/C++编程Write a C program [1] that manages records. Data records are
linked as a list in memory, and can be saved to and loaded from a file.
1 Introduction
Any kind of data can be recorded as a struct by a C program. In this
homework, we will write a program that can manage data records, which
∗The picture above is from http://www.audioasylum.com/audio/vinyl/messages/92/924529.html
1
are represented as a list in memory, where each node contains a piece of
record. The list of records can be saved to or loaded from a file. Different
operations can be performed on the list of records.
1.1 Format of data record
A data record is a structure that contains at least the following fields
• id, which is an unsigned integer. Each record has a unique id.
• type, which is a value belong to some enumerate type defined in this
program. A programmer should design this enumerate type to include
every kind of data. At least the following types are allowed: integer,
char, float, string. You can allow other types, such as a book (a struct),
or a image (a sequence of bytes that represents some photo).
• size, which is an unsigned integer representing the number of bytes of
the data record.
• addr, which is a general pointer (void *), whose value is the address
of the first byte of the data; the data is save somewhere else in the
memory (in the heap) not in this data record structure.
A list node contains a data record, and the address of another list node
(two address if use a double-linked node).
1.2 Format of file
When a list is saved into a file, its content contains the following item, one
by one from the beginning of the file to the end of the file.
• An integer, that largest id that is used in the operation of the list
record.
• An integer, that is the number of data records saved in this file.
• The id of the first record (record 1).
• The type value of record 1.
• The size value of record 1.
• The content of the data of record 1. It means that the sequence of
bytes that appears in the memory at the addr of record 1 is copied
here in the file.
2
• Some other fields of record 1 according to your design of data records.
• The id of the record 2.
• The type value of record 2.
• The size value of record 2.
• The content of the data of record 2.
• Other possible fields of record 2.
• . . .
• The id of the record n.
• The type value of record n.
• The size value of record n.
• The content of the data of record n.
• Other possible fields of record n.
2 User Interface
• The program accepts a command line argument as a file name. For
example, given an executable file name rcdmgr, if we run it using the
command: rcdmgr records.rcd, then the file records.rcd will be accepted
as a file name.
• When the program starts, a user will see a menu as follows:
=========String Manager==========
- G: Generate/create a new file.
- I: Input/Load a file into a list of records in memory
- O: Output/save the list of records to a file.
- P: Print the records in the list
- A: Add a new record
- C: Count the records
- D: Delete a record
- SI: Search a record by ID
- ST: Search records by type
- S: Save the list to file
- Q: Quit the program.
==================================
3
The operations are explained in details here:
• G: Ask the user to type a file name. If the file exists already, print out
some message for this fact and do nothing. Otherwise, the file does
not exists, try to create it. If file creation is not successful, print some
error message. Don’t forget to close the file stream after the file is
created successfully.
• I: Asks the user if the file name provided by the command line is the
choice? If not, asks the user to input a file name. Try to open the file.
If the file cannot be opened successfully, quit the program. After the
file is successfully opened, copy the file content into list of records in
memory. (Hint: use the fopen function in binary mode and allow both
reading and writing).
• O: Save/output the record list into the file that is currently open. If
there is no file currently open, or if there is list created in the memory,
print out some error messages.
• P: Print all records in the list. For each record, its id, type, and data
content should all be shown in some proper way.
• A: Ask the user the type of the record, and then ask the user the data
content. For example, if the record type is a string, then a user should
type a string. The data content should be saved in some allocated
space on the heap. The id of the record should be 1 plus the current
largest id used. The record is wrapped in a list node and append to
the end of the list. Note that the list is created when the first record
node is created.
• C: Count and print the number of records in the list.
• D: Ask the user to provide the id of a record. Then if record with the
id is searched in the list. If such a record is found, its node is deleted
from the list and the memory of the node is freed. If the record is not
found, print out some error message and do nothing.
• SI: Ask the user to provide the id of a record. Then if record with
the id is searched in the list. If such a record is found, its record
information is printed (id, type, data content). If the record is not
found, print out some error message and do nothing.
4
• ST: Ask the user to provide a type. Then list is searched to find all
data records with the type. The search result should be saved in some
array or list that contains the addresses of all the data nodes that are
found.
• S: Save the list of records into the file that is currently open. If no file
is currently open, print some error message and do nothing. Before
the list of records is saved to the file, the read/write position should
be moved to the beginning of the file, and the existing file content will
be removed and replaced by the list records. Don’t forget to write the
max id, and record number at the beginning of the file.
• Q: Quit the program. It is optional if you want to save the list file
before quiting or not. The opened file should be closed before quitting
the program. The memory occupied by the list should be freed.
3 Running behavior of the program
• When the program starts, it opens the file whose name is given as the
command line argument so that the file can be both read and written.
If the command argument is not provided, or the file cannot be
successfully opened, print some error message and quite the program.
• Repeat:
– Show the menu
– Ask the user to provide a string as the choice of a menu item.
– Do the operation according to the user’s choice of menu item.
– If the user’s choice is not listed in the menu, print the error message
and ask the user to do it again.
The program ends when the user choose the menu item “Q”.
4 Code structure of the program
The program contains at least 3 global names.
• The file name. The name of the file that should used to load and save
records. Call it the working file. Initially it is an empty string.
5
• The file stream (FILE *). The file stream of the working file. Initially
its value is NULL.
• The max id. The largest id used for a record so far. Initially its value
is 0.
The program contains 4 files:
• ui.c: It contains at least two functions
– show_menu(), which prints the menu.
– menu_choice(), which asks the user to provide a character for
choosing an item in the menu. Input queue should be cleared after
each input. If user’s input character is not a menu choice, print
some error message and ask the user to choose again. Finally,
return the character of the choice.
• list.c: This file contains the functions to do operations related to a
list.
• list.h: For each function in list.c that will be used by some other files,
its function prototype is contained in list.h. Also, the data types
defined for a list should be in this file. This file should be included by
driver.c and list.c.
• driver.c: It contains the main() function of the program. The functions
defined in ui.c and list.c are used here. The main() function
describes the running behavior of the program.
5 Optional Extra Operations
You can design your own operations of record operations, and extra points
will be earned according to code quality. For example, you can consider the
following extra features.
• Add some special record type, such as certain kind of record, like a
book, or a student.
• Operations to encrypt and decrypt the some data records.
• Allow the user to set some password of the file. When the program
tries to open such a file, the user is asked to type the password. When
the password is wrong, the file cannot be used.
6
• Divide the string in two half (in some way) and save them in two
different files;
• Combine two different record files together to make a .
6 Grading of the Program
Your program will be graded by its quality and style. Detailed policy of
grading is included in the file HmkGradingPolicy.txt. At most 20 points can
be awarded for the optional operations, depending on the complexity and
difficulty.
7 Submission
• Due time: Sunday 03 June 2018, 10:00pm.
• Compile and debug and run your program.
• Your code should be pretty, i.e, you should indent and align your lines
and code nicely and logically following some pretty-printing style.
• At the top of each of your source files (.c or .h files), write your name,
last four digits of student id and date as comments.
• Attach all of your source code files to an email (in his homework, a
single file fib.c is enough). Do not attach binary files (.o, .obj, .exe or
executable) files, since doing so will make your email being considered
as virus by the Gmail system. You are welcome to write a file to
explain your code and discuss the problems that you met in coding.
• Send your email to
LP101must@gmail.com
• The title of your program should be like:
[name][last 4 digits of student id][D1|D2][hmk2 Records]
References
[1] Stephen Prata. C Primer Plus (6th Edition)(Developer’s Library).
Addison-Wesley Professional, Indianapolis, IN, USA, 2013.
7