Strings in C++: A tricky problem

M

M Maloney

Hey all,
I was wondering if anyone could help me with this problem I
have:
Given a text file like this:

8
=ABC
18.00 Dr Who: Underworld
18.30 Collectors
19.00 News
19.30 The 7.30 Report
20.00 Catalyst
=Seven
18.00 Seven News
18.30 Today Tonight
19.00 Home and Away
19.30 My Restaurant Rules
20.30 **END
=31
19.00 Other
20.00 **END

I have to produce a program guide like this:
+-------+-------+-------+-------+-------+-------+
| | 18.00 | 18.30 | 19.00 | 19.30 | 20.00 |
+-------+-------+-------+-------+-------+-------+
| ABC | Dr Wh | Colle | News | The 7 | Catal |
+-------+-------+-------+-------+-------+-------+
| Seven | Seven | Today | Home | My Restaurant |
+-------+-------+-------+-------+-------+-------+
| 31 | | Other | |
+-------+---------------+---------------+-------+

Some features to note: channel names are right-justified with a
cell, everything else is leftjustified;
cell content is truncated to fit cell width; ‘+' is only used in row
separators where there is a ‘|' above or below; channel content may
not fill an entire row.

The description file contains:
• a line containing a single integer specifying the cell width;
thereafter
• lines beginning with ‘=' start a new channel and have the channel
name following the ‘='
• other lines have a time of the form integer.integer followed by a
programme name; time resolution will not be finer than half-hour
increments
• lines are separated with the conventional ASCII LF character

A programme runs from its specified starting time until the start
of the next programme or for half-an-hour if there is no next
programme. The special entry ‘**END' may be used to terminate a
programme entry before the start of the next programme or to given the
last programme of the day a running time of more than half-an-hour,
e.g. a programme may run from 5.30 to 6.00 but the next programme may
not start until 7.00. Programme entries may span more than one column.

Cell width includes the closing but not the opening character so the
above table has a cell width of 8. A table of cell width w with n
columns will have a total line length of n*w+1. The first column
contains the channel names. The first row contains the times. The
ASCII characters ‘-', ‘|' and ‘+' should be used to build the table;
‘+' should be used in the row separator when there
is a ‘|' above or below. At least one space should appear on either
side of ‘|' characters. Depending on the justification sometimes at
most one space should appear.

Each column is half an hour, but a programme may run for longer,
thus consuming two or more columns

I am having greatt difficulty making this work in good C++ O-O
style. If anyone could help me out, that would be greatly appreciated.
Even giving me some skeleton code, or describing how I should begin
would be fine.
Thanks in advance.
 
P

Phlip

M said:
8
=ABC
18.00 Dr Who: Underworld
18.30 Collectors
19.00 News
19.30 The 7.30 Report
20.00 Catalyst
=Seven
18.00 Seven News
18.30 Today Tonight
19.00 Home and Away
19.30 My Restaurant Rules
20.30 **END
=31
19.00 Other
20.00 **END

I have to produce a program guide like this:
+-------+-------+-------+-------+-------+-------+
| | 18.00 | 18.30 | 19.00 | 19.30 | 20.00 |
+-------+-------+-------+-------+-------+-------+
| ABC | Dr Wh | Colle | News | The 7 | Catal |
+-------+-------+-------+-------+-------+-------+
| Seven | Seven | Today | Home | My Restaurant |
+-------+-------+-------+-------+-------+-------+
| 31 | | Other | |
+-------+---------------+---------------+-------+
I am having greatt difficulty making this work in good C++ O-O
style.

Switch to a language like Ruby, with built-in regular expression support,
and output an HTML table. You'l finish before the next sitcom.
 
E

E. Mark Ping

Thanks for your suggestion, but it has to be done using C++

Then consider Boost, which includes regex, which is slated to be in
the next C++ standard (and which is available free now).
 
M

msalters

M said:
Hey all,
I was wondering if anyone could help me with this problem I
have:
Given a text file like this:

8
=ABC
18.00 Dr Who: Underworld
18.30 Collectors
19.00 News
19.30 The 7.30 Report
20.00 Catalyst
=Seven
18.00 Seven News
18.30 Today Tonight
19.00 Home and Away
19.30 My Restaurant Rules
20.30 **END
=31
19.00 Other
20.00 **END

I have to produce a program guide like this:
+-------+-------+-------+-------+-------+-------+
| | 18.00 | 18.30 | 19.00 | 19.30 | 20.00 |
+-------+-------+-------+-------+-------+-------+
| ABC | Dr Wh | Colle | News | The 7 | Catal |
+-------+-------+-------+-------+-------+-------+
| Seven | Seven | Today | Home | My Restaurant |
+-------+-------+-------+-------+-------+-------+
| 31 | | Other | |
+-------+---------------+---------------+-------+

Ok, what you have to do is break down the problem.
First, you need to read the input into a sensible
datastructure. The obvious datastructure would be a
collection of channels. Every time you see a line
starting with =, you add a new channel. Every other
line is converted into a program and added to the last
channel. So, the channel class must contain a name
member and a collection of programs. The program
class contains a name and a time.

After you have done input, you can collect all the
times from all the programs. Put these into a list,
and print the list.
From there it should be easy.

HTH,
Michiel Salters
 
P

Pete Becker

E. Mark Ping said:
Then consider Boost, which includes regex, which is slated to be in
the next C++ standard (and which is available free now).

It's not slated to be in the next C++ standard. It's part of TR1, which
is a technical report. Technical reports don't affect the standard, but
can be used to indicate of possible future directions; that's the
purpose of TR1.
 
P

Phlip

Pete said:
E. Mark Ping wrote:

It's not slated to be in the next C++ standard. It's part of TR1, which
is a technical report. Technical reports don't affect the standard, but
can be used to indicate of possible future directions; that's the
purpose of TR1.

But all the other languages are doing it.
 
E

E. Mark Ping

It's not slated to be in the next C++ standard. It's part of TR1, which
is a technical report. Technical reports don't affect the standard, but
can be used to indicate of possible future directions; that's the
purpose of TR1.

Ah, on looking back on Herb Sutter's Jan'05 article, I see how I
misinterpreted it. Thanks for the correction.
 
M

martinm69x

Thanks very much for your help Michiel, I see now how I should have
been thinking. The rest should be easy for me
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top