1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
char books[100][50];
void viewAll();
void addEntry();
void removeEntry(int lenght);
void bubSort(int length);
int binarySearch(int size, char key[50]);
void editEntry(int length);
char UnknownNullValue;
int main()
{
char subject[30];
char fname[30];
bool flag = true;
int j=0;
//char firstChar;
int choice=-1;
cout << "Enter the subject for the books." << endl;
cin.get(subject,26);
strcpy(fname,subject);
ifstream infile;
infile.open("ForStuff.txt",ios::in);
infile >> UnknownNullValue;
infile.close();
infile.open(strcat(fname,".txt"),ios::in);
cout << "What do you want to do with the list of " << subject << " books?" << endl;
for (int i = 0; i<100;i++)
{
infile.get(books[i],50);
}
//infile >> firstChar;
if (isalnum(books[0][0])||(books[0][0] == UnknownNullValue))
cout << "(There are no previous entries in the file)" << endl;
//infile.close();
//infile.open(fname,ios::in);
cout << "1 to view all entries" << endl << "2 to add an entry" << endl << "3 to remove an entry" << endl << "4 to edit an entry" << endl;
cin >> choice;
int k;
for( k = 0;k<100;k++)
{
if(isalnum(books[k][0])||(books[k][0] == UnknownNullValue))
break;
}
bubSort(k);
switch(choice)
{
case 1:
viewAll();
break;
case 2:
addEntry();
break;
case 3:
removeEntry(k);
break;
case 4:
editEntry(k);
break;
}
infile.close();
bubSort(k);
ofstream outfile;
outfile.open(strcat(fname,".txt"),ios::out);
for(int j=0;j<100;j++)
outfile << books[j];
return 0;
}
void viewAll()
{
for (int i=0;i<100;i++)
{
if (isalnum(books[i][0])||(books[0][0] == UnknownNullValue))
break;
cout << books[i] << endl;
}
}
void addEntry()
{
bool flag = true;
char bookNum[10],nameAssigned[40];
int i;
for (i=0;i<100;i++)
{
if (isalnum(books[i][0])||(books[i][0] == UnknownNullValue))
{
flag = false;
break;
}
}
if (flag)
{
cout << "This array is already full, please remove or edit an entry" << endl;
return;
}
cout << "Enter the book number" << endl;
cin.get(bookNum,10);
cin.ignore(80,'\n');
cout << "Enter the name of the person the book is assigned to" << endl;
cin.get(nameAssigned,40);
strcpy(books[i],bookNum);
int j;
for(j = 0;j<10;j++)
{
if(books[i][j]=='\0')
break;
}
books[i][j]=' ';
j++;
for(int k = 0;k<40;k++)
{
books[i][j+k] = nameAssigned[k];
}
cout << books[i] << " was successfully assigned" << endl;
}
void removeEntry(int length)
{
char bookNum[10];
int i;
cout << "What book number do you want to remove" << endl;
cin.get(bookNum,10);
i = binarySearch(length,bookNum);
if (i==-1)
{
cout << "No entries found with that number" << endl;
return;
}
for (int j = 0;j<50;j++)
books[i][j]=NULL;
cout << "Entry removed." << endl;
}
void bubSort(int length)
{
char temp[50];
for(int i=0; i<=length; i++)
{
for(int j=0; j<length; j++)
{
//Swapping element in if statement
if(strcmp(books[j],books[j+1])>0)
{
strcpy(temp,books[j]);
strcpy(books[j],books[j+1]);
strcpy(books[j+1],temp);
}
}
}
}
int binarySearch(int size, char key[10])
{
int start=1, end=size;
int mid=(start+end)/2;
char nums[100][10];
for (int i = 0;i<100;i++)
{
int j;
for ( j =0;j<=10;j++)
if(books[i][j] == ' ')
break;
int k;
for( k=0;k<j;k++)
nums[i][k]=books[i][k];
nums[i][k+1]='\0';
}
while(start<=end&&strcmp(nums[mid],key)!=0)
{
if(strcmp(nums[mid],key)<0)
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}// While Loop End
if(strcmp(nums[mid],key)==0)
return mid; //Returning
else
return -1;//Returning
}// binarySearch Function Ends Here
void editEntry(int length)
{
char bookNum[10];
int i;
char newName[40];
cout << "What book number do you want to edit" << endl;
cin.get(bookNum,10);
cin.ignore(80,'\n');
i = binarySearch(length,bookNum);
if (i==-1)
{
cout << "No entries found with that number" << endl;
return;
}
cout << "The current entry is: " << books[i] << endl << "What new name do you want to put in?" << endl;
cin.get(newName,40);
int j;
for(j = 0;j<10;j++)
{
if(books[i][j]=='\0')
break;
}
books[i][j]=' ';
j++;
for(int k = 0;k<40;k++)
{
books[i][j+k] = newName[k];
}
cout << books[i] << " was successfully edited" << endl;
}
|