Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help me add to this stack and queue code!!!!

Status
Not open for further replies.

staminch

Programmer
Nov 15, 2001
9
US
hello, i have this code, but i need to add to it. It has to do the following, and i need to do #7-#9. I would appreciate any suggestions.
1. Create a file that holds first and last name. (lets say sta minch).
2. read the file.
3. creat a stack names s_name.
4. push all characters from first name into stack s_name.
5. create a queue named q_name.
6. Place all characters from last name into q_name.
7. Create 2 sets names set1_name and set2_name.
8. Take all charactets out of s_name, print them as a string, and insert them into the set set1_name.
9. Take all charactets out of q_name, print them as a string, and insert them into the set set2_name.
10. create the union, intersection, and difference of 2 sets, set1_name and set2_name, and print results as strings of characters.
Here is the code which executes:
Code:
#pragma warning(disable: 4786)

#include <iostream.h>
#include <stack>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
 
using namespace std;
 
void stack_to_set(stack<char> &s_name,  //create stack
string &f_name1, string &f_name2, set<char> &set1_name) {
	//string to be printed
 int x = 0;
 int c;
 char t[50];
 c = s_name.size();
 while(x < c) {
  t[x] = s_name.top();
  set1_name.insert(s_name.top());
  s_name.pop();
  x++;
 }
 f_name1 = t;
 x = 0;
 while(f_name1[x] > 0) {
  x++;
 }
 f_name1.resize(x, ' ');
 f_name2 = f_name1;
 reverse(f_name1.begin(), f_name1.end());  //reverse name
}
 
void queue_to_set(queue<char> &q_name, string &l_name, set<char> &set2_name) {
 //create queue
	int x = 0;
 int c;
 char t[53];
 c = q_name.size();
 while(x < c) {
  t[x] = q_name.front();
  set2_name.insert(q_name.front());
  q_name.pop();
  x++;
 }
 l_name = t;
 x = 0;
 while(l_name[x] > 0) {
  x++;
 }
 l_name.resize(x, ' ');
}
 
void main() {
 stack<char> s_name;
 queue<char> q_name;
 set<char> set1_name, set2_name;   //create sets
 string f_name1, f_name2, l_name, s_union;
 s_name.push('J'); s_name.push('o'); s_name.push('h'); s_name.push('n'); s_name.push('n'); s_name.push('y'); 
 q_name.push('W'); q_name.push('i'); q_name.push('l'); q_name.push('l'); q_name.push('i'); 
  q_name.push('a'); q_name.push('m'); q_name.push('s');   //first and last names
 stack_to_set(s_name, f_name1, f_name2, set1_name);
 queue_to_set(q_name, l_name, set2_name);
 for(int x = 0; x < f_name1.length(); x++) //first name in order
  cout << f_name1[x];
 cout << endl;
 for(x = 0; x < f_name2.length(); x++) //first name backwards straight out of stack
  cout << f_name2[x];
 cout << endl;
 for(x = 0; x < l_name.length(); x++) //last name out of queue
  cout << l_name[x];
 cout << endl;
 //set1_union_set2(set1_name, set2_name, set_u);
 char temp[50];
 x = 0;
 set<char>::iterator start = set1_name.begin();
 for(;start != set1_name.end(); ++start) {
  temp[x] = *start;
  ++x;

  int c;
  cin >> c;
 }
}
thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top