CS计算机代考程序代写 c++ 2

2

Q1: [Program Style and Basic Programming] (25%)
Calculate the following formula by taking ! (date type: int) and ” (data type: int) as inputs.

• If either of ! and ” is not positive, ask the user to input it again until the inputs are valid
(! > 0 and ” > 0).

• If ! and ” are both positive, then output the result using the following formula.

• Please use “loop” to calculate !!, and do NOT use the function pow(!, “).

• Data type of the result is double and print it in 2 decimal places.

• Finally, check and print the integer part of the result is an even or odd number.

! +
#

# × % × & +
#

% × ‘ × &
! +

#
‘ × ( × &

” +⋯+
#

! × (! + #) × &
#

Expected Outputs (Inputs are underlined):

Example 1
Input the value of y: -2
Input the value of y: 2
Input the value of n: -3
Input the value of n: 3

The result is: 5.33
The integer part of the result is an odd number
Example 2
Input the value of y: 3
Input the value of n: 3

The result is: 8.25
The integer part of the result is an even number
Example 3
Input the value of y: 2
Input the value of n: 1

The result is: 2.00
The integer part of the result is an even number

3

Q2: [Program Development] (25%)
Given a text file “input.txt” (that can be downloaded from Canvas) with a list of positive integer
numbers. Each number is between 1 and 26. Please write a C++ program to do the following.

1). Prompt user to input another integer “, which is also between 1 and 26. Then, the program
reads all the numbers from “input.txt” to put the numbers that are less than ” into one array,
e.g., int arr_int1[50], and the remaining numbers into another array, e.g., arr_int2[50].

• We assume both arrays can store up to 50 numbers and this array size is large enough.
• We assume the integer ” is valid and no need to check its correctness.

2). For each integer array, e.g., arr_int1[50] or arr_int2[50], convert each number in the array to
a letter in the range of ‘a’ .. ‘z’, i.e., convert 1 to ‘a’, 2 to ‘b’, … and 26 to ‘z’, and then store these
letters in a new character array, e.g., char arr_char1[50] or arr_char2[50].

• Converted letters from arr_int1[50] and arr_int2[50] are stored in arr_char1[50] and
arr_char2[50], respectively.

3) Declare a dynamic array with size that is just sufficient to store the total number of letters
from the two character arrays in Step 2). Then concatenate the letters from these two arrays,
store them in the dynamic array, and print the dynamic array on the screen.

• This dynamic array should store the letters from the array with fewer letters, followed by
the letters from the other array with more letters.

• If the number of letters of both arrays are the same, store the letters from the first array,
e.g., arr_char1[50], before those from the second array, e.g., arr_char2[50].

If the input file “input.txt” is:
6 8 12 10
25 24 9 11

Expected Outputs (Inputs are underlined):

Example 1
Enter n: 10
fhiljyxk
Example 2
Enter n: 12
lyxfhjik
Example 3
Enter n: 11
fhjilyxk
Example 4
Enter n: 6
fhljyxik
Example 5
Enter n: 26
fhljyxik

4

Q3: [Program Comprehension and Development] (25%)
Assume there are 10 books and their IDs are represented by the characters from ‘A’ to ‘J’. Please
Implement a class called Book, which contains five private member variables:

• id: a single char variable, storing the book ID
• subject: a cstring, storing the subject this book belongs to, e.g., “Computer Science”
• price: an int variable, storing the price of the book
• month: an int variable, storing the month for the returning date of the book
• day: an int variable, storing the day for the returning date of the book

The class Book also has the following public member functions:

• getID(): returns the book’s ID
• getSub(): returns the book’s subject
• getPrice(), returns the book’s price
• getMonth(), returns the book’s returning month
• getDay(), returns the book’s returning day
• set(char i, char s[], int p, int m, int d), sets each member variable

Part-a): Complete the class definition of Book (do NOT change any identifiers given above and
do NOT add any extra members).

• All the member variables are private, and all the member functions are public
• Declare all the member variables
• Max. length of the book subject is 16 (“Computer Science”). The size of member variable

subject should be just large enough to store this longest subject name, i.e., when
“Computer Science” is stored, all the elements in subject should be fully occupied.

• Implement getID(), getSub(), getPrice(), getMonth() and getDay()
inside the class directly

