• 沒有找到結果。

GettingFamiliarwithC++Language Thishomeworksetcomeswith200points.Ingeneral,everyhomeworksetofourswouldcomewithafullcreditof200points. Homework#1

N/A
N/A
Protected

Academic year: 2022

Share "GettingFamiliarwithC++Language Thishomeworksetcomeswith200points.Ingeneral,everyhomeworksetofourswouldcomewithafullcreditof200points. Homework#1"

Copied!
3
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

Homework #1

TA in charge: Ya-Hsuan Chang and Wei-Yuan Shen RELEASE DATE: 02/22/2012

DUE DATE: 03/05/2012 (Monday), 17:00

As directed below, you need to submit your code to the designated place on the course website.

Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts.

Discussions on course materials and homework solutions are encouraged. But you should write the final solutions alone and understand them fully. Books, notes, and Internet resources can be consulted, but not copied from.

Since everyone needs to write the final solutions alone, there is absolutely no need to lend your homework solutions and/or source codes to your classmates at any time. In order to maximize the level of fairness in this class, lending and borrowing homework solutions are both regarded as dishonest behaviors and will be punished according to the honesty policy.

Both English and Traditional Chinese are allowed for writing any part of your homework (if the compiler recognizes Traditional Chinese, of course). We do not accept any other languages. As for coding, we only allow C++ this time. In particular, the use of printf, scanf and similar functions is not allowed.

This homework set comes with 200 points. In general, every homework set of ours would come with a full credit of 200 points.

Getting Familiar with C++ Language

The purpose of this homework is to give you a quick start with the C++ language. In the language, the variables can be bundled in a class, which is like a C structure but (loosely speaking) allows you to define functions inside for maniplicating the variables directly. We have shown how to define functions inside classes, and how to expose the functions to external use by the public keyword. In your reading assignments this week, you should learn how to construct, or initialize, an instance of the class by a special function called the constructor. Yes, we know that you are not totally familiar with them and the purpose is to force you to reach out for resources and learn. So here comes the valuable hint from your instructor: It would be a really good time to start reading and going to your TAs!

Another type of special function within a class is called the operator, and we have demonstrated its use as well. The operator function specifies what operator-symbol means when applied to instances of a class. This gives the operator some additional meaning, or overloads it. You can redefine the function of most built-in operators globally or on a class-by-class basis. Overloaded operators are implemented as functions.

In this homework set, you are asked to implement a constructor, a function within the class, as well as operator overloading. The class you will be working on defines one-variable polynomials, and you will deal with arithmetic operators, including addition, subtraction, multiplication, division and modulation.

For example, you need to overload the built-in arithmetic addition operator(+) to polynomial addition.

You are also asked to separate definition from implementation. In particular, you should put your definition of the class in a header file like

//poly.h class poly{

void blahblah();

};

and put the implementation of the blahblah function in the cpp file like

1 of 3

(2)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

//poly.cpp

void poly::blahblah(){

//...

}

Requirement

Please implement a C++ class poly for polynomials, with the following items:

(1) (10%) The class header file (2) (20%) The class constructor

(3) (20%) The method show for printing out the polynomial (4) (30%) The operator for polynomial addition (+)

(x3+ x2+ 1) + (x4+ 3x2− 2) = (x4+ x3+ 4x2− 1) (5) (30%) The operator for polynomial subtraction (−)

(x3+ x2+ 1) − (x4+ 3x2− 2) = (−x4+ x3− 2x2+ 3) (6) (30%) The operator for polynomial multiplication (∗)

(x2+ 1) ∗ (x4+ 3x2− 2) = (x6+ 4x4+ x2− 2) (7) (30%) The operator for polynomial division (/)

(x4+ 3x2− 2)/(x2+ 1) = (x2+ 2)

(8) (30%) The operator for polynomial modulation (%) (x4+ 3x2− 2)%(x2+ 1) = (−4)

In this homework, you need to hand in a header file, poly.h, and a C++ program file, poly.cpp.

You need to declare the class in poly.h, and implement them in poly.cpp. The files, when coupled with main.cpp provided by the TAs, can read from the sample input and produce the sample output. The TAs will use different input/output files to test your program, of course.

Input Format

The first line is the number of testing inputs. Each testing input contains three lines, the first two specify the polynomial and the third line corresponds to the operator. In the first two lines, the first number is the degree of that polynomial and the rest are the coefficients. The coefficients are in ascending power, floating point and dense format; Also, the leading coefficient is not zero. For example, 3 1 0 2 4 corresponds to 1 + 2x2+ 4x3. Finally,the third line contain one character(+,-,*,/,%) for the operation.

Note that the degree of input polynomial will not exceed 100, 000.

Output Format

The show function of the class will be used to print the resulting polynomial from {(first polynomial) (operator) (second polynomial)}. The polynomial coefficients should be printed in ascending power using at most two digits after decimal point, in one line and separated by one space. Note that the leading coefficient can not be zero, unless all coefficients are zeros. The TAs will teach you how to print using at most two digits after decimal point with cout online.

Sample Input

3

4 0 2 4 8 16 1 1 2

+

2 of 3

(3)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

2 0 1 2 3 3 2 1 2

*

4 1 4 6 4 1 2 1 2 1 /

Sample Output

1.00 4.00 4.00 8.00 16.00 0.00 3.00 8.00 5.00 4.00 4.00 1.00 2.00 1.00

Related Files

(1) main.cpp: The TAs will test your program in this way, so you need to declare the construc- tor/function/operators corresponding to the usage in main.cpp.

(2) Makefile: The TAs will compile and test your program by the command “make” and “make run”.

Please make sure that your code can be compiled and run with the Makefile on CSIE R217 linux machines. Otherwise your program “fails” its most basic test!

Submission File

Please upload your program as a single ZIP compressed file to CEIBA before 5pm, 03/05/2012 (Monday).

The zip file should be like b86506054.zip, where the file name should be changed to your own school ID. The ZIP file should contain the following items:

• poly.cpp and poly.h

• an optional README, anything you want the TAs to read before grading your code

3 of 3

參考文獻

相關文件

Software protection is very closely linked to assembly language. If you want to protect your software, you *MUST* know assembly

2. In the document, you can explain how to achieve this program and use simple flow chart to show your program architecture. Also, you can show what kind of problems you met on

ˆ Also, you may need to initialize an dense vector c to adapt the technique derived in prob.?. From what you have observed in (a)-(d), can you say that rank(B) must be greater or

The ProxyFactory class provides the addAdvice() method that you saw in Listing 5-3 for cases where you want advice to apply to the invocation of all methods in a class, not just

Data Structures and Algorithms (NTU, Spring 2013) instructor: Hsuan-Tien Lin..

Given a VM host as you created in class, do something so that you can connect your VM to the network with Linux bridge without GUI tool.. Please write down what additional things you

In this homework, you are asked to implement k-d tree for the k = 1 case, and the data structure should support the operations of querying the nearest point, point insertion, and

Once you get down to a purely business level, your influence is gone and the true light of your life isdimmed. You must work in the missionary spirit, with a breadth of charity