void Search( vector<Person> & list, Person p ) { // Search for members in list based on template person p vector<Person> SearchList; // Create search result vector for( unsigned int x = 0; x < list.size(); x++) { // Search each entry of list bool add = true; // We say the entry should be added by default then exclude it based on the template person if( p.getName() != "" ) if( !list[x].getName().contains( p.getName(), Qt::CaseInsensitive ) ) add = false; if( p.getGender() != 0 ) if( !(list[x].getGender() == p.getGender() ) ) add = false; if( p.getBirth().isValid() ) if( !(list[x].getBirth() == p.getBirth() ) ) add = false; if( p.getDeath().isValid() ) if( !(list[x].getDeath() == p.getDeath() ) ) add = false; if(add) SearchList.push_back( list[x] ); } displayPerson( SearchList ); // Display search results }
vector<int> addRelation(vector<Person>& p, vector<Computer>& c) { QTextStream in(stdin); vector<int> relation; unsigned int x, y; QString value; displayPerson(p); do{ cout <<"Select a person: "; cin.ignore(); value = in.readLine(); x = value.toInt(); if( x > p.size() || x < 1 ) cout << "Please choose a number from 1 to " << p.size() << endl; else relation.push_back(x); } while( x > p.size() || x < 1 ); displayComputer(c); do{ cout << "Select a computer to connect to the chosen person: "; value = in.readLine(); y = value.toInt(); if( y > c.size() || y < 1 ) cout << "Please choose a number from 1 to " << c.size() << endl; else relation.push_back(y); } while( y > c.size() || y < 1 ); return relation; }
vector<int> removeRelation(vector<Person>& p, vector<Computer>& c) { QTextStream in(stdin); vector<int> relation; unsigned int x, y; QString value; displayPerson(p); do{ cout <<"Select a person: "; value = in.readLine(); x = value.toInt(); if(x > p.size() || x < 1) cout << "Please choose a number from 1 to " << p.size() << endl; else relation.push_back(x - 1); } while(x > p.size() || x < 1); Person pers = p[x - 1]; vector<Computer> comps; for(int i = 0; i < pers.getSize(); i++){ for(unsigned int j = 0; j < c.size(); j++){ if(pers.getComputer(i) == c[j].getId()){ comps.push_back(c[j]); } } } displayComputer(comps); do{ cout << "Select a connected computer to remove from chosen person: "; value = in.readLine(); y = value.toInt(); if(y > comps.size() || y < 1) cout << "Please choose a number from 1 to " << comps.size() << endl; else{ Computer comp = comps[y - 1]; int z = -1; while( c[z].getId() != comp.getId() ) { z++; if( c[z].getId() == comp.getId() ) relation.push_back(z); } } } while( y > c.size() || y < 1 ); return relation; }
int main(int argc, char * argv[],char * envp[]) { char buffer[512]=""; int personDB=open("myDB",O_RDONLY); struct stat fileInfo; bool dirtyFlag=false; char response[10]; int nRecs,i; struct PERSON *baseAddress; if(!personDB) { perror("missing myDB database file"); exit(1); } //Determine the number of records in the file - version 1 fprintf(stdout,"There are %d records in myDB\n",lseek(personDB,0,SEEK_END)/(sizeof(struct PERSON))); lseek(personDB,0,SEEK_SET); //reset the file pointer to the beginning of the file //2nd method of determining # of records stat("myDB",&fileInfo); fprintf(stdout,"There are %d records in myDB\n",nRecs=(fileInfo.st_size/(sizeof (struct PERSON)))); struct PERSON fileInMemory[nRecs]; read(personDB,fileInMemory,nRecs); //Load the personDB into memory close(personDB); while(true) { prompt(CLEARSCREEN); goto(0,0); fprintf(stdout,"1. Sort by name\n"); fprintf(stdout,"2. Sort by age\n"); fprintf(stdout,"3. Sort by rank\n"); fprintf(stdout,"4. Display records\n"); fprintf(stdout,"5. Save to file\n"); fprintf(stdout,"6. Quit\n\n\n"); prompt("Select an action: "); fgets(response,sizeof response,stdin); __fpurge(stdin); //Use this for clearing an input buffer switch(atoi(response)) { case 1: qsort(fileInMemory,nRecs,sizeof (struct PERSON),sortByName); dirtyFlag=true; break; case 2: qsort(fileInMemory,nRecs,sizeof (struct PERSON),sortByAge); dirtyFlag=true; break; case 3: qsort(fileInMemory,nRecs,sizeof (struct PERSON),sortByRank); break; case 4: //Display the records in memory for(i=0; i<nRecs;i++) displayPerson(fileInMemory[i]); prompt("Press return to continue"); fgets(response,sizeof response,stdin); __fpurge(stdin); break; case 5: if(dirtyFlag) { personDB=open("myDB", O_WRONLY); write(personDB,fileInMemory,strlen(buffer)); dirtyFlag=false; } break; case 6: if(dirtyFlag) //exiting the program { prompt("File has changed - proceed or cancel (P/C)"); fgets(response,sizeof response,stdin); __fpurge(stdin); if(toupper(response[0])=='P') exit(0); } } //end of switch } //end of while return 0; }