Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin
Homework #3
TA in charge: Lydian Lee RELEASE DATE: 03/31/2011 DUE DATE: 04/12/2011, 17:20
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 nal solutions alone and understand them fully. Books, notes, and Internet resources can be consulted, but not copied from.
Since everyone needs to write the nal 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, either C or C++ or a mixture of them is allowed.
This homework set comes with 200 points and 10 bonus points. In general, every home- work set of ours would come with a full credit of 200 points.
3.1 More on Arrays
(1) (10%) Use either C or any pseudo-code to do Additional Exercise 1 on page 99 of the textbook (Sec. 2.9).
(2) (10%) Use either C or any pseudo-code to do Additional Exercise 2 on page 99 of the textbook (Sec. 2.9).
3.2 Strings
(1) (10%) Do Exercise 6 on page 98 of the textbook (Sec 2.7).
(2) (20%) Do Exercise 9 on page 98 of the textbook (Sec 2.7) with patterns in (a), (b), (c) and (d) abcdacbcabd
(3) (Bonus 10%) Explain how you can modify or reuse the KMP algorithm to deal with a simple wild- card matching using `*', where a pattern of \abc*def" means matching one sub-string starting with
\abc" and ending with \def". You can assume that only one `*' would appear in the pattern.
3.3 Stacks and Queues
(1) (10%) Do Exercise 4 on page 112 of the textbook (Sec 3.1).
(2) (10%) Do Exercise 2 on page 114 of the textbook (Sec 3.2).
(3) (10%) Do Exercise 4 on page 119 of the textbook (Sec 3.3).
1 of 3
Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin
3.4 Expressions
(1) (20%) Do Exercise 1(b)(c)(d)(e) on pages 136 and 137 of the textbook (Sec 3.6).
(2) (20%) Do Exercise 6(a) on pages 138 of the textbook (Sec 3.6) for the four expressions you worked on above.
3.5 Calculators
In this problem, you will be asked to implement two calculators: an \integer calculator" that works on 4-byte integers and supports plus, minus, multiply, divide as well as the boolean operations in C; a
\scientic calculator" that works on 8-byte doubles and supports arithmetic operations as well as some common function calls.
(1) (40%) Implement the integer calculator (hw3 5 1.fc, cppg). You can assume that the input will contain characters only from numbers and needed operators and ignore all other characters (including space). You need to implement the following operations (please check Figure 3.12 of the textbook) with the correct precedence and associativity:
parentheses, as the highest precedence
one's complement
unary minus or plus
multiplicative
binary add or substract
bitwise and
bitwise exclusive or
bitwise or
Your program should satisfy the following requirements.
allow the user to input a line of inx expression that supports all the operations above on integers
show your stack operations on how to transform the inx expression to a postx one
show the corresponding postx expression
show the evaluated result, which should be exactly the same as the result computed by a usual C statement on the same expression
Please print out TWO test cases that you tried along with the output (the \show") of your program on the written part.
(2) (40%) Implement the scientic calculator (hw3 5 2.fc, cppg). Now you may have oating point numbers of the form 2:354. You need to implement the following operations (please check Figure 3.12 of the textbook) with the correct precedence and associativity:
parentheses and function calls, as the highest precedence
unary minus or plus
multiplicative
binary add or substract
You also need to implement the following function calls (as dened in math.h)
sin, cos
exp, log
pow, sqrt
fabs
2 of 3
Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin
Your program should satisfy the following requirements.
allow the user to input a line of inx expression that supports all the operations above on doubles (Note: pow(2; 3) is of postx notation 2 3 pow)
show your stack operations on how to transform the inx expression to a postx one
show the corresponding postx expression
show the evaluated result, which should be exactly the same as the result computed by a usual C statement on the same expression (if the inputs are all doubles)
Please print out TWO test cases that you tried along with the output (the \show") of your program on the written part.
Submission File
Please submit your written part of the homework on all problems together before the deadline in the Tuesday class (04/12/2011) or to the TA at CSIE R217. Also, you need to upload your coding part as a single ZIP compressed le to CEIBA. The zip le should be like b86506054.zip, where the le name should be changed to your own school ID. The ZIP le should contain the following items:
hw3 5 1.c or hw3 5 1.cpp
hw3 5 2.c or hw3 5 2.cpp
The TAs will use the Makefile provided on the course website to test your code. Please make sure that your code can be compiled with the Makefile on CSIE R217 linux machines.
3 of 3