• Write the prototype of set(…) inside the class. Implement it outside the class and do
NOT use strcpy(…)function.

Part-b): In Q3(), we provide the information for ten books, including their IDs, subjects, prices
and returning dates. Please declare an array to store 10 Book objects, and use the information
provided above to initialize each Book object.

Next, implement a non-member function, sortBooks(Book bArr[]). It sorts an array of
Book objects, according to their prices in an ascending (increasing) order. We assume these 10
books have different prices.

5

Part-c): Please implement another non-member function, sortBooksAdv (Book bArr[]).
It sorts an array of Book objects according to:

• Returning date, including both month and day, in an ascending (increasing) order, e.g.,
o book returned on Dec. 1 is smaller than the book returned on Dec. 15, and
o book returned on Oct. 15 is smaller than the book of Dec. 1

• If two books have the same returning date, further sort them according to their IDs in an
ascending (increasing) order.

Expected Output:

Example
Sort books by their prices:
C(10) A(20) B(25) G(30) D(45) E(55) I(60) J(65) H(85) F(100)

Sort books by their returning dates and IDs:
D(3-1) C(5-1) A(9-1) E(9-15) I(9-20) B(10-1) F(10-15) G(12-1) H(12-15) J(12-20)

In the first sorted result, e.g., for C(10), C is the book ID and 10 is the price.
In the second sorted result, e.g., D(3-1), D is the book ID, 3 is the returning month, and 1 is the
returning day.

6

Q4: [Program Development] (25%)
Write a program to analyze words in a given sentence (less than 100 characters). In this question,
you may use strlen()from , but do not use other functions from this library.

Part-a) The program reads a sentence (containing spaces) from keyboard into a cstring, e.g.,
str[100]. It then scans the sentence for words (which is a consecutive sequence of one or
more uppercase/lowercase English letters).

It further prompts user to input an integer ” and a lower-case English letter %.

• In part a), we assume the input string contains English letter(s) and/or space(s) only.
• We assume the integer ” is valid and no need to check its correctness.
• We assume the lower-case English letter % is valid and no need to check its correctness.

Write in the Q4() and Q4_Part_A(…) functions to print the following information:

• Print the number of words of length ” (i.e., the number of letters) in the input string.
• Print the number of words that contains % in the input string. This decision is NOT case-

sensitive, i.e., if % is ‘a’ but one word has only ‘A’ without ‘a’, this word is still counted.
• Print the longest word in the input string. If there are multiple words having the same

(longest) length, the first one should be printed.
Hints:

• You can use a loop to go through the string. In each iteration, use if() to find the first
letter of each word.

• You can declare two variables vMax and vPos to store the length and position (index of
its first letter in the string) of the longest word that have been seen so far.

Part-b) The program reads another sentence from keyboard into str[100]. However, in
addition to space, the string may contains some non-alphabet characters, e.g., ‘+’, ‘5’, ‘?’, etc.

Write in the Q4() and Q4_Part_B(…) functions to print the following information:

• Print the shortest word, i.e., the word that has the shortest length (i.e., the number of
letters). If there are multiple words having the same (shortest) length, the first one should
be printed.

• Non-English letters should NOT be counted as part of a word and should be ignored, e.g.,
“C++” is treated as “C” of length 1, “#A+5-b” is treated as “Ab” of length 2, etc.

• When you print the shortest word, print the English letter(s) only.

7

Expected Outputs (Inputs are underlined):
Example 1
Enter a string: I am a CityU student
Enter an integer: 1
Enter a lower-case English letter: c

Number of word(s) of length 1 is: 2
Number of word(s) containing c is: 1
The first longest word is: student

Enter another string: C++ is a programming language
The first shortest word is: C
Example 2
Enter a string: I come from CS
Enter an integer n: 3
Enter a lower-case English letter: m

Number of word(s) of length 3 is: 0
Number of word(s) containing m is: 2
The first longest word is: come

Enter another string: B! is less than A?
The first shortest word is: B
Example 3
Enter a string: I am learning computer programming
Enter an integer n: 8
Enter a lower-case English letter: m

Number of word(s) of length 8 is: 2
Number of word(s) containing m is: 3
The first longest word is: programming

Enter another string:
The first shortest word is: iostream

— END —