Coding assignment

Discussion in 'Computer Science & Culture' started by Adam, Apr 26, 2002.

Thread Status:
Not open for further replies.
  1. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Our latest project in C programming.

    ----------------------------------------------------------

    Programme must receive as parameters directions to
    any number of text files, which it will reformat
    to fit a standard width. A single space should
    separate words and a double separate sentences.
    Paragraphs should be separated by an empty line.
    Command line instruction to activate programme
    should be "fmt -l50" or whatever the number of
    characters across should be. Incorrect
    instructions should recieve an error message.

    So, it reads from standard input. Output to screen.
    If no line length is specified, the default should
    be 66 characters.

    The programme should also read any subsequent command
    line arguments as files to open and work with.

    For a little extra credit, make it justify the text
    so both edges are neat and straight. This will use
    the command line argument -j. Also maybe add the
    option to use the argument -c to center each line
    of text in the output. Bonus marks if you can get
    both flags working in one nunch, such as "-l25c".

    ----------------------------------------------------------

    I'm not looking for other people to code this for me, I'd like to do it myself. BUT I would appreciate some hints and suggestions on how to begin.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Okay, I had a stroke of pure genius (coz I'm SO damn good

    Please Register or Log in to view the hidden image!

    ) and whipped up the function for centering lines of text. I pass in the length of the current line being worked on, and the string of that line itself. I'm not sure if I'm doing that right though. I don't like the idea of chopping the inputted file into separate strings to pass into the function. Does it look okay?

    ------------------------------------

    void centretext(linelength, currentline)
    {
    int startspace = (linelength - sizeof(currentline)) / 2;
    for(int i = 0; i < startspace; i++)
    printf("%c", ' ');
    printf("%s", currentline);
    }

    ------------------------------------
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Ash711 Registered Member

    Messages:
    24
    dunno

    dunno if its a good idea or not, but personally i'd have a <U>global</U> variable containing the text (as a huge string), another one containing current line, another one containing the current char in the text (i'd rather use a pointer for that)
    Why ? Because that way you d'ont have to make a copy of each lines for passing it to each formatting fx, as they will be able to directly access it.

    First i will cut the line so none will be more than switch_l char, this can be understood by checking that no more than switch_l chars separate two newline char. This can be done in the same pass that will separate each word by an unique space, and each sentence by two space, in a while loop, breaking if you find a newline char before switch_l char, or by having processed switch_l chars (taking into account that you cleaned some space)
    it can look like this:

    while(curr_char - &glob_text<SIZEOFTEXT){
    curr_char++;
    if(found_space && (*curr_char)==SPACE)
    continue; //we rip additional spaces
    found_space=0;
    if(found_sentence_sep && (*curr_char)==SEPARATOR)
    continue;
    found_sentence_sep=0;
    //if we are here it's that we have a clean character to output
    if(*curr_char==SPACE)
    found_space=1;


    }

    Once we 'cleaned' the current string, we then justify it or center it, in two separate functions
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    What's going on there with while(curr_char - &glob_text curr_char++;? The while has no end, and I don't get the &glob_text curr_char++;

    PS: Thanks for this.
     
  8. Ash711 Registered Member

    Messages:
    24
    ooooopppppssss sorry hitted submit too soon

    i should have wrote:

    while(curr_char - &glob_text){
    curr_char++;
    ......
    //if we are here it's that we have a clean character to output
    if(*curr_char==SPACE)
    found_space=1;
    if(*curr_char==SEPARATOR)
    found_sentence_sep=1
    if(*curr_char=NEWLINE){
    *temp_line=NEWLINE;
    last_newLine=curr_char;
    process_formating_and_output(temp_line)
    continue;
    }
    if(curr_char - last_newLine < line_lenght_set_by_command_line){
    ///copy char in a temp string that will be processed for formatting
    *temp_line=*curr_char;
    temp_line++;
    }else{
    //max number of char per line found, create a new line and process
    *temp_line=NEWLINE;
    last_newLine=curr_char+1;
    process_formating_and_output(temp_line)
    }
    }
     
  9. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Damn that's some sexy code. God I'm good.

    Please Register or Log in to view the hidden image!

     
  10. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    See in the justify function there, how would I go about placing extra spaces in here and there to fill out lines and justify it? Any ideas?

    -----------------

    void justify(int, char);
    void centreline(int, char);

    /* Justify passed lines of text */
    void justify(int linelength, char currentline)
    {
    int linespaces = 0, extraspaces = 0;
    for(int linespaces = 0; getc(linespaces) < linelength; linespaces++)
    linespaces++;

    }

    /* Centre passed lines of text */
    void centreline(int linelength, char currentline)
    {
    int startspace = (linelength - sizeof(currentline)) / 2;
    for(int i = 0; i < startspace; i++)
    printf("%c", ' ');
    printf("%s", currentline);
    }

    -----------------

    PS: Sititng in my dorm room at uni, drinking cheap port and cola, programming in Notepad. Ain't life grand?

    Please Register or Log in to view the hidden image!

     
  11. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Hey, this has to be in tomorrow and I have NO idea what I'm doing. Anyone got any C code to paste in and help me out?
     
  12. sjmarsha Registered Senior Member

    Messages:
    363
    Sorry. I haven't enough time. Good Luck!!
     
  13. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Well this is what I have so far. We've been given another week on this one.


    (--- to replace triangle brackets)

    #include ---stdio.h---

    /* Function prototypes */
    int flags_length(char);
    char flags_format(char);
    void centreline(int, char);
    void justify(int, char);

    /* Main */
    int main(int argc, char *argv[])
    {

    int linelength = flags_length(argv[1]);
    char options = flags_format(argv[1]);

    while(argc++)
    {
    while(argv != EOF)
    {
    switch(options)
    {
    case 'c':
    centreline(currentline);
    break;
    case 'j':
    default:
    justify(currentline);
    break;
    }
    }

    }

    }

    /* Examine command line arguments for line length options */
    int flags_length(char passedstuff)
    {
    for(int i = 0; i != ' '; i++)
    {
    if(passedstuff == isnum())

    }
    }

    /* Examine command line arguments for format options */
    char flags_format(char passedstuff)
    {
    for(int i = 0; i != ' '; i++)
    {
    if(passedstuff == 'c')
    return 'c';
    else
    return 'j';
    }
    }

    /* Justify passed lines of text */
    void justify(int linelength, char currentline)
    {
    int spacestoadd = (linelength - sizeof(currentline)), currentspaces = 0;
    for(int i = 0; i < linelength; i++)
    {
    if(scanf(currentline) == ' ')
    currentspaces++;
    }
    }

    /* Centre passed lines of text */
    void centreline(int linelength, char currentline)
    {
    int startspace = (linelength - sizeof(currentline)) / 2;
    for(int i = 0; i < startspace; i++)
    printf("%c", ' ');
    printf("%s", currentline);
    }
     
  14. WAJ Registered Member

    Messages:
    22
    I don't know anything about your program or c++ (I'm just trying to learn it now before I go to uni) but I just want to say that I love challenges

    Please Register or Log in to view the hidden image!



    Good luck, btw!
     
  15. sjmarsha Registered Senior Member

    Messages:
    363
    I'll have a gander tommorrow...

    Looks like fun!
     
  16. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    You know to really write a good programme you have to sort of get into the groove? That's what I'm doing this weekend, burying myself in programming until things all fit together.
     
  17. WAJ Registered Member

    Messages:
    22
    Well, you gotta be careful. You shouldn't get too buried -- that can make the task boring, stressful and tedious, and you shouldn't make it too easy on yourself either.

    One must reach the perfect programming state-of-mind equilibrium. The seventh heaven...

    Please Register or Log in to view the hidden image!

     
  18. sjmarsha Registered Senior Member

    Messages:
    363
    Sorry Adam. The grandparents made an unexpected visit this weekend....

    I haven't had time to do anything...

    I had fun for half an hour on it though. Your code you pasted is littered with errors...

    1) You are getting pointers and variables modeled up...
    2) Your protypes are wrong.
    3) I couldn't get isnum() to work. What header file do you need? I tried ctype.h.

    Otherwise a good start.
     
  19. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Sorry, isnum() was something we worked on in class. I have been trying to write this thing in Notepad since no compilers would install properly on my computer, but I finally got LCC installed last week.
     
Thread Status:
Not open for further replies.

Share This Page