當前位置:首頁 » 編程語言 » c語言文件通訊錄

c語言文件通訊錄

發布時間: 2022-08-23 15:52:25

『壹』 c語言通訊錄程序

以前寫了一個簡單的:
#include <stdio.h>
#include <stdlib.h> /*與malloc.h差不多*/
#include <string.h>
#include <iostream>
using namespace std;
#define maxlen 15
struct persons
{int num; /*定義結構體數組用於緩存數據*/
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
}persons[maxlen];

typedef struct lnode
{ /*通訊錄結構中結點的定義*/
int num;
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
struct lnode *next;
}listnode,*linklist;

linklist head=NULL,r=NULL; /*定義頭指針和尾指針*/
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int i;
char name1[10],ch;
char tel_no1[15];
char arch1;
char sim_no1;
char e_addr1[20];
char s1[20];
FILE *fp; /*定義文件指針*/

void creat() /*將文件的信息讀入結構體數組在轉存入鏈表中*/
{ int j;
long k;
fp=fopen("數據文件.txt","r t"); /*打開文件*/
if(fp!=NULL)
{for(i=0;i<=maxlen;i++ )
{ j=fgetc(fp);
if(j==EOF)
return;
k=i;
fseek(fp,k*sizeof(struct persons),0); /*讀取一個人的信息*/
fread(&persons[i],sizeof(struct persons),1,fp);

s=(linklist)malloc(sizeof(listnode)); /*裝存入鏈表中*/
s->num=persons[i].num;
strcpy(s->name,persons[i].name);
strcpy(s->e_addr,persons[i].e_addr);
strcpy(s->tel_no,persons[i].tel_no);
s->sim_no=persons[i].sim_no;
s->arch=persons[i].arch;
if(head==NULL) /*用尾插法將其插入鏈表中*/
{head=s;r=head;head->next=NULL;}
else
{r->next=s;
r=s;r->next=NULL;
}
}fclose(fp);
}
else
{ fp=fopen("數據文件.txt","w"); /*不能打開另開辟一個文件*/
i=1;
}
}

void Show()
{printf("成功調用該函數\n");}
void Delete()
{printf("成功調用該函數\n");}

void Input() /*向.通訊錄中輸入一個人的信息*/
{ s=(linklist)malloc(sizeof(listnode));
printf("\n\n\t請輸入該用戶的信息:");
printf("姓名:");
scanf("%s",&s->name);
printf("電話號碼:");
scanf("%s",&s->tel_no);
printf("單鍵撥號:");
scanf("%s",&s->sim_no);
printf("E-mail地址:");
scanf("%s",&s->e_addr);
printf("類別:");
scanf("%s",&s->arch);

if(head==NULL)printf("\n\n");
else
{p8=head;
while(p8!=NULL&&strcmp(s->name,p8->name)!=0&&strcmp(s->tel_no,p8->tel_no)!=0)
p8=p8->next;
if(p8!=NULL)
{printf("您添加的用戶已存在!");
free(s);}}
if(head==NULL)
{
s->next = 0;
head=s;
r = s;
}
else
{
s->next = 0;
r->next = s;
r = s;

}
}
void Alter()
{ printf("success!\n");}
int main()
{
system("color a");
creat();
do
{
printf("\n\n\t\t請選擇操作:");
printf("\n\t\t1.顯示通訊錄");
printf("\n\t\t2.刪除通訊錄");
printf("\n\t\t3.添加通訊錄");
printf("\n\t\t4.編輯通訊錄");
printf("\n\n\n");
printf("\t請選擇:");
cin>>ch;
switch(ch)
{ case '1': Show(); /*用單條件多選擇語句實現調用與循環*/
break;
case '2': Delete();
break;
case '3': Input();
break;
case '4': Alter();
break;
fclose(fp);
exit(0);
break;
default:
printf("\n\t The num should 1-6!!! \n");
break;
}
}
while(1);
}

『貳』 如何用C語言做通訊錄

剽竊代碼即可。。


