"C the Programming Language" exercise 1-9
Is there a cleaner more compact way of doing this
Code:
#include <stdio.h>
// Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank
#define INABLANK a
#define CURRENTCHAR c
#define FALSE 0
#define TRUE 1
main(){
int CURRENTCHAR = 0;
int INABLANK = FALSE;
while((CURRENTCHAR = getchar()) != EOF){
if(INABLANK == TRUE && CURRENTCHAR == ' '){
;
}
else
if(INABLANK == FALSE && CURRENTCHAR == ' '){
putchar(CURRENTCHAR);
INABLANK = TRUE;
}
else{
putchar(CURRENTCHAR);
INABLANK = FALSE;
}
}
}
why the heck are you using #defines as variable names
Code:
sed -E 's/ +/ /g'Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
PHP Code:
#include <stdio.h>
void main()
{
char c;
int previousBlank = 0;
while ((c = getchar()) != EOF) {
if (c == ' ') {
if (!previousBlank) {
putchar(' ');
previousBlank = 1;
}
}
else {
putchar(c);
previousBlank = 0;
}
}
}
OneSadCookie Wrote:why the heck are you using #defines as variable namesHe must be Russian. The only other person I know who does this is also Russian. They are a strange breed. Then again this other bloke does stuff like this...
Code:
struct {
int dolt ...
} Dolty;
...
Dolty increadibleGlobal;
...
#define WHATAMI increadibleGlobal.dolt
...
WHATAMI = BLOODYHELL;So when you get down to the WHATAMI=... the debugger has a fit in that it can't show you what it is or it's value. You have to hunt the code for it's meaning and when you do, you completely forget the context on the chunk you are trying to debug. Programmers (actually anyone who does this should remove programmer from their description) who do this are idiots.
"He must be Russian."
I'm from Canada, they say I'm slow eh
"why the heck are you using #defines as variable names "
lol I don't know, I am at very beginning stages of learning C. I find I make mistakes in consistently odd ways.
This program works as it should for everything but backspaces, why?...
I'm from Canada, they say I'm slow eh
"why the heck are you using #defines as variable names "
lol I don't know, I am at very beginning stages of learning C. I find I make mistakes in consistently odd ways.
This program works as it should for everything but backspaces, why?...
Code:
// Exercise 1-10
#include <stdio.h>
main(){
int currentchar = 0;
while((currentchar = getchar()) != EOF){
if(currentchar == '\t'){
putchar('\\');
putchar('t'); }
else
if(currentchar == '\b'){
putchar('\\');
putchar('b');}
else
if(currentchar == '/'){
putchar('/');
putchar('/');}
else
putchar(currentchar);
}
}
Just starting! Oh well then, no worries. 
getchar() probably doesn't work how you want it to work. Or at least on a windows box, which I am on at the moment, I have to hit enter for getchar() to return control back to the loop. Replacing getchar() with something like _getch() which will return a key value once you type something at the keyboard will work as you expect. (Or at least on the windows box. I don't know what the exact unix function would be called as _getch(), _getche() and _kbhit() are system dependent routines found in conio.h. Haven't done a lot of console stuff on the mac to know if this exists there as well.)
A bit of a search at the apple site ther is a getch() in curses.h which will probably do what you are looking for. But curses.h from my cobwebbed head is for console display manipulation. I.E. for old school text based applications. In other words, there might be more involved when using it. I've never used it either.
http://developer.apple.com/documentation...ch.3x.html

