Anyone care to proof-read my code?

This is a discussion on Anyone care to proof-read my code? within the C++ Programming forums, part of the General Programming Boards category; Well, I finished up my project... one week early too! So I figured I'll post it here and perhaps someone ...

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    Cool Anyone care to proof-read my code?

    Well, I finished up my project... one week early too! So I figured I'll post it here and perhaps someone could blow a whistle and tell me of some things I could do to improve it!

    Basically, the program reads in a file with a single equation, in postix form, and solves it.

    I basically used a linked list implementation of a stack (because I wanted to, dammit! ), but it would be nice if I could think of a way to pass one parameter to pop(*node, *node) rather than 2.

    Any other comments (memory leaks, inefficeincy, insults, etc.) are welcomed, and the code is released for public use under the GPL.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <fstream.h>
    #include <cstring>
    
    struct node
    	{ int key; struct node *next;};
    
    typedef node* stack_ptr;
    
    void push(stack_ptr& a_stack, int number);
    void print_list(stack_ptr& a_stack, stack_ptr& b_stack);
    int pop(stack_ptr& a_stack,stack_ptr& b_stack);
    
    main()
    	{
    	char inFileName[16], str1[50];	
    	int n, v, total=0;
    	struct node *t, *x;
    	ifstream read;
    	
    	/* initialize the list */
    	t = new node;
       	t->next = NULL;
    	x = t;
    	
    	/****** Ask user for input file *****/
    	cout <<"Enter file name to be read: ";
    	cin >> inFileName;
    	
    	/***** open file **********/
    	read.open(inFileName);
    	if (read.fail())
    		{
    		cout << "Input file opening failed!"<<endl;
    		exit(1);
    		}
    
    	/*********************************************************
    	 * Read file for digits and operators
    	 * (it is assumed there are no illegal chars in the file
    	 *********************************************************/
    	for (int i = 0; !read.eof(); i++)
    		{
    		read.getline(str1,50, ' ');
    		if (isdigit(str1[0]))
    	 		{
    			v = atoi(str1);
    			push(t, v);
    			}
    		else if (str1[0] == '+')
    			{
    			v = pop(t, x) + pop(t,x);
    			push (t, v);
    			}
    		else if (str1[0] == '-')
    			{
    			v = pop(t, x) - pop(t,x);
    			push (t, v);
    			}	
    		else if (str1[0] == '*')
    			{
    			v = pop(t, x) * pop(t,x);
    			push (t, v);
    			}
    		else if (str1[0] == '/')
    			{
    			v = pop(t, x) / pop(t,x);
    			push (t, v);
    			}
    		
    		}
    	
    	/****************************************************
    	 * Now print all the numbers left in the linked list
    	 * which should only be one left if correct postfix 
    	 * notation was used in the input file.
    	 ****************************************************/		
    	print_list(t, x);
    	
    	return 0;
    	}
    void push(stack_ptr& a_stack, int number)
    	{
    	a_stack->next = new node;
          	a_stack->next->next = NULL;
    	a_stack->key =  number;
    	a_stack = a_stack->next;
    	}
    void print_list(stack_ptr& a_stack, stack_ptr& b_stack)
    	{
    	if (b_stack->next == NULL)
    		{
    		cout << "The stack is currently empty! "<<endl;
    		}
    	else
    		{
    		a_stack = b_stack;
       		do
       			{	
          			if (a_stack->next != NULL)
    				cout << a_stack->key << " ";
          			a_stack = a_stack->next;
       			}while(a_stack->next != NULL);
    		cout << endl;
    		}
    	}
    int pop(stack_ptr& a_stack,stack_ptr& b_stack)	
    	{
    	int rtn_value = 0;
    	if (b_stack->next == NULL)
    		{
    		cout << "The stack is currently empty! "<<endl;
    		}
    	else
    		{
    		a_stack = b_stack;
    		while(a_stack->next->next != NULL)
    			a_stack = a_stack->next;
    		rtn_value = a_stack->key;
    		delete a_stack->next;
    		a_stack->next = NULL;
    		}
    	return rtn_value;
    	}
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    #include <iostream.h>
    #include <stdio.h>
    #include <fstream.h>
    #include <cstring>

    I'd stick with the standard headers..

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <cstring>

    Also, use cerr to print errors. It's used exactly like cout... cerr << "Error!"; cout is STDIO, while cerr is STDERR. If someone wants to redirect the output of your program, this will make it easier.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >Any other comments (memory leaks, inefficeincy, insults, etc.) are welcomed

    okay... that is some atrocious brace placement
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Well... this is the C++ board and not the C board, you do know about the STL stack container correct? Using one of these would eliminate the need for you to even have those push and pop functions (the container will take care of it for you) and simplify the whole parameter passing issue you talked about.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Originally posted by hk_mp5kpdw
    Well... this is the C++ board and not the C board, you do know about the STL stack container correct? Using one of these would eliminate the need for you to even have those push and pop functions (the container will take care of it for you) and simplify the whole parameter passing issue you talked about.
    Yes, I know of the STL stack container. In fact, it's illustrated in out book. But the idea of the assignment, however, was to demonstrate our knowledge of linked lists in order to emulate a stack.
    I am Error. When all else fails, use fire.

    My Current Screenshot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to i correct this code to read the file correctly?
    By funit49 in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2008, 07:56 PM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 05:45 AM
  3. How do you read code?
    By someprogr in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-08-2008, 01:27 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21