CS111问题求解

Homework 1 : Knowledge items of C++ (part 1)
C++ : CS111 EIE111 LP104
MUST 2021 Spring
March 22 2021
I. Purpose of the homework
In order to write a good program, you need to know the following
(a) The target tasks:
The problems to be solved, the requirements and constraints.
(b) The proper computation process for the task
The algorithms.
The data structure.
(c) Enough knowledge of the programming language.
So that the computation process can be implement as a program.
There other homeworks that will emphasize the part (a) and (b). This homework emphasizes on part (c), and it will test different knowledge items
(KI). The approach is based on the following observation:
A knowledge item (KI) can be described or tested by at least two ways:
Asking and answering some questions.
Doing some tasks that are related to the KI.
II. Requirements of the homework
Write a C++ program that satisfy two aspects of requirements:
A: Use the C++ style as much as possible
If there are different ways to write the code, choose the C++ way, not the C way. For example:
For input and output, use cout and cin , instead of printf() and scanf() .
Include the head file cstring in stead of string.h .
B: Implement each task descried in Section IV
Section IV describes a sequence of numbered tasks. Each task requires writing some simple code to reflect your understanding of some
knowledge items of C++.
For each task, say the n
th
task, in your program you should the write some comment of the form / / besides the statements that
implement the KP.
If a task can be implemented by several different pieces of code in the program, the comment mentioning a task number only needs to appear
once in the program. .
For example, for the 5
th
requirement :
5: Declare a variable of type int .
The corresponding code that cover this knowledge point in the program could be like the following:
int x; / <5> /
If there are 3 statements declaring integer variables in the, the comment / <5> / only needs to appear once in your program.
If a piece of code can cover several tasks, then the task numbers can be mentioned together. For example, if a statement that can cover the tasks
4 and 6, the its comment can look like:
/ <4> <6> /
// some statement
III The knowledge needed
The tasks will cover the knowledge aspects covered from chapter 1 to chapter 9 in the textbook
These tasks cover the knowledge points of C++ in the following aspects:
Compile programs of C++, C++ 2011 at command line.
Structure of a program. Relationships between .cpp and .h files.
The proper content of head files and the .cpp files.
How to use the names defined in other files.
Data types, constants, variables, arrays.
composite data types:
struct, union, enum
Variable and array initialization
How to create data storage on different memory sections
static area
stack
heap (allocation and release)
pointers and references
Function definition, call, parameters, arguments
An array name as an argument of function calls
Recursive function
Three loop statements
new style of for statements in C++
if, if-else statements
switch statements
input and output
cin and cout
know the status of previous input
clear input queue
reading and writing files.
name spaces.
template functions
Define functions to solve simple computation problems
IV Tasks
This section describe a list of tasks that need to be fulfilled in the program. For some tasks, their related knowledge items (marked by [k]), hints to
do it ( [hint] ), and the design ideas ( [design] ) are mentioned.

  1. / ------ files of the program, relationship of files------ /
    Create 4 files hmk1.cpp , tools.cpp , data.cpp , tools.h , and info.txt . These are the files of this program.

[design]
hmk1.cpp is the driver; it contains the main() function.
tools.cpp defines all the tool functions.
data.cpp defines some data that can be used by other .cpp files.
info.txt , a file that your program will read and write.

  1. In hmk1.cpp and tools.cpp (with some preprocessor instruction) include the head file needed for using cout and cin .
  2. In tools.cpp , include the library head files needed to call the library function isspace and strlen .
  3. In tools.cpp , and hmk1.cpp , include the head file tools.h
  4. Let hmk1.cpp contain only one function definition -- the main() function.
  1. Let all of the other functions (except main ) be defined in tools.cpp .
  1. Provide a function prototype in tools.cpp . The name of the function is clear_input_queue . It has no parameter and no return. It can only
    be called by the code in tools.cpp .