getchar() probably doesn't work how you want it to work. Or at least on a windows box, which I am on at the moment, I have to hit enter for getchar() to return control back to the loop. Replacing getchar() with something like _getch() which will return a key value once you type something at the keyboard will work as you expect. (Or at least on the windows box. I don't know what the exact unix function would be called as _getch(), _getche() and _kbhit() are system dependent routines found in conio.h. Haven't done a lot of console stuff on the mac to know if this exists there as well.)
A bit of a search at the apple site ther is a getch() in curses.h which will probably do what you are looking for. But curses.h from my cobwebbed head is for console display manipulation. I.E. for old school text based applications. In other words, there might be more involved when using it. I've never used it either.
http://developer.apple.com/documentation...ch.3x.html
Quote:Programmers (actually anyone who does this should remove programmer from their description) who do this are idiots.I disagree, programmers who do this are probably not writing code for legability. I imagine he might have been coding for hours up really late and did somthing like this as a joke to himself, and probably being an expert programmer doesn't see this as confusing.
Sir, e^iπ + 1 = 0, hence God exists; reply!
unknown Wrote:I disagree, programmers who do this are probably not writing code for legability. I imagine he might have been coding for hours up really late and did somthing like this as a joke to himself, and probably being an expert programmer doesn't see this as confusing.The only reason to use macroes or #defines is to improve legibility. However if it leads to other problems like causing grief for debeugging purposes then it is worthless. Plus, who writes code so that it isn't legible. That is wrong on so many levels. If you want to obfuscate then there are plenty of tools out there that do just that. No need to make it hard on yourself to begin with.
But personally, I stand by my opinion. People who do that are idiots and I curse their name every time I have to maintain their code.
Zekaric Wrote:The only reason to use macroes or #defines is to improve legibility.
Goodness, no. #define exists so that you can assign a value to a constant in a single place and use it throughout your program. If you want to change the value of the constant (or the behavior of the macro), you can change it in a single centralized location, instead of having to find and replace every occurrence throughout your application. Improving legibility is secondary.
- Alex Diener
Quote:Goodness, no. #define exists so that you can assign a value to a constant in a single place and use it throughout your program. If you want to change the value of the constant (or the behavior of the macro), you can change it in a single centralized location, instead of having to find and replace every occurrence throughout your application. Improving legibility is secondary.
Yes that is true as well but, based on point of view, I see that as an offshoot of why you used a macro/#define. Why? Case...
Code:
switch (flag) {
case 0:
...
case 10:
...
case 42:
...
}Code:
/* #defines or an enum, tomato tomatoe in this instance.*/
typedef enum {
flagADD_NEW,
flagPROCESS,
flagDELETE_ME
} Flag;
...
switch (flag) {
case flagADD_NEW:
...
case flagPROCESS:
...
case flagDELETE_ME:
...
}Code:
value = var1 * 2.718281828;Code:
value = var1 * NATURAL_NUMBER_E;
But that's another thread which has reached quite a length.)My view of the world, you program for legibility first. Code that is easy to read is easy to understand. Remove the guesswork where ever you can. So in my world, #defines are there to improve legibility; not as much for the reason you pointed out but that is also a very valid reason...
Erm... This is slowing turning into that other thread about commenting and we're getting clearly off topic here.
Your backspace problem may have something to do with the fact that backspaces are rarely stored in text - usually it just deletes a character instead. What are you reading from?
Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Perhaps the question I'm on is a trick one.
from the book, page 20 : "Exercise 1-10 Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\.
from the book, page 20 : "Exercise 1-10 Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\.
I think most modern terminal emulators keep a line buffer and instead of inserting backspaces they delete a character from the buffer before it's sent to the program... in which case you can probably ignore that part of the exercise
Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Byron Clarke Wrote:This program works as it should for everything but backspaces, why?..
Zekaric Wrote:getchar() probably doesn't work how you want it to work. Or at least on a windows box, which I am on at the moment, I have to hit enter for getchar() to return control back to the loop. Replacing getchar() with something like _getch() which will return a key value once you type something at the keyboard will work as you expect. (Or at least on the windows box. I don't know what the exact unix function would be called as _getch(), _getche() and _kbhit() are system dependent routines found in conio.h. Haven't done a lot of console stuff on the mac to know if this exists there as well.)
The problem is that, from I can gather from the description of every exercise, this little programs are not for interactive use.
Try this:
1. Take the code I posted above (be sure to fix the spaces/weird chars that may appear if you copy it from the webpage).
2. Paste it in a text file, call it "noextraspaces.c", and save it in the Desktop.
3. Create another text file, type some stuff in it, including a few runs of spaces, call it test.txt and save it to the Desktop as well.
4. Open the Terminal and type:
Code:
cd Desktop
gcc noextraspaces.c -o noextraspaces.o
./noextraspaces.o < test.txtThe last line will run your compiled program, giving it the text file as its input. The output will be that file, with extra spaces removed.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| which programming language is best? | petr6534 | 27 | 10,588 |
Sep 30, 2004 10:00 AM Last Post: PowerMacX |
|

