Assignment Three (10%)
Due Date: 22 April 2022, 23:59 PM [Week 14]
1. This assignment contains four questions. You are required to submit a complete C++ program (.cpp only) for each question on PASS before the deadline.
2. You can submit as many times as you want before the deadline. We will grade your latest version.
Copyright By PowCoder代写 加微信 powcoder
3. Only a small set of the test cases are visible for the testing, and a more comprehensive set of test cases will be used for grading. In other word, passing all the visible test cases does not mean that you can get the full mark for this question. Try your best to thoroughly test your program.
4. Hidden test cases will not be released.
5. The marking of each question is based on the percentage of the total test cases that your solution can pass. If your submitted solution leads to a compilation error on PASS, zero mark will be given to that question and no manual checking to your solution is provided in such a case.
6. No late submission will be accepted.
7. Plagiarism check will be performed.
8. You only need to use the material learnt from this course. It is NOT necessary to include any other library, except
To ensure timely feedback to the students’ questions, each of the following TAs will be responsible for one question. If you have any questions, you can contact the corresponding TA directly.
• Q1: Mr. NING
• Q2: Ms. NANDI
• Q3: Mr. TOROMANOVIC
• Q4: Mr. ZHANG
Q1. Cstring and Pointer
C++ has a set of functions implementing operations on strings inherited from the C language. These functions support various operations, such as copying, concatenation, searching, etc.
Write a program to implement some of these operations using your own code.
Requirements:
Declare your own functions as the followings:
1. char * mystrcpy (char * destination, const char * source);
Copy the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
2. int mystrcmp (const char * str1, const char * str2);
Compares the string str1 to the string str2. There are some rules for comparing the string.
Compare the first element of str1 and str2. If first element of str1 and str2 is same, compare the second element of str1 and str2 and so on. First different element will determine which string is bigger according to the order of ASCII code.
If str1 is the substring of str2, then str1 is smaller than str2 and vice versa. Here are some examples.
1. “A” < "B" 2. "A" < "AB"
3. "A" < "a" 4. "AB" < "a"
5. "A B" <"AB" (There is a “space” between A and B in left side)
3. int mystrstr (char * str1, const char * str2);
The mystrstr function is used to determine if str2 is a substring of str1. If so, the function returns the location where str2 first appeared in str1; Otherwise, -1 is returned.
Hint: The location is started from 0.
Call your own functions in main function. Follow the same format of inputs and outputs with examples.
1. mystrcpy
2. mystrcmp
Input the source string:
CS2310 computer programming
The copied result string is: CS2310 computer programming
Input the string1:
Input the string2:
string1 is bigger than string2
3. mystrstr
Input the string1:
CS2310 computer programming
Input the string2:
The location where string2 first appeared in string1 is: 7
Q2. Library borrowing system
In this question, you are required to design a simple library borrowing system and finish tasks as the followings:
1. Save the information about the books, including book name, book published year, the number of each book and subject.
2. Assign the books into different subject categories according to the input. For simplicity, assume:
l There are only three subject categories in this small library.
l Each book only can be assigned into one subject.
3. List all the books and their information under each subject category respectively.
l You should print the number of the books that in the same subject.
l The books listed in each subject should be sorted by date (oldest to newest). 4. Accept the name of the book that the reader wants to borrow.
l If the number of this book is 0, tell the reader this book was borrowed and ask the reader to input again.
l If the number of each book is 0, you should print this sentence: "There is no book in library." Then you can choose to 1) Quit this system because there is no book. 2) Save new books 3) List books, but the numbers of all books should be 0, and the number of each book should also be 0.
l We assume the name of the book you input is right, please do not input the non-existing book name. For example, if the system only has book1 and book2, you shouldn’t input book3 to borrow this non-existing book, otherwise you should save it in library first.
Requirements:
1. The concept of Subject is implemented as a C++ class.
l Each Subject contains its name, the name is one of Engineering, Science, and History.
l Each Subject contains its own list of Books and the count of Books.
Hint: here you can create an object array to store all the Books that belong to
same subject.
2. The concept of Book is implemented as a C++ class.
l Each Book object has its own Name (cstring), Published Year (int) and
number (int, if=0, means this book is not available).
3. You cannot use #include
4. You should design a sort algorithm to sort the published year of each book. Maybe you can use the learned knowledge like bubble_sort or insert_sort.
5. All data members of each object can only be accessed by the object itself. More description about the above two classes:
Book Class (may contain other data members)
Name Published Year Count
The name of this book object The Published year of the book object The number of this book object
Subject Class (may contain other data members)
Name Count List
Subject Name
The number of Books that with same subject
An array of Books (should be sort by date)
Here is an example:
HINT: S means Save books, L means List books, B means Borrow books, Q means Quit.
Welcome to the Library, what do you want to do?
Input the number of books:
Input the information of books (name, pub_year, number, subject):
Book1 1995 1 History
Book2 2007 3 Science
Book3 2017 3 Engineering
Book4 1997 2 History
Book5 2014 3 Science
Book6 2000 3 Engineering
All books saved. What do you want to do next? L
Books in the library:
Number of History Books: 3
Book1 1995 1
Book4 1997 2
Number of Science Books: 6
Book2 2007 3
Book5 2014 3
Number of Engineering Books: 6
Book6 2000 3
Book3 2017 3
All books listed. What do you want to do next? B
Input the name of the book you want to borrow:
Succeed! What do you want to do next?
Input the name of the book you want to borrow:
Sorry, the book is not available. Try again:
Succeed! What do you want to do next?
Books in the library:
Number of History Books: 2
Book1 1995 0
Book4 1997 2
Number of Science Books: 6
Book2 2007 3
Book5 2014 3
Number of Engineering Books: 5
Book6 2000 3
Book3 2017 2
All books listed. What do you want to do next? Q
Have a nice day, bye!
Q3. Bridge game
In this assignment, you will write a C++ program that can perform a simple Bridge game (a kind of card game) under the following encoding scheme.
There are two parts in this encoding scheme. The first part aims to initialize a sequence of cards used to play the simple Bridge game. The second part is playing the simple Bridge game.
Part A. Initializing a Sequence of Cards
Your first task is to write a program that can initialize a sequence of playing cards, which is used for playing the simple Bridge game. The program takes one integer from the user, and it is the seed for the card sequence generation. As shown in the following Table 1, the value of the seed is from 1 to 20, and each seed corresponds to one specific sequence. Then it prints the entire sequence of cards. We provide a skeleton of code in the file asg3_skeleton_partA.cpp, and you need to complete the program.
Now to begin, a card has three attributes: suit, value, and number. There are 4 suits with their corresponding values: Heart with value 1, Diamond with value 2, Club with value 3, and Spade with value 4. There are 13 numbers: 1(A), 2, 3, 4, 5, 6, 7, 8, 9, 10, 11(J), 12(Q), and 13(K). A card is a combination of suit and number (52 cards in total), Such as Heart 3, Diamond J, Club 1, and .
A class called Card is provided in the skeleton code, with the above attributes defined
as private members. Suit is a pointer to char, i.e., char *, or equivalently cstrings. Some access function prototypes are also defined. There is also a prototype of the default constructor. You need to implement these access functions and the default constructor in order to complete the class definition of Card.
To initialize a sequence of cards, you need to implement one function. A sequence is simply an array of Card objects as you can see in main(). The initSequence(Card* cardSeq, char suitName[][10], int* j, int* num) is the function to
initialize the sequence of cards. For one sequence, there are 13 cards. When you create each card, you should first set its value and its suit according to the value as shown in Table 1. Then, combining the suit with the number, you can get a card. Note that the sequence is passed to the function as a pointer (Card *), and you should not change this.
We provide a help function called printSequence which simply takes the sequence (again as a pointer) and prints each Card object sequentially. This is called in main() to check the output of your code.
1. Based on skeleton code, complete the class definition of Card.
2. You are not allowed to delete any part of the skeleton code.
3. Do not add/move attributes or member functions.
4. Finish all the default constructors or functions.
5. Table 1 is shown below.
Seed No. 1
2 3 4 5 6 7 8 9
Card Sequence
SpadeQ ClubJ Diamond10 HeartJ Heart4 DiamondJ SpadeJ ClubK Club3 Diamond7 Diamond2 Diamond5 Club9 Diamond3 Diamond5 Club7 Heart8 ClubJ Spade4 Spade10 Spade2 ClubQ Spade9 SpadeJ DiamondQ Spade3 SpadeJ Club10 Diamond10 Heart10 DiamondA SpadeK Diamond7 Heart7 Club9 Club6 ClubQ Heart4 ClubJ Spade10 Spade7 Diamond9 Heart4 Club10 ClubQ Club3 HeartK Heart5 ClubK Spade4 SpadeA SpadeJ Spade5 Club4 ClubK SpadeA Spade3 Diamond7 ClubQ SpadeJ Heart10 HeartK Heart7 Heart5 Heart4
Club7 Spade3 SpadeJ Spade4 HeartK Spade2 DiamondQ Heart10 Club3 Spade10 Spade6 Club10 Club9 Diamond5 Heart3 SpadeA Spade9 Heart4 DiamondQ HeartJ Spade2 ClubQ Diamond2 SpadeJ SpadeK Diamond9 Club3 Heart8 Spade2 DiamondJ Spade3 Spade9 Heart5 Diamond2 Spade5 Heart4 ClubQ Diamond10 Spade7 SpadeJ ClubQ DiamondQ Club4 Diamond10 Club7 Spade6
SpadeA Club6 ClubK HeartK Diamond5 Heart4 Club10 Club6 Spade7 SpadeQ Diamond9 Spade6 ClubJ DiamondJ Spade5 Diamond5 DiamondA ClubK Heart4 HeartJ Diamond2 Heart4 Spade5 HeartK Club3 Spade7 Diamond7 Heart3 SpadeA ClubQ Spade10 Club4 Heart3 Heart4 Club3 Spade4 Spade5 SpadeK ClubJ DiamondJ Heart5 Spade2 Diamond5 HeartK DiamondQ
Club10 Spade10 Spade5 Spade6 Heart7 Club9 Heart8 Diamond10 Diamond2 HeartJ DiamondQ Spade4 Spade2 Diamond7 HeartK DiamondQ DiamondA Club6 SpadeA Heart7 Diamond2 Spade2 Heart4 Diamond5 Heart5 Spade9 Club7 Club10 Diamond3 Spade9 SpadeQ Heart5 DiamondA Spade5 Club6 Spade3 Club3 Diamond9 Diamond2 Spade5 Spade9 Spade2 SpadeJ Spade3 Diamond7 SpadeA Heart4 DiamondQ Diamond9 SpadeQ Club7 Diamond10 Spade9 ClubK Club9 ClubQ Spade6 Spade10 Club7 Spade4 Diamond10 HeartK Club6 Diamond8 Heart4
Club3 Club4 ClubQ Club9 HeartJ ClubK ClubJ Spade4 DiamondQ Heart4 SpadeK Diamond5 Diamond9 Diamond2 Heart4 HeartK ClubK Heart5 SpadeA Diamond5 Club3 Spade2 Spade4 Spade5 DiamondQ Diamond7 Diamond7 Club3 Club9 SpadeK Spade9 Heart3 Diamond9 Club10 Diamond5 Diamond3 Spade3 ClubK DiamondA
Expected Outcomes:
Example 1:
Example 2:
Part B: Playing the Simple Bridge Game
Extend your program in Part A, so it can play the simple Bridge game with you as the only player. We provide a skeleton of code in the file asg3_skeleton_partB.cpp, and you need to complete the program.
In the beginning, the program asks the user to input the seed for the card sequence generation. It initializes a sequence of cards which are your cards. Then the game starts.
The default function in the skeleton code will call the function you designed in part A and initialize following specific sequence of cards for computer (HeartQ Club8 Heart9 Diamond6 Spade8 HeartA ClubA DiamondK Club2 Heart6 Heart2 Diamond4 Club5). Then the computer will show you the first card in its sequence.
The rules of the Bridge game are as follows. Players play one card per round. The winner of the previous round goes first (the human player goes first at the first round). The first player of each round can play any card within the card sequence of him/her. The second player needs to play cards according to the following rules: if he/she has a card of the same suit as the first player, he/she must play a card of that suit. If he/she does not have a card of that suit, he/she can play any card.
The rules for winning each round are as follows. If the second player plays the card with the same suit as the first player, the player with the highest number (2 < 3 < ... < 10 < J < Q < K < A) in the round wins. Otherwise, the first player wins. The winner gets a point. The winner of each round goes first in the next round.
Repeat the above process until all players’ cards are played, and then the game is over. The one who has more points wins the game.
The playing strategy of the computer is as follows. If it goes first, it will play the card with the highest number in its card sequence. If there are multiple choices, the first eligible card in the card sequence of it will be chosen. If the computer is the second to play and it has
cards of the same suit as the card played by the human player, it will play the card with the highest number of that suit. If the computer is the second to play and it does not have the card of the same suit as the card played by the human player, it will play the card with the smallest number in its card sequence. If there are multiple choices, the first eligible card in the card sequence of it will be chosen.
The program should remove the card from the card sequence of the player when the player (human or computer) plays a card. After the human player played, the program should print the remaining card sequence. For specific output format, please refer to the following example. Note your program output should be the same as the sample output below, using the same seed.
1. Refer to the Notes of Part A.
2. You may add new member functions to Card and new non-member functions to
the program.
3. If there are some suitable cards for you to play, but you do not play any one of
them, you will lose.
Expected Outcomes:
Example 1:
Example 2:
Q4. Recursion
In this question, you are required to find how many possible combinations that a number can be decomposed into the multiple of integers (smaller than the number itself) by a recursion function.
Suppose we have a positive number 𝑋 as input, and it need to be decomposed into the multiple of several integers, 𝑥!, where each 𝑥!, is smaller than 𝑋(e.g., 𝑋 = 𝑥" ∗ 𝑥# ∗ 𝑥$ ∗ ... ∗ 𝑥% ). In addition, these decomposed integers can only be arranged in an ascending order (𝑥" <= ⋯ <= 𝑥%). Then, output how many kinds of combinations in total.
For example, when 𝑋 is 20, the combinations are: 1*20, 2*10, 2*2*5, 4*5. So your program should output 4.
Expected Outcomes:
Please input the number: 32000
The amount of possible combinations is 401
Please input the number: 100
The amount of possible combinations is 9
Please input the number: 10000
The amount of possible combinations is 109
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com