コード例 #1
0
ファイル: bags1.cpp プロジェクト: amkaufma/ECS60
int main(int argc, char *argv[])
{
	short val;
	short end;
	
	int j = 0;
	int num = atoi(argv[2]);
	Queue<short> myQ(num);
	char *filename = argv[1];

	typedef Queue<short> *q_ptr;
	q_ptr p1 = &myQ;
	q_ptr ptr;

	StackLi<q_ptr> mystack;

	ifstream infile;
	infile.open(filename);

	
	while( infile >> val)
	{
		if(j < num)
		{
			p1->enqueue(val);
			j++;
			
		}
		else
		{	
			mystack.push(p1);
			j=0;
			p1 = new Queue<short>(num);
			p1->enqueue(val);
			j++;
			
		}
	} 	

	mystack.push(p1);
	
	while(!mystack.isEmpty())
	{
	
		ptr = mystack.top();
	
		while(!ptr->isEmpty())
		{
			end = ptr->dequeue();
			cout << end << " ";
		}
	
		mystack.pop();
	}

	cout << endl;
	return 0;
}
コード例 #2
0
ファイル: StackLi.cpp プロジェクト: ZhiyingLi/ecs40spring2016
        const StackLi<Object> & StackLi<Object>::
        operator=( const StackLi<Object> & rhs )
        {
            if( this != &rhs )
            {
                makeEmpty( );
                if( rhs.isEmpty( ) )
                    return *this;

                ListNode *rptr = rhs.topOfStack;
                ListNode *ptr  = new ListNode( rptr->element );
                topOfStack = ptr;

                for( rptr = rptr->next; rptr != NULL; rptr = rptr->next )
                    ptr = ptr->next = new ListNode( rptr->element );
            }
            return *this;
        }
