Tips for HW5 - TAMU Computer Science Student Pages

advertisement
HW 5
Overview
• Try/catch framework is required for main
function
• Please indent your files rightly
• I suggest that you try all examples on the book
if you find it difficulty to go on with homework
• Do not wait for the last moment to ask for
help, have a schedule for each problem ahead.
PR 1
First run
:: ./a.out
There were 99 0's.
There were 119 1's.
There were 118 2's.
There were 98 3's.
There were 87 4's.
There were 105 5's.
There were 94 6's.
There were 94 7's.
There were 102 8's.
There were 84 9's.
Second run
:: ./a.out
There were 97 0's.
There were 100 1's.
There were 99 2's.
There were 87 3's.
There were 107 4's.
There were 104 5's.
There were 104 6's.
There were 96 7's.
There were 93 8's.
There were 113 9's.
PR 1
• You will write three files, randint.h,
randint.cpp, hw5pr1.cpp
• Source code for randint.cpp is given
• Why and how to write code in separate files.
– http://cse230.artifice.cc/lecture/splittingcode.html
– P.51 on text book about linking
• How to compile multiple source code.
– g++-4.7 -std=c++11 hw5pr1.cpp randint.cpp
PR 2
file1:
abc
def
ghi
file2:
abc
DEf
ghi
uub
output:
1:OK
2:DIFF
3:DIFF
4:DIFF
PR 2
• Read and Write file, see Chapter 10.4 & 10.5
on text book
• Use getline(…) function
string str;
ifstream file("file.dat");
While(getline(file, str))
{
// do something here
}
PR 3
•
•
•
•
Input
(2000,1,2)
Output
(2000,1,3)
•
•
•
•
Input
(2000,12,31)
Output
(2001,1,1)
PR 3
• Based on Chrono.h and Chrono.cpp of section
9.8.
– Add dummy “return d” in the last 3 functions on
page 332 to make the code compiles OK.
– Add ++ operator overload to class Data type
• putting the declaration in the Date class in Chrono.h
and the definition in Chrono.cpp.
PR 3
• Pseudocode for ++ overload is
Date& operator++(){
//add 1 to d //tomorrow, unless we were at the end of the month
//if is_date is false
//
//need to change to first of next month
// set d to 1
// if m is December
//
//need to change to next year too
// set m to January
// increment y
// else
// increment m
return *this;
}
• More about prefix and suffix ++ overload, see
this page
• http://publib.boulder.ibm.com/infocenter/co
mphelp/v8v101/index.jsp?topic=%2Fcom.ibm.
xlcpp8a.doc%2Flanguage%2Fref%2Fcplr330.ht
m
Download