Thread: Suggestions

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Suggestions

    This program works but it looks like crap. Any suggestions on how to make it better and how to make the output look better

    The output looks like this:
    Symbol 1 / does match Symbol 2 *
    Symbol 1 * does match Symbol 2 /
    Symbol 1 ( does match Symbol 2 )
    Symbol 1 [ does match Symbol 2 ]
    Symbol 1 ( does match Symbol 2 )
    End of program reached when unmatched symbol
    Symbol 1 ( does not match Symbol 2 ]
    )
    (
    ]
    [
    )
    (
    /
    *
    *
    /
    All Symbols correctly balanced
    I would like it to look like this

    Symbol 1 /* does match Symbol 2 */
    Symbol 1 ( does match Symbol 2 )
    Symbol 1 [ does match Symbol 2 ]
    Symbol 1 ( does match Symbol 2 )
    End of program reached when unmatched symbol
    Symbol 1 ( does not match Symbol 2 ]
    ()
    []
    ()
    /**/
    All Symbols correctly balanced
    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip> 
    #include<cstdlib> 
    
    #include"stackLL.h"
    #include"StackLLImple.cpp"
    
    using namespace std;
    
    int main()
    {
        char answer;
    	char InputChar1, InputChar2;
        int Number=0;
    	char num;
        int success=0;
    	
    
    	character link;
    	int i=0;
    	ifstream InFile;
    	ofstream OutFile;
    
    	InFile.open("input1.txt");
        OutFile.open("output2.txt");
    
        
    
    	InFile>>InputChar1;
    	i++;
    	InFile>>InputChar2;
    	i++;
    	while(InFile != NULL)
    	{
    
    
    	if((InputChar1 == '[') && (InputChar2 == ']') || (InputChar1 == ']') && (InputChar2 == '['))
    	{
    		 link.push(InputChar1);
    		 link.push(InputChar2);
    		 Number = 1;
    		 
    	}	 
    	else if((InputChar1 == '(') && (InputChar2 == ')') || (InputChar1 == ')') && (InputChar2 == '('))
    	{
    		 link.push(InputChar1);
    		 link.push(InputChar2);
    		 Number = 1;
    	}
    		else if((InputChar1 == '/') && (InputChar2 == '*') || (InputChar1 == '*') && (InputChar2 == '/'))
    	{
    			link.push(InputChar1);
    			link.push(InputChar2);
    			Number = 1;
    	}
    else
    Number = 2;
    	
    	
    	switch (Number)
    	{
     case 1: OutFile<<"Symbol 1 "<<InputChar1<<" does match Symbol 2 "<<InputChar2<<endl;
    		Number =0;
    		break;
    	case 2 :OutFile<<" End of program reached when unmatched symbol"<<endl;
    		OutFile<<"Symbol 1 "<<InputChar1<<" does  not match Symbol 2 "<<InputChar2<<endl;
    while(i >2)
    {
    	OutFile<<link.pop(InputChar1)<<endl;
    	i--;
    	OutFile<<link.pop(InputChar2)<<endl;
    	i--;
    	
    }
    	Number = 3;
    	case 3 :OutFile<<"All Symbols correctly balanced"<<endl;
    	
    	default: ;
    	}
    	
    	
    	InFile>>InputChar1;
    	i++;
    	InFile>>InputChar2;
    	i++;
    	}
    	return 0;
    }
    	
    #include<iostream>
    
    //#include"stackLL.h"
    
    using namespace std;
    //constructor
    character::character()
    {
        head=NULL;
        cur=NULL;
        pre=NULL;
    }
    
    //destructor
    character::~character()
    {
        node *ptr;
    
        while(head!=NULL)     //delete all the chrbers
        {
            ptr=head->next;
            delete head;
            head=ptr;
        }
    }
    
    char character::push(char chr)
    {
    
     node *temp = new node;
        temp->chr = chr;
        temp->next = head;     // if head was NULL, next will now be NULL (end of list)
        head = temp;
    return chr;
    }
    
    
    //****remove at the head, this is stack, push at the head remove at the head.
    char character::pop(char chr)
    {
        //int chr;
        node *temp = head;
    
        if (temp == NULL) return -1;
    
        chr = temp->chr;
        head = temp->next;
        delete temp;
    
        return chr;
    
    }
    
    #include<iostream>
    struct node
    {
        char chr;
        node *next;
    };
    
    class character
    {
        public:
            character();
            ~character();
    
            char push(char chr);
            char pop(char chr);
            
            
    
        private:
            node *head;
            node *cur;
            node *pre;
    };

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    25
    I just browsed through the code quickly so this is just a halfqualified guess:
    change
    Code:
    while(i >2)
    {
    OutFile<<link.pop(InputChar1)<<endl;
    i--;
    OutFile<<link.pop(InputChar2)<<endl;
    i--;
    
    }
    to
    Code:
    while(i >2)
    {
    OutFile<<link.pop(InputChar1);
    i--;
    OutFile<<link.pop(InputChar2)<<endl;
    i--;
    
    }
    Last edited by dydoko; 09-19-2003 at 03:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  2. Hi, Suggestions on my program please..
    By Siggy in forum C++ Programming
    Replies: 44
    Last Post: 11-23-2004, 10:55 PM
  3. Math Book Suggestions
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-09-2003, 10:43 AM
  4. Need Suggestions
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2003, 05:46 AM
  5. Learning C for OSX, tutorial suggestions please
    By Nexum in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 04:57 AM