コード例 #3
0
int main(int argc, char* argv[]){
    StackLi<char> tokenStack;
    StackLi<int> lineStack;
    char input;
    int lineCount = 1;
    lineStack.push(1);
    ifstream inputFile;
    inputFile.open(argv[1]);

    while(!inputFile.eof()){
        inputFile.get(input);
        
        //EOF
        if(inputFile.peek() == EOF){
            if(!tokenStack.isEmpty()){
                cout << "Unmatched " << tokenStack.topAndPop() << " on line #" << lineStack.topAndPop() << endl;
                return 0;
            }
            if(tokenStack.isEmpty()){
                cout << "OK" << endl;
            }
        }

        //end of line
        else if(input == '\n'){
            lineCount++;
            //cout << input;
        }

        //reading char
        else{
            
            //
            //match and ignore /* */
            if(input == '/'){
                inputFile.get(input); // now in /*
                if(input == '*'){
                    tokenStack.push('*');
                    lineStack.push(lineCount);
                    bool asterisk = 0;
                    bool closed = false;
                    do{
                        inputFile.get(input);
                        if(input == '*'){
                            asterisk = true;
                        }
                        else if (input != '/') {
                            asterisk = false;
                        }
                        if(asterisk == true && input == '/'){
                            //cout << asterisk << input << endl;
                            closed = true;
                            tokenStack.topAndPop();
                            lineStack.topAndPop();
                        }
                    }while(closed != true && !inputFile.eof());
                    if(inputFile.eof()){
                        cout << "Unmatched /* on line #" << lineStack.topAndPop() << endl;
                        return 0;
                    }
                }
            }
            //end matching /* */
            

            // find */
            if(input == '*'){
                inputFile.get(input);
                if(input == '/'){
                    cout << "Unmatched */ on line #" << lineCount << endl;
                    return 0;
                }
            }            
            //

            if(input == '(' || input == '{' || input == '['){
                tokenStack.push(input);
                lineStack.push(lineCount);                
            }
            if(input == ')'){
                if(tokenStack.topAndPop() != '('){
                    cout << "Unmatched " << input << " on line #" << lineStack.topAndPop() << endl;
                    return 0;
                }
            }
            if(input == '}'){
                if(tokenStack.topAndPop() != '{'){
                    cout << "Unmatched " << input << " on line #" << lineStack.topAndPop() << endl;
                    return 0;
                }
            }
            if(input == ']'){
                if(tokenStack.topAndPop() != '['){
                    cout << "Unmatched " << input << " on line #" << lineStack.topAndPop() << endl;
                    return 0;
                }
            }
        }
    }
    
    return 0;
}
コード例 #4
0
ファイル: balance1.cpp プロジェクト: amkaufma/ECS60
int main(int argc, char *argv[])
{

	char *filename = argv[1];
	int count = 1;				//count set to one because you start at line 1
	ifstream infile;
	infile.open(filename);

	Token t1;
	typedef Token *t_ptr;			//assign a pointer to a struct
	t_ptr ptr = &t1;
	t_ptr end;

	StackLi<t_ptr> mystack;			
	char current;
	char next;
	bool comment = false;			//comment flag
	bool done = false;			//if closing bracket without opening bracket preceding it, you are done

	while(infile.get(current))
	{
		
		if(!done)
		{
	
			switch(current)
			{
				case '/':
					if(comment)				//if part of comment, don't do anything
						break;
					next = infile.peek();
					//infile.get(next);
					if(next == '*')
					{
						infile.get(next);
						comment = true;			//set comment flag
						ptr->c = current;
						ptr->c2 = next;
						ptr->line = count;
						mystack.push(ptr);
						ptr = new Token;
					}
					break;
				case '*':	
					next = infile.peek();
					//infile.get(next);
					if(next == '/')
					{
						if(!mystack.isEmpty())
						{
							infile.get(next);
							end = mystack.top();
							if(end->c == '/' && end->c2 == '*')
							{
								mystack.pop();
								comment = false;		//end comment
							}
						}
						else
						{
							ptr->c = current;
							ptr->c2 = next;
							ptr->line = count;
							mystack.push(ptr);
							ptr = new Token;
							done = true;
						}
					
					}
					break;
				case '[':
					if(comment)			//if within comment, don't push bracket
						break;
					ptr->line = count;
					ptr->c = current;
					mystack.push(ptr);
					ptr = new Token;
					break;
				case ']':
					if(comment)			//if within comment, don't push bracket
						break;
					if(!mystack.isEmpty())
					{
						end = mystack.top();
						if(end->c == '[')
							mystack.pop();
					}
					else
					{
						ptr->c = current;
						ptr->line = count;
						mystack.push(ptr);
						ptr = new Token;
						done = true;			//if no opening bracket, set flag to quit
					}
					break;
				case '{':
					if(comment)
						break;			//if within comment, don't push bracket
					ptr->line = count;
					ptr->c = current;
					mystack.push(ptr);
					ptr = new Token;
					break;
				case '}':
					if(comment)				//if part of comment don't do anything
						break;
					if(!mystack.isEmpty())
					{
						end = mystack.top();
						if(end->c == '{')
							mystack.pop();
					}
					else
					{
						ptr->c = current;
						ptr->line = count;
						mystack.push(ptr);
						ptr = new Token;
						done = true;			//if no opening bracket, set flag to quit
					}
					break;
				case '(':
					if(comment)			//if within comment, don't push bracket
						break;
					ptr->line = count;
					ptr->c = current;
					mystack.push(ptr);
					ptr = new Token;
					break;
				case ')':
					if(comment)				//if part of comment, don't do anything
						break;
					if(!mystack.isEmpty())
					{
						end = mystack.top();
						if(end->c == '(')
							mystack.pop();
					}
					else
					{
						ptr->c = current;
						ptr->line = count;
						mystack.push(ptr);
						ptr = new Token;
						done = true; 		//if no opening bracket, set flag to quit
					}
					break;
				case '\n':
					++count;			//increment line count when a newline character is reached
					break;
				default:
					break;
			}
		}
		
	}
	

	if(mystack.isEmpty())						//if all brackets are paired, the stack is empty
		cout << "OK" << endl;
	else								//top of the stack is the first unmatched bracket
	{
		end = mystack.top();
		cout << "Unmatched " << end->c << end->c2 << " on line #" << end->line << endl;  		
	}
	
	return 0;

}