Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------
- // Name: Bowen Butler
- // Email: bnb5329@psu.edu
- // Class: CMPSC 122, Section 3
- // Program 2.2
- // Due Date: February 28, 2018, 11:59 PM
- //
- // Description: This program implements a basic integer vector
- // and provides the user with an interactive prompting system
- // to issue commands over an instance of our BasicVector class.
- //
- // Acknowledgements:
- // 1. Please use this portion of the banner comment to list
- // any resources or individuals you consulted on the creation
- // of this program.
- //---------------------------------------------------------------------
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- template<typename T>
- class BasicVector
- {
- private:
- int vector_size;
- int vector_capacity;
- T* data;
- void resize();
- public:
- BasicVector() {}; // default constructor does nothing
- BasicVector(int);
- ~BasicVector();
- T& at(int);
- T& operator[](int);
- T& front();
- T& back();
- void push_back(T);
- void insert(int, T);
- void pop_back();
- int size();
- int capacity();
- void print();
- };
- template<typename T>
- void BasicVector::resize()
- {
- int* tempdata = data;
- vector_capacity = (vector_capacity * 2);
- data = new int [vector_size];
- for (int i = 0; i < vector_size; i++) {
- data [i] = tempdata[i];
- tempdata[i] = 0;
- }
- delete [] tempdata;
- }
- template<typename T>
- BasicVector::BasicVector(int capacity)
- {
- if (capacity <= 16) {
- vector_capacity = 16;
- }else {
- int power = 16;
- vector_capacity = power;
- do {
- power = (power * 2);
- vector_capacity = power;
- }while (power <= capacity);
- }
- data = new int [vector_size];
- }
- template<typename T>
- BasicVector::~BasicVector()
- {
- for (int i = 0; i < vector_size; i++) {
- data[i] = 0;
- }
- delete [] data;
- vector_size = 0;
- vector_capacity = 0;
- }
- template<typename T>
- T& BasicVector::at(int index)
- {
- if (index > vector_size) {
- exit(1);
- }
- return data[index];
- }
- template<typename T>
- T& BasicVector::operator[](int index)
- {
- if (index > vector_size) {
- exit(1);
- }
- return data[index];
- }
- // Add in the rest of the public functions
- template<typename T>
- T& BasicVector::front(){
- return data[0];
- }
- template<typename T>
- T& BasicVector::back(){
- return data[(vector_size - 1)];
- }
- template<typename T>
- void BasicVector::push_back(T number){
- if (vector_size == vector_capacity) {
- resize();
- }
- vector_size++;
- data[(vector_size - 1)] = number;
- }
- template<typename T>
- void BasicVector::insert(int tempindex, T number){
- if (vector_size == vector_capacity) {
- resize();
- }
- for (int i = vector_size; i >= tempindex; i--) {
- data[i] = data [i];
- }
- data[tempindex] = number;
- vector_size++;
- }
- template<typename T>
- void BasicVector::pop_back(){
- vector_size--;
- data[vector_size] = 0;
- }
- template<typename T>
- int BasicVector::size(){
- return vector_size;
- }
- template<typename T>
- int BasicVector::capacity(){
- return vector_capacity;
- }
- template<typename T>
- void BasicVector::print(){
- cout << "elements(" << vector_size << "): ";
- for (int i = 0; i < vector_size; i++) {
- cout << data[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- int type;
- int runs;
- do{
- if (runs > 1){
- cout << endl <<"You have made an invalid selection. Please try again." << endl;
- }
- cout << "Specify what data type to store in vector: ";
- cout << "1) int" << endl << "2) float" << endl;
- cout << "3) double" << endl << "4) string" << endl << "5) bool" << endl;
- cin >> type;
- runs++;
- cout << endl;
- }while (type > 5 || type < 1);
- int capacity;
- cout << "Enter starting capacity of vector: ";
- cin >> capacity;
- cout << "Now accepting commands (quit to exit program):" << endl;
- if (type == 1) {
- BasicVector<int> Vector(capacity);
- }else if (type == 2){
- BasicVector<float> Vector(capacity);
- }else if (type == 3){
- BasicVector<double> Vector(capacity);
- }else if (type == 4){
- BasicVector<string>(capacity);
- }else if (type == 5){
- BasicVector<bool>(capacity);
- }
- bool run = true;
- // Implement command prompt loop
- while(run == true)
- {
- string command;
- cout << "> ";
- cin >> command;
- if (command == "at") {
- int tempindex;
- cin >> tempindex;
- cout << vector.at(tempindex) << endl;
- } else if (command == "get"){
- int tempindex;
- cin >> tempindex;
- cout << vector[(tempindex)] << endl;
- }else if (command == "front"){
- cout << vector.front() << endl;
- }else if (command == "back"){
- cout << vector.back() << endl;
- }else if (command == "insert"){
- int tempindex;
- int tempvalue;
- cin >> tempindex;
- cin >> tempvalue;
- vector.insert(tempindex, tempvalue);
- }else if (command == "push"){
- int tempvalue;
- cin >> tempvalue;
- vector.push_back(tempvalue);
- }else if (command == "pop"){
- vector.pop_back();
- }else if (command == "size"){
- cout << vector.size() << endl;
- } else if (command == "capacity"){
- cout << vector.capacity() << endl;
- }else if (command == "print"){
- vector.print();
- }else if (command == "quit"){
- run = false;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement