Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream.>
- #include <cstring>
- #include <cstdlib>
- using namespace std;
- struct Entry
- {
- char nameFirst[20];
- char nameLast[20];
- char phoneNumber[20];
- char eMail[40];
- bool DELETED;
- };
- int getChoice();
- Entry* openFile(Entry* fpBuffer,size_t fBufferSize, int &eleCount,const size_t resizeStep, string &filename);
- void listEntries(Entry* fpBuffer, int elemCount);
- void changeEntry (Entry* fpBuffer, int elemCount);
- void deleteEntry (Entry* fpBuffer, int &elemCount);
- void addEntry (Entry* fpBuffer, int &elemCount,size_t &fBufferSize, size_t resizeStep);
- Entry* resizeArrayCopy(Entry*& fpBuffer,const size_t resizeStep,size_t &bufferSize);
- Entry* resizeArrayNoCopy (Entry*& fpBuffer, size_t &bufferSize,const size_t resizeStep);
- void saveFile (string filename, Entry* fpArray, int elementsCount);
- void saveFileAs (string filename, Entry* fpArray, int elementsCount);
- void searchEntry (Entry* fpArray, int elemCount);
- void exportTextFile (Entry* fpArray, int elemCount);
- void exportText (string fileExport, int elemCount, Entry* fpArray);
- int main()
- {
- Entry* pArray=NULL;
- Entry* sortArray=NULL;
- string filename;
- size_t bufferSize=4;
- const size_t resizeStep=15;
- int choice;
- int elementsCounter;
- choice=getChoice();
- while (choice!=10)
- {
- switch (choice)
- {
- case 1 :
- pArray = openFile(pArray, bufferSize, elementsCounter, resizeStep,filename);
- break;
- case 2 :
- saveFile (filename, pArray, elementsCounter);
- break;
- case 3 :
- saveFileAs (filename, pArray, elementsCounter);
- break;
- case 4 :
- addEntry (pArray, elementsCounter, bufferSize, resizeStep);
- break;
- case 5 :
- changeEntry (pArray, elementsCounter);
- break;
- case 6 :
- deleteEntry (pArray, elementsCounter);
- break;
- case 7 :
- searchEntry (pArray, elementsCounter);
- break;
- case 8 :
- listEntries(pArray, elementsCounter);
- break;
- case 9:
- {
- sortArray=new Entry[bufferSize];
- for(size_t i = 0; i < bufferSize; i++)
- {
- strcpy(sortArray[i].nameFirst, pArray[i].nameFirst);
- strcpy(sortArray[i].nameLast, pArray[i].nameLast);
- strcpy(sortArray[i].phoneNumber, pArray[i].phoneNumber);
- strcpy(sortArray[i].eMail, pArray[i].eMail);
- sortArray[i].DELETED=false;
- }
- exportTextFile (sortArray, elementsCounter);
- }
- break;
- }
- choice=getChoice();
- }
- cout<<"Program will now exit.";
- system ("pause");
- return 0;
- }
- int getChoice()
- {
- int option;
- cout<<"[1] Open file"<<endl;
- cout<<"[2] Save file"<<endl;
- cout<<"[3] Save file as.."<<endl;
- cout<<"[4] Add entry"<<endl;
- cout<<"[5] Change entry"<<endl;
- cout<<"[6] Delete entry"<<endl;
- cout<<"[7] Search entry"<<endl;
- cout<<"[8] Show all entries"<<endl;
- cout<<"[9] Export to text file"<<endl;
- cout<<"[10] Exit"<<endl;
- cout<<"What would you like to do? ";
- cin>>option;
- while (option<0 || option>10 || !option )
- {
- //cout<<"Wrong input!"<<endl;
- //cout<<"What would you like to do? ";
- //cin.sync();
- cin.clear();
- cin.ignore();
- cin>>option;
- }
- return option;
- }
- Entry* openFile(Entry* fpBuffer,size_t fBufferSize, int &eleCount,const size_t resizeStep,string &filename)
- {
- delete[] fpBuffer;
- Entry newEntry;
- Entry* pBuffer=new Entry[fBufferSize];
- size_t i=0;
- eleCount=0;
- cout<<"Enter filename: ";
- cin>>filename;
- ifstream file(filename.c_str(),ios::binary);
- if(!file.is_open())
- {
- cout << "Cannot open "<<filename<<"!";
- return NULL;
- }
- while(file.read((char*)&newEntry, sizeof(newEntry)))
- {
- i++;
- if (i>=fBufferSize)
- {
- pBuffer=resizeArrayNoCopy(pBuffer,fBufferSize, resizeStep);
- }
- }
- file.close();
- ifstream file2(filename.c_str(),ios::binary);
- while(file2.read((char*)&newEntry, sizeof(newEntry)))
- {
- pBuffer[eleCount]=newEntry;
- eleCount++;
- }
- cout<<"A total of "<<eleCount<<" elements read."<<endl;
- file2.close();
- return pBuffer;
- }
- void listEntries(Entry* fpBuffer, int elemCount)
- {
- if (elemCount==0)
- {
- cout<<"There are no entries loaded into memory."<<endl;
- return;
- }
- for (int i=0; i<elemCount; i++)
- {
- cout<<i+1<<"."<<endl;
- cout<<"First name: ";
- cout<<fpBuffer[i].nameFirst<<endl;
- cout<<"Last name: ";
- cout<<fpBuffer[i].nameLast<<endl;
- cout<<"Phone number: ";
- cout<<fpBuffer[i].phoneNumber<<endl;
- cout<<"E-Mail: ";
- cout<<fpBuffer[i].eMail<<endl<<endl;
- }
- }
- void addEntry (Entry* fpBuffer, int &elemCount,size_t &fBufferSize,const size_t resizeStep)
- {
- if (elemCount==fBufferSize)
- {
- fpBuffer = resizeArrayCopy(fpBuffer, resizeStep, fBufferSize);
- if (fpBuffer!=NULL)
- {
- cout<<"Adding entry number "<<elemCount+1<<endl;
- cout<<"First name: ";
- cin.sync();
- cin>>fpBuffer[elemCount].nameFirst;
- cout<<"Last name: ";
- cin>>fpBuffer[elemCount].nameLast;
- cout<<"Phone number: ";
- cin>>fpBuffer[elemCount].phoneNumber;
- cout<<"E-Mail: ";
- cin>>fpBuffer[elemCount].eMail;
- cout<<"Entry added successfully!"<<endl;
- elemCount++;
- }
- else
- {
- cout<<"Allocation failed!"<<endl;
- return;
- }
- }
- else
- {
- cout<<"Adding entry number "<<elemCount+1<<endl;
- cout<<"First name: ";
- cin>>fpBuffer[elemCount].nameFirst;
- cout<<"Last name: ";
- cin>>fpBuffer[elemCount].nameLast;
- cout<<"Phone number: ";
- cin>>fpBuffer[elemCount].phoneNumber;
- cout<<"E-Mail: ";
- cin>>fpBuffer[elemCount].eMail;
- cout<<"Entry added successfully!"<<endl;
- elemCount++;
- }
- }
- void changeEntry (Entry* fpBuffer, int elemCount)
- {
- int changeOption;
- cout<<endl<<"Which entry would you like to change? (press 0 for main menu)"<<endl;
- cin>>changeOption;
- if (changeOption==0) return;
- while (changeOption>elemCount || changeOption<0 || !changeOption )
- {
- cout<<"Wrong input or entry number!"<<endl;
- cin.sync();
- cin.clear();
- cin.ignore();
- cin>>changeOption;
- }
- cout<<"Changing entry number "<<changeOption<<endl;
- cout<<"First name: ";
- cin>>fpBuffer[changeOption-1].nameFirst;
- cout<<"Last name: ";
- cin>>fpBuffer[changeOption-1].nameLast;
- cout<<"Phone number: ";
- cin>>fpBuffer[changeOption-1].phoneNumber;
- cout<<"E-Mail: ";
- cin>>fpBuffer[changeOption-1].eMail;
- cout<<"Entry changed successfully!"<<endl;
- }
- void deleteEntry (Entry* fpBuffer, int &elemCount)
- {
- int deleteOption;
- cout<<"Which entry would you like to delete? (press 0 for main menu)"<<endl;
- cin>>deleteOption;
- while (deleteOption>elemCount || deleteOption<0 || !deleteOption)
- {
- cin.clear();
- cin.ignore();
- cin>>deleteOption;
- }
- if (deleteOption==0)
- return;
- for (int i=deleteOption; i<=elemCount; i++ )
- {
- strcpy(fpBuffer[i-1].nameFirst, fpBuffer[i].nameFirst);
- strcpy(fpBuffer[i-1].nameLast, fpBuffer[i].nameLast);
- strcpy(fpBuffer[i-1].phoneNumber, fpBuffer[i].phoneNumber);
- strcpy(fpBuffer[i-1].eMail, fpBuffer[i].eMail);
- }
- elemCount--;
- return;
- }
- Entry* resizeArrayCopy(Entry*& fpBuffer, const size_t resizeStep, size_t &bufferSize)
- {
- Entry* pNewBuffer = new Entry[bufferSize + resizeStep];
- if(pNewBuffer == NULL)
- {
- return NULL;
- }
- for(size_t i = 0; i < bufferSize; i++)
- {
- //strcpy(pNewBuffer[i].nameFirst, fpBuffer[i].nameFirst);
- //strcpy(pNewBuffer[i].nameLast, fpBuffer[i].nameLast);
- //strcpy(pNewBuffer[i].phoneNumber, fpBuffer[i].phoneNumber);
- // strcpy(pNewBuffer[i].eMail, fpBuffer[i].eMail);
- pNewBuffer[i].DELETED=false;
- pNewBuffer[i]=fpBuffer[i];
- }
- //delete [] fpBuffer;
- bufferSize+=resizeStep;
- fpBuffer = pNewBuffer;
- return fpBuffer;
- }
- Entry* resizeArrayNoCopy (Entry*& fpBuffer, size_t& bufferSize,const size_t resizeStep )
- {
- Entry* pNewBuffer=new Entry[bufferSize+resizeStep];
- if(pNewBuffer == NULL)
- {
- cout<<"Allocation failed!"<<endl;
- return NULL;
- }
- delete[] fpBuffer;
- fpBuffer=pNewBuffer;
- bufferSize+=resizeStep;
- return fpBuffer;
- }
- void saveFile (string filename, Entry* fpArray, int elementsCount)
- {
- if (fpArray==NULL)
- {
- cout<<"You need to open a file first!"<<endl;
- return;
- }
- ofstream file(filename.c_str(),ios::binary);
- if(!file.is_open())
- {
- cout << "Cannot open "<<filename<<"!";
- return;
- }
- file.seekp(0, ios::beg);
- for (int i=0; i<elementsCount; i++)
- {
- file.write((char*)&fpArray[i],sizeof(fpArray[i]));
- }
- file.close();
- }
- void saveFileAs (string filename, Entry* fpArray, int elementsCount)
- {
- char option;
- string saveAsFilename;
- if (fpArray==NULL)
- {
- cout<<"You need to open a file first!"<<endl;
- return;
- }
- cout<<"Enter a filename: ";
- cin>>saveAsFilename;
- ifstream file(saveAsFilename.c_str(),ios::binary);
- if (file.is_open())
- {
- cout<<endl<<"This file already exists, do you want to overwrite it?"<<endl;
- cout<<"Type 'y' to overwrite or anything else to return to main menu: ";
- cin>>option;
- if (option=='y'||option=='Y')
- {
- file.close();
- ofstream file2(saveAsFilename.c_str(),ios::binary);
- file2.seekp(0, ios::beg);
- for (int i=0; i<elementsCount; i++)
- {
- file2.write((char*)&fpArray[i],sizeof(fpArray[i]));
- }
- file2.close();
- }
- else
- {
- cout<<"Returning to main menu."<<endl;
- return;
- }
- }
- else
- {
- file.close();
- ofstream file2(saveAsFilename.c_str(),ios::binary);
- file2.seekp(0, ios::beg);
- for (int i=0; i<elementsCount; i++)
- {
- file2.write((char*)&fpArray[i],sizeof(fpArray[i]));
- }
- file2.close();
- }
- }
- void searchEntry (Entry* fpArray, int elemCount)
- {
- char search[40];
- cout<<endl<<"Search for: ";
- cin>>search;
- for(int i=0; i<elemCount; i++)
- {
- if (strstr((char*)fpArray[i].nameFirst, (char*)search)!=NULL
- || strstr((char*)fpArray[i].nameLast, (char*)search)!=NULL
- || strstr((char*)fpArray[i].eMail, (char*)search)!=NULL
- || strstr((char*)fpArray[i].phoneNumber, (char*)search)!=NULL)
- {
- cout<<endl;
- cout<<"First name: ";
- cout<<fpArray[i].nameFirst<<endl;
- cout<<"Last name: ";
- cout<<fpArray[i].nameLast<<endl;
- cout<<"Phone number: ";
- cout<<fpArray[i].phoneNumber<<endl;
- cout<<"E-Mail: ";
- cout<<fpArray[i].eMail<<endl<<endl;
- }
- }
- cout<<endl;
- }
- void exportTextFile (Entry* fpArray, int elemCount)
- {
- if (fpArray==NULL)
- {
- cout<<"You need to open a file first!"<<endl;
- return;
- }
- int option;
- char fileExport[20];
- cout<<"Enter filename to export to: ";
- cin>>fileExport;
- cout<<"How do you want to sort your entries? (enter 0 to return to main menu)"<<endl;
- cout<<"[1] By first name"<<endl;
- cout<<"[2] By last name"<<endl;
- cout<<"[3] By phone number"<<endl;
- cout<<"[4] By e-Mail address "<<endl;
- cout<<"Choice: ";
- cin>>option;
- while (option>4 || option<0 || !option )
- {
- cout<<"Wrong input or entry number!"<<endl;
- cin.sync();
- cin.clear();
- cin.ignore();
- cin>>option;
- }
- switch (option)
- {
- case 1 :
- {
- Entry temp;
- for(int i = 0; i < elemCount; i++)
- {
- for (int k=i; k<elemCount; k++)
- {
- if(strcmp(fpArray[i].nameFirst,fpArray[k].nameFirst) > 0)
- {
- temp = fpArray[i];
- fpArray[i] = fpArray[k];
- fpArray[k] = temp;
- }
- exportText (fileExport, elemCount, fpArray);
- }
- }
- }
- break;
- case 2 :
- {
- Entry temp;
- for(int i = 0; i < elemCount; i++)
- {
- for (int k=i; k<elemCount; k++)
- {
- if(strcmp(fpArray[i].nameLast,fpArray[k].nameLast) > 0)
- {
- temp = fpArray[i];
- fpArray[i] = fpArray[k];
- fpArray[k] = temp;
- }
- exportText (fileExport, elemCount, fpArray);
- }
- }
- }
- break;
- case 3 :
- {
- Entry temp;
- for(int i = 0; i < elemCount; i++)
- {
- for (int k=i; k<elemCount; k++)
- {
- if(strcmp(fpArray[i].phoneNumber,fpArray[k].phoneNumber) > 0)
- {
- temp = fpArray[i];
- fpArray[i] = fpArray[k];
- fpArray[k] = temp;
- }
- exportText (fileExport, elemCount, fpArray);
- }
- }
- }
- break;
- case 4 :
- {
- Entry temp;
- for(int i = 0; i < elemCount; i++)
- {
- for (int k=i; k<elemCount; k++)
- {
- if(strcmp(fpArray[i].eMail,fpArray[k].eMail) > 0)
- {
- temp = fpArray[i];
- fpArray[i] = fpArray[k];
- fpArray[k] = temp;
- }
- exportText (fileExport, elemCount, fpArray);
- }
- }
- }
- break;
- }
- }
- void exportText (string fileExport, int elemCount, Entry* fpArray)
- {
- ofstream file(fileExport.c_str());
- file.seekp(0, ios::beg);
- for (int k=0; k<elemCount; k++)
- {
- file<<k+1<<"."<<endl;
- file<<"First name: "<<fpArray[k].nameFirst<<endl;
- file<<"Last name: "<<fpArray[k].nameLast<<endl;
- file<<"Phone number: "<<fpArray[k].phoneNumber<<endl;
- file<<"E-mail: "<<fpArray[k].eMail<<endl;
- }
- file.close();
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement