A straight-line program to do date format conversion
In this brief lab you will write a straight-line program to perform
date format conversion. Straight-line programs have no flow-control:
no conditional statements, loops or recursive function calls. (In
fact, you won't see flow control until Chapter 4.) Although people seldom
write straight-line programs, they can make for instructive and
challenging puzzles.
Date conversion (1 point)
-
Write and compile a straight-line program to perform date format
conversions. I'll explain by example. The user will type input in
the form
10/05/98 and the computer should output, say,
Oct 05, 1998. (See Exercise P2.22 on page 86 for a hint.
Another hint: It's easiest if your program first reads in the month,
10, as an integer, then reads in the rest of the line
/05/98, as a string. You should only use stuff you have
read about in Chapters 1 and 2 in the book.)
Be sure that all variable names are carefully chosen, and that the
program is indented properly. You can mark the whole file as the
region using C-x h and then indent the file using
C-M-\ or M-x indent-region.
Be sure your program compiles and runs. Perform several tests of the
program before getting checked off.
-
Check-off: Leave the window with your tests open on your
screen, as well as the Emacs file containing your program.
Improving the program (OPTIONAL)
You are encouraged to work with a neighbor on this part. If you do
work with someone else, you'll be checked off together; you needn't
each have your own copy of a working program.
To do this assignment, you'll need a way to convert digits to
integers: If str is a string, the expression
(str[4] - '0') returns an integer whose value
is the digit in position 4 of the string. (Recall that the first
position is 0, so this converts the 5th character in
str to an integer.)
For example, the following program
prints the number 400 followed by a newline.
#include <iostream>
#include "ccc_ansi.h"
/*#include <string> */ /* Not Needed */
using namespace std;
main() {
string str = "Pi=3.14159";
cout << 100*(str[6] - '0') << endl;
}
I should make a couple additional notes on how to compile this
program:
- I am recommending you use Horstmann's string class
rather than the one built into Gnu's C++ compiler. This is why the
top of the file reads has the two lines #include "ccc_ansi.h"
and using namespace std;. Further it does not include
Gnu's <string>.
- You need to compile this with one additional flag to
g++, that being -I/usr/local/include/cccfiles, to
tell the compiler where to find Horstmann's file
"ccc_ansi.h". You'll want to jot this down for future
reference.
Improve your program by making the following improvements. Be sure to
save your last properly working copy in a backup file before you take
on each new challenge. This way, if you get stuck, at least you can
go back to something that works.
- Improve the format so that a leading zero in front of the date
is removed. I.e.,
-
10/05/98 gets displayed as Oct 5, 1998
-
10/25/98 gets displayed as Oct 25, 1998
- Make it so any year below 50 is interpreted as occuring after
the year 2000. So,
-
12/18/23 gets displayed as Dec 18, 2023
-
12/18/50 gets displayed as Dec 18, 1950
Hint: Use integer arithmetic operations, i.e., % / * + and/or -.
- Check-off: Leave the window with your tests open on your
screen, as well as the Emacs file containing your improved program.
- (optional challenge) Make it so that the months are written unabbreviated:
December 18, 2023. The challenge here is that there
should not be additional leading or trailing spaces, so months
use up a variable number of characters depending on how long it is
written out in English.