SI204 Class 31: Simple Structs II

Lecture

The main goal of this lecture is to get a little more
comfortable with using structs to solve problems.

What’s the Point?

Last class's lecture notes showed you how you can
do some line drawing using Excel by creating text files with x and y values,
importing them into Excel, then using the "Scatterplot" feature to
get Excel to connect the dots. Let's write a program that sort of plots a
course this way. You start out at (0,0), and then you can enter in
"moves". A move of (-2,4) means move down 2 units and to the right 4
units from wherever your current position is. The user enters moves continually
until he finally enters a q to
quit. My first solution is a pretty simple
program. The meat of the program is:

// Initialization

ofstreamOUT("out.txt");

Pointp,m;

p.x=p.y=m.x=m.y=0;

// Get moves & write moves

do{

// Compute new position p from move m

p.x=p.x+m.x;

p.y=p.y+m.y;

// Write move

OUT<<p.x<<'\t'<<p.y<<endl;

}while(getMove(m,cin));

While it may be simple, it would be nice to be able
to write the function as if Point
were a built-in type, meaning that I could add Points p and m
by saying p + mrather
than having to copy each data member of p and m together explicitly as the
above program does inside the do..while loop.. This wouldn't
change what I could accomplish, but it would add a little "syntactic
sugar" to sweeten the program a bit. Then the meat of the program would
look like:

// Initialization

ofstreamOUT("out.txt");

Pointp,m;

p.x=p.y=m.x=m.y=0;

// Get moves & write moves

do{

p=p+m;// compare this line with the above loop

OUT<<p.x<<'\t'<<p.y<<endl;

}while(getMove(m,cin));

To do this, we need to be able to tell the compiler
what it means to + to
Point objects. Doing this is
quite easy once you understand that a +
b is just the same as the function call
operator+(a,b) in C++. So if you
want to tell the compiler what +
means for two Point objects,
you need to define the function operator+(Point
a, Point b) --- i.e. overload the
+ operator for
Points. The prototype is clear:

Point operator+(Point a, Point b);

... at least I hope it's clear that we should
return a Point when we add two Points. The function definition is ... just like
any other function definition:

Point operator+(Point a, Point b)

{

Point sum;

sum.x = a.x + b.x;

sum.y = a.y + b.y;

return sum;

}

So with that addition, here's
my second version of the program. Now, the
syntactic sugar may not seem worth the effort here, but you'll probably be
using the Point struct over
and over, and you'll like being able to add Points.
Wouldn't it be nice to define the midPoint
function like this:

Point midPoint(Point a, Point b)

{

return (a + b)/2.0;

}

Now, in addition to defining
operator+ for two
Point objects, what else would you need?
Well, (a + b) is an object
of type Point, and I'm
dividing it by an object of type double,
so I need to define operator/(Point,double).
What type of object should be returned here?

Point operator/(Point P, double z)

{

Point Q;

Q.x = P.x / z;

Q.y = P.y / z;

return Q;

}

Problems

1.Be the bank! Write a program that manages account
information for a simple bank. You have the file BankAccts.txt,
which lists all your bank account information at the beginning of the year. You
also have the file Transactions.txt,
which lists all the deposit transactions for the year, each transaction
consisting of a line giving the date, the account number, and the $'s
deposited. Your program should print out an end of the year report that lists
all the accounts with their account numbers, owner name, and end of year balance.
Here's my solution.

2.In HTML, colors are specified by a "number".
for example, yellow is specified by the "number" #FFFF00. And you can color words using
this. For example, if I want to Write "Hello",
in HTML (i.e. Hello with a yellow H on the front), I use the following HTML:

<font color="#FFFF00">H</font>ello

The file colors.txt
contains a list of number/names. Write a program that reads in this file,
allows the user to enter colors and words, and finally produces an HTML file
that writes those words in those colors. Example:

3.Write a program that reads in Points from the file
points.txt and writes out the lower left and
upper right coordinates of the smallest rectangle containing all the Points
from the file. Here's My Solution.