Strings in C++: A very challenging problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Strings in C++: A very challenging problem

  1. #1
    Join Date
    Mar 2005
    Posts
    5

    Strings in C++: A very challenging problem

    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 | |
    +-------+---------------+---------------+-------+

    Its a bit hard to get it to look right in this but you get the idea. Channels on the left, and times up the top, with the top right cell being empty.

    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 great 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. :)
    Last edited by maloney; 03-31-2005 at 02:24 AM.

  2. #2
    Join Date
    Mar 2004
    Posts
    78
    Hi,

    I do not think that this channel will help you with questions like this one. Here we give advices how to solve your partiqular problems but you require solution.

    Well, if we give you a solution it will not make you think or at least think better or learn something that you do not know.

    I STILL WANT TO HELP YOU.

    So, having what you have to do tell us:

    1. how you intend to make it. You mentioned OO - good! Now share you idea(if any)
    2. What difficulties you meet

    My goal here is to help you build your first solution in our (I mean the channel) way. It is always different and better if you work along someone else.

    If you agree with my offer - I'll make my best when you make your next post.

    Mr.PLS

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    The best way to start is to decide what you need. You need, for example, a print routine that prints the processed data. Therefore you need a data processing method. You need a way to store the text from the file. You therefore need file processing. Write all the needs down, and the class members and methods take shape.

  4. #4
    Join Date
    Mar 2005
    Posts
    5
    Quote Originally Posted by PLS
    Hi,

    1. how you intend to make it. You mentioned OO - good! Now share you idea(if any)
    2. What difficulties you meet

    My goal here is to help you build your first solution in our (I mean the channel) way. It is always different and better if you work along someone else.

    If you agree with my offer - I'll make my best when you make your next post.

    Mr.PLS
    Ok, what I've figured out to do is this:
    First, read the input into a sensible
    datastructure. The obvious datastructure would be a
    collection of channels. Every time there is a line
    starting with =, I 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 all input is collected, can then collect all the
    times from all the programs. Put these into a list,
    and print the list.

    I have one question to ask though, If I read the above file in as strings, c++ will give me all strings delimited by '/0' for example if i read in this line as a string: 19.30 My Restaurant Rules, and print it on one line, say: cout << string1 << endl; as I read it in, it will print: 19.30
    My
    Restaurant
    Rules
    How do I overcome this problem? So that whilst I'm reading the file, it will give me two strings,: 19.30 and My Restaurant Rules so I can add them and use them in my respective classes.
    Thanks for any help

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    How are you reading the file? C++ has a lot of different ways to read... getline seems best (to me) here, then split off the time and data if its a data line, do special actions when you get the '=' on the start or the '**' marker. Why is that 8 at the top of the file, you have to handle that too.

  6. #6
    Join Date
    Mar 2005
    Posts
    5
    The 8 is the cell width. I'm reading in like this:
    ifstream input(file.c_str()); // where file is the file name passed as an argument to the program
    input >> n; //read '8' in as a int n
    while(input >> s1) // read in till end of file, s1 is a string
    {
    // do stuff
    }
    does getline() read the entire line, or only strings like ifstream seems to do?

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    it gets a full line (or N chars), but if you put in a reasonable number of chars, it will always get the full line. Look it up & give it a try...

  8. #8
    Join Date
    Mar 2004
    Posts
    78
    Hi again,

    I am glad to see that you decided to pariticipate in the channel.

    Well if you decide to use this data structure - it is ok. I really describes something real.

    I'll try to hit your problem with the strings. If you have the data in you classes and you want to print it in the appropriate way I think that you will have to specifiy what exactly one may mean by appropriate way. You can do this by creating a function for example in the channel or program class and on the basis of your data you will have to construct a new string that will show the data in the way you need. Guess what will happen if you need to show the info in differnt ways? Just create new function that will give you a new string with the data stored in the new way.

    there are fuctions like spritf and many other that will help you here.

    About reading the strings: you can read the hole line and then to extract the data in it by using the separator - in your case ' '. Just start from the begining with strLine[0] and read until you meet the separator. copy every character that you have read. so you will have the data for hour, program and so on. Pls note that what ever you read it will not be longer than the whole line. Furthermore note that you can check the input for errors here. Just be sure that you will not go beyond the limits ot of the string.
    I am sure that there are sutable routines to do more safely this but I'll tell you when I am ready with them.
    when using the "new" operator you can specify dynamicly the size of the string. char* pstrLine = new char[k];

    That is it for now.

    A hint - try to find a better data structure in the view of the otput that you will have to produce. this one works, but seems not to be the best in the veiw of the otput you need - the only thing that you need at all(this is why it is important).

    I hope that my opinion is usefull :))

    Mr.PLS

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    don't use a char * -- just make a fixed array and avoid that whole issue if you don't know how to use the string datatype.

    char str[1000];
    str will work in all the char * read and manip routines.

  10. #10
    Join Date
    Mar 2005
    Posts
    5
    Ok guys
    I'm running into problems with my datastructure. The first problem is that before I start reading input I don't know how many channels or programs I'm gonna need. So inside my while loop I have Channel c1; and then i just keep writing to the same object!
    Like PLS mentioned before I need to think of a better datastructure in terms of my output, but I'm having problems thinking of a good solution. Any help would be appreciated.
    Thanks

  11. #11
    Join Date
    Dec 2003
    Posts
    3,366
    Off the top of my head, I would do channel objects that have a vector of strings for the time and text fields? Then maybe a vector of the channels to finish up the requirements. Something along those lines. Design of a solution should be harder than the coding. If the coding is harder, your design is probably lacking(or the coding is uber-specialized)

  12. #12
    Join Date
    Mar 2005
    Posts
    5
    yes, but while i'm reading in the file how do I createall the different channel objects if I don't know how many channels there are?

  13. #13
    Join Date
    Mar 2004
    Posts
    78
    So,

    In the first post you said that your program is intended to parse the input given below to a table, also given below.
    Before we try to code something we have to analise the input and output and find out what exactly we have to do, I mean the software. Obviously we have to read the input and to store the data in to some kind of a datastructure. Then we have to output the data.

    When we read the input we recongnize in a sequence channel, time and the name of the program that will run on the given channel. And all of this is given channel by channel. Obviouslu the input data is structured in a channel that includes the program information. This will give us a plain data structure - a Channel object that will agregate the program objects associated with the channel. Someone noted that we can use vector because the number of programs is not known or at least limited. I'll agree but will note that the vector structure is not a part of the C++. STL is a library but as our new friend is not so in deep with the STL concepts I think that it will be usefiull for him to solve the problem himself.
    Therefore I suggest instead of vectors to use a linked list. Every elemnt of the list will include the time, when the program starts and the its name. As we are talking about linked lists you will have to include a pointer to the next element in the list. Be sure that the last element wil have a next pointer with NULL value - this will show you when to stop.
    Use another linked list to store the channel list of objects.

    If you are not familiar with linked list - read a short refernece on the topic.
    and If it is too hard for you instead of linked list use arrays of a constant length - 100 elements for example BUT you program will have a constraint of the maximum channels and programs in evevry channel.

    When reading the input - create an object(use the "new" key word) that will reprezent the channel. Then set its name(you read it from the input). Then read the time/program pair from the input and call the addProgram method of your channel object giving the pair as input to the method. The Add method will create the new program object and will store the input and will add the program object in the channel object - this means that the Add method will add the newly crated Program object in the linked list or array - whatever you decide to use.
    When you reach a delimiter for a new channel - save the channel you were working with in a linked list or an array, then crate a new (empty ) channel object and do the same as with the previos one.
    Finally you will have a datstructure holding all data in separate channel objects that hold the info for their programs.
    I'll even advise you to store all the channel objects in an object - TVPareserer. To load the data - give as input the file name. And in its load method do all the initialization work.
    When you need some kind of data or output call the appropriate methods - get or print - whatever you decide to be meanful.

    Ok, try to code this and to see that it is working. Test by debugging or simply print the object's data after initialization - you should see the input data in a new output image. This is how your data is stored :)) Later we will find a way to twist the output in the required way.
    I even suppose that we will do some big changes but let the moment become.

    If you have problem with coding - WRITE to us.

    WHEN WE FINISH WITH THE INPUT WE WILL ANALIZE THE OUTPUT!
    What are we going to find ???? It's gettin interesting....

    Input:

    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

    Output:
    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 | |
    +-------+---------------+---------------+-------+

  14. #14
    Join Date
    Mar 2004
    Posts
    78
    Input:

    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

    Output:
    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 | ____________ |
    +-------+---------------+---------------+-------+
    here '_' is given insteand of a space - so that we all see the real table

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center