c語言電話本
1. c語言設計個人通訊錄管理系統
1、創建學生社團管理系統的歡迎界面,該歡迎界面以圖片的方式呈現,系統啟動時再屏幕中居中顯示。
2. 用C語言做通訊錄
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct Telephone
{
char number[200];
char name[20];
char off[20];
char addrass[20];
char mail[20];
char telephone[20];
struct Telephone *next;
};
typedef struct Telephone TEL;
TEL *head=NULL;
void Menu(); /*菜單*/
void Crease(); /*添加條目*/
void print(); /*輸出條目*/
void Search(); /*查找條目( 按姓名 )*/
void Delate(); /*刪除信息*/
void Save(); /*保存到文件*/
void Open(); /*打開文件*/
void Change(); /*修改信息*/
void Arrange(); /*排序*/
void main()
{
char ch;
Open(); /*打開文件*/
while(1)
{
Menu(); /*顯示菜單*/
scanf(" %c",&ch);
switch(ch)
{
case '1':Crease(); /*添加條目*/
break;
case '2':Search(); /*查找條目1.按姓名*/
break;
case '3':Change(); /*修改信息*/
print();
break;
case '4': Delate(); /*刪除信息*/
print(); /*輸出刪除後的結果*/
break;
case '5':print(); /*輸出條目*/
break;
case '0':Save(); /*保存並釋放內存*/
exit(0); /*退出*/
break;
default:
printf("選擇錯誤!");
break;
}
}
}
/*菜單*/
void Menu()
{
printf("\n*****************通訊錄系統*****************\n");
printf("\t1.錄入。\n");
printf("\t2.按姓名查詢\n");
printf("\t3.修改信息\n");
printf("\t4.刪除\n");
printf("\t5.輸出\n");
printf("\t0.保存並退出!\n");
printf("*************************************************\n");
printf("\t請選擇:");
}
/*添加條目*/
void Crease()
{
TEL *p1=NULL,*p2=NULL;
p1=(TEL *)malloc(sizeof(TEL)); /*申請結點*/
printf("輸入編號:");
scanf("%s",p1->number);
printf("輸入姓名:"); /*添加信息*/
scanf("%s",p1->name);
printf("輸入單位:");
scanf("%s",p1->off);
printf("輸入地址:");
scanf("%s",p1->addrass);
printf("輸入郵箱:");
scanf("%s",p1->mail);
printf("輸入電話:");
scanf("%s",p1->telephone);
p1->next=NULL; /*保存到鏈表*/
if(head==NULL)
{
head=(TEL *)malloc(sizeof(TEL)); /*申請空間*/
head->next=p1;
}
else
{
for(p2=head;p2->next!=NULL;p2=p2->next); /*找到結點尾*/
p2->next=p1;
}
printf("此信息已添加!");
}
/*輸出學生信息*/
void print()
{
TEL *p=NULL;
if(head==NULL)
{
printf("此通訊錄中無記錄,請輸入記錄後在使用本功能!\n");
return;
}
printf("**************通訊錄系統*********************\n"); /*輸出信息*/
printf("編號\t姓名\t單位\t地址\t郵箱\t電話\n");
for(p=head->next;p!=NULL;p=p->next)
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
}
/*查找信息1.按姓名*/
void Search()
{
TEL *p;
char findname[20];
printf("請輸入要查找的姓名:");
scanf("%s",findname);
printf("**************通訊錄系統*********************\n");
printf("編號\t姓名\t單位\t地址\t郵箱\t電話\n");
for(p=head;p!=NULL;p=p->next)
{
if(strcmp(p->name,findname)==0)
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
}
}
/*刪除信息*/
void Delate()
{
char findname[20]; /*先查找 後刪除*/
TEL *p = head, *pr ;
printf("輸入要刪除的姓名:");
scanf(" %s",findname);
if (head->next == NULL)
{
printf("空鏈表!\n");
return;
}
while ((strcmp(p->name,findname)!=0 )&& p->next != NULL)
{
pr =p;
p =p->next;
}
if (strcmp(findname, p->name)==0) /*輸出刪除信息*/
{
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
if (p == head->next)
head->next = p->next;
else
pr->next = p->next;
free(p);
printf("此信息已刪除!");
}
else printf("無此信息!\n");
}
void Save() /*保存鏈表信息到文件並釋放內存空間*/
{
TEL *p=NULL;
FILE *fp;
char *Book="books.txt";
if(head==NULL)
{
printf("\n記錄為空!\n");
return;
}
else
p=head->next;
if((fp=fopen(Book,"wb+"))==NULL)
{
printf("\n打不開文件!\n");
return;
}
while(p!=NULL) /*保存信息*/
{
fwrite(p,sizeof(TEL),1,fp);
p=p->next;
}
printf("保存完畢!\n");
fclose(fp);
/*****釋放鏈表空間*****/
for(;head->next!=NULL;)
{
p=head->next;
head->next=head->next->next;
free(p);
}
free(head);
}
/*文件信息輸出到鏈表*/
void Open()
{
FILE *fp;
TEL *p1=NULL,*p2=NULL,*temp=NULL;
if((fp=fopen("books.txt","rb+"))==NULL)
{
printf("\n****************通訊錄******************\n");
return;
}
head=(TEL *)malloc(sizeof(TEL));
head->next=NULL;
temp=p2=head;
while(! feof(fp)) /*循環讀取*/
{
p1=(TEL *)malloc(sizeof(TEL));
temp=p2;
p2->next=p1;
p2=p1;
fread(p1,sizeof(TEL),1,fp);
}
temp->next=NULL;
fclose(fp); /*關閉文件*/
}
void Change()
{
TEL *p;
char name[20];
printf("輸入要修改人的姓名:");
scanf("%s",name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
{printf("**************通訊錄系統*********************\n");
printf("編號\t姓名\t單位\t地址\t郵箱\t電話\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
printf("輸入要修改的內容:\n");
printf("輸入編號:");
scanf("%s",p->number);
printf("輸入姓名:");
scanf("%s",p->name);
printf("輸入單位:");
scanf("%s",p->off);
printf("輸入地址:");
scanf("%s",p->addrass);
printf("輸入郵箱:");
scanf("%s",p->mail);
printf("輸入電話:");
scanf("%s",p->telephone);
}
else printf("無此信息!\n");
}
}
自己對比一下
3. 用c語言編寫通訊錄
自己寫的,看一下
// 頭文件部分
//============================
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
// 初始化數據部分
int Menu();
void Start();
void Write();
void Read();
void Search();
void Detele();
void beauty();
void Rework();
void Lock();
int Judge();
//============================
struct Student{
char name[20];
char Tel[20];
char QQ[20];
int age;
}xy[100];
char wenjian[10000];
char Number[10];
char number=0;
int GOTO;
//
int main()
{
Start();
for(;;)
{
GOTO=Menu();
if(GOTO==1)
{
for(;;)
{
Write();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==2)
{
for(;;)
{
Read();
system("pause");
break;
}
}
if(GOTO==3)
{
for(;;)
{
Search();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==4)
{
for(;;)
{
Rework();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==5)
{
Detele();
break;
}
if(GOTO==6)
{
beauty();
break;
}
if(GOTO==7)
{
}
if(GOTO==8)
{
printf("");
break;
}
if(GOTO==9)
{
printf("\t\t\t退出系統\n\n");
return 0;
}
if(GOTO==0)
{
printf("");
break;
}
}
// beauty();
//
// Write();
// Read();
// Search();
// Rework();
// Detele();
return 0;
}
void Write()
{
printf("\t\t\t請輸入好友名字\n");
scanf("%s",&xy[number].name);
printf("\t\t\t請輸入好友電話號碼\n");
scanf("%s",&xy[number].Tel);
printf("\t\t\t請輸入好友QQ號碼\n");
scanf("%s",&xy[number].QQ);
printf("\t\t\t請輸入好友年齡\n");
scanf("%d",&xy[number].age);
FILE *file;
if((file=fopen("Friend.txt","rt"))==NULL)
{
file=fopen("Friend.txt","wt");
fprintf(file,"%-20s%-20s%-20s%-20s","姓名","電話","QQ","年齡");
}
file=fopen("Friend.txt","at");
fprintf(file,"%-20s%-20s%-20s%-20d\n",xy[number].name,xy[number].Tel,xy[number].QQ,xy[number].age);
fclose(file);
number++;
Number[0]=number;
FILE * Sta;
if((Sta=fopen("number.txt","r"))=NULL)
{
Sta=fopen("number.txt","wt");
exit(1);
}
Sta=fopen("number.txt","wt");
fprintf(Sta,"%d",Number[0]);
fclose(Sta);
}
void Read()
{
printf("\t\t\t您通訊錄中一共有%d位好友\n",Number[0]);
FILE* read;
if((read=fopen("Friend.txt","r"))=NULL)
{
printf("\t\t\t您通訊中當前沒有好友,請返回添加\n");
}
read=fopen("Friend.txt","rt");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
for(int i=0;i<number;i++)
{
fscanf(read,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
/* fread(wenjian,10000,1,read);
for(int i=0;i<number*80+80;i++)
{
printf("%c",wenjian[i]);
}
printf("\n");
*/
fclose(read);
}
void beauty()
{
int d;
do{
printf("choose");
scanf("%d",&d);
}while(d<1&&d>5);
if(d==1)
{
system("color 27");
printf("===1===");
}
if(d==2)
{
system("color 37");
printf("===2===");
}
if(d==3)
{
system("color 47");
printf("===3===");
}
if(d==4)
{
system("color 57");
printf("===4===");
}
if(d==5)
{
system("color 67");
printf("===5===");
}
}
void Start()
{
FILE* Start;
if((Start=fopen("number.txt","r"))==NULL)
{
Start=fopen("number.txt","w");
number=0;
Number[0]=number;
fscanf(Start,"%d",&Number[0]);
printf("\t\t\t您的通訊錄中還沒有任何好友,趕快添加吧.此次操作需要重新運行程序。\n");
exit(1);
}
Start=fopen("number.txt","r");
fscanf(Start,"%d",&Number[0]);
number=Number[0];
fclose(Start);
}
void Search()
{
int change=0;
char Searchname[20];
printf("\t\t\t輸入要查找的好友的名字\n\n");
scanf("%s",&Searchname);
fflush(stdin);
printf("\t\t\t您要查找的好友是 %s \n\n",Searchname);
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Searchname)==0)
{
printf("\t\t\t下面是該好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
}
fclose(Search);
}
if(change==0)
{
printf("\t\t\t您要查找的好友不存在,請返回菜單添加。\n");
}
}
void Rework()
{
char Rework[20];
printf("\t\t\t輸入要修改的好友的名字\n\n");
scanf("%s",&Rework);
printf("\t\t\t您要修改 %s 的信息\n\n",Rework);
int Change=0;
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Rework)==0)
{
printf("\t\t\t下面是該好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
Change=1;
int Choose;
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
//---------------------------------------------------------------------
do
{
printf("\t\t\tchoose\t\t\t1-----3");
scanf("%d",&Choose);
}while(Choose>3||Choose<1);
if(Choose==1){
printf("\t\t\t請輸入好友電話號碼\n");
scanf("%s",&xy[i].Tel);
}
if(Choose==2)
{
printf("\t\t\t請輸入好友QQ號碼\n");
scanf("%s",&xy[i].QQ);
}
if(Choose==3)
{
printf("\t\t\t請輸入好友年齡\n");
scanf("%d",&xy[i].age);
}
Search=fopen("Friend.txt","wt");
for(int sta=0;sta<i;sta++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[sta].name,xy[sta].Tel,xy[sta].QQ,xy[sta].age);
}
if(sta==i)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
i++;
}
for(i;i<=number;i++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
printf("\t\t\t修改後的信息為:\n\n");
Read();
}
fclose(Search);
}
if(Change==0)
{
printf("\t\t\t您要查找的好友不存在,請返回菜單添加。\n");
}
}
void Detele()
{
int change=0;
char delete_friend[20];
printf("\t\t\t輸入要刪除的好友的名字\n\n");
scanf("%s",&delete_friend);
fflush(stdin);
printf("\t\t\t您要刪除的好友是 %s \n\n",delete_friend);
FILE *DeleteF;
DeleteF=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(DeleteF,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,delete_friend)==0)
{
printf("\t\t\t下面是該好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
DeleteF=fopen("Friend.txt","wt");
for(int st=0;st<i;st++)
{
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[st].name,xy[st].Tel,xy[st].QQ,xy[st].age);
}
i++;
for(i;i<=number;i++)
{
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
}
//--------------------------------------------------------------------------------
if(change==1)
{
printf("\t\t\t刪除後的通訊錄信息為:\n\n");
number--;
Number[0]=number;
FILE * St;
if((St=fopen("number.txt","r"))=NULL)
{
St=fopen("number.txt","wt");
exit(1);
}
St=fopen("number.txt","wt");
fprintf(St,"%d",Number[0]);
fclose(St);
fclose(DeleteF);
Start();
Read();
}
}
if(change==0)
{
printf("\t\t\t您要刪除的好友不存在。\n");
}
}
void Lock()
{
}
int Menu()
{
printf("\t\t*****歡迎進入通訊管理界面*****\n\n");
printf("\t\t\t1.添加我的好友\n");
printf("\t\t\t2.顯示所有好友\n");
printf("\t\t\t3.查找我的好友\n");
printf("\t\t\t4.修改好友信息\n");
printf("\t\t\t5.刪除我的好友\n");
printf("\t\t\t6.設置背景顏色\n");
printf("\t\t\t7.設置管理密碼\n");
printf("\t\t\t8.使用注意事項\n");
printf("\t\t\t9.退出通訊系統\n");
printf("\t\t\t0.作者想說的話\n");
printf("\t\t******************************\n\n");
do{
printf("\t\t\t請輸入你的選擇:\n\n");
scanf("%d",&GOTO);
}while(GOTO>9||GOTO<1);
return GOTO;
}
int Judge()
{
char JUDGE;
do
{
printf("\t\t\t是否繼續當前操作\n\n Y(yes) / N(no)\n");
printf("\t\t\t輸入你的選擇\n");
JUDGE=getch();
if(JUDGE=='y'||JUDGE=='Y')
{
return 1;
}
if(JUDGE=='n'||JUDGE=='N')
{
return 2;
}
}while(JUDGE!='y'||JUDGE!='n');
}
4. c語言程序設計實例
// 電話本Dlg.cpp : 實現文件
//
#include stdafx.h
#include 電話本.h
#include 電話本Dlg.h
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// C電話本Dlg 對話框
C電話本Dlg::C電話本Dlg(CWnd* pParent /*=NULL*/)
: CDialog(C電話本Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);
}
void C電話本Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(C電話本Dlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT1, C電話本Dlg::OnName)
ON_EN_CHANGE(IDC_EDIT2, C電話本Dlg::OnPhoto)
ON_EN_CHANGE(IDC_EDIT3, C電話本Dlg::OnAddress)
ON_EN_CHANGE(IDC_EDIT4, C電話本Dlg::OnShow)
ON_BN_CLICKED(IDC_BUTTON1, C電話本Dlg::OnFill)
ON_BN_CLICKED(IDC_BUTTON2, C電話本Dlg::OnInsert)
ON_BN_CLICKED(IDC_BUTTON3, C電話本Dlg::OnFind)
ON_BN_CLICKED(IDC_BUTTON4, C電話本Dlg::OnDelete)
ON_BN_CLICKED(IDC_BUTTON5, C電話本Dlg::OnEdit)
ON_BN_CLICKED(IDC_BUTTON7, C電話本Dlg::OnSave)
END_MESSAGE_MAP()
// C電話本Dlg 消息處理程序
BOOL C電話本Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
// 執行此操作
SetIcon(m_hIcon, TRUE); // 設置大圖標
SetIcon(m_hIcon, FALSE); // 設置小圖標
// TODO: 在此添加額外的初始化代碼
ifstream InFile(Wab.fsd,ios::in);
listWabList::iterator ItWab;
char Tmp1[8],Tmp2[16],Tmp3[32];
Wab.clear();
while(InFile Tmp1 Tmp2 Tmp3)
{
OutWab.Name=Tmp1;
OutWab.Photo=Tmp2;
OutWab.Address=Tmp3;
Wab.push_front(OutWab);
}
C電話本Dlg::OnFill();
return TRUE; // 除非將焦點設置到控制項,否則返回 TRUE
}
// 如果向對話框添加最小化按鈕,則需要下面的代碼
// 來繪制該圖標。對於使用文檔/視圖模型的 MFC 應用程序,
// 這將由框架自動完成。
void C電話本Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用於繪制的設備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_castWPARAM(dc.GetSafeHdc()), 0);
// 使圖標在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(rect);
int x = (rect.Width() - cxIcon 1) / 2;
int y = (rect.Height() - cyIcon 1) / 2;
// 繪制圖標
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//當用戶拖動最小化窗口時系統調用此函數取得游標顯示。
//
HCURSOR C電話本Dlg::OnQueryDragIcon()
{
return static_castHCURSOR(m_hIcon);
}
void C電話本Dlg::OnName()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT1);
pMaxLen-SetLimitText(6);
GetDlgItemText(IDC_EDIT1,InWab.Name);
}
void C電話本Dlg::OnPhoto()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT2);
pMaxLen-SetLimitText(11);
GetDlgItemText(IDC_EDIT2,InWab.Photo);
}
void C電話本Dlg::OnAddress()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT3);
pMaxLen-SetLimitText(30);
GetDlgItemText(IDC_EDIT3,InWab.Address);
}
void C電話本Dlg::OnShow()
{
// TODO: 如果該控制項是 RICHEDIT 控制項,則它將不會
// 發送該通知,除非重寫 CDialog::OnInitDialog()
// 函數並調用 CRichEditCtrl().SetEventMask(),
// 同時將 ENM_CHANGE 標志「或」運算到掩碼中。
// TODO: 在此添加控制項通知處理程序代碼
}
void C電話本Dlg::OnFill()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
OutWab=*ItWab;
OutTemp=OutWab.Name Space OutWab.Photo Space OutWab.Address;
OutAll=OutAll OutTemp Enter;
}
SetDlgItemText(IDC_EDIT4,OutAll);
}
void C電話本Dlg::OnInsert()
{
Wab.push_front(InWab);
C電話本Dlg::OnFill();
}
void C電話本Dlg::OnFind()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
}
void C電話本Dlg::OnDelete()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
break;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
if(ItWab!=Wab.end()) Wab.erase(ItWab);
}
void C電話本Dlg::OnEdit()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
break;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
if(ItWab!=Wab.end())
{
Wab.erase(ItWab);
Wab.push_front(InWab);
}
}
void C電話本Dlg::OnSave()
{
CString Tmp1,Tmp2,Tmp3;
ofstream OutFile(Wab.fsd,ios::out);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
Tmp1=ItWab-Name;
Tmp2=ItWab-Photo;
Tmp3=ItWab-Address;
OutFile Tmp1 ' ' Tmp2 ' ' Tmp3 endl;
}
}
只是個用C語言編寫的電話本程序 很有實用意義
5. C語言的通訊錄代碼是什麼
#include
6. 請大家使用C語言完成電話號碼查詢系統。 設有一個電話號碼薄,它
有虛的,原來的憲法的電話號碼的查詢的系統有設置一個電話號碼和QQ號碼不就可以完成了
7. 通訊錄代碼 C語言 跪求
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 100 void input();//添加新用戶函數
void amend();//修改用戶信息函數
void delete_client();//刪除用戶信息函數
void demand_client();//用戶信息查詢函數
void collect_telephone();//用戶信息匯總函數
void save_client(struct telephone message);//保存函數
void demand_name();//按用戶名查詢
void demand_telephone();//按電話號碼查詢
struct telephone
{
char client_name[20];
char client_address[30];
char client_telephone[15];
}; //添加新用戶函數
void input()
{
struct telephone message;
char reply='y';
char save='y';
while (reply=='y')
{ printf("用戶姓名:");
scanf("%s",message.client_name);
printf("電話號碼:");
scanf("%s",message.client_telephone); save_client(message);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //保存函數
void save_client(struct telephone message)
{
FILE *fp;
fp=fopen("message.dat","a+");
if (fp!=NULL)
{
fwrite(&message,sizeof(struct telephone),1,fp);
}
else
{
printf("\n打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
} //修改用戶信息函數
void amend()
{
struct telephone message;
FILE *fp;
char amend_name[20];
char reply='y';
char found='y';
char save='y';
int size=sizeof(struct telephone);
while (reply=='y')
{
found='n';
fp=fopen("message.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("\n請輸入要修改的姓名:");
scanf("%s",amend_name);
while ((fread(&message,size,1,fp))==1)
{
if ((strcmp(amend_name,message.client_name))==0)
{
found='y';
break;
}
}
if (found=='y')
{ printf("==========================================\n");
printf("\n用戶姓名:%s\n",message.client_name);
printf("\n電話號碼:%s\n",message.client_telephone);
printf("==========================================\n");
printf("修改用戶信息:\n");
printf("\n用戶姓名:");
scanf("%s",message.client_name); printf("\n電話號碼:");
scanf("%s",message.client_telephone);
printf("\n要保存嗎?(y/n):");
scanf(" %c",&save);
if (save=='y')
{
fseek(fp,-size,1);
fwrite(&message,sizeof(struct telephone),1,fp);
}
}
else
{
printf("無此人信息!\n");
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //刪除用戶信息函數
void delete_client()
{
struct telephone message[N];
struct telephone temp_str;
struct telephone delete_str;
int i=0,j=0;
char reply='y';
char found='y';
char confirm='y';
char delete_name[20];
FILE *fp;
while (reply=='y')
{
system("cls");
fp=fopen("message.dat","r");
if (fp!=NULL)
{
i=0;
found='n';
printf("\n請輸入姓名:");
scanf("%s",delete_name);
while ((fread(&temp_str,sizeof(struct telephone),1,fp))==1)
{
if ((strcmp(delete_name,temp_str.client_name))==0)
{
found='y';
delete_str=temp_str;
}//查找要刪除的記錄
else
{
message[i]=temp_str;
i++;
}//將其它無關記錄保存起來
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
if (found=='y')
{
printf("==========================================\n");
printf("用戶姓名:%s\n",delete_str.client_name);
printf("電話號碼:%s\n",delete_str.client_telephone);
printf("==========================================\n");
}
else
{
printf("無此人信息,按任意鍵返回……\n");
getchar();
break;
}
printf("確定要刪除嗎?(y/n):");
scanf(" %c",&confirm);
if (confirm=='y')
{
fp=fopen("message.dat","w");
if (fp!=NULL)
{
for(j=0;j<i;j++)
{
fwrite(&message[j],sizeof(struct telephone),1,fp);
}
printf("記錄已刪除!!!\n");
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
}
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
}
//用戶信息查詢函數
void demand_client()
{
int choice=1;
while (choice!=3)
{
system("cls");
printf("電話查詢菜單\n");
printf(" 1 按聯系人姓名查詢\n");
printf(" 2 按聯系人電話號碼查詢\n");
printf(" 3 返回主菜單\n");
printf("請選擇(1-3):");
scanf("%d%*c",&choice);
if (choice>3)
{
printf("請輸入1-6之間的整數\n");
printf("按任意鍵返回菜單……\n");
getchar();
continue;
}
if (choice==1)
{
demand_name();
}
else if (choice==2)
{
demand_telephone();
}
}
} //按用戶名查詢
void demand_name()
{
struct telephone message;
FILE *fp;
char amend_name[20];
char reply='y';
char found='y';
while (reply=='y')
{
found='n';
fp=fopen("message.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("\n請輸入姓名:");
scanf("%s",amend_name);
while ((fread(&message,sizeof(struct telephone),1,fp))==1)
{
if ((strcmp(amend_name,message.client_name))==0)
{
found='y';
break;
}
}
if (found=='y')
{ printf("==========================================\n");
printf("用戶姓名:%s\n",message.client_name); printf("電話號碼:%s\n",message.client_telephone);
printf("==========================================\n");
}
else
{
printf("無此人信息!\n");
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //按電話號碼查詢
void demand_telephone()
{
struct telephone message;
FILE *fp;
char telephone[20];
char reply='y';
char found='y';
while (reply=='y')
{
found='n';
fp=fopen("message.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("\n請輸入電話號碼:");
scanf("%s",telephone);
while ((fread(&message,sizeof(struct telephone),1,fp))==1)
{
if ((strcmp(telephone,message.client_telephone))==0)
{
found='y';
break;
}
}
if (found=='y')
{ printf("==========================================\n");
printf("用戶姓名:%s\n",message.client_name); printf("電話號碼:%s\n",message.client_telephone);
printf("==========================================\n");
}
else
{
printf("無此電話號碼的有關信息!\n");
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //用戶信息匯總函數
void collect_telephone()
{
struct telephone message;
FILE *fp;
fp=fopen("message.dat","r");
if (fp!=NULL)
{
system("cls");
printf("\n用戶姓名\t\t電話號碼\n");
while ((fread(&message,sizeof(struct telephone),1,fp))==1)
{
printf("\n%-24s",message.client_name); printf("%-12s\n",message.client_telephone);
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("按任意鍵返回主菜單……\n");
getch();
} void main()
{
char choice[10]="";
int len=0;
while (choice[0]!='7')
{ printf("\t==========電話本號碼查詢系統=============\n"); printf("\t\t 1、添加新聯系人\n");
printf("\t\t 2、修改聯系人信息\n");
printf("\t\t 3、刪除聯系人信息\n");
printf("\t\t 4、聯系人信息查詢\n");
printf("\t\t 5、聯系人信息匯總\n");
printf("\t\t 7、退出\n");
printf("\t=========================================\n");
printf("請選擇(1-7):");
scanf("%s",choice);
len=strlen(choice);
if (len>1)
{
printf("請輸入1-6之間的整數\n");
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
continue;
} switch (choice[0]) {
case '1':
input();
break;
case '2':
amend();
break;
case '3':
delete_client();
break;
case '4':
demand_client();
break;
case '5':
collect_telephone();
break; default:
break;
}
}
}