Thread: Programming In C: Part III

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Programming In C: Part III

    Last question on this project. I'm wondering how the professionalism of the coding is, since by the way you guys code, it looks to be of a pretty high standard.
    Code:
    /* Project 2:Reversing a stack */
    /* Programmed by:              */
    /* Revision 1                  */
    
    #include <stdio.h>                          /*Library needed to run this program*/
    
    #define MAXSTACK 64                         /*MAXSTACK is a symbolic constant, used to define*/
    typedef char StackEntry;                    /*the maximum length of the user-defined sentence*/ 
    typedef struct stack                        
    {
    	int top;
    	StackEntry entry[MAXSTACK];
    }Stack;
    
    bool StackFull(Stack *s);                   /*These are the preliminary function calls,*/
    bool StackEmpty(Stack *s);                   
    void Push(StackEntry item, Stack *s);
    void Pop(StackEntry *item, Stack *s);
    void CreateStack(Stack *s);
    void StackRevMain(void);
    
    void main(void)                             /*Main Program, calls the StackRevMain program*/
    {
    	printf("This is a small program made to reverse a sentence inputted by a user.\n");
    	printf("Please type your sentence and hit enter when done. Thank you\n\n");
        StackRevMain();
    	printf("\n");
    }
    
    void StackRevMain(void)                     /*Desc: Most important function, calls all other functions   */
    {                                           /*      and uses them to compile and reassemble the sentence */
    	StackEntry item;                        /*PRE:  The preliminary function calls must all be completed */
    	Stack stack;                            /*POST: Reversed sentence is displayed to user               */ 
    	CreateStack(&stack);
    	while(!StackFull(&stack)&&(item = getchar()) != '\n')
    		Push(item, &stack);
    	while(!StackEmpty(&stack)) 
    	{
    		Pop(&item, &stack);
    		putchar(item);
    	}
    	putchar('\n');
    }
    
    void Push(StackEntry item, Stack *s)        /*Desc: Function adds a letter to the array              */
    {                                           /*Pre:  The stack exists and is not full                 */
    	if(StackFull(s))                        /*Post: An additional item is stored on top of the stack */
    		printf("Full Stack");
    	else
    		s->entry[s->top++] = item;
    }
    
    void Pop(StackEntry *item, Stack *s)        /*Desc: Function removes a letter from the array         */
    {                                           /*Pre:  The stack exists and is not empty                */
    	if (StackEmpty(s))                      /*Post: The top of the stack is removed and stored as *s */
    		printf("Stack Empty");
    	else
    		*item = s->entry[--s->top];
    }
    
    bool StackEmpty(Stack *s)                   /*Desc: Checks to see if stack is empty                  */
    {                                           /*Pre:  The stack must exist and have been initialized   */
    	return s->top <= 0;                     /*Post: TRUE if stack is empty, FALSE if otherwise       */
    }
    
    bool StackFull(Stack *s)                    /*Desc: Checks to see if stack is full                   */
    {                                           /*Pre:  The stack must exist and have been initialized   */
    	return s->top >= MAXSTACK;              /*Post: TRUE if stack is full, FALSE if otherwise        */
    }
    
    void CreateStack(Stack *s)                  /*Desc: Creates a new, initialized but empty stack       */
    {                                           /*Pre:  None											 */
    	s->top = 0;                             /*Post: The stack is created and is initialized to empty */
    }

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    I'm far from a professional but everything looks great to me except
    Code:
    void main(void)
    It should be
    Code:
    int main(void)
    Also you should return (0); at the the end of main.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Originally posted by Salem

    However:
    Since this is homework, your commenting guidelines may be that you have to comment your use of the language to show that you understand what is going on and why you are using certain features.

    In professional code, comments which describe what you are using, or state the obvious are bad. They should tell you why something is being done and explaining the non-obvious.
    Yeah, the overcommenting would be the result of a professor that wants us to explain the pre and post conditions for EVERYTHING we do. It's a little overboard for me, since the pre and post conditions for all the push and pop things happen to be the exact same as in the book.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    GUAH. I swear I didn't change anything and although it worked last night, now I get 12 error messages regarding my prototypes. HELP PLEAAASE I just don't get this
    --------------------Configuration: Stacks1 - Win32 Debug--------------------
    Compiling...
    Stacks1.c
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(21) : error C2061: syntax error : identifier 'StackFull'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(21) : error C2059: syntax error : ';'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(21) : error C2059: syntax error : 'type'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(22) : error C2061: syntax error : identifier 'StackEmpty'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(22) : error C2059: syntax error : ';'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(22) : error C2059: syntax error : 'type'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(39) : warning C4013: 'StackFull' undefined; assuming extern returning int
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(41) : warning C4013: 'StackEmpty' undefined; assuming extern returning int
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(65) : error C2061: syntax error : identifier 'StackEmpty'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(65) : error C2059: syntax error : ';'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(65) : error C2059: syntax error : 'type'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(70) : error C2061: syntax error : identifier 'StackFull'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(70) : error C2059: syntax error : ';'
    C:\Windows\Profiles\Denny\My Documents\CS Projects\StacksProgram\Stacks1.c(70) : error C2059: syntax error : 'type'
    Error executing cl.exe.

    Stacks1.obj - 12 error(s), 2 warning(s)

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Code:
    /* Project 2:Reversing a user-defined sentence string               */
    /* Group    :Ady Perrott, Amie Tran, Nathan Brickner, Andrew Peaso, */
    /*           Yeva Adamyan Chris Terborg Dennis Freeland, Khang Pham */
    /* Revision 2 - 2/24/03                                             */
    
    #include<stdio.h>                          /*Library needed to run this program*/
    
    #define MAXSTACK 64                        /*MAXSTACK is a symbolic constant, used to define*/
    typedef char StackEntry;                   /*the maximum length of the user-defined sentence*/ 
    typedef struct stack                        
    {
    	int top;
    	StackEntry entry[MAXSTACK];
    }Stack;
    
    void Push(StackEntry item, Stack *s);	   /*Prototypes*/
    void Pop(StackEntry *item, Stack *s);
    void StackRevMain();
    void CreateStack(Stack *s);
    void StackRevMain(void);
    bool StackFull(Stack *s);                   
    bool StackEmpty(Stack *s);  
    
                     
    int main(void)                             /*Main Program, calls the StackRevMain program*/
    {
    	printf("This is a small program made to reverse a sentence inputted by a user.\n");
    	printf("Please type your sentence and hit enter when done. Thank you\n\n");
        StackRevMain();
    	printf("\n");
    	return(0);
    }
    
    void StackRevMain(void)                     /*Desc: Most important function, calls all other functions   */
    {                                           /*      and uses them to compile and reassemble the sentence */
    	StackEntry item;                        /*PRE:  The preliminary function calls must all be completed */
    	Stack stack;                            /*POST: Reversed sentence is displayed to user               */ 
    	CreateStack(&stack);
    	while(!StackFull(&stack)&&(item = getchar()) != '\n')
    		Push(item, &stack);
    	while(!StackEmpty(&stack)) 
    	{
    		Pop(&item, &stack);
    		putchar(item);
    	}
    	putchar('\n');
    }
    
    void Push(StackEntry item, Stack *s)        /*Desc: Function adds a letter to the array              */
    {                                           /*Pre:  The stack exists and is not full                 */
    	if(StackFull(s))                        /*Post: An additional item is stored on top of the stack */
    		printf("Full Stack");
    	else
    		s->entry[s->top++] = item;
    }
    
    void Pop(StackEntry *item, Stack *s)        /*Desc: Function removes a letter from the array         */
    {                                           /*Pre:  The stack exists and is not empty                */
    	if (StackEmpty(s))                      /*Post: The top of the stack is removed and stored as *s */
    		printf("Stack Empty");
    	else
    		*item = s->entry[--s->top];
    }
    
    bool StackEmpty(Stack *s)                   /*Desc: Checks to see if stack is empty                  */
    {                                           /*Pre:  The stack must exist and have been initialized   */
    	return s->top <= 0;                     /*Post: TRUE if stack is empty, FALSE if otherwise       */
    }
    
    bool StackFull(Stack *s)                    /*Desc: Checks to see if stack is full                   */
    {                                           /*Pre:  The stack must exist and have been initialized   */
    	return s->top >= MAXSTACK;              /*Post: TRUE if stack is full, FALSE if otherwise        */
    }
    
    void CreateStack(Stack *s)                  /*Desc: Creates a new, initialized but empty stack       */
    {                                           /*Pre:  None											 */
    	s->top = 0;                             /*Post: The stack is created and is initialized to empty */
    }

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    * Project 2:Reversing a user-defined sentence string */
    /* Group :Ady Perrott, Amie Tran, Nathan Brickner, Andrew Peaso, */
    /* Yeva Adamyan Chris Terborg Dennis Freeland, Khang Pham */
    /* Revision 2 - 2/24/03
    You mean you have that many in your group and could not figure out the errors!!! Ugh somedays I wonder why I became an college computer instructor!!!!
    Mr. C: Author and Instructor

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Well, you see, we have that many in the group, but I'm the only one that has a remote idea what he's doing, so I'm in charge of programming and they're ALL 'presenters' of the project

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    actually, that's what it was. I was discussing the project with my instructor this morning, and basically he said the guy that wrote all the code for the textbook where I got the examples for, included completely custom library files when he made his project, therefore he had things like 'Error' and 'Bool' defined already, whereas we still had to set up the typedefs. Thanks for all your help guys. Odds are I'll be back

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get domain part from URL
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 07-23-2008, 12:06 PM
  2. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  3. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  4. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  5. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM