Пример #1
0
int main()
{
  bool palindrome = true;
  char character;
  StackType stack;
  QueType queue(40);
  char stackChar;
  char queChar;
  cout << "Enter a string; press return." << endl;
  cin.get(character);
  while (character != '\n')
  {
    stack.Push(character);
    queue.Enqueue(character);
    cin.get(character);
  }
  
  while (palindrome && !queue.IsEmpty())
  {
    stackChar = stack.Top();
    stack.Pop();
    queue.Dequeue(queChar);
   
    if (stackChar != queChar)
      palindrome = false;
  }
  
  if (palindrome)
    cout << "String is a palindrome" << endl;
  else
    cout << "String is not a palindrome" << endl;
  return 0;
}            
Пример #2
0
int main()
{
  char symbol;
  StackType stack;
  bool balanced = true;
  char openSymbol;
  
  cout << "Enter an expression and press return." << endl;
  cin.get(symbol); 

  while (symbol != '\n' && balanced)
  {
    if (IsOpen(symbol))
      stack.Push(symbol);

	else if (IsClosed(symbol))
    {
      if (stack.IsEmpty())
        balanced = false;
      else
      {
        openSymbol = stack.Top();
        stack.Pop();
        balanced = Matches(symbol, openSymbol);
      }
    }
    cin.get(symbol);
  }

  if (balanced)
    cout << "Expression is well formed." << endl;
  else
    cout << "Expression is not well formed."  << endl;
  return 0;
}
Пример #3
0
// Reads messages file and pushes email messages to the stack
void readmessages(StackType& email, ifstream& in)
{
	Message message; // Redo the constructor

	while (!(in.eof())) {

		char Body[1000];
		char FROM[6], sender[50];
		char DATE[6], date[15];
		char SUBJECT[9], subject[60]; // Body, sender,date, and subject initialized as character arrays

		in >> FROM;
		in.ignore(1);
		in.getline(sender,50);
		in >> DATE >> date;
		in >> SUBJECT;
		in.ignore(1);
		in.getline(subject,60);
		in.ignore(100,'\n');  // reads the data from messagesfile.txt

		in.get(Body,1000,'#');

		message.setSender(sender);
		message.setDate(date);
		message.setSubject(subject);
		message.setBody(Body); // Sets the information into a message object

		email.Push(message);   // Pushes the message object onto the emails stack

		in.ignore(100,'\n');
	}
}
Пример #4
0
// Reads and executes commmands from queriesfile.txt
void readqueries(StackType& email, ifstream& in){
	char command[30], address[50];
	int k;

	while (!(in.eof())) {
		in >> command;
		if (strcmp(command,"LIST-MESSAGES-BY-DATE") == 0)
				email.Report();
		if (strcmp(command,"LIST-MESSAGES-FROM") == 0){
			 in>>address;
			 email.ReportfromSender(address);
		}
		if (strcmp(command,"DELETE-MOST-RECENT-MESSAGE") == 0)
			email.Pop();
		if (strcmp(command,"DISPLAY") == 0 ){
			in >> k;
			email.Get(k);
		}
Пример #5
0
  void process_iterator_commands(StackType& s, std::string preface) {
    std::string allowable[] = {"<","e","*","+","i","c","*a","ea","f","q",""};
    StackType::Iterator i = s.begin();
    for (;;)
      try {
        std::cout << std::endl;
        std::cout << preface+"i = " << i.str() << std::endl;
        std::string i_command = ics::prompt_string(preface+
            "Enter iterator command(<[<]/e[rase]/*/+[+i]/i[++]/c[ommands]/*a[ll]/ea[ll]/f[or]/q[uit])","",allowable);
        if (i_command == "<")
          std::cout << preface +" << = " << i << std::endl;
        else if (i_command == "e")
          std::cout << preface+"  erase = " << i.erase() << std::endl;
        else if (i_command == "*")
          std::cout << preface+"  * = " << *i << std::endl;
        else if (i_command == "+")
          std::cout << preface+"  ++i returned = " << ++i << std::endl;
        else if (i_command == "i")
          std::cout << preface+"  i++ returned = " << i++ << std::endl;
        else if (i_command == "c")
          process_commands(preface);
        else if (i_command == "*a") {
          std::cout << preface+"  initially i = " << i << std::endl;
          for (; i != s.end(); ++i)
            std::cout << preface+"  *(all) = " << *i << std::endl;
          std::cout << preface+"  finally i = " << i << std::endl;
        }
        else if (i_command == "ea") {
          std::cout << preface+"  initially i = " << i << std::endl;
         for (; i != s.end(); ++i)
            std::cout << preface+"  erase(all) = " << i.erase() << std::endl;
          std::cout << preface+"  finally i = " << i << std::endl;
        }
        else if (i_command == "f") {
          for (auto v : s)
            std::cout << preface+"  *(all) = " << v << std::endl;
        }
        else if (i_command == "q")
          break;

      } catch (ics::IcsError& e) {
        std::cout << preface+"  " << e.what() << std::endl;
      }
  };
Пример #6
0
    std::string menu_prompt (std::string preface) {
      std::cout << std::endl << std::endl;
      std::cout << preface+"stack s = " << s.str() << std::endl;
      std::cout << preface+"Mutators         Accessors              General" << std::endl;
      std::cout << preface+"  v  - push        m  - empty             lf - load from file"    << std::endl;
      std::cout << preface+"  V  - push_all    s  - size              l{ - load from file"    << std::endl;
      std::cout << preface+"  ^  - pop         p  - peek              it - iterator commands" << std::endl;
      std::cout << preface+"  x  - clear       <  - <<                q  - quit"              << std::endl;
      std::cout << preface+"  =  - =           r  - relations " << std::endl;

      std::string allowable[] = {"v","V","^","x","=","m","s","p","<","r","lf","l{","it","q",""};
      std::cout << std::endl;
      return ics::prompt_string(preface+"Enter stack command","",allowable);
    }
Пример #7
0
  void process_commands(std::string preface) {
    for (;;) try {
      std::string command = menu_prompt(preface);

      if (command == "v") {
        std::string e = ics::prompt_string(preface+"  Enter element to add");
        std::cout << preface+"  push = " << s.push(e) << std::endl;
      }
      else if (command == "V") {
        StackType s2(prompt_stack(preface));
        std::cout << "  push_all = " << s.push_all(s2) << std::endl;;
      }
      else if (command == "^")
        std::cout << preface+"  pop = " << s.pop() << std::endl;

      else if (command == "x")
        s.clear();

      else if (command == "=") {
        StackType s2(prompt_stack(preface));
        s = s2;
        std::cout << "  s now = " << s << std::endl;
      }

      else if (command == "m")
        std::cout << preface+"  empty = " << s.empty();

      else if (command == "s")
        std::cout << preface+"  size = " << s.size() << std::endl;


      else if (command == "p") {
        std::cout << preface+"  peek = " << s.peek() << std::endl;
      }

      else if (command == "<")
        std::cout << preface+"  << = " << s << std::endl;

      else if (command == "r") {
        std::cout << preface+"  s == s = " << (s == s) << std::endl;
        std::cout << preface+"  s != s = " << (s != s) << std::endl;

        StackType s2(prompt_stack(preface));
        std::cout << preface+"  s = " << s << " ?? s2 = " << s2 << std::endl;
        std::cout << preface+"  s == s2 = " << (s == s2) << std::endl;
        std::cout << preface+"  s != s2 = " << (s != s2) << std::endl;
      }

      else if (command == "lf") {
        std::ifstream in_stack;
        ics::safe_open(in_stack,preface+"  Enter file name to read", "loadstack.txt");
        std::string e;
        while (getline(in_stack,e))
          s.push(e);
        in_stack.close();
      }

      else if (command == "l{")
        s = StackType{"c","b","d","e","a"};


      else if (command == "it")
        process_iterator_commands(s, "it:  "+preface);

      else if (command == "q")
        break;

      else
        std::cout << preface+"\""+command+"\" is unknown command" << std::endl;

    } catch (ics::IcsError& e) {
      std::cout << preface+"  " << e.what() << std::endl;
    }

  };