int main()
{
  // this code block prints my name and the assignment's title
  cout << "Lab 3b, My Static Array \n";
  cout << "Programmer: Nicholas Tie\n";
  cout << "Editor(s) used: Codeblocks\n";
  cout << "Compiler(s) used: GNU\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  // declare variables
  StaticArray<int,100> sArray;
  int c = sArray.capacity();
  char buff[MAX_CHARS_PER_LINE] = {};

  // User input
  for (int i=0; i < c; i++){
    cout << "Input an index and a value [Q to quit]: ";
    cin.getline(buff, MAX_CHARS_PER_LINE);

    // parse input into tokens
    int n = 0;
    token[0] = strtok(buff, DELIMITER); // first token
    if (token[0]){ // zero if line is blank
      for (n = 1; n < MAX_TOKENS_PER_LINE; n++){
        token[n] = strtok(0, DELIMITER); // subsequent tokens
        if (!token[n]) break; // no more tokens
      }
    }

    if (buff[0] == 'Q' || buff[0] == 'q'){
      break;
    }
    else{
      int a = atoi(token[0]);
      int b = atoi(token[1]);
      sArray[a] = b;
    }
  } // end input

  // # Values stored:
  cout << endl << "I stored this many values: " << sArray.size() <<endl;

  // List of all entered values:
  cout << "\nThe values inputted are: " << endl;
  for(int j = 0; j <= c; j++){
    if(sArray.containsKey(j))
      cout << j << " : " << sArray[j] << endl;
  }

  // Index Lookup
  string buf;
  for (int i=0; i < c; i++){
    cout << endl << "Input an index for me to look up [Q to quit]: ";
    cin >> buf;
    int x = atoi(buf.c_str());

    if (buf == "Q" || buf == "q"){
      cout << endl << "!*****Program has been Terminated*****!" << endl;
      break;
    }
    else if(sArray.containsKey(x)){
      cout << "Value stored at " << x << " is "
           << sArray[x] << endl;
    }
    else{
      cout << "I couldn't find it!" << endl;
    }
  }
}//end main
Exemplo n.º 2
0
 unsigned capacity() const {
   return x.capacity();
 }
Exemplo n.º 3
0
int main()
{
  // print my name and this assignment's title 
  cout << "LAB 3a: Write A Static Array Class Template\n"; 
  cout << "Programmer: Jacky Chow\n"; 
  cout << "Editor(s) used: Notepad++\n"; 
  cout << "Compiler(s) used: Visual C++\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
  
  vector<int> k;
  StaticArray<int, 10> a;

  //Test capacity() and size()
  cout << "Capacity of a should be 10\nCapacity returns " << a.capacity() << endl;
  assert(10 == a.capacity());
  cout << "Size should be 0\nSize returns " << a.size() << endl; 
  assert(0 == a.size());

  //Test kes()  
  k = a.keys();
  cout << "Keys of a in use: ";
  for(int i = 0; i < k.size(); i++)
    cout << k[i] << " ";
  cout << endl << endl;  

  //Test operator[] setter and getter
  a[3] = 7;
  cout << "a[3] was set to 7\na[3] is returns " << a[3] << endl;
  assert(7 == a[3]);
  cout << "Size should be 1\nSize returns " << a.size() << endl; 
  assert(1 == a.size());  
  k = a.keys();
  cout << "Keys of a in use: ";
  for(int i = 0; i < k.size(); i++)
    cout << k[i] << " ";
  cout << endl << endl; 

  a[5] = 20;
  cout << "a[5] was set to 20\na[5] is returns " << a[5] << endl;
  assert(20 == a[5]);
  a[7] = 15;
  cout << "a[7] was set to 15\na[7] is returns " << a[7] << endl;
  assert(15 == a[7]);
  a[9] = 3;
  cout << "a[9] was set to 3\na[9] is returns " << a[9] << endl;
  assert(3 == a[9]);  
  k = a.keys();
  cout << "Keys of a in use: ";
  for(int i = 0; i < k.size(); i++)
    cout << k[i] << " ";
  cout << endl << endl; 

  //Test containsKey()
  cout << "\nKey 1 is currently not in use.\n";
  if(a.containsKey(1)) cout << "Error, a[1] is not is use\n";
  else 
    cout << "containsKey() on a[1] returned a false\n";
  assert(!(a.containsKey(1)));

  a[1] = 8;
  cout << "a[1] was set to 8\na[1] is returns " << a[1] << endl;
  assert(8 == a[1]);
  cout << "Key 1 is now in use.\n";

  if(a.containsKey(1)) cout << "containsKey() on a[1] now returns true\n";
  else 
    cout << "Error! a[1] did not return a true\n";
  assert(a.containsKey(1));
  k = a.keys();
  cout << "Keys of a in use: ";
  for(int i = 0; i < k.size(); i++)
    cout << k[i] << " ";
  cout << endl << endl; 

  //Test deleteKey()
  a.deleteKey(1);
  cout << "a[1] was deleted\n";
  cout << "Key 1 is again not in use.\n";
  if(a.containsKey(1)) cout << "Error, a[1] is not is use\n";
  else 
    cout << "a[1] returned a false\n";
  k = a.keys();
  cout << "Keys of a in use: ";
  for(int i = 0; i < k.size(); i++)
    cout << k[i] << " ";
  cout << endl << endl; 

  //Cout values in current inUse keys    
  for(int i = 0; i < k.size(); i++)
  {
    cout << "a[" << k[i] << "] = " << a[k[i]] << endl;
  }

  // object copy testing
  {
    const StaticArray<int, 10> copy = a; // a read-only copy
    cout << endl;
    k = copy.keys();
    for(int i = 0; i < k.size(); i++)
    {
      cout << "copy1[" << k[i] << "] = " << copy[k[i]] << endl;
    }
    cout << endl;
    for (int i = 0; i < copy.capacity(); i++)
      cout << "copy1[" << i << "] = " << copy[i] << endl;
    cout << "Keys of a in use: ";
    for(int i = 0; i < k.size(); i++)
      cout << k[i] << " ";
    cout << endl << endl; 
  }

  // object assignment testing
  {
    cout << endl;
    StaticArray<int, 10> copy; copy = a;
    k = copy.keys();
    for(int i = 0; i < k.size(); i++)
    {
      cout << "copy2[" << k[i] << "] = " << copy[k[i]] << endl;
    }
    cout << endl;
    for (int i = 0; i < copy.capacity(); i++)
      cout << "copy2[" << i << "] = " << copy[i] << endl;
    cout << "Keys of a in use: ";
    for(int i = 0; i < k.size(); i++)
      cout << k[i] << " ";
    cout << endl << endl; 
  }  
  
  //Test clear()
  cout << "\nTestting clear() on a." << endl;
  a.clear();
  k = a.keys();
  cout << "Keys of a in use: ";
  for(int i = 0; i < k.size(); i++)
    cout << k[i] << " ";
  cout << endl;   
  cout << "Size should be 0\nSize returns " << a.size() << endl; 
  assert(0 == a.size());
}