예제 #1
0
int main()
{
  // print my name and this assignment's title
  cout << "Lab 3b, The \"Write A Static Array Class Template\" Program \n";
  cout << "Programmer: Licong Wang\n";
  cout << "Editor(s) used: Visual studio 2013\n";
  cout << "Compiler(s) used:  Microsoft c++ complier\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  StaticArray<int, 100> a;
  string buf;
  int key;
  int value;

    while (true)
    {
      cout << "Input an index and a value [Q to quit]: ";
    
      cin >> buf;
      if (buf == "Q" || buf == "q")
        break;
      key = atoi(buf.c_str());

      cin >> buf;
      value = atoi(buf.c_str());
      a[key] = value;
    }

  cout << "\nI stored this many values : " << a.size() << endl;
  cout << "These values are:" << endl;

  vector<int> keys = a.keys();
  for (int i = 0; i < keys.size(); i++)
    cout << keys[i] << " " << a[keys[i]] << endl;

  while (true)
  {
    cout << "Input an index for me to look up [Q to quit]: ";
    cin >> buf;

    if (buf == "Q" || buf == "q")
      break;
    else
    {
      key = atoi(buf.c_str());
      if (a.containsKey(key))
        cout << "Found it -- the value stored at " << key << " is " << a[key] << endl;
      else
        cout << "I didn't find it" << endl;
    }
  }
  
  cout << "\nPress ENTER to continue.\n";
  cin.ignore();  // prevent cin.get() from getting a new line from previous cin
  cin.get();
}
예제 #2
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());
}