Share on ... Twitter LinkedIn Facebook Pinterest Telegram Telefon Defteri Programı Telefon numaralarını ve isimleri kaydeden bir program: İndir FilenameFilesizeLast modified telefondefteri.cpp7.9 KiB2019/05/08 21:25 telefondefteri.exe42.7 KiB2019/05/08 21:25 telefondefteri.jpg8.9 KiB2019/05/08 21:25 telefondefteri.txt6.5 KiB2019/05/08 21:25 Kaynak Kodu: Telefon numaralarını ve isimleri kaydeden bir program ( C++ Class yapıları uygulaması).#include <iostream.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <stdio.h> #include <fstream.h> class Person //The main class defined { public: void UpdateName(char * NewName); //public methods void UpdateSurname(char * NewSurname); void UpdateNumber(char * NewNumber); void DisplayRecord() ; char* GetName(); char* GetSurname(); char* GetNumber() ; private: char Name[20]; //private variables char Surname[20]; char PhoneNumber[12]; }; void Person::UpdateName(char *NewName) { strcpy(Name,NewName); } void Person::UpdateSurname(char *NewSurname) { strcpy(Surname,NewSurname); } void Person::UpdateNumber(char *NewNumber) { strcpy(PhoneNumber,NewNumber); } void Person::DisplayRecord() { cout.width(12); cout.setf(ios::left); cout<< Name; cout.width(12); cout.setf(ios::left); cout<< Surname; cout.width(12); cout.setf(ios::left); cout<< PhoneNumber <<endl; } char* Person::GetName() { return Name; } char* Person::GetSurname(void) { return Surname; } char* Person::GetNumber(void) { return PhoneNumber; } void ReadfromFile(Person* Record2); void NewEntry(Person* Record2,int& end); void ModifyDbase(Person* Record2,int itemno); void MainMenu(); void SavetoFile(Person* Record2,int end); void ChangeEntry(Person* Record2,int end); void PressKey(); void SearchName(Person* Record2,int end); void SearchSurname(Person* Record2,int end); void SearchNumber(Person* Record2,int end); void ListItems(Person* Record2,int end); void PressKey() { cout<<endl<<"Press Any Key to return the Main Menu"; getch(); MainMenu(); } int main() { int exit; int last; Person Record[100]; for(int i=0;i<100;i++)//makes Record Empty { Record[i].UpdateName(""); Record[i].UpdateSurname(""); Record[i].UpdateNumber(""); } ReadfromFile(Record); //Record is updated from the file for(i=0;i<100;i++) //used to find the first empty entry { if(strcmp((Record+i)->GetName(),"")==0) { last=i; break; }//if }//for loop MainMenu(); //show the options on the screen do { //enters this region if exit is 1 char MenuNumber; MenuNumber=cin.get(); //take the menu entry from the user switch(MenuNumber) { case '1': ListItems(Record,last); //Listing the items PressKey(); break; case '2': clrscr(); NewEntry(Record,last); //Take a new entry break; case '3': SearchName(Record,last); //Search a name break; case '4': SearchSurname(Record,last); //Search Surname break; case '5': SearchNumber(Record,last); //Search a number break; case '6': ChangeEntry(Record,last); //Modify an entry break; case '7': cout<< "Do you want to save changes? (Y/N)"; char ch; cin>>ch; if(ch=='Y'||ch=='y') //if y is taken from user it saves the Record SavetoFile(Record,last); exit=0; //in the case 7 it makes exit 0 whch breaks do while loop break; default:MainMenu(); break; }//switch }while(exit); //if exit is zero it breaks the do while statement return 0; } void ReadfromFile(Person* Record2){ //Read the data from the file ifstream dbFileRd("backup.mpb",ios::binary); //create a object called dbFileRd if ( !dbFileRd) //if not read { cout<<"Unable to open database file for reading"; } for(int i=0;i<100;i++) dbFileRd.read((char*) (Record2+i),sizeof *Record2); dbFileRd.close(); //close the file }//ReadfromFile void ListItems(Person* Record2,int end) //Lists the items of the Record2 { clrscr(); //clearscreen for(int i=0;i<end;i++) { cout.width(3); cout.setf(ios::right); cout<<i+1<<") "; Record2[i].DisplayRecord(); } //for } //ListItems void ChangeEntry(Person* Record2,int end) //used to change an entry { int entry; clrscr(); for(int i=0;i<end;i++) //Listing Items on the screen { cout.width(3); cout.setf(ios::right); cout<<i+1<<") "; Record2[i].DisplayRecord(); } //for cout<<endl<<"To change a Person's Record\n"; cout<<"Enter the Number of the Entry than press Enter-->"; cin>>entry; ModifyDbase(Record2,entry-1); //(since array start from 0 call with entry-1) } //ChangeEntry void NewEntry(Person* Record2,int& end) //Used for new Entry,end is reference to a integer(last will come) { end=end+1; ModifyDbase(Record2,end-1); }// NewEntry void ModifyDbase(Person* Record2,int itemno) { char buffer[10]; //used fo input char buffer2[12];//used fo input cout<<"Enter the Name-->"; cin>>buffer; Record2[itemno].UpdateName(buffer); //taken value is written cout<<endl<<"Enter the Surname-->"; cin>>buffer; Record2[itemno].UpdateSurname(buffer); //taken value is written cout<<endl<<"Enter the Phone Number-->"; cin>>buffer2; Record2[itemno].UpdateNumber(buffer2); //taken value is written cout<<endl<<"The record was updated:"<<endl<<endl; Record2[itemno].DisplayRecord(); //Displays the updated Entry PressKey(); //Called to show Menu } void SearchName(Person* Record2,int end) { char SearchKey[10]; int NoRecord=1; //NoRecord is 1 means there is no record clrscr(); cout<<"Enter a Name to search -->"; cin>>SearchKey ; for(int i=0;i<end;i++) { if (strcmp(SearchKey,(Record2+i)->GetName())==0) { (Record2+i)->DisplayRecord(); NoRecord=0;//Norecord is changed to 0 means there is a record } }//for loop if (NoRecord==1) //if no record is found cout<<"The Name could not be found\n"; PressKey(); //Called to show Menu }//SearchName void SearchSurname(Person* Record2,int end) { char SearchKey[10]; int NoRecord=1; //NoRecord is 1 means there is no record clrscr(); cout<<"Enter a Surname to search -->"; cin>>SearchKey ; for(int i=0;i<end;i++) { if (strcmp(SearchKey,(Record2+i)->GetSurname())==0) { (Record2+i)->DisplayRecord(); NoRecord=0;//Norecord is changed to 0 means there is a record } }//for loop if (NoRecord==1) //if no record is found cout<<"The Surname could not be found\n"; PressKey(); //Called to show Menu }//SearchSurname void SearchNumber(Person* Record2,int end) { clrscr(); char SearchKey[10]; cout<<"Enter a Phone Number to search -->"; cin>>SearchKey ; clrscr(); int NoRecord=1; //NoRecord is 1 means there is no record for(int i=0;i<end;i++) { if (strcmp(SearchKey,(Record2+i)->GetNumber())==0) { (Record2+i)->DisplayRecord(); NoRecord=0; //Norecord is changed to 0 means there is a record } }//for loop if (NoRecord==1) //if no record is found cout<<"The Phone Number could not be found\n"; PressKey(); //Called to show Menu } void MainMenu(){ //the screen display of Menu clrscr(); cout<<" .: MyPhoneBook 1.0 :. "<<endl; cout<<"----------------------------------------"<<endl; cout<<"|Menu 1| List all Entries |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 2| Add a new Entry |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 3| Search for a Name |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 4| Search for a Surname |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 5| Search for a Phone Number |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 6| Change an Entry |"<<endl; cout<<"--------"<<endl; cout<<"|Menu 7| Exit the Program |"<<endl; cout<<"----------------------------------------"<<endl; cout<<"Enter a Menu Number(Between 1 and 7)"; } //MainMenu void SavetoFile(Person* Record2,int end) //Saves the the Record to the file { ofstream dbFileWr("backup.mpb",ios::binary); //file is opened for writing if(!dbFileWr) //if not opened { cout<<"Unable to open database file for writing"; } for(int j=0;j<end;j++) //all Record values arewritten dbFileWr.write((char*) (Record2+j),sizeof *Record2); dbFileWr.close(); //close the file }//SavetoFile projelerim/programlama/cplusplus/telefondefteri.txt Son değiştirilme: 2019/05/08 21:25(Dışarıdan düzenle) Giriş yap