دفترچه تلفن به زبان سي پلاس پلاس
چند مدت پيش در خواست بچه هاي سي فروم براي نوشتن يه دفترچه تلفن رو ديدم كه وقتي برام پيش اومد و بالاخره نوشتمش حالا مي ذارم هر كي خواست استفاده كنه!(در ضمن مشكلي براتون در اين مورد پيش اومد بگيد).
کد:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class PhoneBook
{
public:
PhoneBook();
~PhoneBook();
void ShowInfo();
void SetInfo(const string&,const string&);
void DeleteContact(const string&);
void search(const string&);
void clear();
void sort();
private:
ofstream fout;
ifstream fin;
};
PhoneBook::PhoneBook()
{
fout.open("f:\\PhoneBook.txt",ios::app);
if(!fout)
{
cout <<"file not opened!\n";
return;
}
fin.open("f:\\PhoneBook.txt");
if(!fin)
{
cout <<"file not opened!\n";
return;
}
}//End
PhoneBook::~PhoneBook()
{
fout.close();
fin.close();
}//End
//METHODS=======>
void PhoneBook::ShowInfo()
{
ifstream fin("f:\\PhoneBook.txt");
char ch;
bool test = false;
while(fin.get(ch))
{
if(ch == '\n' && !test)
{
ch = '\t';
test = true;
}
else
if(ch == '\n' && test)
test = false;
cout <<ch;
}
}//End
void PhoneBook::SetInfo(const string& name,const string& number)
{
fout <<name<<"\n"<<number<<endl;
}//End
void PhoneBook::search(const string& name)
{
bool test = false,test2 = false;
string str;
ifstream fin1("f:\\PhoneBook.txt");
while(getline(fin1,str,'\n'))
{
if(str == name)
{
cout <<str<<"\t";
test = true;
test2 = true;
}
else
if(test)
{
cout <<str<<endl;
test = false;
}
}
fin1.close();
if(!test2)
cout <<"this contact not in Phone Book!\n";
}//End
void PhoneBook::clear()
{
ofstream temp("f:\\PhoneBook.txt");
temp.close();
}//End
void PhoneBook::sort()
{
string str,* book,* SortBook;
int offset = 0,i = 0,sb = 0;
ifstream fin1("f:\\PhoneBook.txt");
while(getline(fin1,str,'\n'))
++offset;
fin1.close();
book = new string[offset];
ifstream fin2("f:\\PhoneBook.txt");
while(getline(fin2,str,'\n'))
{
book[i] = str;
++i;
}
fin2.close();
SortBook = new string[offset];
for(int a = 'a',b = 'A';a <= 'z' && b <= 'Z';a++,b++)
for(i = 0;i < (offset-1)&& sb < (offset-1);i++)
if(book[i][0] == a || book[i][0] == b)
{
SortBook[sb] = book[i];
SortBook[++sb] = book[++i];
++sb;
}
ofstream temp("f:\\PhoneBook.txt");
for(i = 0;i < offset;i++)
fout <<SortBook[i]<<endl;
temp.close();
cout <<"Phone book sorted...\n";
}//End
void PhoneBook::DeleteContact(const string& name)
{
int offset = 0,i = 0;
bool test = false,test2 = false;
string str,* book;
ifstream fin1("f:\\PhoneBook.txt");
while(getline(fin1,str,'\n'))
{
if(str == name)
test = true;
else
if(test)
test = false;
else
{
++offset;
test2 = true;
}
}
if(!test2)
{
cout <<"this contact not in Phone Book!\n";
return;
}
fin1.close();
test = false;
book = new string[offset];
ifstream fin2("f:\\PhoneBook.txt");
while(getline(fin2,str,'\n'))
{
if(str == name)
test = true;
else
if(test)
test = false;
else
{
book[i] = str;
++i;
}
}
fin2.close();
ofstream temp("f:\\PhoneBook.txt");
for(i = 0;i < offset;i++)
fout <<book[i]<<endl;
temp.close();
}//End
int main()
{
int choice;
PhoneBook user;
while(true)
{
cout <<"(1)NewContact(2)ShowContacts(3)DeleteContact\n";
cout <<"(4)SearchContact(5)SortName(6)clear(0 to quit):";
cin>>choice;
cin.sync();
switch(choice)
{
case 1:
{
string name,number;
cout <<"\nEnter contact name:";
cin>>name;
cout <<"\nEnter contact number:";
cin>>number;
user.SetInfo(name,number);
break;
}
case 2:
user.ShowInfo();
break;
case 3:
{
string name;
cout <<"\nEnter contact name:";
cin>>name;
user.DeleteContact(name);
break;
}
case 4:
{
string name;
cout <<"\ncontact name:";
cin>>name;
user.search(name);
break;
}
case 5:
user.sort();
break;
case 6:
user.clear();
break;
default: return NULL;
}
}
return 0;
}