📄 telephone_directory.cpp
字号:
int valid, saved=0 ;
int t_code ;
t_code = last_code() ;
t_code++ ;
if (t_code == 1)
{
code = t_code ;
strcpy(name,"abc") ;
strcpy(phone1,"-") ;
strcpy(phone2,"-") ;
strcpy(phone3,"-") ;
strcpy(e_mail,"-") ;
strcpy(address,"xyz") ;
fstream file ;
file.open("TDIARY.DAT", ios::out | ios::app ) ;
file.write((char *) this, sizeof(diary)) ;
file.close() ;
delete_record(t_code) ;
}
char ch ;
do
{
clrscr() ;
gotoxy(3,3) ;
cout <<"Code # " <<t_code ;
gotoxy(3,5) ;
cout <<"Name : " ;
gotoxy(3,6) ;
cout <<"Phone 1 : " ;
gotoxy(3,7) ;
cout <<"Phone 2 : " ;
gotoxy(3,8) ;
cout <<"Phone 3 : " ;
gotoxy(3,9) ;
cout <<"e_mail : ";
gotoxy(3,10) ;
cout <<"Address : " ;
m.line_hor(1,79,23,196) ;
do
{
valid = 1 ;
gotoxy(1,25) ; clreol() ;
gotoxy(3,25) ;
cout <<"ENTER NAME OF THE PERSON" ;
gotoxy(13,5) ;
gets(name) ;
strupr(name) ;
if (strlen(name) > 19 || strlen(name) == 0)
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,24) ;
cout <<"LENGTH SHOULD NOT BE A BLANK OR GREATER THAN 19" ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
gotoxy(1,24) ; clreol() ;
gotoxy(1,25) ; clreol() ;
gotoxy(13,5) ; clreol() ;
}
} while ( !valid ) ;
do
{
valid = 1 ;
gotoxy(1,25) ; clreol() ;
gotoxy(3,25) ;
cout <<"ENTER THE PHONE NO. OF THE PERSON, <ENTER> FOR BLANK" ;
gotoxy(13,6) ;
gets(phone1) ;
if ((strlen(phone1) < 7 && strlen(phone1) > 0) || (strlen(phone1) > 9))
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,24) ;
cout <<"LENGTH SHOULD NOT BE GREATER THAN 9 OR LESS THAN 7" ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
gotoxy(1,24) ; clreol() ;
gotoxy(1,25) ; clreol() ;
gotoxy(13,6) ; clreol() ;
}
} while ( !valid ) ;
if (strlen(phone1) == 0)
strcpy(phone1,"-") ;
do
{
valid = 1 ;
gotoxy(1,25) ; clreol() ;
gotoxy(3,25) ;
cout <<"ENTER THE PHONE NO. OF THE PERSON, <ENTER> FOR BLANK" ;
gotoxy(13,7) ;
gets(phone2) ;
if ((strlen(phone2) < 7 && strlen(phone2) > 0) || (strlen(phone2) > 9))
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,24) ;
cout <<"LENGTH SHOULD NOT BE GREATER THAN 9 OR LESS THAN 7" ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
gotoxy(1,24) ; clreol() ;
gotoxy(1,25) ; clreol() ;
gotoxy(13,7) ; clreol() ;
}
} while ( !valid ) ;
if (strlen(phone2) == 0)
strcpy(phone2,"-") ;
do
{
valid = 1 ;
gotoxy(1,25) ; clreol() ;
gotoxy(3,25) ;
cout <<"ENTER THE PHONE NO. OF THE PERSON, <ENTER> FOR BLANK" ;
gotoxy(13,8) ;
gets(phone3) ;
if ((strlen(phone3) < 7 && strlen(phone3) > 0) || (strlen(phone3) > 9 ))
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,24) ;
cout <<"LENGTH SHOULD NOT BE GREATER THAN 9 OR LESS THAN 7" ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
gotoxy(1,24) ; clreol() ;
gotoxy(1,25) ; clreol() ;
gotoxy(13,8) ; clreol() ;
}
} while ( !valid ) ;
if (strlen(phone3) == 0)
strcpy(phone3,"-") ;
do
{
valid = 1 ;
gotoxy(1,25) ; clreol() ;
gotoxy(3,25) ;
cout <<"ENTER THE E_MAIL ADDRESS OF THE PERSON,<ENTER> FOR BLANK" ;
gotoxy(13,9) ;
gets(e_mail) ;
if ((strlen(e_mail) < 10 && strlen(e_mail) > 0))
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,24) ;
cout <<"LENGTH SHOULD NOT BE GREATER THAN 29 OR LESS THAN 10" ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
gotoxy(1,24) ; clreol() ;
gotoxy(1,25) ; clreol() ;
gotoxy(13,9) ; clreol() ;
}
} while ( !valid ) ;
if (strlen(e_mail) == 0)
strcpy(e_mail,"-") ;
do
{
valid = 1 ;
gotoxy(1,25) ; clreol() ;
gotoxy(3,25) ;
cout <<"ENTER THE ADDRESS OF THE PERSON" ;
gotoxy(13,10) ;
gets(address) ;
strupr(address) ;
if (strlen(address) > 43 || strlen(address) == 0)
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,24) ;
cout <<"SHOULD NOT BE BLANK OR GREATER THAN 48" ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
gotoxy(1,24) ; clreol() ;
gotoxy(1,25) ; clreol() ;
gotoxy(13,10) ; clreol() ;
}
} while ( !valid ) ;
gotoxy(1,25) ; clreol() ;
gotoxy(1,23) ; clreol() ;
gotoxy(1,13) ;
cout <<"Do you want to save the record (y/n) : " ;
do
{
valid = 1 ;
gotoxy(41,13) ;
ch = getche() ;
ch = toupper(ch) ;
if (ch != 'Y' && ch != 'N')
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(41,13) ; clreol() ;
}
} while ( !valid ) ;
if (ch == 'Y')
{
saved = 1 ;
code = t_code ;
fstream file ;
file.open("TDIARY.DAT", ios::out | ios::app ) ;
file.write((char *) this, sizeof(diary)) ;
file.close() ;
t_code++ ;
}
gotoxy(1,14) ;
cout <<"Do you want to add more records (y/n) : " ;
do
{
valid = 1 ;
gotoxy(41,14) ;
ch = getche() ;
ch = toupper(ch) ;
if (ch != 'Y' && ch != 'N')
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(41,14) ; clreol() ;
}
} while ( !valid ) ;
} while (ch == 'Y') ;
if (saved)
sort() ;
}
//**********************************************************
// CLASS NAME : DIARY
// FUNCTION NAME : DELETE_RECORD
// DETAILS : IT DELETES THE RECORD IN THE DIARY'S
// FILE (TDIARY.DAT) FOR THE GIVEN CODE
//**********************************************************
void diary :: delete_record(int t_code)
{
fstream file ;
file.open("TDIARY.DAT", ios::in) ;
fstream temp ;
temp.open("temp.dat", ios::out) ;
file.seekg(0,ios::beg) ;
while ( !file.eof() )
{
file.read((char *) this, sizeof(diary)) ;
if ( file.eof() )
break ;
if ( code != t_code )
temp.write((char *) this, sizeof(diary)) ;
}
file.close() ;
temp.close() ;
file.open("TDIARY.DAT", ios::out) ;
temp.open("temp.dat", ios::in) ;
temp.seekg(0,ios::beg) ;
t_code = 1 ;
while ( !temp.eof() )
{
temp.read((char *) this, sizeof(diary)) ;
code = t_code ;
if ( temp.eof() )
break ;
file.write((char *) this, sizeof(diary)) ;
t_code++ ;
}
file.close() ;
temp.close() ;
}
//**********************************************************
// CLASS NAME : DIARY
// FUNCTION NAME : DELETION
// DETAILS : IT GIVES THE CODE NO. TO DELETE THE
// RECORD IN DIARY'S FILE (TDIARY.DAT)
//**********************************************************
void diary :: deletion(void)
{
menu m ;
clrscr() ;
int valid ;
int t_code=0, t;
char t1_code[5], t2_code[5] ;
char ch ;
gotoxy(3,3) ;
cout <<"Enter Code no. of the record to be deleted " ;
gotoxy(3,4) ;
cout <<"or Press <ENTER> to see from the list or `0' to exit : " ;
gets(t1_code) ;
t = atoi(t1_code) ;
t_code = t ;
if (t1_code[0] == '0')
return ;
if (t1_code[0] != '\0')
{
clrscr() ;
if (!found_record(t_code))
{
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(3,15) ;
cout <<"Record not found" ;
m.line_hor(1,79,24,196) ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
return ;
}
display_record(t_code) ;
gotoxy(3,12) ;
cout <<"Do you want to delete this Record (y/n) : " ;
do
{
valid = 1 ;
gotoxy(45,12) ;
ch = getche() ;
ch = toupper(ch) ;
if (ch != 'Y' && ch != 'N')
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(45,12) ; clreol() ;
}
} while (!valid ) ;
if (ch == 'N')
return ;
gotoxy(1,25) ;
cout <<"Wait..." ;
delete_record(t_code) ;
clrscr() ;
gotoxy(1,14) ;
cout <<"Record Deleted\n" ;
m.line_hor(1,79,24,196) ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
return ;
}
display_list() ;
gotoxy(1,25) ;
clreol() ;
gotoxy(3,25) ;
cout <<"Enter Code no. of the record or <ENTER> to exit " ;
gets(t2_code) ;
t = atoi(t2_code) ;
t_code = t ;
if (t2_code[0] == '\0')
return ;
clrscr() ;
if (!found_record(t_code))
{
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(3,15) ;
cout <<"Record not found" ;
m.line_hor(1,79,24,196) ;
gotoxy(1,25) ;
cout <<"Press any key to continue..." ;
getch() ;
return ;
}
display_record(t_code) ;
gotoxy(3,12) ;
cout <<"Do you want to delete this Record (y/n) : " ;
do
{
valid = 1 ;
gotoxy(45,12) ;
ch = getche() ;
ch = toupper(ch) ;
if (ch != 'Y' && ch != 'N')
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(45,12) ; clreol() ;
}
} while (!valid ) ;
if (ch == 'N')
return ;
gotoxy(1,25) ;
cout <<"Wait..." ;
delete_record(t_code) ;
clrscr() ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(3,14) ;
cout <<"Record Deleted" ;
m.line_hor(1,79,24,196) ;
gotoxy(1,25) ;
cout <<"Press any key to continue...." ;
getch() ;
}
//**********************************************************
// CLASS NAME : DIARY
// FUNCTION NAME : MODIFY_RECORD
// DETAILS : IT MODIFIES THE RECORD IN THE DIARY'S
// FILE (TDIARY.DAT) FOR THE GIVEN CODE
//**********************************************************
void diary :: modify_record(int t_code)
{
menu m ;
int valid, modified=0 ;
char ch ;
m.line_hor(1,79,12,196) ;
gotoxy(3,14) ;
cout <<"Code # " <<t_code ;
gotoxy(3,16) ;
cout <<"Name : " ;
gotoxy(3,17) ;
cout <<"Phone 1 : " ;
gotoxy(3,18) ;
cout <<"Phone 2 : " ;
gotoxy(3,19) ;
cout <<"Phone 3 : " ;
gotoxy(3,20) ;
cout <<"e_mail : " ;
gotoxy(3,21) ;
cout <<"Address : " ;
gotoxy(1,25) ; clreol() ;
m.line_hor(1,79,23,196) ;
gotoxy(13,16) ;
cout <<"Change (y/n) : " ;
do
{
valid = 1 ;
gotoxy(28,16) ;
ch = getche() ;
ch = toupper(ch) ;
if (ch != 'Y' && ch != 'N')
{
valid = 0 ;
sound(500) ;
delay(100) ;
nosound() ;
gotoxy(28,16) ; clreol() ;
}
} while (!valid) ;
valid = 0 ;
while (!valid && ch == 'Y')
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -