예제 #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
파일: 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;

}