已經按照你的要求做了一個,VC6上運行確認了:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedefstructPersonalInfo
{
charname[50];
charaddress[30];
chartelno[30];
charpostcode[30];
structPersonalInfo*next;
}INFO;
INFO*head;
voidInitiate();
voidMenu();
voidCreate();//的功能是:創建新的通訊錄。
voidAdd();//在通訊錄的末尾,寫入新的信息,並返回選單
voidFind();//查找記錄
voidAlter();//修改記錄如果未找到要修改的人,則提示通訊錄中沒有此人的信息,並返回選單。
voidDelete();//刪除某人的信息,如果未找到要刪的人,提示通訊錄中沒有此人的信息,並返回選單。
voidList();//的功能是:顯示通訊錄中的所有記錄。
//初始化
voidInitiate()
{
if((head=(INFO*)malloc(sizeof(INFO)))==NULL)exit(1);
head->next=NULL;
}
//顯示菜單
voidMenu()
{
printf("**************歡迎使用通訊錄系統**************");
printf(" ");
printf(" ");
printf("1.創建通訊錄。 ");
printf("2.插入信息。 ");
printf("3.查詢記錄 ");
printf("4.修改記錄 ");
printf("5.刪除記錄! ");
printf("6.顯示所有記錄 ");
printf("0.退出通訊錄 ");
printf("請輸入0~6 ");
}
//創建通訊錄
voidCreate()
{
INFO*p1[100],*p2;
intm,i;
printf("請輸入創建個數:");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
p1[i]=(INFO*)malloc(sizeof(INFO));
printf("請輸入第%d條信息! ",i);
printf("姓名: ");
scanf("%s",&p1[i]->name);
printf("地址: ");
scanf("%s",&p1[i]->address);
printf("電話: ");
scanf("%s",&p1[i]->telno);
printf("郵編: ");
scanf("%s",&p1[i]->postcode);
p1[i]->next=NULL;
if(head->next==NULL)
head->next=p1[i];
else
{
for(p2=head;p2->next!=NULL;p2=p2->next);//找到結點尾
p2->next=p1[i];
}
}
printf("信息已添加! ");
return;//保存到鏈表
}
//添加通訊錄信息
voidAdd()
{
INFO*p,*q;
if((q=(INFO*)malloc(sizeof(INFO)))==NULL)exit(1);
printf("請輸入要添加的信息! ");
printf("姓名: ");//添加信息
scanf("%s",&q->name);
printf("性別: ");
scanf("%s",q->address);
printf("電話: ");
scanf("%s",q->telno);
printf("城市: ");
scanf("%s",q->postcode);
for(p=head;p->next!=NULL;p=p->next);
p->next=q;
q->next=NULL;
printf("此信息已添加!");
return;
}
//查找通訊錄信息
voidFind()
{
INFO*p;
charname[50];
if(head->next==NULL)
{
printf("此通訊錄為空! ");
return;
}
printf("請輸入要查找的姓名: ");
scanf("%s",&name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
{
printf("姓名 地址 郵編 電話 ");
printf("%s %s %s %s ",p->name,p->address,p->postcode,p->telno);
}
elseif(p->next==NULL)
printf("無此信息! ");
}
}
//修改通訊錄信息
voidAlter()
{
charname[50];//先查找後刪除
INFO*p,*p1;
if(head->next==NULL)
{
printf("此通訊錄為空! ");
return;
}
printf("請輸入要修改的姓名: ");
scanf("%s",name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
break;
elseif(p->next==NULL)
{
printf("無此信息! ");
return;
}
}
p1=(INFO*)malloc(sizeof(INFO));
printf("姓名: ");//添加信息
scanf("%s",p1->name);
strcpy(p->name,p1->name);
printf("性別: ");
scanf("%s",p1->address);
strcpy(p->address,p1->address);
printf("電話: ");
scanf("%s",p1->telno);
strcpy(p->telno,p1->telno);
printf("城市: ");
scanf("%s",p1->postcode);
strcpy(p->postcode,p1->postcode);
printf("此信息已修改! ");
//顯示修改的信息
printf("姓名 地址 郵編 電話 ");
printf("%s %s %s %s ",p->name,p->address,p->postcode,p->telno);
free(p1);
}
//刪除通訊錄信息
voidDelete()
{
charname[50];//先查找後刪除
INFO*p=head->next,*p1=head->next,*p2;
if(head->next==NULL)
{
printf("此通訊錄為空! ");
return;
}
printf("請輸入要刪除的姓名: ");
scanf("%s",name);
while((strcmp(p->name,name)!=0)&&p->next!=NULL)
{
p1=p;
p=p->next;
}
if(strcmp(name,p->name)==0)//輸出刪除信息
{
if(p==head->next&&p->next!=NULL)
head->next=p->next;
elseif(p==head->next&&p->next==NULL)
{
head->next=p->next;
printf("信息已刪除,先此通訊錄為空!! ");
return;
}
else
p1->next=p->next;
}
else
{
printf("此信息不存在!!! ");
return;
}
printf("此信息已刪除!");
printf("姓名 地址 郵編 電話 ");
for(p2=head->next;p2!=NULL;p2=p2->next)
printf("%s %s %s %s ",p2->name,p2->address,p2->postcode,p2->telno);
}
//顯示所有記錄
voidList()
{
INFO*p;
if(head->next==NULL)
{
printf("此通訊錄中無記錄! ");
return;
}
printf("姓名 地址 郵編 電話 ");
for(p=head->next;p!=NULL;p=p->next)
printf("%s %s %s %s ",p->name,p->address,p->postcode,p->telno);
}
voidmain()
{
intchoice;
charyes_no;

system("colora");
Initiate();
do
{
Menu();
printf("請選擇0-6的數字 ");
scanf("%d",&choice);
printf(" ");
switch(choice)
{
case1:Create();
break;
case2:Add();
break;
case3:Find();
break;
case4:Alter();
break;
case5:Delete();
break;
case6:List();
break;
case0:
printf("************感謝您的使用************ ");
exit(0);
break;
default:
printf("輸入有誤!請重新輸入 ");
break;
}
printf("是否繼續YorN? ");
do
{
scanf("%c",&yes_no);
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}

『叄』 c語言編寫通信錄

/*編程建立一通訊簿,存放有姓名、電話號碼、住址,然後對通信簿進行查找、添加、修改及刪除。*/
#include<stdio.h>
struct person
{
char name[8];
char tel[15];
char addr[50];
};
char filename[20];
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void delete();
main()
{
int m;

creat();
while(1)
{
printf("\n\n添加,請按1");
printf("\n查找,請按2");
printf("\n修改,請按3");
printf("\n刪除,請按4");
printf("\n輸出,請按5");
printf("\n退出,請按0\n");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delete();
break;
case 5: output();
break;
case 0: exit();
}
printf("\n\n操作完畢,請再次選擇!");
}
else
printf("\n\n選擇錯誤,請再次選擇!");
}
}
void creat()
{
struct person one;
long s1;
printf("\n請輸入通訊簿名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\n不能建立通訊簿!");
exit();
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","電話號碼","住址");
printf("\n請輸入姓名、電話號碼及住址(以0結束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
printf("\n\n%20s\n","通 訊 簿");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
printf("\n請輸入添加的姓名、電話號碼及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
}
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n請輸入新姓名、電話號碼及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
else
printf("\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}
void delete()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,記錄為");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n確實要刪除,按1;不刪除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n","","","");
}
}
else
printf("\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}

『肆』 一個簡單的c語言通訊錄 高分懸賞 運行成功加分。

#include "stdio.h"
typedef struct
{
char *num;

int age;

char *name;

char *tel;
char *birth;

}telephone;

void input(telephone *in)
{
telephone temp=*in;
scanf(「%s %d %s %s %s」,&temp.num,&temp.age,&temp.name,&temp.tel,&temp.birth);

*in=temp;
}
void main()
{
FILE *fp;

fp=fopen("091105401.txt","w+");

telephone a[3];

int i=0;
if(fp!=NULL)
{
while(i<3)

{
input(a[i]);
fprintf(fp,"%s %d %s %s %s」,a[i].num,a[i]age,a[i].name,a[i].tel,a[i].birth);
i++;
}
fclose(fp);
}

}

『伍』 C語言簡易通訊錄製作(不包含指針文件)

建立同學通訊錄
#include"stdio.h"
#define NUM 3
struct mem
{
char name[20];
char phone[10];
};
main()
{
struct mem man[NUM];
int i;
for(i=0;i<NUM;i++)
{
printf("input name:\n");
gets(man[i].name);
printf("input phone:\n");
gets(man[i].phone);
}
printf("name\t\t\tphone\n\n");
for(i=0;i<NUM;i++)
printf("%s\t\t\t%s\n",man[i].name,man[i].phone);
}

『陸』 用c語言編寫通訊錄程序

這個屬於基本的結構體數組和文件讀寫操作。

『柒』 c語言通訊錄編寫

靠山山倒,靠樹樹倒,靠自己最好!
自己寫吧,一個
結構體{
姓名;
性別;
年齡;
手機;
}
如果不會用鏈表,那你就用數組!
錄入:就是往數組里寫入數據
刪除:就清為0就可以了唄
查詢:就是從數組的第一項到保存數查找一遍嘍。
大哥
,你是在做課程設計吧?計科的?
傷不起!!!!!!

『捌』 c語言編寫一個通訊錄系統 用簡單一點的c😊

#defineN100
#defineSIZE30
#include<stdio.h>
#include<string.h>
#include<conio.h>

structstudent{
charnum[SIZE];
charname[SIZE];
chartel[SIZE];
};
/*函數聲明*/
voidmyprint();/*顯示菜單*/
intmycreat(structstudent*p,intn);/*輸入通訊錄*/voidmydisplay(structstudent*p,intn);/*顯示通訊錄*/voidmysearch(structstudent*p,intn);/*查找*/
voidmymodify(structstudent*p,intn);/*修改通訊錄*/intmyadd(structstudent*p,intn);/*增加通訊錄*/intmydelete(structstudent*p,intn);/*刪除*/voidmysort(structstudent*p,intn);/*排序*/
voidsch_num(structstudent*p,intn);/*按學號查找*/voidsch_name(structstudent*p,intn);/*按姓名查找*/
intloadinfo(structstudent*p,intn);/*載入通訊錄信息*/intsaveinfo(structstudent*p,intn);/*保存通訊錄信息*/voidmain(){
/*文件定義*/charchoose,yes_no;
structstudentrecord[N];/*存放通訊錄信息,共100條*/inttotal=0,flag;/*通訊錄總數*/
total=loadinfo(record,N);/*從文件讀取記錄信息,不超過100條*/do{
myprint();/*顯示菜單*/
printf("請選擇:");do{
choose=getchar();}
while(choose<'0'||choose>'9');switch(choose){
case'1':
total=mycreat(record,total);break;case'2':
mydisplay(record,total);break;case'3':
mysearch(record,total);break;case'4':
mymodify(record,total);break;case'5':
total=myadd(record,total);break;case'6':
total=mydelete(record,total);break;case'7':
mysort(record,total);break;case'8':
flag=saveinfo(record,total);if(flag==1)
printf(" 保存成功! ");else
printf(" 保存失敗! ");break;case'0':
break;default:
printf(" %c為非法選項! ",choose);}
if(choose=='0')break;printf(" 要繼續選擇嗎(Y/N)?");do{
yes_no=getchar();}
while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');}
while(yes_no!='Y'||yes_no!='y');}
voidmyprint()/*顯示菜單信息*/{
printf(" ");printf(" ");printf("請輸入選項編號(0--7) ");printf("1---批量輸入通訊錄 ");printf("2---顯示通訊錄 ");printf("3---查詢通訊錄 ");printf("4---修改通訊錄 ");printf("5---添加通訊錄 ");printf("6---刪除通訊錄 ");printf("7---排序通訊錄 ");printf("8---保存通訊錄 ");printf("0---退出 ");printf(" ");
}
intmycreat(structstudent*p,intn)/*批量輸入學生信息*/{
inti=0;intm;
printf("添加前的記錄,共有<%d>個: ",n);printf(" 記錄號學號姓名電話號碼 ");
i=0;
while((i<n)&&(strcmp(p->num,"")!=0)){
printf("%d:%15s%15s%15s ",i+1,p->num,p->name,p->tel);i++;p++;}
printf(" 請輸入要輸入的學生人數(<%d):",N-n);
scanf("%d",&m);i=0;
while(i<m){
printf(" ");
printf("請輸入第%d個記錄: ",i+1);printf("學號:");do{
gets(p->num);}
while(strcmp(p->num,"")==0);printf("姓名:");gets(p->name);
printf("電話號碼:");gets(p->tel);p++;i++;}
returnm+n;}
voidmydisplay(structstudent*p,intn)/*顯示學生信息*/{
/*clrscr();*/inti=0;
printf(" 記錄號學號姓名電話號碼 ");while(i<n){
printf("%d:%15s%15s%15s ",i+1,p->num,p->name,p->tel);p++;i++;}}
voidmymodify(structstudent*p,intn)/*修改學生信息*/{
inti;
charnam[10];mydisplay(p,n);
printf(" 請輸入要修改記錄的姓名:");gets(nam);
for(i=0;i<n;i++,p++)
if(strcmp(nam,p->name)==0)break;if(i==n)
printf("無此人! ");else{
printf("請輸入正確的學號:");do{
gets(p->num);}
while(strcmp(p->num,"")==0);printf("請輸入正確的姓名:");gets(p->name);
printf("請輸入正確的電話號碼:");gets(p->tel);}
}
voidmysearch(structstudent*p,intn)/*查找指定學生*/{
charc;
mydisplay(p,n);
printf(" 按學號查詢(h),還是按姓名查詢(m)?");c=getchar();
if(c=='h'||c=='H')
sch_num(p,n);elseif(c=='m'||c=='M')
sch_name(p,n);else
printf(" 非法字元! ");}
voidsch_num(structstudent*p,intn)/*按學號查詢*/{
inti,flag=0;charnm[10];
printf(" 請輸入要查詢記錄的學號:");gets(nm);
for(i=0;i<n;i++)
if(strcmp(nm,p[i].num)==0)
{
printf(" 學號姓名電話號碼 ");printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);flag=1;
}
if(flag==0)
printf("無此人! ");}
voidsch_name(structstudent*p,intn)/*按姓名查詢*/{
inti,flag=0;charnam[10];
printf(" 請輸入要查詢記錄的姓名:");gets(nam);
for(i=0;i<n;i++)
if(strcmp(nam,p[i].name)==0){
printf(" 學號姓名電話號碼 ");printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);flag=1;}
if(flag==0)
printf("無此人! ");
}
intmyadd(structstudent*p,intn)/*增加學生信息*/{
inti=0;charc;
structstudentt,*q;
printf("添加前的記錄: ");printf(" 學號姓名電話號碼 ");i=0;q=p;
/*顯示原有記錄信息*/while(i<n){
printf("%15s%15s%15s ",q->num,q->name,q->tel);i++;q++;}do{
printf("請輸入新記錄的學號: ");do{
gets(t.num);
}
while(strcmp(t.num,"")==0);/*學號不能為空*/printf("請輸入新記錄的姓名:");gets(t.name);
printf("請輸入新記錄的電話號碼:");gets(t.tel);
for(i=0;i<n;i++)
if(strcmp(t.num,(p+i)->num)==0||strcmp(t.tel,(p+i)->tel)==0){
printf("學號或電話號碼重,要重新輸入嗎(Y/N)?");c=getchar();putchar(' ');break;}
/*找到最後,沒有重復*/if(i==n){
*(p+i)=t;
printf("添加後的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n+1;i++)
printf("%15s%15s%15s ",(p+i)->num,(p+i)->name,p[i].tel);break;}
}
while(c=='y'||c=='Y');returnn+1;}
intmydelete(structstudent*p,intn)/*刪除學生信息*/{
inti,x;
charc,nm[10],nam[10];
printf("刪除前的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);printf("按學號刪除(h),還是按姓名刪除(m)?");c=getchar();
if(c=='h'||c=='H'){
printf(" 請輸入要刪除記錄的學號:");gets(nm);
for(x=0;x<n;x++)
{
gets(t.num);
}
while(strcmp(t.num,"")==0);/*學號不能為空*/printf("請輸入新記錄的姓名:");gets(t.name);
printf("請輸入新記錄的電話號碼:");gets(t.tel);
for(i=0;i<n;i++)
if(strcmp(t.num,(p+i)->num)==0||strcmp(t.tel,(p+i)->tel)==0){
printf("學號或電話號碼重,要重新輸入嗎(Y/N)?");c=getchar();putchar(' ');break;}
/*找到最後,沒有重復*/if(i==n){
*(p+i)=t;
printf("添加後的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n+1;i++)
printf("%15s%15s%15s ",(p+i)->num,(p+i)->name,p[i].tel);break;}
}
while(c=='y'||c=='Y');returnn+1;}
intmydelete(structstudent*p,intn)/*刪除學生信息*/{
inti,x;
charc,nm[10],nam[10];
printf("刪除前的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);printf("按學號刪除(h),還是按姓名刪除(m)?");c=getchar();
if(c=='h'||c=='H'){
printf(" 請輸入要刪除記錄的學號:");gets(nm);
for(x=0;x<n;x++)
intj,i,k;charc1,c2;
structstudenttemp;
printf(" 排序前的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);printf(" 按學號排序(h),還是按姓名排序(m)?");c1=getchar();
if(c1=='h'||c1=='H'){
printf(" 按升序排序(s),還是按降序排序(j)?");c2=getchar();/*升序*/
if(c2=='s'||c2=='S'){
for(i=0;i<n-1;i++){
k=i;
for(j=i;j<n;j++)
if(strcmp(p[k].num,p[j].num)<0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;
}
printf(" 排序後的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);}
elseif(c2=='j'||c2=='J')/*降序*/{
for(i=0;i<n-1;i++){
k=i;
for(j=0;j<n;j++)
if(strcmp(p[k].num,p[j].num)<0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;
}
printf("排序後的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);}
elseprintf(" 非法字元 ");}
elseif(c1=='m'||c1=='M')/*按姓名排序*/{
printf(" 按升序排序(s),還是按降序排序(j)?");c2=getchar();
if(c2=='s'||c2=='S'){
for(i=0;i<n-1;i++){
k=i;
for(j=0;j<n;j++)
if(strcmp(p[k].name,p[j].name)<0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;}
printf("排序後的記錄: ");printf(" 學號姓名電話號碼 ");for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);
}
elseif(c2=='j'||c2=='J')/*降序*/{
for(i=0;i<n-1;i++){
k=i;
for(j=i;j<n;j++)
if(strcmp(p[k].name,p[j].name)<0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;
}
printf("排序後的記錄: ");printf(" 學號
姓名電話號碼 ");
for(i=0;i<n;i++)
printf("%15s%15s%15s ",p[i].num,p[i].name,p[i].tel);}
elseprintf(" 非法字元! ");}
elseprintf(" 非法字元! ");}
intloadinfo(structstudent*p,intn)/*載入通訊錄信息*/{
FILE*fp;inti,all=0;
charfilename[30]="tongxunlu.txt";fp=fopen(filename,"r");if(fp==NULL){
printf(" ***不能打開文件%s,或%s文件不存在,創建一個新文件!
",filename,filename);*/
fp=fopen(filename,"w");}
fscanf(fp,"%d ",&all);
if(all>N)/*讀取記錄總數*/{
printf("文件記錄數太多,不能讀取");return0;}
for(i=0;i<all;i++)/*讀取記錄信息*/{
fscanf(fp,"%s%s%s ",p[i].num,p[i].name,p[i].tel);}
fclose(fp);/*關閉文件*/returnall;}
intsaveinfo(structstudent*p,intn){
FILE*fp;inti,all=0;
charfilename[30]="txl.dat";/*文件可以用記事本打開*/fp=fopen(filename,"w");if(fp==NULL){
printf(" ***不能建立或打開文件%s!***",filename);return0;}
fprintf(fp,"%d ",n);for(i=0;i<n;i++)
fprintf(fp,"%s%s%s ",p[i].num,p[i].name,p[i].tel);fclose(fp);return1;}

抄來的

『玖』 用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');

}

『拾』 c語言程序 通訊錄

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define N 3
typedef struct student
{
char num[11];
char name[15];
char sex;
char birth[15];
char phone[15];
char email[15];
char addr[30];
struct student *next;
}STUDENT;
/*以下是函數原型*/
STUDENT *init(); //初始化
void create(STUDENT **headp); //創建鏈表
STUDENT *delete(STUDENT *head); //刪除
STUDENT *update(STUDENT *head); //修改
void print(STUDENT *head); //列印記
void search(STUDENT *head); //查找
void save(STUDENT *head); //保存
STUDENT *load(); //讀入
STUDENT *insert(STUDENT *head); //插入記錄
void append(); //追加記錄

int main()
{
int i;
char choose;
STUDENT *head;
head=init();
printf("1、創建通訊錄\n");
printf("2、插入通訊錄\n");
printf("3、刪除通訊錄\n");
printf("4、修改通訊錄\n");
printf("5、查詢通訊錄\n");
printf("6、追加通訊錄\n");
printf("7、列印通訊錄\n");
printf("8、保存通訊錄到文件\n");
printf("9、從文件中讀取通訊錄\n");
printf("0、退出\n");
do{
choose=getchar();
switch(choose)
{
case '1':
create(&head);
break;
case '2':
head=insert(head);
break;
case '3':
head=delete(head);
break;
case '4':
head=update(head);
break;
case '5':
search(head);
break;
case '6':
append();
break;
case '7':
print(head);
break;
case '8':
save(head);
break;
case '9':
head=load(head);
break;
case '0':
exit(0);
}
}while(choose!='0');
}
STUDENT *init()
{
return NULL;
}
void create(STUDENT **headp)
{
int i,s;
char c;
STUDENT *head=NULL,*tail;
head=(STUDENT *)malloc(sizeof(STUDENT));
tail=head;
for(;;)
{
tail->next=(STUDENT *)malloc(sizeof(STUDENT));
tail=tail->next;
if(!tail)
{
printf("\n內存溢出!");
return NULL;
}
printf("輸入一個記錄:\n");
scanf("%s %s %c %s %s %s %s",tail->num,tail->name,&tail->sex,tail->birth,tail->phone,tail->email,tail->addr);
printf("繼續輸入?\n");
c=getchar();
getchar();
if('Y'!=c||'y'!=c)
break;
}
tail->next=NULL;
*headp=head;
}
void print(STUDENT *head)
{
STUDENT *p;
p=head;
while(p!=NULL)
{
printf("%s %s %c %s %s %s %s %s %s\n",p->num,p->name,p->sex,p->birth,p->phone,p->email,p->addr);
p=p->next;
}
}
STUDENT *delete(STUDENT *head)
{
STUDENT *p,*q;
char s[11];
printf("要刪除的學生的編號\n");
scanf("%s",s);
q=p=head;
while(strcmp(p->num,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\n表中數據為空!\n",s);
else
{
printf("%s %s %c %s %s %s %s %s %s\n",p->num,p->name,p->sex,p->birth,p->phone,p->email,p->addr);
getchar();
if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
printf("\n成功刪除編號為%s的學生\n",s);
}
return(head);
}
void search(STUDENT *head)
{
STUDENT *p;
char s[15];
printf("要查找的學生的編號\n");
scanf("%s",s);
p=head;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
if(p==NULL)
printf("\n沒有編號為%s的學生\n",s);
else
printf("%s %s %c %s %s %s %s %s %s\n",p->num,p->name,p->sex,p->birth,p->phone,p->email,p->addr);
}
STUDENT *update(STUDENT *head)
{
STUDENT *p,*info;;
char s[15];
printf("要修改的學生的編號\n");
scanf("%s",s);
p=head;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
printf("\n輸入學生數據:\n");
scanf("%s %s %c %s %s %s %s",p->num,p->name,&p->sex,p->birth,p->phone,p->email,p->addr);
info->next=NULL;
p=head;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
return head;
}
STUDENT *insert(STUDENT *head)
{
STUDENT *p,*q,*info;
char s[11];
printf("輸入要在哪個編號前插入:\n");
scanf("%s",s);
printf("\n輸入學生數據:\n");
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\n內存溢出!");
return NULL;
}
scanf("%s %s %c %s %s %s %s",info->num,info->name,&info->sex,info->birth,info->phone,info->email,info->addr);
info->next=NULL;
p=head;
q=head;
while(strcmp(info->num,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
if(p==head)
head=info;
else
q->next=info;
else
if(p==head)
{
info->next=p;
head=info;
}
else
{
info->next=p;
q->next=info;
}
printf("\n插入成功!");
return(head);
}
void save(STUDENT *head)
{
FILE *fp;
STUDENT *p;
char outfile[10];
printf("輸入要保存的文件名如c:\\test.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("文件打開失敗!\n");
exit(1);
}
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(STUDENT),1,fp);
p=p->next;
}
fclose(fp);
printf("數據保存成功!\n");
}
STUDENT *load()
{
STUDENT *p,*q,*head=NULL;
FILE *fp;
char infile[10];
printf("輸入要打開的文件名如c:\\test.txt:\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{
printf("文件打開失敗!\n");
exit(1);
}
p=(STUDENT *)malloc(sizeof(STUDENT));
if(!p)
{
printf("內存溢出!\n");
return head;
}
head=p;
while(!feof(fp))
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break;
p->next=(STUDENT *)malloc(sizeof(STUDENT));
if(!p->next)
{
printf("內存溢出!!\n");
return head;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("文件讀取成功!\n");
return head;
}
void append()
{
FILE *fp;
STUDENT *info;
int s1,i;
char infile[10];
printf("\n輸入要追加的學生數據:\n");
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\n內存溢出!");
return ;
}
scanf("%s %s %c %s %s %s %s",info->num,info->name,&info->sex,info->birth,info->phone,info->email,info->addr);
info->next=NULL;
printf("輸入文件名如c:\\test.txt:\n");
scanf("%s",infile);
if((fp=fopen(infile,"ab"))==NULL)
{
printf("文件打開失敗!\n");
exit(1);
}
if(1!=fwrite(info,sizeof(STUDENT),1,fp))
{
printf("文件寫入失敗!\n");
return;
}
printf("寫入成功!\n");
fclose(fp);
}

熱點內容
androidtextview字體 發布:2025-01-18 07:51:18 瀏覽:555
c語言在哪下載 發布:2025-01-18 07:43:29 瀏覽:566
c語言pq 發布:2025-01-18 07:42:40 瀏覽:83
個人精選蘿莉本解壓碼 發布:2025-01-18 07:42:37 瀏覽:696
演算法是什麼意思 發布:2025-01-18 07:42:02 瀏覽:732
安卓手機怎麼加速進程 發布:2025-01-18 07:29:48 瀏覽:681
塞恩拐彎腳本 發布:2025-01-18 07:29:37 瀏覽:742
師資配置含哪些內容 發布:2025-01-18 07:17:35 瀏覽:706
江西腳本 發布:2025-01-18 07:14:38 瀏覽:392
php中i方法 發布:2025-01-18 07:13:19 瀏覽:369