یه دفتر چه تلفن با قابلیت ( اضافه کردن ، حذف ، مرتب سازی ، جست و جو ، نمایش ، ذخیره سازی در فایل ) :
// Count.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "string.h"
#include "conio.h"
using namespace std;
struct PhoneBookObject
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
};
void Add(PhoneBookObject PhoneBook[],int &NumObject,bool &Change)
{
cout<<"\nFirst Name : ";
cin.getline(PhoneBook[NumObject].FirstName,19);
cout<<"\nLast Name : ";
cin.getline(PhoneBook[NumObject].LastName,19);
cout<<"\nPhon Number : ";
cin.getline(PhoneBook[NumObject].PhoneNumber,19);
NumObject++;
Change=1;
}
void Sort(PhoneBookObject PhoneBook[],int NumObject,bool &Change)
{
PhoneBookObject Temp;
for(int i=0;i<NumObject;i++)
for(int j=0;j<NumObject-i-1;j++)
if(strncmp(PhoneBook[j].LastName ,PhoneBook[j+1].LastName,19)>0)
{
Temp=PhoneBook[j];
PhoneBook[j]=PhoneBook[j+1];
PhoneBook[j+1]=Temp;
}
Change=1;
}
void Show(PhoneBookObject PhoneBook[],int NumObject)
{
for(int i=0;i<NumObject;i++)
cout<<"\n"<<i<<") "<<PhoneBook[i].FirstName<<" "<<PhoneBook[i].LastName<<"\tTel: "<<PhoneBook[i].PhoneNumber;
}
int Search(PhoneBookObject PhoneBook[],int NumObject)
{
char Input[20];
int Index=-1;
cout<<"\nLast Name : ";
cin.getline(Input,19);
for(int i=0;i<NumObject;i++)
if(strstr(PhoneBook[i].LastName,Input))
{
Index=i;
break;
}
if(Index!=-1)
{
cout<<"\nFirst Name: "<<PhoneBook[Index].FirstName<<"\tLast Name: "<<PhoneBook[Index].LastName<<"\tTel: "<<PhoneBook[Index].PhoneNumber;
return Index;
}
else
{
cout<<"\n\""<<Input<<"\" Not Find!";
return Index;
}
}
void Save(PhoneBookObject PhoneBook[],int NumObject,bool &Change)
{
ofstream ofile;
ofile.open("c:\\PhoneBook.txt");
if(Change==1)
{
ofile<<NumObject;
for(int i=0;i<NumObject;i++)
ofile<<"\n"<<PhoneBook[i].FirstName<<"\n"<<PhoneBook[i].LastName<<"\n"<<PhoneBook[i].PhoneNumber;
cout<<"\nSaved!";
Change=0;
}
else
cout<<"\nPhone Number Save befor!";
}
void Delete(PhoneBookObject PhoneBook[],int &NumObject,bool &Change)
{
int Find;
char Sure;
Find=Search(PhoneBook,NumObject);
if(Find!=-1)
{
cout<<"\nAre You Sure to Delete this Number(yes/no)?";
Sure=getch();
switch(Sure)
{
case 'y':
{
for(int i=Find;i<NumObject-1;i++)
PhoneBook[i]=PhoneBook[i+1];
NumObject--;
Change=1;
break;
}
case 'n':
break;
default : cout<<"\nWrong Character!";
}
}
}
void Exit(char &Input,PhoneBookObject PhoneBook[],int NumObject,bool &Change)
{
char Sure;
if(Change==1)
{
cout<<"\nDo you want to seva the phone Number before Exit on Program(yes/no)?";
Sure=getch();
switch(Sure)
{
case 'y':
{
Save(PhoneBook,NumObject,Change);
break;
}
case 'n':
break;
default : cout<<"\nWrong Character!";
}
}
cout<<"\nAre you sure to exit on program(yes/no)?";
Sure=getch();
switch(Sure)
{
case 'y':
break;
case 'n':
Input='0';
break;
default : cout<<"\nWrong Character!";
}
}
void main()
{
PhoneBookObject PhoneBook[20];
int NumObject=0;
bool Change=1;
char Input=0;
ifstream ifile;
ifile.open("c:\\PhoneBook.txt");
ifile>>NumObject;
for(int i=0;i<NumObject;i++)
{
ifile>>PhoneBook[i].FirstName;
ifile>>PhoneBook[i].LastName;
ifile>>PhoneBook[i].PhoneNumber;
}
cout<<" *---* Welcome to your Phone Book *---*";
while(Input!='7')
{
cout<<"\n1)Add\n2)Delete\n3)Sort\n4)Show\n5)Search \n6)Save\n7)Exit";
cout<<"\nPlease enter number of your act : ";
Input=getch();
switch(Input)
{
case '1':Add(PhoneBook,NumObject,Change);
break;
case '2':Delete(PhoneBook,NumObject,Change);
break;
case '3':Sort(PhoneBook,NumObject,Change);
break;
case '4':Show(PhoneBook,NumObject);
break;
case '5':Search(PhoneBook,NumObject);
break;
case '6':Save(PhoneBook,NumObject,Change);
break;
case '7':Exit(Input,PhoneBook,NumObject,Change);
break;
default:cout<<"\n *---* The input character is wrong!*---*\n\n";
}
}
}