/* This is the header file for sets.cpp. */
#include <iostream.h>
#include <string.h>
#include <cctype>
#include <cstdlib>

namespace mine {
  /* Globals */
  const int MAX_ELEMENTS = 50;
  const int MAX_SUBSETS = 50;
  const int MAX_CHARS = 10;
  
  /* Objects */
  /* a set is made up of elements and other sets */
  class Set {
  public:
    Set();
    ~Set();
    int get_elements(char[], Set *);
    void print_elements(Set *);
    int print_subsets(Set *);
    
  private:
    char elements[MAX_ELEMENTS][MAX_CHARS];
    int num_elements;
    int num_subsets;
  }; 

  int Set::print_subsets(Set *set) {
    
    cout <<"Subsets: \n";
    for (int x = 0; x < num_elements; x++) {
      cout <<"\t{";
      cout <<elements[x];
      cout <<"}\n";
      for (int y = x+1; y < num_elements; y++) {
	cout <<"\t{";
	cout <<elements[x];
	cout <<",";
	cout <<elements[y];
	cout <<"}\n";
      }
    }
    cout <<"{}\n";
    return 1;
  }

  void Set::print_elements(Set *set) {
    cout <<"Elements: \n";
    if (num_elements == 1) {
      cout <<elements[0]<<endl;
    }
    else {
      num_elements--;
      for (int x = 0; x < num_elements; x++) {
	cout <<"\t"<<elements[x] <<endl;
      }
    }
  }

  int Set::get_elements(char input[], Set *set) {
    int brace_count = 0;
    char *current;
    int x = 0;
    current = new char;
   
    /* for every char in the set */
    //for (int x = 0; x < (int)sizeof(input)-1; x++) {
    while (input[x] != '\0') {  
    /* get the next char */
      char next_char;
      next_char = input[x];
      
      cout <<"input "<<x<<": "<<next_char<<endl;
      x++;
      switch(next_char) {
      case ',' : {
	strcpy(elements[num_elements], current);
	num_elements++;
	cout <<", "<<elements[num_elements-1]<<"\n";
	strcpy(current, "");
	break;
      }
      case '{' : {
	brace_count++;
	if (brace_count > 1) {
	  if (strcmp(current, "") != 0) {
	    *current = next_char;
	  }
	  else {
	    strcat(current, &next_char);
	  }
	}
	break;
      }
      case '}' : {
	if (brace_count >= 1) {
	  strcat(current, &next_char);
	  strcpy(elements[num_elements], current);
	  num_elements++;
	}
	else {
	  cout <<elements[num_elements]<<endl;
	  cout <<*current<<endl;
	  strcpy(elements[num_elements], current);
	  num_elements++;
	}
	strcpy(current, "");
	brace_count--;
	if (brace_count == 0) {
	  return num_elements;
	}
	break;
      }
      default : {
	if (isdigit(next_char)) {
	  if (strcmp(current, "") == 0) {
	    *current = next_char;
	  }
	  else {
	    strcat(current, &next_char);
	  }
	}
	else {
	  return 0;
	}
	break;
      }
      }//end switch
      cout <<"current: "<<*current<<endl;
    }//end for
    return 0;
  }//end func
} //end namespace
	
	    

