Advertisement
Guest User

Project

a guest
May 28th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream.>
  3. #include <cstring>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. struct Entry
  8. {
  9.     char nameFirst[20];
  10.     char nameLast[20];
  11.     char phoneNumber[20];
  12.     char eMail[40];
  13.     bool DELETED;
  14.  
  15. };
  16.  
  17. int getChoice();
  18. Entry* openFile(Entry* fpBuffer,size_t fBufferSize, int &eleCount,const size_t resizeStep, string &filename);
  19. void listEntries(Entry* fpBuffer, int elemCount);
  20. void changeEntry (Entry* fpBuffer, int elemCount);
  21. void deleteEntry (Entry* fpBuffer, int &elemCount);
  22. void addEntry (Entry* fpBuffer, int &elemCount,size_t &fBufferSize, size_t resizeStep);
  23. Entry* resizeArrayCopy(Entry*& fpBuffer,const size_t resizeStep,size_t &bufferSize);
  24. Entry* resizeArrayNoCopy (Entry*& fpBuffer, size_t &bufferSize,const size_t resizeStep);
  25. void saveFile (string filename, Entry* fpArray, int elementsCount);
  26. void saveFileAs (string filename, Entry* fpArray, int elementsCount);
  27. void searchEntry (Entry* fpArray, int elemCount);
  28. void exportTextFile (Entry* fpArray, int elemCount);
  29. void exportText (string fileExport, int elemCount, Entry* fpArray);
  30.  
  31. int main()
  32. {
  33.     Entry* pArray=NULL;
  34.     Entry* sortArray=NULL;
  35.     string filename;
  36.     size_t bufferSize=4;
  37.     const size_t resizeStep=15;
  38.     int choice;
  39.     int elementsCounter;
  40.     choice=getChoice();
  41.     while (choice!=10)
  42.     {
  43.  
  44.         switch (choice)
  45.         {
  46.         case 1 :
  47.             pArray = openFile(pArray, bufferSize, elementsCounter, resizeStep,filename);
  48.             break;
  49.         case 2 :
  50.             saveFile (filename, pArray, elementsCounter);
  51.             break;
  52.         case 3 :
  53.             saveFileAs (filename, pArray, elementsCounter);
  54.             break;
  55.         case 4 :
  56.             addEntry (pArray, elementsCounter, bufferSize, resizeStep);
  57.             break;
  58.         case 5 :
  59.             changeEntry (pArray, elementsCounter);
  60.             break;
  61.         case 6 :
  62.             deleteEntry (pArray, elementsCounter);
  63.             break;
  64.         case 7 :
  65.             searchEntry (pArray, elementsCounter);
  66.             break;
  67.         case 8 :
  68.             listEntries(pArray, elementsCounter);
  69.             break;
  70.         case 9:
  71.         {
  72.             sortArray=new Entry[bufferSize];
  73.             for(size_t i = 0; i < bufferSize; i++)
  74.             {
  75.                 strcpy(sortArray[i].nameFirst, pArray[i].nameFirst);
  76.                 strcpy(sortArray[i].nameLast, pArray[i].nameLast);
  77.                 strcpy(sortArray[i].phoneNumber, pArray[i].phoneNumber);
  78.                 strcpy(sortArray[i].eMail, pArray[i].eMail);
  79.                 sortArray[i].DELETED=false;
  80.             }
  81.  
  82.  
  83.             exportTextFile (sortArray, elementsCounter);
  84.         }
  85.         break;
  86.         }
  87.         choice=getChoice();
  88.     }
  89.     cout<<"Program will now exit.";
  90.     system ("pause");
  91.     return 0;
  92. }
  93.  
  94. int getChoice()
  95. {
  96.     int option;
  97.     cout<<"[1] Open file"<<endl;
  98.     cout<<"[2] Save file"<<endl;
  99.     cout<<"[3] Save file as.."<<endl;
  100.     cout<<"[4] Add entry"<<endl;
  101.     cout<<"[5] Change entry"<<endl;
  102.     cout<<"[6] Delete entry"<<endl;
  103.     cout<<"[7] Search entry"<<endl;
  104.     cout<<"[8] Show all entries"<<endl;
  105.     cout<<"[9] Export to text file"<<endl;
  106.     cout<<"[10] Exit"<<endl;
  107.     cout<<"What would you like to do? ";
  108.     cin>>option;
  109.     while (option<0 || option>10 || !option )
  110.     {
  111.         //cout<<"Wrong input!"<<endl;
  112.         //cout<<"What would you like to do? ";
  113.         //cin.sync();
  114.         cin.clear();
  115.         cin.ignore();
  116.         cin>>option;
  117.     }
  118.     return option;
  119. }
  120. Entry* openFile(Entry* fpBuffer,size_t fBufferSize, int &eleCount,const size_t resizeStep,string &filename)
  121. {
  122.     delete[] fpBuffer;
  123.     Entry newEntry;
  124.     Entry* pBuffer=new Entry[fBufferSize];
  125.     size_t i=0;
  126.     eleCount=0;
  127.     cout<<"Enter filename: ";
  128.     cin>>filename;
  129.     ifstream file(filename.c_str(),ios::binary);
  130.     if(!file.is_open())
  131.     {
  132.         cout << "Cannot open "<<filename<<"!";
  133.         return NULL;
  134.     }
  135.     while(file.read((char*)&newEntry, sizeof(newEntry)))
  136.     {
  137.  
  138.         i++;
  139.         if (i>=fBufferSize)
  140.         {
  141.             pBuffer=resizeArrayNoCopy(pBuffer,fBufferSize, resizeStep);
  142.         }
  143.     }
  144.     file.close();
  145.     ifstream file2(filename.c_str(),ios::binary);
  146.     while(file2.read((char*)&newEntry, sizeof(newEntry)))
  147.     {
  148.  
  149.         pBuffer[eleCount]=newEntry;
  150.         eleCount++;
  151.     }
  152.     cout<<"A total of "<<eleCount<<" elements read."<<endl;
  153.     file2.close();
  154.     return pBuffer;
  155. }
  156.  
  157. void listEntries(Entry* fpBuffer, int elemCount)
  158. {
  159.     if (elemCount==0)
  160.     {
  161.         cout<<"There are no entries loaded into memory."<<endl;
  162.         return;
  163.     }
  164.     for (int i=0; i<elemCount; i++)
  165.  
  166.  
  167.     {
  168.         cout<<i+1<<"."<<endl;
  169.         cout<<"First name: ";
  170.         cout<<fpBuffer[i].nameFirst<<endl;
  171.         cout<<"Last name: ";
  172.         cout<<fpBuffer[i].nameLast<<endl;
  173.         cout<<"Phone number: ";
  174.         cout<<fpBuffer[i].phoneNumber<<endl;
  175.         cout<<"E-Mail: ";
  176.         cout<<fpBuffer[i].eMail<<endl<<endl;
  177.     }
  178. }
  179.  
  180. void addEntry (Entry* fpBuffer, int &elemCount,size_t &fBufferSize,const size_t resizeStep)
  181. {
  182.     if (elemCount==fBufferSize)
  183.     {
  184.         fpBuffer = resizeArrayCopy(fpBuffer, resizeStep, fBufferSize);
  185.         if (fpBuffer!=NULL)
  186.         {
  187.             cout<<"Adding entry number "<<elemCount+1<<endl;
  188.             cout<<"First name: ";
  189.             cin.sync();
  190.             cin>>fpBuffer[elemCount].nameFirst;
  191.             cout<<"Last name: ";
  192.             cin>>fpBuffer[elemCount].nameLast;
  193.             cout<<"Phone number: ";
  194.             cin>>fpBuffer[elemCount].phoneNumber;
  195.             cout<<"E-Mail: ";
  196.             cin>>fpBuffer[elemCount].eMail;
  197.             cout<<"Entry added successfully!"<<endl;
  198.             elemCount++;
  199.  
  200.         }
  201.         else
  202.         {
  203.             cout<<"Allocation failed!"<<endl;
  204.             return;
  205.         }
  206.     }
  207.  
  208.     else
  209.     {
  210.         cout<<"Adding entry number "<<elemCount+1<<endl;
  211.         cout<<"First name: ";
  212.         cin>>fpBuffer[elemCount].nameFirst;
  213.         cout<<"Last name: ";
  214.         cin>>fpBuffer[elemCount].nameLast;
  215.         cout<<"Phone number: ";
  216.         cin>>fpBuffer[elemCount].phoneNumber;
  217.         cout<<"E-Mail: ";
  218.         cin>>fpBuffer[elemCount].eMail;
  219.         cout<<"Entry added successfully!"<<endl;
  220.         elemCount++;
  221.  
  222.     }
  223. }
  224.  
  225.  
  226.  
  227. void changeEntry (Entry* fpBuffer, int elemCount)
  228. {
  229.     int changeOption;
  230.     cout<<endl<<"Which entry would you like to change? (press 0 for main menu)"<<endl;
  231.     cin>>changeOption;
  232.     if (changeOption==0) return;
  233.     while (changeOption>elemCount || changeOption<0 || !changeOption )
  234.     {
  235.         cout<<"Wrong input or entry number!"<<endl;
  236.         cin.sync();
  237.         cin.clear();
  238.         cin.ignore();
  239.         cin>>changeOption;
  240.     }
  241.     cout<<"Changing entry number "<<changeOption<<endl;
  242.     cout<<"First name: ";
  243.     cin>>fpBuffer[changeOption-1].nameFirst;
  244.     cout<<"Last name: ";
  245.     cin>>fpBuffer[changeOption-1].nameLast;
  246.     cout<<"Phone number: ";
  247.     cin>>fpBuffer[changeOption-1].phoneNumber;
  248.     cout<<"E-Mail: ";
  249.     cin>>fpBuffer[changeOption-1].eMail;
  250.     cout<<"Entry changed successfully!"<<endl;
  251. }
  252.  
  253. void deleteEntry (Entry* fpBuffer, int &elemCount)
  254. {
  255.     int deleteOption;
  256.     cout<<"Which entry would you like to delete? (press 0 for main menu)"<<endl;
  257.     cin>>deleteOption;
  258.     while (deleteOption>elemCount || deleteOption<0 || !deleteOption)
  259.     {
  260.         cin.clear();
  261.         cin.ignore();
  262.         cin>>deleteOption;
  263.     }
  264.     if (deleteOption==0)
  265.         return;
  266.     for (int i=deleteOption; i<=elemCount; i++ )
  267.     {
  268.         strcpy(fpBuffer[i-1].nameFirst, fpBuffer[i].nameFirst);
  269.         strcpy(fpBuffer[i-1].nameLast, fpBuffer[i].nameLast);
  270.         strcpy(fpBuffer[i-1].phoneNumber, fpBuffer[i].phoneNumber);
  271.         strcpy(fpBuffer[i-1].eMail, fpBuffer[i].eMail);
  272.     }
  273.     elemCount--;
  274.     return;
  275. }
  276.  
  277.  
  278.  
  279. Entry* resizeArrayCopy(Entry*& fpBuffer, const size_t resizeStep, size_t &bufferSize)
  280. {
  281.     Entry* pNewBuffer = new Entry[bufferSize + resizeStep];
  282.  
  283.     if(pNewBuffer == NULL)
  284.     {
  285.         return NULL;
  286.     }
  287.  
  288.     for(size_t i = 0; i < bufferSize; i++)
  289.     {
  290.         //strcpy(pNewBuffer[i].nameFirst, fpBuffer[i].nameFirst);
  291.         //strcpy(pNewBuffer[i].nameLast, fpBuffer[i].nameLast);
  292.         //strcpy(pNewBuffer[i].phoneNumber, fpBuffer[i].phoneNumber);
  293.        // strcpy(pNewBuffer[i].eMail, fpBuffer[i].eMail);
  294.         pNewBuffer[i].DELETED=false;
  295.         pNewBuffer[i]=fpBuffer[i];
  296.     }
  297.  
  298.     //delete [] fpBuffer;
  299.     bufferSize+=resizeStep;
  300.     fpBuffer = pNewBuffer;
  301.     return fpBuffer;
  302. }
  303.  
  304.  
  305. Entry* resizeArrayNoCopy (Entry*& fpBuffer, size_t& bufferSize,const size_t resizeStep )
  306. {
  307.     Entry* pNewBuffer=new Entry[bufferSize+resizeStep];
  308.     if(pNewBuffer == NULL)
  309.     {
  310.         cout<<"Allocation failed!"<<endl;
  311.         return NULL;
  312.     }
  313.     delete[] fpBuffer;
  314.     fpBuffer=pNewBuffer;
  315.     bufferSize+=resizeStep;
  316.     return fpBuffer;
  317. }
  318. void saveFile (string filename, Entry* fpArray, int elementsCount)
  319. {
  320.     if (fpArray==NULL)
  321.     {
  322.         cout<<"You need to open a file first!"<<endl;
  323.         return;
  324.     }
  325.     ofstream file(filename.c_str(),ios::binary);
  326.     if(!file.is_open())
  327.     {
  328.         cout << "Cannot open "<<filename<<"!";
  329.         return;
  330.     }
  331.     file.seekp(0, ios::beg);
  332.     for (int i=0; i<elementsCount; i++)
  333.     {
  334.         file.write((char*)&fpArray[i],sizeof(fpArray[i]));
  335.     }
  336.  
  337.     file.close();
  338. }
  339.  
  340. void saveFileAs (string filename, Entry* fpArray, int elementsCount)
  341. {
  342.     char option;
  343.     string saveAsFilename;
  344.     if (fpArray==NULL)
  345.     {
  346.         cout<<"You need to open a file first!"<<endl;
  347.         return;
  348.     }
  349.     cout<<"Enter a filename: ";
  350.     cin>>saveAsFilename;
  351.     ifstream file(saveAsFilename.c_str(),ios::binary);
  352.     if (file.is_open())
  353.     {
  354.         cout<<endl<<"This file already exists, do you want to overwrite it?"<<endl;
  355.         cout<<"Type 'y' to overwrite or anything else to return to main menu: ";
  356.         cin>>option;
  357.         if (option=='y'||option=='Y')
  358.         {
  359.             file.close();
  360.             ofstream file2(saveAsFilename.c_str(),ios::binary);
  361.             file2.seekp(0, ios::beg);
  362.             for (int i=0; i<elementsCount; i++)
  363.             {
  364.                 file2.write((char*)&fpArray[i],sizeof(fpArray[i]));
  365.             }
  366.  
  367.             file2.close();
  368.  
  369.         }
  370.         else
  371.         {
  372.             cout<<"Returning to main menu."<<endl;
  373.             return;
  374.         }
  375.  
  376.     }
  377.     else
  378.     {
  379.         file.close();
  380.         ofstream file2(saveAsFilename.c_str(),ios::binary);
  381.         file2.seekp(0, ios::beg);
  382.         for (int i=0; i<elementsCount; i++)
  383.         {
  384.             file2.write((char*)&fpArray[i],sizeof(fpArray[i]));
  385.         }
  386.  
  387.         file2.close();
  388.     }
  389. }
  390. void searchEntry (Entry* fpArray, int elemCount)
  391. {
  392.     char search[40];
  393.     cout<<endl<<"Search for: ";
  394.     cin>>search;
  395.     for(int i=0; i<elemCount; i++)
  396.     {
  397.         if (strstr((char*)fpArray[i].nameFirst, (char*)search)!=NULL
  398.                 || strstr((char*)fpArray[i].nameLast, (char*)search)!=NULL
  399.                 || strstr((char*)fpArray[i].eMail, (char*)search)!=NULL
  400.                 || strstr((char*)fpArray[i].phoneNumber, (char*)search)!=NULL)
  401.         {
  402.             cout<<endl;
  403.             cout<<"First name: ";
  404.             cout<<fpArray[i].nameFirst<<endl;
  405.             cout<<"Last name: ";
  406.             cout<<fpArray[i].nameLast<<endl;
  407.             cout<<"Phone number: ";
  408.             cout<<fpArray[i].phoneNumber<<endl;
  409.             cout<<"E-Mail: ";
  410.             cout<<fpArray[i].eMail<<endl<<endl;
  411.         }
  412.     }
  413.     cout<<endl;
  414. }
  415.  
  416.  
  417. void exportTextFile (Entry* fpArray, int elemCount)
  418. {
  419.     if (fpArray==NULL)
  420.     {
  421.         cout<<"You need to open a file first!"<<endl;
  422.         return;
  423.     }
  424.     int option;
  425.     char fileExport[20];
  426.     cout<<"Enter filename to export to: ";
  427.     cin>>fileExport;
  428.     cout<<"How do you want to sort your entries? (enter 0 to return to main menu)"<<endl;
  429.     cout<<"[1] By first name"<<endl;
  430.     cout<<"[2] By last name"<<endl;
  431.     cout<<"[3] By phone number"<<endl;
  432.     cout<<"[4] By e-Mail address "<<endl;
  433.     cout<<"Choice: ";
  434.     cin>>option;
  435.     while (option>4 || option<0 || !option )
  436.     {
  437.         cout<<"Wrong input or entry number!"<<endl;
  438.         cin.sync();
  439.         cin.clear();
  440.         cin.ignore();
  441.         cin>>option;
  442.     }
  443.  
  444.     switch (option)
  445.  
  446.     {
  447.     case 1 :
  448.     {
  449.         Entry temp;
  450.         for(int i = 0; i < elemCount; i++)
  451.         {
  452.             for (int k=i; k<elemCount; k++)
  453.             {
  454.                 if(strcmp(fpArray[i].nameFirst,fpArray[k].nameFirst) > 0)
  455.                 {
  456.                     temp = fpArray[i];
  457.                     fpArray[i] = fpArray[k];
  458.                     fpArray[k] = temp;
  459.                 }
  460.                 exportText (fileExport, elemCount, fpArray);
  461.             }
  462.         }
  463.     }
  464.     break;
  465.     case 2 :
  466.     {
  467.         Entry temp;
  468.         for(int i = 0; i < elemCount; i++)
  469.         {
  470.             for (int k=i; k<elemCount; k++)
  471.             {
  472.                 if(strcmp(fpArray[i].nameLast,fpArray[k].nameLast) > 0)
  473.                 {
  474.                     temp = fpArray[i];
  475.                     fpArray[i] = fpArray[k];
  476.                     fpArray[k] = temp;
  477.                 }
  478.                 exportText (fileExport, elemCount, fpArray);
  479.             }
  480.         }
  481.     }
  482.     break;
  483.     case 3 :
  484.     {
  485.         Entry temp;
  486.         for(int i = 0; i < elemCount; i++)
  487.         {
  488.             for (int k=i; k<elemCount; k++)
  489.             {
  490.                 if(strcmp(fpArray[i].phoneNumber,fpArray[k].phoneNumber) > 0)
  491.                 {
  492.                     temp = fpArray[i];
  493.                     fpArray[i] = fpArray[k];
  494.                     fpArray[k] = temp;
  495.                 }
  496.                 exportText (fileExport, elemCount, fpArray);
  497.             }
  498.         }
  499.     }
  500.     break;
  501.     case 4 :
  502.     {
  503.         Entry temp;
  504.         for(int i = 0; i < elemCount; i++)
  505.         {
  506.             for (int k=i; k<elemCount; k++)
  507.             {
  508.                 if(strcmp(fpArray[i].eMail,fpArray[k].eMail) > 0)
  509.                 {
  510.                     temp = fpArray[i];
  511.                     fpArray[i] = fpArray[k];
  512.                     fpArray[k] = temp;
  513.                 }
  514.                 exportText (fileExport, elemCount, fpArray);
  515.             }
  516.         }
  517.     }
  518.     break;
  519.     }
  520.  
  521. }
  522.  
  523. void exportText (string fileExport, int elemCount, Entry* fpArray)
  524. {
  525.     ofstream file(fileExport.c_str());
  526.     file.seekp(0, ios::beg);
  527.     for (int k=0; k<elemCount; k++)
  528.     {
  529.         file<<k+1<<"."<<endl;
  530.         file<<"First name: "<<fpArray[k].nameFirst<<endl;
  531.         file<<"Last name: "<<fpArray[k].nameLast<<endl;
  532.         file<<"Phone number: "<<fpArray[k].phoneNumber<<endl;
  533.         file<<"E-mail: "<<fpArray[k].eMail<<endl;
  534.  
  535.     }
  536.     file.close();
  537.     return;
  538.  
  539. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement