How can I check if array is full and double/half the size of it?

时间:2016-03-04 18:17:30

标签: c++

This program reads information from a text file, stores it in an array, and performs one of 3 functions. I need to be able to check if the array is full and double the size if it is or half the size if their is a deletion 1/4 of the size of the array. Tried to be brief so if you need more information, let me know.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct info{
  char letter;
  string SSN;
  string firstName;
  string lastName;
};

void insertion(int &count, int &validInsertationCount, string &SSN, char &letter, string &firstName, string &lastName, info *list);
void deletion(int &count, int &validDeletionCount, string &SSN, char &letter, string &firstName, string &lastName, info *list);
void retrieval(int &count, int &validRetrievalCount, string &SSN, string &firstName, string &lastName, info *list);

int main(int argc, char* argv[]){

  int arraySize = 1000;
  struct info list[1000];
  fstream input(argv[1]);
  int count = 0;
  int validInsertationCount = 0;
  int validDeletionCount = 0;
  int validRetrievalCount = 0;
  while(!input.eof()){
    input >> list[count].letter >> list[count].SSN >> list[count].firstName >> list[count].lastName;

    if(list[count].letter == 'i'){
      insertion(count, validInsertationCount, list[count].SSN, list[count].letter, list[count].firstName, list[count].lastName, list);
    }
    else if(list[count].letter == 'd'){
      deletion(count, validDeletionCount, list[count].SSN, list[count].letter, list[count].firstName, list[count].lastName, list);
    }
    else if(list[count].letter == 'r'){
      retrieval(count, validRetrievalCount, list[count].SSN, list[count].firstName, list[count].lastName, list);
    }
    count++;
  }
  input.close();

  int numberOfItems = validInsertationCount - validDeletionCount;

  cout << "The Number of Valid Insertation: " << validInsertationCount << endl;
  cout << "The Number of Valid Deletion: " << validDeletionCount << endl;
  cout << "The Number of Valid Retrieval: " << validRetrievalCount << endl;
  cout << "Item Numbers in the array: " << numberOfItems << endl;
  cout << "Array Size is: " << arraySize  << endl;
  //cout << "Time Elapsed: " <<  <<endl;                                                                                                                     
}
void insertion(int &count, int &validInsertationCount, string &SSN, char &letter, string &firstName, string &lastName, info *list){
  for(int i = 0; i < count; i++){
    if(SSN == list[i].SSN && list[i].letter == 'i'){
      for(int k = i; k < count; k++){
      list[k].SSN = list[k+1].SSN;
      list[k].letter = list[k+1].letter;
      list[k].firstName = list[k+1].firstName;
      list[k].lastName = list[k+1].lastName;
      }
      count--;
      return;
    }
  }
  validInsertationCount++;
  return;
}
void deletion(int &count, int &validDeletionCount, string &SSN, char &letter, string &firstName, string &lastName, info *list){
  for(int i = 0; i < count; i++){
    if(SSN == list[i].SSN && firstName == list[i].firstName && lastName == list[i].lastName){
      for(int k = i; k < count; k++){
      list[k].SSN = list[k+1].SSN;
      list[k].letter = list[k+1].letter;
      list[k].firstName = list[k+1].firstName;
      list[k].lastName = list[k+1].lastName;
      }
      count--;
      validDeletionCount++;
      return;
    }
  }
}
void retrieval(int &count, int &validRetrievalCount, string &SSN, string &firstName, string &lastName, info *list){
  for(int i = 0; i < count; i++){
    if(SSN == list[i].SSN && firstName == list[i].firstName && lastName == list[i].lastName){
      validRetrievalCount++;
    }
  }
  return;
}

1 个答案:

答案 0 :(得分:0)

您无法简单地调整静态分配的数组的大小,因此您希望使用 std :: vector malloc / new 来分配动态数组。但是,在这种情况下,您无法使用 sizeof()来确定数组的大小。所以你要么保留一个大小变量,要么使用&#34;分隔符&#34;值以便精确定位数组的末尾。