View Full Version : Coding assignment


Adam
04-25-02, 11:45 PM
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.

Adam
04-26-02, 12:46 AM
Okay, I had a stroke of pure genius (coz I'm SO damn good :) ) 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);
}

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

Ash711
04-26-02, 03:00 AM
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

Adam
04-26-02, 04:14 AM
while(curr_char - &glob_text 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;


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.

Ash711
04-26-02, 04:30 AM
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)
}
}

Adam
04-30-02, 03:16 AM
Originally posted by Adam

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


Damn that's some sexy code. God I'm good. :)

Adam
04-30-02, 03:40 AM
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? :)

Adam
05-09-02, 04:50 AM
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?

sjmarsha
05-09-02, 12:23 PM
Sorry. I haven't enough time. Good Luck!!

Adam
05-09-02, 11:53 PM
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[i] != 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[i] == isnum())

}
}

/* Examine command line arguments for format options */
char flags_format(char passedstuff)
{
for(int i = 0; i != ' '; i++)
{
if(passedstuff[i] == '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);
}

WAJ
05-10-02, 09:29 AM
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 :p

Good luck, btw!

sjmarsha
05-10-02, 03:01 PM
I'll have a gander tommorrow...

Looks like fun!

Adam
05-10-02, 03:17 PM
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.

WAJ
05-10-02, 04:33 PM
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...:p

sjmarsha
05-13-02, 02:05 AM
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.

Adam
05-13-02, 02:44 AM
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.