void RunStackLi(ifstream& stream){ char iord; int number; //string dummy; //getline(stream, dummy); // clears first line in data.txt char dummy[256]; stream.getline(dummy, 256); StackLi<int> stack; while(stream >> iord >> number){ if(iord == 'i'){ stack.push(number); } else{ stack.pop(); } } stream.clear(); stream.seekg(0, ios::beg); }
void RunStackLi(const char* filename) { StackLi <int> RunStackLi; ifstream myfile; myfile.open(filename); char line1 [1000]; char command; int value; myfile.getline(line1,1000); while(myfile >> command >> value) { //cout << command << value << endl; if (command == 'i') { RunStackLi.push(value); //++itr; } else //command == 'd' { RunStackLi.pop(); } } }
void RunStackLi(string filename) { StackLi<int> list; string command; string first_line; string line; string num_string; int num; ifstream c_file; c_file.open(filename.c_str()); getline(c_file, first_line); while (getline(c_file, line, ' ')) { command = line.substr(0,1); num_string = line.substr(1); num = atoi(num_string.c_str()); if (command == "i") list.push(num); else if (command == "d") list.pop(); } c_file.close(); }
void RunStackLi (char *filename) { StackLi<int> stack; ifstream inf(filename); char comm, s[512]; int value; inf.getline(s, 512); while (inf >> comm >> value) //while values are being read in. { if (comm == 'i') //if the file says to read in. stack.push(value); else //otherwise. stack.pop(); } }//RunStackLi
void RunStackLi(string name) { StackLi <int> stackli; char c; int n; ifstream inf(name.c_str()); inf.ignore(512,'\n'); while (inf>>c>>n) { if (c== 'i') stackli.push(n); else if (c=='d') stackli.pop(); } inf.close(); }
void RunStackLi(char *filename) { ifstream inFile(filename); StackLi<int> stack; char c, ln[LINE_LENGTH]; int v; inFile.getline(ln, LINE_LENGTH); while(inFile >> c >> v) { if(c == 'i') stack.push(v); else stack.pop(); } }
void RunStackLi(char * filename) { char command, first[512]; int num; StackLi<int> object; ifstream read(filename); read.getline(first,512); while (read >> command >> num) { if (command == 'i') { object.push(num); } if (command == 'd') { object.pop(); } } }
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; }
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; }
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; }
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; }