(provided by tools.cpp ) does not need to know the details of it. So, maybe it is ok to hide clear_input_queue() for the code outside
of tools.cpp .

  1. For all of functions defined in tools.cpp , except the "hidden" ones like clear_input_queue() , put their prototypes in tools.h .
  1. In tools.h , create two symbolic constants (in two different ways)
    TITLE_LEN which is 80
    NAME_LEN which is 50
    [hint] One by #define and the other by a const definition.
  2. / ------ Data: types, constants, and storage holders ------ /
    In hmk1.cpp , in the main function,
    define a local variable for each of the following types:
    Every char type. There are at least 5 different char types in C++.
    Every integer type (other than the char types).
    Every floating-point type.
  3. Initialize each of the local variables that are described in task item 10, with a constant of its type.
  1. In tools.h , create a structure ( struct ) data type called book , which contains 3 fields:
    title , which is an array of char, with the TITLE_LEN elements.
    authors , an array of char with NAME_LEN elements.
    price , a double.
    id , a union (about its type, you can choose it to be a nameless union type, or with some separately declared union type) of two fields
    isbn . It will record some string
    [hint] For its implementation, you can choose a char array or a string object
    library_id , which is an integer.
    [design] A book needs some id, which is either some ISBN number of a number chosen by the library, but not both. Therefore, for
    saving memory space, we can let id has an union type.
  2. In tools.h , declare an enum type Weekday , which contains the enumerators for each day in a week, from Mon to Sun .
  3. In tools.h , declare a enum type Month , which contains the enumerators of each each month, from Jan to Dec .
  4. In tools.h , declare a structure type called Date , which has three fields:
    year , an unsigned int
    month , of the type Month .
    weekday , of the type Weekday .
  5. In hmk1.cpp , define a global variable today , of the type Date . It has external linkage (can be used by other files).
  6. In data.cpp , create a global character array myName , with NAME_LEN elements. It can be used by the code of other files.
  7. In data.cpp , define a global variable bestBook , whose type is Book . It has external linkage.
    [design] The global names today , myName and bestBook are defined in two different files, and they all can be used by some other files.
  8. In the function main , asks the user the input data to initialize today , myName and bestBook . Then print the values of them.
  9. In the function main() , declare a integer variable x such that its value cannot be modified.
  10. In the main() function declare an integer pointer variable xp that
    its value be modified.
    it cannot be used to modify that data tht it points to.
  11. Declare an pointer variable xp2 that
    It points to an int array of 5 elements.
    It cannot be used to modify the data that it points to.
  12. Declare an array ar1 of 5 integer addresses.
  13. Declare an function pointer variable fp1 and initialize it with the library function name strlen , without using keyword auto .
  14. Declare a variable fp2 using the keyword auto , and initialize it with the library function name strncpy .
  15. In the program, all of the data that are allocated using new or new [] should be released by delete or delete [] when they are not used
    any more or before the end of the program.
  16. / ----- Compilation ----- /
    In a comment at the top of the hmk1.c , provide a command-line command that can compile the program into an executable file using
    the compiler g++.
    [hint] All the .cpp files of the program are needed by the command line. Some features are supported only C++11, like the usage of
    auto . To allow c++11 features, some special option is needed in a command to use g++ to compile the program.
  17. / ----- input and output ----- /
    In a single statement using cout to print out values of at least 3 different names.
  18. Show an example of a "message statement" mention in Page 61 of the book.
    [hint] Use some comment to indicate it.
  19. Print the largest value of int and smallest value of long long using some defined symbolic constant.
  20. Initialized a variable (Assign a value to a variable when declare it) using some syntax that is not allowed by C, but allowed by C++.
  21. Print the values of all the variables defined at task 10 (Do not use printf)
    [hint] to print out a constant of each character type, see page 89 and 90 of the textbook. Do not use printf .)
  22. Given an integer variable, print its decimal, octal, and hexadecimal values using cout .
  23. Using a single statement, using cout to print the character 'A', and its integer value.
  24. Print the number of bytes cooupied by some array using sizeof.
  25. Initialize an integer variable using some syntax of C++ that is not allowed by C.
  26. Record an input sentence with multiple words, p typed by the user, as a c-string in a character array.
  27. Record a sentence with multiple words, provided by the user, as an object of the string class.
  28. Input a single character and record it in some char variable. The input character can be space or return.
  29. In the main function,
    open the file info.txt .
    output (write) the string recorded in myName into the file,
    read the content of the file into some character array, and print it on the screen.
    [hint] This is to check whether the operating of writing to file is successful. You can do it by reading the characters one by one and
    print them, or you can read the file content into some string (object or charcter array) and print the string.
  30. / -------- function definitions ------------ /
    In tools.c , provide the definition of the function clear_input_queue (Its prototype is described by task 7).
    It clears the input queue for the standard input stream (from keyboard).
  1. Call the function clear_input_queue() in the program at some proper situation.
  2. In hmk1.cpp , define the main function. The computation of this function are partially described the other task items. Especially,
    In the function main() , call at least once every function whose prototypes is in tools.h .
  3. In tools.cpp , define a function print_my_name , which prints myName that is defined in data.cpp .
    [k] In a file, how to use some name defined in another file?
  4. In tools.cpp define a function get_my_name , which asks a user to provide a name from keyboard with multiple words, save the name in
    myName defined in data.cpp , and clear the input queue. This function is called in the main() function.
    [hint] You can decide the return type of this function.
    [k] Input multiple words from the keyboard.
  5. Define a function that can reverse a string in place of a character array.
  6. Define a function that can reverse the order of element of any array in place.
    [hint] You can design the parameters of the function. You can either using function template, or use void * to do it.
  7. Find an algorithm that print a decimal integer into binary form.
    Describe the algorithm in the comment.
    Provide function definition.
    Test it in the main() function.
  8. Define an inline function that can return the larger one among two integer parameters. Test it in the main function
  9. Define and test a function, that is non-recursive, using some loop, to compute the largest one among the elements of an integer array.
    [hint] Define the function in tools.cpp and test it in the main() function in hmk1.cpp .
  10. Define and use a recursive function arr_max to compute the largest one among the elements of an integer array.
    [hint] it is like the above task, but the difference is that it is recursive.
  11. Define and test a recursive function sum_rec to compute the sum from 0 to n, given an non-negative integer n as its parameter.
  12. Define and test a function sum_while to compute the sum from 0 to n, given an non-negative integer n as its parameter, using while
    statement in its body.
  13. Define and test a function sum_for to compute the sum from 0 to n, given an non-negative integer n as its parameter, using the for
    statement in its body.
    Use some syntax of the for statement that is not allowed by C but allowed by C++.
  14. Define a function week_plans() , which has a parameter day of the type Weekday as its parameter, and then prints a different sentence for
    each day of a week. For example
    if day is monday, it prints "Focus on study`.
    if day is Friday, it prints "Have some fun`.
    ...
    you have to use the switch statement to implement the function.
  15. Define and test a function hello in tools.cpp . The function has the following features
    It has no parameter.
    It prints a line: "Hello" followed by the value of the variable myName .
    It "remembers" the number of times that the function is called using a local variable defined in its body, and returns this number.
    [hint] Can ca local variable have a duration that is longer than a function call?
  16. Define and test a function grades , which has an integer parameter n , and prints a character according the following conditions:
    if , print A
    if , print B
    if , print C
    if , print D
    Otherwise, if , print F
    You have to use the if and else statements to implement this function.
  17. Define a function nine_roots() , which accepts an integer n as its parameter, then prints out square roots of the nine numbers from n from
    n+8 , in a grid of 9 blocks, perfectly aligned. Each square root is printed keep 3 decimal points. Use cout to print the numbers and strings.
    Call the function in main() . The definition of this function should do the following
    Set the width and precision of cout .
    After printing, resetting the printing format of cout before calling the function.
    n >= 90
  18. <= n < 90
  19. <= n < 80
  20. <= n < 70
    n < 60
    [hint] There is some sample program in Chapter 8.
    For example, when nine_roots(10) is called, it will print the square roots from 10 to 18, as follows:
    one two three
  21. 3.162 3.316 3.464
  22. 3.605 3.741 3.872
  23. 4.000 4.123 4.242
  24. / -------- Function Template ---------- /
    In tools.cpp . Define a template function, find_arr_max .
    This function will return the largest element of an array with general element type, by comparing elements using the < operator.
    You can design the prototype of this function.
  25. Answer a question: Will your definition of find_arr_max work for all possible array? provide your answer in some comment besides the
    definition of find_arr_max .
  26. Provide special function definition of find_arr_max so that when the type of the array element is char , it will perform an additional
    operation: printing a message "I am calling find_arr_max for char".
  27. Use some statement in tools.cpp to prevent the function find_arr_max be specialized when the array elements are string objects.
  28. / ----- Function overloading ------ /
    In tools.cpp Define a 3 functions that have the same name together .
    Version 1: it accept two integers and return its sum.
    Version 2: it accept two C-strings (pointer to character), and return a c-string allocated on heap that is the concatenation of the two
    strings.
    Version 3: it accept two string objects and return a new string object that concatenates the two.
    Test these three function in hmk1.cpp .
  29. Define 2 functions with the same name swap :
    Version 1: it accept two integer addresses at its parameters and swap the data the two addresses.
    Version 2: it accept two integer references at its parameters and swap the data that are ferenced.
    Call (test) these two functions in the main() function.
  30. / -------- Namespaces --------- /
    In tools.h , declare two namespaces, FI (the faculty of information technology) and FA (Faculty of Humanities and Arts) for two faculties in
    our university. Both namespaces contain two names:
    url : is a string object recording the url of of the university
    In FI, it records https://www.must.edu.mo/fi
    In FA, it records https://www.must.edu.mo/fa
    welcome , it is a function that prints some multi-line welcome message. Different messages should be printed by the function the two
    different namespaces. (You can decide its prototype)
  31. In the main function, print the urls of FI and FA, and call the welcome function of the two name spaces.
    Submission of the homework
    Each student needs to finish the homework alone.
    No group work or plagiarism is allowed.
    Submit all of the source code files related to this homework to the Moodle website of this course.
    In the Moodle website, you should find the folder for this assignment.
    Also uplode a file readme.txt that describes the status of your homework:
    Among the task items, which are done perfectly, which have problems, which are not tried.
    Your experience. How do you feel about this homework. You are encouraged to use English, but you can use Chinese.
    Deadline: 2021 April 1 Thursday 11:00pm
    References
    [1] "C++ Primer Plus", Stephen Prata, edition 6, ISBN:978-0321-77640-2, Pearson

你可能感兴趣的:(c++)