//listing.h
#pragma once
#include "Person.h"
#include <vector>
using namespace std;
enum SortStatus {unsorted,sortedByName,sortedByCode};
class listing
{
public:
unsigned int size() { return vectorOfPerson.size();}
Person getPerson(int i);
void clear(){sortStatus=unsorted;vectorOfPerson.clear();}
void customerList();
void addNewListing(string n, string c, string p, string a);
private:
SortStatus sortStatus;
vector<Person> vectorOfPerson;
};
//listing.cpp
#include "listing.h"
#include <algorithm>
#include <map>
void listing::addNewListing(string n, string c, string p, string a)
{
map<string,Person> phoneData;
Person data(n,c,p,a);
phoneData[n]=data;
}
Person listing::getPerson(int i)
{
return vectorOfPerson[i];
}
//misc.h #pragma once #include <string> using namespace std; void stringToLower(string & s); void stringToUpper(string & s); string padLeft(string s, char fill, unsigned int size); string padRight(string s,char fill, unsigned int size); string intToString(int x); int stringToInt(string s); string intToDollarString(int x); string randStrings(int numOfChars); int randInt(int lower,int upper);
//misc.cpp
#include "misc.h"
void stringToLower(string & s)
{
for(unsigned int i=0;i<s.length();i++)
{
s[i]=tolower(s[i]);
}
}
void stringToUpper(string & s)
{
for(unsigned int i=0;i<s.length();i++)
{
s[i]=toupper(s[i]);
}
}
string padLeft(string s,char fill, unsigned int size)
{
while(s.length()<size)
{
s= fill + s;
}
return s;
}
string padRight(string s,char fill, unsigned int size)
{
while(s.length()<size)
{
s= s + fill;
}
return s;
}
//All of the following would work fine.
//The first two give warnings. Try them out and read the warnings.
//
//itoa(x,temp,10);
//_itoa(x,temp,10);
//_itoa_s(x,temp,255,10);
//
//The last one is "safe" because it's guaranteed to not go beyond 255 characters.
//In this case it can't happen but it would be safe even if temp was very small array as long as the third parameter was also appropriately small.
string intToString(int x)
{
string result;
char temp[256];
//itoa(x,temp,10);
//_itoa(x,temp,10);
_itoa_s(x,temp,255,10);
result=temp;
return result;
}
int stringToInt(string s)
{
return atoi(s.c_str());
}
string intToDollarString(int x)
{
string result=intToString(x);
result=padLeft(result,'0',3);
result.insert(result.length()-2,".");
return result;
}
//'A' is a char literal for the ascii value of a Capital A. (It's really just a one byte integer.)
//
//Adding a random value between 0 and 25 to the ascii value of A gives you the ascii value of a letter between A and Z.
//
//Play with it.
//
//cout<<(char)('A'+2)<<endl;
//
//prints a C on the screen.
string randStrings(int numOfChars)
{
string result;
for(int i=0;i<numOfChars;i++)
{
result+='A'+rand()%26;
}
return result;
}
int randInt(int lower,int upper)
{
if(upper-lower<RAND_MAX)
{
return (rand()%(upper-lower))+lower;
}
else
{
int r=rand()*RAND_MAX+rand();
return (r % (upper-lower))+lower;
}
}
//phoneBook.h
#pragma
#include "listing.h"
using namespace std;
class phoneBook
{
public:
void run();
void ADD();
void HELP();
void FIND();
void REPORT();
void generateRandItems(int num);
private:
listing Listing;
};
//phoneBook.cpp
#include "phoneBook.h"
#include "misc.h"
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
void phoneBook::run()
{
HELP();
string command=" ";
while((command!="QUIT")&&(command!="8"))
{
cout<<"Enter a command: ";
cin>>command;
if((command=="Add")||(command=="1"))
{
ADD();
}
else if((command=="Find")||(command=="2"))
{
FIND();
}
else if((command=="Report")||(command=="5"))
{
REPORT();
}
else if((command=="TEST")||(command=="7"))
{
generateRandItems(10);
REPORT();
}
else if((command=="QUIT")||(command=="8"))
{
cout <<"Goodbye"<<endl;
}
else if ((command=="HELP")||(command=="0"))
{
HELP();
}
else
{
cout<<"Unrecognized command."<<endl;
}
}
}
void phoneBook::HELP()
{
cout<<"Phone Book Commands"<<endl;
cout<<"----------------------"<<endl;
cout<<" 0) HELP"<<endl;
cout<<" 1) Add"<<endl;
cout<<" 2) Find"<<endl;
cout<<" 5) Report"<<endl;
cout<<" 7) Test"<<endl;
cout<<" 8) Quit"<<endl;
cout<<endl;
}
void phoneBook::ADD()
{
string n;
string c;
string p;
string a;
map<string,Person> phoneData;
cin.ignore();
cout << "Enter your name: ";
getline(cin, n);
cout<<"Enter your category: ";
getline(cin, c);
cout << "Enter your phone number: ";
getline(cin, p);
cout << "Enter your address: ";
getline(cin, a);
Person data(n,c,p,a);
phoneData[p]=data;
}
void phoneBook::FIND()
{
string n;
map<string,Person> phoneData;
cin.ignore();
cout << "Enter a name to find: ";
cin >> n;
map<string,Person>::iterator mapiter;
mapiter=phoneData.begin();
while(mapiter!=phoneData.end())
{
cout<<(*mapiter).first<<" --- "<<(*mapiter).second<<endl;
mapiter++;
}
}
void phoneBook::REPORT()
{
cout<<Person::HeaderString()<<endl;
for(unsigned int i=0;i<Listing.size();i++)
{
cout<< Listing.getPerson(i).ToString()<<endl;
}
}
void phoneBook::generateRandItems(int num)
{
Person gi;
for(int i=0;i<num;i++)
{
gi.randomize();
//Listing.addNewListing(string n, string c, string p, string a);
}
}
//Person.h
//Person.h
#pragma once
#ifndef Person_h
#define Person_h
#include <string>
using namespace std;
enum PersonIndex{byName,byCategory};
class Person
{
public:
static PersonIndex index;
Person(){}
Person(string n,string c,string p,string a);
string ToString();
static string HeaderString();
void randomize();
bool operator >(const Person & rhs) const;
bool operator ==(const Person & rhs) const;
bool operator >=(const Person & rhs) const {return ((*this) > rhs ) || ((*this) == rhs );}
bool operator <(const Person & rhs) const {return !((*this) >= rhs );}
bool operator <=(const Person & rhs) const {return !((*this) > rhs );}
bool operator !=(const Person & rhs) const {return !((*this) == rhs );}
friend ostream& operator << (ostream& ostr, const Person& p);
public:
string name,category,phone,address;
};
ostream& operator << (ostream& ostr, const Person& p);
#endif
//Person.cpp
//Person.cpp
#include "Person.h"
#include "misc.h"
#include <stdlib.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
using namespace std;
PersonIndex Person::index = byName;
Person::Person(string n, string c, string p, string a)
{
name = n;
category = c;
phone = p;
address = a;
}
bool Person::operator >(const Person & rhs) const
{
if(Person::index == byName)
{
return name>rhs.name;
}
else
{
assert(index == byCategory);
return category>rhs.category;
}
}
// Changed so that BST::find is more practical
bool Person::operator ==(const Person & rhs) const
{
return (name==rhs.name);
}
ostream& operator << (ostream& ostr, const Person& p)
{
// a little example of C++ formated output
ostr<<setiosflags(ios::left);
ostr<<setw(10);
ostr << p.name;
ostr<<setw(22);
ostr << p.category;
ostr<<setw(12);
ostr << p.phone;
ostr<<setw(30);
ostr << p.address;
return ostr;
}
string Person::ToString()
{
string result="";
result+=(name,' ',15);
result+=(category, ' ' , 15);
result+=(phone, ' ' , 15);
result+=(address, ' ' , 15);
return result;
}
string Person::HeaderString()
{
char fill='-';
string result="";
result+=padRight("Name",fill,15);
result+=padLeft("Category",fill,10);
result+=padLeft("Phone Number",fill,8);
result+=padLeft("Address",fill,8);
return result;
}
void Person::randomize()
{
name=randStrings(5);
category=randStrings(15);
phone=randInt(7,10);
address=randStrings(255);
}
[/code
[code]
//main.cpp
#include <iostream>
#include <string>
#include "phoneBook.h"
int main()
{
phoneBook myphoneBook;
myphoneBook.run();
return 0;
}
This post has been edited by JackOfAllTrades: 19 November 2011 - 04:56 AM
Reason for edit:: Split into the individual files the code represents

New Topic/Question
Reply



MultiQuote




|